| 1 | |||||||||||||||||||||||||||||
| 2 | # -*-Fundamental-*- | ||||||||||||||||||||||||||||
| 3 | 5 | require 5; # Time-stamp: "2004-03-27 17:19:11 AST" | |||||||||||||||||||||||||||
| 4 | package Sort::ArbBiLex; | ||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use vars qw(@ISA $Debug $VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | $VERSION = "4.01"; | |||||||||||||||||||||||||||
| 8 | 5 | $Debug = 0; | |||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use integer; # vroom vroom | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | BEGIN { *UNICODE = eval('chr(256)') ? sub(){1} : sub(){0} } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | #POD at end | ||||||||||||||||||||||||||||
| 15 | ########################################################################### | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=10 SCAR=24.0
| |||||||||||||||||||||||||||||
| 18 | 7 | my $class_name = shift(@_); | |||||||||||||||||||||||||||
| 19 | 7 | my $into = scalar caller; | |||||||||||||||||||||||||||
| 20 | 7 | ▶ | return unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | croak "Argument list in 'use $class_name' must be list of pairs" if @_ % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 4 | my($sym, $spec); | |||||||||||||||||||||||||||
| 23 | 4 | ▶ | while(@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 4 | ($sym, $spec) = splice(@_,0,2); | |||||||||||||||||||||||||||
| 25 | 4 | ▶ | defined $sym or croak "Can't use undef as the name of a sub to make"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | length $sym or croak "Can't use \"\" as the name of a sub to make"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 4 | ▶ | defined $spec or croak "Can't use undef as a sort-order spec"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | length $sym or croak "Can't use \"\" as a sort-order spec"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | $sym = $into . '::' . $sym unless $sym =~ m/::/ or $sym =~ m/'/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sym =~ /::/ or $sym =~ /'/
MC/DC0%0/2
$sym =~ /::/ or $sym =~ /'/
$sym =~ /::/
$sym =~ /'/
Truth table33%1/3
A or B
A= $sym =~ /::/B= $sym =~ /'/
| |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 4 | *{$sym} = maker($spec); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 4 | return; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | ▶ | sub maker { | ||||||||||||||||||||||||||
|
Subroutine
maker: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 4 | ▶ | my $subr = eval(&source_maker(@_)); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | die "Compile error <$@> in eval!?!" if $@; # shouldn't be possible! | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 4 | return $subr; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Implementation note: I didn't /need/ to use eval(). I could just return | ||||||||||||||||||||||||||||
| 45 | # an appropriate closure. But one can't do tr/$foo/$bar/ -- eval is the | ||||||||||||||||||||||||||||
| 46 | # only way to get things to (so to speak) interpolate there; and the | ||||||||||||||||||||||||||||
| 47 | # efficiency cost of requiring that Perl parse more code is offset by | ||||||||||||||||||||||||||||
| 48 | # the efficiency benefit of being able to use tr/// (instead of s///) in | ||||||||||||||||||||||||||||
| 49 | # appropriate cases. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 20 | ▶ | sub source_maker { | ||||||||||||||||||||||||||
|
Subroutine
source_maker: called CC=30 SCAR=34.4
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 5 | ▶ | no locale; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 20 | my($decl) = $_[0]; | |||||||||||||||||||||||||||
| 56 | 34 | ▶ | croak "usage: Sort::ArbBiLex::maker(DECLARATION). See the docs." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | unless @_ == 1; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 50 | my $one_level_mode = 0; | |||||||||||||||||||||||||||
| 60 | 22 | my @decl; | |||||||||||||||||||||||||||
| 61 | 26 | ▶ | if(ref $decl) { # It's a rLoL declaration | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 24 | ▶ | croak "Sort order declaration must be a string or a listref" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | unless ref($decl) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 64 | 22 | ▶ | print "rLoL-decl mode\n" if $Debug > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | # Make @decl into a list of families | ||||||||||||||||||||||||||||
| 66 | 28 | @decl = @$decl; | |||||||||||||||||||||||||||
| 67 | # and each one of the items in @decl must be a ref to a list of scalars | ||||||||||||||||||||||||||||
| 68 | 52 | foreach my $f (@decl) { | |||||||||||||||||||||||||||
| 69 | 150 | ▶ | croak "Each family must be a listref" unless ref($f) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 116 | ▶ | @$f = grep(defined($_) && length($_), @$f); # sanity | ||||||||||||||||||||||||||
|
Condition100%3/3
defined $_ && length $_
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 71 | 94 | foreach my $g (@$f) { # more sanity. | |||||||||||||||||||||||||||
| 72 | 160 | ▶ | croak "A reference found where a glyph was expected" if ref($g); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | } else { # It's a string-style declaration | ||||||||||||||||||||||||||||
| 77 | 34 | ▶ | print "string-decl mode\n" if $Debug > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | # Make @decl into a list of families | ||||||||||||||||||||||||||||
| 79 | 26 | ▶ | if($decl =~ /[\cm\cj\n]/) { # It contains majors and minors | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 26 | @decl = grep /\S/, split( /[\cm\cj]+/, $decl ); | |||||||||||||||||||||||||||
| 81 | } else { # It's all majors, on one line | ||||||||||||||||||||||||||||
| 82 | 12 | ▶ | print "Strangeness trap 1.\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 68 | @decl = grep /\S/, split( /\s+/, $decl ); | |||||||||||||||||||||||||||
| 84 | 68 | $one_level_mode = 1; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Now turn @decl into a list of lists, where each element is a | ||||||||||||||||||||||||||||
| 88 | # family -- i.e., a ref to a list of glyphs in that family. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 70 | ▶ | print "Glyph map:\n", map(" {<$_>}\n", @decl) if $Debug > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 70 | foreach my $d (@decl) { # in place changing | |||||||||||||||||||||||||||
| 92 | #print " d $d -> ", map("<$_> ",grep($_ ne '',split(/\s+/, $d))), "\n"; | ||||||||||||||||||||||||||||
| 93 | 74 | $d = [ grep($_ ne '', split(/\s+/, $d)) ]; | |||||||||||||||||||||||||||
| 94 | #print " d $d -> ", map("<$_> ", @$d), "\n"; | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 18 | @decl = grep( scalar(@{$_}), @decl); # nix empty families | |||||||||||||||||||||||||||
| 99 | 18 | ▶ | croak "No glyphs in sort order declaration!?" unless @decl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 18 | ▶ | @decl = map [$_], @{$decl[0]} if @decl == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | # Change it from a family of N glyphs into N families of one glyph each | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Iterate thru the families and their glyphs and build the tables | ||||||||||||||||||||||||||||
| 105 | 18 | my(@glyphs, @major_out, @minor_out); | |||||||||||||||||||||||||||
| 106 | 18 | my $max_glyph_length = 0; | |||||||||||||||||||||||||||
| 107 | 18 | my $max_family_length = 0; | |||||||||||||||||||||||||||
| 108 | 18 | my %seen; | |||||||||||||||||||||||||||
| 109 | 18 | my($glyph, $minor); # scratch | |||||||||||||||||||||||||||
| 110 | 18 | ▶ | for (my $major = 0; $major < @decl; $major++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 122 | ▶ | print "Family $major\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 122 | croak "Too many major glyphs" if !UNICODE and $major > 255; | |||||||||||||||||||||||||||
| 113 | 24 | $max_family_length = @{ $decl[$major] } | |||||||||||||||||||||||||||
| 114 | 122 | ▶ | if @{ $decl[$major] } > $max_family_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 122 | ▶ | for ($minor = 0; $minor < @{ $decl[$major] }; $minor++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 140 | $glyph = $decl[$major][$minor]; | |||||||||||||||||||||||||||
| 118 | 140 | ▶ | print " Glyph ($major)\:$minor (", $glyph, ")\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | croak "Glyph <$glyph> appears twice in the sort order declaration!" | ||||||||||||||||||||||||||||
| 120 | 140 | ▶ | if $seen{$glyph}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 140 | croak "Too many minor glyphs" if !UNICODE and $minor > 255; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 140 | ▶ | $max_glyph_length = length($glyph) if length($glyph) > $max_glyph_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 140 | $glyph =~ s/([^a-zA-Z0-9])/_char2esc($1)/eg; | |||||||||||||||||||||||||||
| 126 | 140 | push @glyphs, $glyph; | |||||||||||||||||||||||||||
| 127 | 140 | push @major_out, _num2esc($major); | |||||||||||||||||||||||||||
| 128 | 140 | push @minor_out, _num2esc($minor); | |||||||||||||||||||||||||||
| 129 | # or unpack 'H2', pack 'C', 12 or unpack 'H2', chr 12; ? | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 18 | ▶ | die "Unexpected error: No glyphs?!?" if $max_glyph_length == 0; # sanity | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 18 | ▶ | $one_level_mode = 1 if $max_family_length == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | ######################################################################### | ||||||||||||||||||||||||||||
| 136 | # Now start building the code. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 18 | my($prelude, $coda, $code, $minor_code, $major_code); | |||||||||||||||||||||||||||
| 139 | 18 | ▶ | if($max_glyph_length == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | # All glyphs are single characters, so we can do this all with tr's | ||||||||||||||||||||||||||||
| 141 | 14 | $prelude = "# Single character mode."; | |||||||||||||||||||||||||||
| 142 | 14 | $coda = ''; | |||||||||||||||||||||||||||
| 143 | 14 | my $glyphs = join '', @glyphs; | |||||||||||||||||||||||||||
| 144 | 14 | my $major_out = join '', @major_out; | |||||||||||||||||||||||||||
| 145 | 14 | my $minor_out = join '', @minor_out; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 14 | $minor_code = <<"EOMN"; # contents of a FOR block mapping $$x[0] => $$x[2] | |||||||||||||||||||||||||||
| 148 | \$x->[2] = \$x->[0]; | ||||||||||||||||||||||||||||
| 149 | \$x->[2] =~ tr[$glyphs][]cd; | ||||||||||||||||||||||||||||
| 150 | \$x->[2] =~ tr[$glyphs] | ||||||||||||||||||||||||||||
| 151 | [$minor_out]; | ||||||||||||||||||||||||||||
| 152 | EOMN | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 14 | $major_code = <<"EOMJ"; # expression returning a scalar as a major key | |||||||||||||||||||||||||||
| 155 | do { # major keymaker | ||||||||||||||||||||||||||||
| 156 | my(\$key) = \$_; | ||||||||||||||||||||||||||||
| 157 | \$key =~ tr[$glyphs][]cd; | ||||||||||||||||||||||||||||
| 158 | \$key =~ tr[$glyphs] | ||||||||||||||||||||||||||||
| 159 | [$major_out]; | ||||||||||||||||||||||||||||
| 160 | scalar(\$key); | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | EOMJ | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # End of single-glyph stuff. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | } else { | ||||||||||||||||||||||||||||
| 167 | # There are glyphs over 2 characters long -- gotta use s's. | ||||||||||||||||||||||||||||
| 168 | # End of multi-glyph stuff. | ||||||||||||||||||||||||||||
| 169 | 4 | my $glyphs = join ',', map "\"$_\"", @glyphs; | |||||||||||||||||||||||||||
| 170 | 4 | my $major_out = join ',', map "\"$_\"", @major_out; | |||||||||||||||||||||||||||
| 171 | 4 | my $minor_out = join ',', map "\"$_\"", @minor_out; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 4 | ▶ | if(!$one_level_mode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 2 | $prelude = <<"EOPRELUDE"; | |||||||||||||||||||||||||||
| 175 | { # Multi-character mode. So we need a closure for these variables. | ||||||||||||||||||||||||||||
| 176 | my(\%major, \%minor); | ||||||||||||||||||||||||||||
| 177 | \@major{$glyphs} | ||||||||||||||||||||||||||||
| 178 | = ($major_out); | ||||||||||||||||||||||||||||
| 179 | \@minor{$glyphs} | ||||||||||||||||||||||||||||
| 180 | = ($minor_out); | ||||||||||||||||||||||||||||
| 181 | my \$glyph_re = join "|", map(quotemeta, | ||||||||||||||||||||||||||||
| 182 | sort {length(\$b) <=> length(\$a)} keys \%major); | ||||||||||||||||||||||||||||
| 183 | # put the longest glyphs first | ||||||||||||||||||||||||||||
| 184 | EOPRELUDE | ||||||||||||||||||||||||||||
| 185 | } else { # Multi-character mode | ||||||||||||||||||||||||||||
| 186 | 2 | $prelude = <<"EOPRELUDE2"; | |||||||||||||||||||||||||||
| 187 | { # Multi-character mode. So we need a closure for these variables. | ||||||||||||||||||||||||||||
| 188 | my(\%major); # just one-level mode, tho. | ||||||||||||||||||||||||||||
| 189 | \@major{$glyphs} | ||||||||||||||||||||||||||||
| 190 | = ($major_out); | ||||||||||||||||||||||||||||
| 191 | my \$glyph_re = join "|", map(quotemeta, | ||||||||||||||||||||||||||||
| 192 | sort {length(\$b) <=> length(\$a)} keys \%major); | ||||||||||||||||||||||||||||
| 193 | # put the longest glyphs first | ||||||||||||||||||||||||||||
| 194 | EOPRELUDE2 | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | 4 | $coda = "} # end of closure."; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 4 | $minor_code = <<"EOMN2"; # contents of a FOR block mapping $$x[0] => $$x[2] | |||||||||||||||||||||||||||
| 199 | \$x->[2] = join '', | ||||||||||||||||||||||||||||
| 200 | map \$minor{\$_}, | ||||||||||||||||||||||||||||
| 201 | \$x->[0] =~ m<(\$glyph_re)>go; | ||||||||||||||||||||||||||||
| 202 | EOMN2 | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 4 | $major_code = <<"EOMJ2"; # expression returning a scalar as a major key | |||||||||||||||||||||||||||
| 205 | join('', map \$major{\$_}, m<(\$glyph_re)>go) # major keymaker | ||||||||||||||||||||||||||||
| 206 | EOMJ2 | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | ### | ||||||||||||||||||||||||||||
| 211 | # Now finish cobbling the code together. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 18 | my $now = scalar(gmtime); | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 18 | ▶ | if(!$one_level_mode) { # 2-level mode | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 8 | $code = <<"EOVOODOO"; | |||||||||||||||||||||||||||
| 217 | \# Generated by Sort::ArbBiLex v$VERSION at $now GMT | ||||||||||||||||||||||||||||
| 218 | $prelude | ||||||||||||||||||||||||||||
| 219 | # Two-level mode | ||||||||||||||||||||||||||||
| 220 | sub { # change that to "sub whatever {" to name this function | ||||||||||||||||||||||||||||
| 221 | no locale; # we need the real 8-bit ASCIIbetical sort() | ||||||||||||||||||||||||||||
| 222 | use strict; | ||||||||||||||||||||||||||||
| 223 | return | ||||||||||||||||||||||||||||
| 224 | # map sort map is the Schwartzian Transform. See perlfaq4. | ||||||||||||||||||||||||||||
| 225 | map { \$_->[0] } | ||||||||||||||||||||||||||||
| 226 | sort { | ||||||||||||||||||||||||||||
| 227 | \$a->[1] cmp \$b->[1] || | ||||||||||||||||||||||||||||
| 228 | do { | ||||||||||||||||||||||||||||
| 229 | foreach my \$x (\$a, \$b) { | ||||||||||||||||||||||||||||
| 230 | if( !defined(\$x->[2]) and defined(\$x->[0]) ) { | ||||||||||||||||||||||||||||
| 231 | $minor_code | ||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | \$a->[2] cmp \$b->[2]; # return value of this do-block | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | map { [ \$_, | ||||||||||||||||||||||||||||
| 238 | $major_code | ||||||||||||||||||||||||||||
| 239 | , undef | ||||||||||||||||||||||||||||
| 240 | ] | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | \@_; | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | $coda | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | EOVOODOO | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | } else { # one-level mode | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 10 | $code = <<"EOVOODOO2"; | |||||||||||||||||||||||||||
| 251 | \# Generated by Sort::ArbBiLex v$VERSION at $now GMT | ||||||||||||||||||||||||||||
| 252 | $prelude | ||||||||||||||||||||||||||||
| 253 | # One-level mode | ||||||||||||||||||||||||||||
| 254 | sub { # change that to "sub whatever {" to name this function | ||||||||||||||||||||||||||||
| 255 | no locale; # we need the real 8-bit ASCIIbetical sort() | ||||||||||||||||||||||||||||
| 256 | use strict; | ||||||||||||||||||||||||||||
| 257 | return | ||||||||||||||||||||||||||||
| 258 | # map sort map is the Schwartzian Transform. See perlfaq4. | ||||||||||||||||||||||||||||
| 259 | map { \$_->[0] } | ||||||||||||||||||||||||||||
| 260 | sort { \$a->[1] cmp \$b->[1] } | ||||||||||||||||||||||||||||
| 261 | map { [ \$_, | ||||||||||||||||||||||||||||
| 262 | $major_code | ||||||||||||||||||||||||||||
| 263 | ] | ||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | \@_; | ||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | $coda | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | EOVOODOO2 | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 18 | ▶ | print "\nCode to eval:\n", $code, "__ENDCODE__\n\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 18 | return $code; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 56 | ▶ | sub _char2esc { | ||||||||||||||||||||||||||
|
Subroutine
_char2esc: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 281 | 22 | my $in = ord( $_[0] ); | |||||||||||||||||||||||||||
| 282 | 22 | ▶ | return sprintf "\\x{%x}", $in if $in > 255; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 18 | return sprintf "\\x%02x", $in; | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 280 | ▶ | sub _num2esc { | ||||||||||||||||||||||||||
|
Subroutine
_num2esc: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 287 | 280 | my $in = $_[0]; | |||||||||||||||||||||||||||
| 288 | 280 | ▶ | return sprintf "\\x{%x}", $in if $in > 255; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 280 | return sprintf "\\x%02x", $in; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | ########################################################################### | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | # "cmp" returns -1, 0, or 1 depending on whether the left argument is | ||||||||||||||||||||||||||||
| 295 | # stringwise less than, equal to, or greater than the right argument. | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 8 | ▶ | sub xcmp { | ||||||||||||||||||||||||||
|
Subroutine
xcmp: called CC=6 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 298 | 8 | ▶ | carp "usage: xcmp(\\&sorter,$a,$b)" unless @_ and ref($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[0]
MC/DC0%0/2
@_ and ref $_[0]
@_
ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 299 | 8 | ▶ | return 0 if $_[1] eq $_[2]; # We have to trap this early. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 6 | ▶ | return 1 if $_[1] ne ( $_[0]->($_[1], $_[2]) )[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | # If they were switched when sorted, then the original-first was | ||||||||||||||||||||||||||||
| 302 | # lexically GT than the original-second. | ||||||||||||||||||||||||||||
| 303 | 4 | ▶ | return -1 if $_[1] eq ( $_[0]->($_[2], $_[1]) )[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | # If they were switched BACK when REVERSED and sorted, then the | ||||||||||||||||||||||||||||
| 305 | # original-first was lexically LT than the original-second. | ||||||||||||||||||||||||||||
| 306 | 2 | return 0; | |||||||||||||||||||||||||||
| 307 | # Otherwise they were lexically identical. | ||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # And two actually simpler ones: | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 14 | ▶ | sub xlt { | ||||||||||||||||||||||||||
|
Subroutine
xlt: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 313 | 14 | ▶ | carp "usage: xlt(\\&sorter,$a,$b)" unless @_ and ref($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ and ref $_[0]
MC/DC0%0/2
@_ and ref $_[0]
@_
ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 314 | #AKA: xcmp(@_) == -1; | ||||||||||||||||||||||||||||
| 315 | 14 | ▶ | return 0 if $_[1] eq $_[2]; # We have to trap this early. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 12 | ▶ | return 1 if $_[1] eq ( $_[0]->($_[2], $_[1]) )[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | # If they were switched BACK when REVERSED and sorted, then the | ||||||||||||||||||||||||||||
| 318 | # original-first was lexically LT than the original-second. | ||||||||||||||||||||||||||||
| 319 | 8 | return 0; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 14 | ▶ | sub xgt { | ||||||||||||||||||||||||||
|
Subroutine
xgt: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 323 | 14 | ▶ | carp "usage: xgt(\\&sorter,$a,$b)" unless @_ and ref($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[0]
MC/DC0%0/2
@_ and ref $_[0]
@_
ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 324 | #AKA: xcmp(@_) == -1; | ||||||||||||||||||||||||||||
| 325 | 14 | ▶ | return 0 if $_[1] eq $_[2]; # We have to trap this early. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 12 | ▶ | return 1 if $_[1] ne ( $_[0]->($_[1], $_[2]) )[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | # If they were switched when sorted, then the original-first was | ||||||||||||||||||||||||||||
| 328 | # lexically GT than the original-second. | ||||||||||||||||||||||||||||
| 329 | 8 | return 0; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | # And then two easy ones: | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 8 | ▶ | sub xle { | ||||||||||||||||||||||||||
|
Subroutine
xle: called CC=3 SCAR=13.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 335 | 8 | ▶ | carp "usage: xle(\\&sorter,$a,$b)" unless @_ and ref($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[0]
MC/DC0%0/2
@_ and ref $_[0]
@_
ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 336 | 8 | !xgt(@_); #AKA: xcmp(@_) < 1; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 8 | ▶ | sub xge { | ||||||||||||||||||||||||||
|
Subroutine
xge: called CC=3 SCAR=13.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 340 | 8 | ▶ | carp "usage: xge(\\&sorter,$a,$b)" unless @_ and ref($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[0]
MC/DC0%0/2
@_ and ref $_[0]
@_
ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 341 | 8 | !xlt(@_); #AKA: xcmp(@_) > -1; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | ########################################################################### | ||||||||||||||||||||||||||||
| 345 | 5 | 1; | |||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | __END__ | ||||||||||||||||||||||||||||