| 1 | ;;;;# -*-coding:utf-8;-*- µ ← col73 | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 11 | require 5; | |||||||||||||||||||
| 4 | 11 | ▶ | use 5.8.0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | package Text::Unidecode; | ||||||||||||||||||||
| 6 | 11 | $Last_Modified =' Time-stamp: "2016-11-26 05:01:56 MST"'; | |||||||||||||||||||
| 7 | 11 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 11 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 11 | ▶ | use integer; # vroom vroom! | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 11 | ▶ | use vars qw($VERSION @ISA @EXPORT @Char $UNKNOWN $NULLMAP $TABLE_SIZE $Last_Modified | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | $Note_Broken_Tables %Broken_Table_Size %Broken_Table_Copy | ||||||||||||||||||||
| 12 | 11 | ); | |||||||||||||||||||
| 13 | 11 | $VERSION = '1.30'; | |||||||||||||||||||
| 14 | 11 | require Exporter; | |||||||||||||||||||
| 15 | 11 | @ISA = ('Exporter'); | |||||||||||||||||||
| 16 | 11 | @EXPORT = ('unidecode'); | |||||||||||||||||||
| 17 | 11 | $Note_Broken_Tables = 0; | |||||||||||||||||||
| 18 | 11 | ▶ | BEGIN { *DEBUG = sub () {0} unless defined &DEBUG } | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 19 | 11 | $UNKNOWN = '[?] '; | |||||||||||||||||||
| 20 | 11 | $TABLE_SIZE = 256; | |||||||||||||||||||
| 21 | 11 | $NULLMAP = [( $UNKNOWN ) x $TABLE_SIZE]; # for blocks we can't load | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | #-------------------------------------------------------------------------- | ||||||||||||||||||||
| 24 | { | ||||||||||||||||||||
| 25 | 11 | my $x = join '', "\x00" .. "\x7F"; | |||||||||||||||||||
| 26 | 11 | ▶ | die "the 7-bit purity test fails!" unless $x eq unidecode($x); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | #-------------------------------------------------------------------------- | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 685 | ▶ | sub unidecode { | ||||||||||||||||||
|
Subroutine
unidecode: called CC=10 SCAR=25.9
Pod
documented
| |||||||||||||||||||||
| 32 | # Destructive in void context -- in other contexts, nondestructive. | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 685 | ▶ | unless(@_) { # Sanity: Nothing coming in! | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 0 | ▶ | return() if wantarray; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | return ''; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 685 | ▶ | if( defined wantarray ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | # We're in list or scalar context (i.e., just not void context.) | ||||||||||||||||||||
| 41 | # So make @_'s items no longer be aliases. | ||||||||||||||||||||
| 42 | 685 | @_ = map $_, @_; | |||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||
| 44 | # Otherwise (if we're in void context), then just let @_ stay | ||||||||||||||||||||
| 45 | # aliases, and alter their elements IN-PLACE! | ||||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 685 | foreach my $n (@_) { | |||||||||||||||||||
| 49 | 685 | ▶ | next unless defined $n; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # Shut up potentially fatal warnings about UTF-16 surrogate | ||||||||||||||||||||
| 52 | # characters when running under perl -w | ||||||||||||||||||||
| 53 | # This is per https://rt.cpan.org/Ticket/Display.html?id=97456 | ||||||||||||||||||||
| 54 | 11 | ▶ | no warnings 'utf8'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 685 | ▶ | $n =~ s~([^\x00-\x7f])~${$Char[ord($1)>>8]||t($1)}[ord($1)&255]~egs; | ||||||||||||||||||
|
Condition66%2/3
$Text::Unidecode::Char[ord($1) >> 8] || t($1)
MC/DC0%0/2
$Text::Unidecode::Char[ord($1) >> 8] || t($1)
$Text::Unidecode::Char[ord($1) >> 8]
t($1)
Truth table66%2/3
A || B
A= $Text::Unidecode::Char[ord($1) >> 8]B= t($1)
| |||||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | # That means: | ||||||||||||||||||||
| 59 | # Replace character 0xABCD with $Char[0xAB][0xCD], loading | ||||||||||||||||||||
| 60 | # the table 0xAB as needed. | ||||||||||||||||||||
| 61 | # | ||||||||||||||||||||
| 62 | #====================================================================== | ||||||||||||||||||||
| 63 | # | ||||||||||||||||||||
| 64 | # Yes, that's dense code. It's the warp core! | ||||||||||||||||||||
| 65 | # Here is an expansion into pseudocode... as best as I can manage it... | ||||||||||||||||||||
| 66 | # | ||||||||||||||||||||
| 67 | # $character = $1; | ||||||||||||||||||||
| 68 | # $charnum = ord($character); | ||||||||||||||||||||
| 69 | # $charnum_lowbits = $charnum & 255; | ||||||||||||||||||||
| 70 | # $charnum_highbits = $charnum >> 8; | ||||||||||||||||||||
| 71 | # | ||||||||||||||||||||
| 72 | # $table_ref = $Char->[$charnum_highbits]; | ||||||||||||||||||||
| 73 | # | ||||||||||||||||||||
| 74 | # if($table_ref) { | ||||||||||||||||||||
| 75 | # # As expected, we got the arrayref for this table. | ||||||||||||||||||||
| 76 | # } else { | ||||||||||||||||||||
| 77 | # # Uhoh, we couldn't find the arrayref for this table. | ||||||||||||||||||||
| 78 | # # So we call t($character). | ||||||||||||||||||||
| 79 | # # It loads a table. Namely, it does: | ||||||||||||||||||||
| 80 | # Load_Table_For( $charnum_highbits ); | ||||||||||||||||||||
| 81 | # # ...which does magic, and puts something in | ||||||||||||||||||||
| 82 | # # $Char->[$charnum_highbits], | ||||||||||||||||||||
| 83 | # # so NOW we actually CAN do: | ||||||||||||||||||||
| 84 | # $table_ref = $Char->[$charnum_highbits]; | ||||||||||||||||||||
| 85 | # } | ||||||||||||||||||||
| 86 | # | ||||||||||||||||||||
| 87 | # $for_this_char | ||||||||||||||||||||
| 88 | # = $table_ref->[ $charnum_lowbits ]; | ||||||||||||||||||||
| 89 | # | ||||||||||||||||||||
| 90 | # # Although the syntax we actually use is the odd | ||||||||||||||||||||
| 91 | # but COMPLETE EQUIVALENT to this syntax: | ||||||||||||||||||||
| 92 | # | ||||||||||||||||||||
| 93 | # $for_this_char | ||||||||||||||||||||
| 94 | # = ${ $table_ref }[ $charnum_lowbits ]; | ||||||||||||||||||||
| 95 | # | ||||||||||||||||||||
| 96 | # and $for_this_char is the replacement text for this | ||||||||||||||||||||
| 97 | # character, in: | ||||||||||||||||||||
| 98 | # $n =~ s~(char)~replacement~egs | ||||||||||||||||||||
| 99 | # | ||||||||||||||||||||
| 100 | # (And why did I use s~x~y~ instead of s/x/y/ ? | ||||||||||||||||||||
| 101 | # It's all the same for Perl: perldoc perlretut says: | ||||||||||||||||||||
| 102 | # As with the match "m//" operator, "s///" can | ||||||||||||||||||||
| 103 | # use other delimiters, such as "s!!!" and "s{}{}", | ||||||||||||||||||||
| 104 | # I didn't do it for sake of obscurity. I think it's just to | ||||||||||||||||||||
| 105 | # keep my editor's syntax highlighter from crashing, | ||||||||||||||||||||
| 106 | # which was a problem with s/// when the insides are as gory | ||||||||||||||||||||
| 107 | # as we have here. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 685 | ▶ | return unless defined wantarray; # void context | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 685 | ▶ | return @_ if wantarray; # normal list context -- return the copies | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | # Else normal scalar context: | ||||||||||||||||||||
| 112 | 437 | ▶ | return $_[0] if @_ == 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 0 | return join '', @_; # rarer fallthru: a list in, but a scalar out. | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | #====================================================================== | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 134 | ▶ | sub make_placeholder_map { | ||||||||||||||||||
|
Subroutine
make_placeholder_map: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 119 | 134 | return [( $UNKNOWN ) x $TABLE_SIZE ]; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | 0 | ▶ | sub make_placeholder_map_nulls { | ||||||||||||||||||
|
Subroutine
make_placeholder_map_nulls: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 122 | 0 | return [( "" ) x $TABLE_SIZE ]; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | #====================================================================== | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 538 | ▶ | sub t { # "t" is for "t"able. | ||||||||||||||||||
|
Subroutine
t: called CC=11 SCAR=36.9
Pod
undocumented
| |||||||||||||||||||||
| 128 | # Load (and return) a char table for this character | ||||||||||||||||||||
| 129 | # this should get called only once per table per session. | ||||||||||||||||||||
| 130 | 538 | my $bank = ord($_[0]) >> 8; | |||||||||||||||||||
| 131 | 538 | ▶ | return $Char[$bank] if $Char[$bank]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 538 | load_bank($bank); | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | # Now see how that fared... | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 538 | ▶ | if(ref($Char[$bank] || '') ne 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$Text::Unidecode::Char[$bank] || ''
MC/DC0%0/1
$Text::Unidecode::Char[$bank] || ''
$Text::Unidecode::Char[$bank]
Truth table50%1/2
A
A= $Text::Unidecode::Char[$bank]
| |||||||||||||||||||||
| 138 | 0 | DEBUG > 1 and print | |||||||||||||||||||
| 139 | " Loading failed for bank $bank (err $@). Using null map.\n"; | ||||||||||||||||||||
| 140 | 0 | return $Char[$bank] = $NULLMAP; | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 538 | DEBUG > 1 and print " Loading succeeded.\n"; | |||||||||||||||||||
| 145 | 538 | my $cb = $Char[$bank]; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | # Sanity-check it: | ||||||||||||||||||||
| 148 | 538 | ▶ | if(@$cb == $TABLE_SIZE) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | # As expected. Fallthru. | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | } else { | ||||||||||||||||||||
| 152 | 0 | ▶ | if($Note_Broken_Tables) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | 0 | $Broken_Table_Size{$bank} = scalar @$cb; | |||||||||||||||||||
| 154 | 0 | $Broken_Table_Copy{$bank} = [ @$cb ]; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 0 | ▶ | if(@$cb > $TABLE_SIZE) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 158 | 0 | DEBUG and print "Bank $bank is too large-- it has ", scalar @$cb, | |||||||||||||||||||
| 159 | " entries in it. Pruning.\n"; | ||||||||||||||||||||
| 160 | 0 | splice @$cb, $TABLE_SIZE; | |||||||||||||||||||
| 161 | # That two-argument form splices everything off into nowhere, | ||||||||||||||||||||
| 162 | # starting with the first overage character. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | } elsif( @$cb < $TABLE_SIZE) { | ||||||||||||||||||||
| 165 | 0 | DEBUG and print "Bank $bank is too small-- it has ", scalar @$cb, | |||||||||||||||||||
| 166 | " entries in it. Now padding it.\n"; | ||||||||||||||||||||
| 167 | 0 | ▶ | if(@$cb == 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | 0 | DEBUG and print " (Yes, ZERO entries!)\n"; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | 0 | push @$cb, | |||||||||||||||||||
| 171 | ( $UNKNOWN ) x ( $TABLE_SIZE - @$cb) | ||||||||||||||||||||
| 172 | # i.e., however many items, times the deficit | ||||||||||||||||||||
| 173 | ; | ||||||||||||||||||||
| 174 | # And fallthru... | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | } else { | ||||||||||||||||||||
| 177 | 0 | die "UNREACHABLE CODE HERE (INSANE)"; | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | # Check for undefness in block: | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 538 | ▶ | for(my $i = 0; $i < $TABLE_SIZE; ++$i) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | 137728 | ▶ | unless(defined $cb->[$i]) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | 0 | DEBUG and printf "Undef at position %d in block x%02x\n", | |||||||||||||||||||
| 186 | $i, $bank; | ||||||||||||||||||||
| 187 | 0 | $cb->[$i] = ''; | |||||||||||||||||||
| 188 | } | ||||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 538 | return $Char[$bank]; | |||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | #----------------------------------------------------------------------- | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 11 | our $eval_loaded_okay; | |||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | 538 | ▶ | sub load_bank { | ||||||||||||||||||
|
Subroutine
load_bank: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | # This is in its own sub, for sake of sweeping the scary thing | ||||||||||||||||||||
| 201 | # (namely, a call to eval) under the rug. | ||||||||||||||||||||
| 202 | # I.e., to paraphrase what Larry Wall once said to me: if | ||||||||||||||||||||
| 203 | # you're going to do something odd, maybe you should do it | ||||||||||||||||||||
| 204 | # in private. | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 538 | my($banknum) = @_; # just as an integer value | |||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 538 | DEBUG and printf | |||||||||||||||||||
| 209 | "# Eval-loading %s::x%02x ...\n"; | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 538 | $eval_loaded_okay = 0; | |||||||||||||||||||
| 212 | 538 | my $code = | |||||||||||||||||||
| 213 | sprintf( "require %s::x%02x; \$eval_loaded_okay = 1;\n", | ||||||||||||||||||||
| 214 | __PACKAGE__, | ||||||||||||||||||||
| 215 | $banknum); | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | { | ||||||||||||||||||||
| 218 | 538 | local $SIG{'__DIE__'}; | |||||||||||||||||||
| 219 | 538 | eval($code); | |||||||||||||||||||
| 220 | } | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 538 | ▶ | return 1 if $eval_loaded_okay; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 223 | 0 | return 0; | |||||||||||||||||||
| 224 | } | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | #====================================================================== | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 11 | 1; | |||||||||||||||||||
| 229 | __END__ | ||||||||||||||||||||