chiark / gitweb /
Island/arch names which uniquely match initial substring are also ok
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 12 Aug 2009 11:12:07 +0000 (12:12 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 12 Aug 2009 11:12:07 +0000 (12:12 +0100)
yarrg/web/routetextstring

index 4202ce9a4881b69c2343ca00400f52105d926d2b..70d614483d4efe3de4888af97413388628d2061d 100644 (file)
@@ -41,22 +41,29 @@ if ($format =~ 'json') {
 foreach my $each (split m#[/|,]#, $string) {
        $each =~ s/^\s*//;  $each =~ s/\s*$//;  $each =~ s/\s+/ /g;
        next if !length $each;
-       my $pat= "\%$each\%";
-       my $nrows= $sth->execute($pat,$pat);
        my $err= sub {
                my $msg= sprintf $_[0], encode_entities($each);
                $output_wrong->($msg);
                $m->abort();
        };
-       my $results= $sth->fetchall_arrayref();
-       if (!@$results) {
-               $err->('no island or arch matches "%s"');
-       } elsif (@$results > 3) {
-               $err->('&nbsp;');
-       } elsif (@$results > 1) {
-               my @m= map { $_->[2] } @$results;
-               $err->('ambiguous island or arch "%s", could be '.
-                       join(', ', @m));
+       my @m;
+       my $results;
+       foreach my $pat ("$each\%", "\%$each\%") {
+               $sth->execute($pat,$pat);
+               $results= $sth->fetchall_arrayref();
+               last if @$results==1;
+               push @m, @$results;
+               $results= undef;
+       }
+       if (!$results) {
+               if (!@m) {
+                       $err->('no island or arch matches "%s"');
+               } elsif (@m > 3) {
+                       $err->('&nbsp;');
+               } else {
+                       $err->('ambiguous island or arch "%s", could be '.
+                               join(', ', map { $_->[2] } @m));
+               }
        }
        push @results, $results->[0];
 }