File: mk-city-regex.pl

package info (click to toggle)
postgis 2.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 75,792 kB
  • sloc: ansic: 139,314; sql: 136,281; xml: 48,954; sh: 4,906; perl: 4,509; makefile: 2,897; python: 1,198; yacc: 441; cpp: 305; lex: 132
file content (50 lines) | stat: -rw-r--r-- 1,140 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!  /usr/bin/perl
#!/usr/bin/perl -w
use strict;
use Regexp::Assemble;

my @cities = split(/[\r\n]+/, qx(cat usps-st-city-name.txt));

my %st= ();
for my $x (@cities) {
    my ($st, $ct) = split(/\t/, $x);
    push @{$st{$st}}, $ct;
}

my $re;
my $ra = Regexp::Assemble->new(flags => "i");

my %re =();
for my $x (sort keys %st) {
    $ra->add(@{$st{$x}});
    $re = $ra->re;
    $re =~ s/\\/\\\\/g;
    $re{$x} = $re;
}

print "#define NUM_STATES " . scalar (keys %re) . "\n\n";
print "    static const char *states[NUM_STATES] = \n";
print "        {\"" . join('","', sort keys %re) . "\"};\n\n";
print "    static const char *stcities[NUM_STATES] = {\n";
my $cnt = 0;
my $a = '';
my $b = '';
for my $x (sort keys %re) {
    $re = "(?:\\\\b)($re{$x})\$";
    print "  ,\n" if $cnt; 
    print "  /* -- $x -- $x -- $x -- $x -- $x -- $x -- $x -- $x -- $x -- $x -- */\n";
    while ($re =~ s/^(.{1,65})//) {
        $a = $1;
        if ($a =~ s/(\\+)$//) {
            print "  \"$b$a\"\n";
            $b = $1;
        }
        else {
            print "  \"$b$a\"\n";
            $b = '';
        }
    }
    $cnt++;
}
print "    };\n";