charset: skip option to omit rows
[sheet.git] / charset.plp
index 79ebd96deb2ff338c4bdf494ca7425fa94e785a1..e1efd83c0880e8885f06569486751206fb63e784 100644 (file)
@@ -15,7 +15,7 @@ Html({
        data => [qw'charset-unicode.inc.pl charset-utf8.inc.pl'],
 });
 
-my @tablist = split /[^\w-]+/, $Request || 'default';
+my @tablist = split m{/+}, $Request || 'default';
 
 :>
 <h1>Character encoding</h1>
@@ -31,7 +31,7 @@ sub tabinput {
        my $input = shift or return;
 
        state $ALIAS = {
-               default    => [qw( unicode+0-192 utf-8 iso-8859-1 iso-8859-15 cp1252 cp437 cp850 )],
+               default    => [qw( unicode+0-639 utf-8 iso-8859-1 iso-8859-15 cp1252 cp437 cp850 )],
                us         => [qw( cp437 cp863 AdobeStandardEncoding gsm0338 )],
                ebcdic     => [qw( cp37 cp500 cp875 cp1026 cp1047 posix-bc )],
                iso        => [map {"iso-8859-$_"} 1 .. 11, 13 .. 16],
@@ -89,7 +89,7 @@ sub tabinput {
                        'iso-8859-6'  => ['iso-8859-1' => '160'],
                        'iso-8859-7'  => ['iso-8859-1' => '160'],
                        'iso-8859-8'  => ['iso-8859-1' => '160'],
-                       'iso-8859-9'  => ['iso-8859-1' => '208'],
+                       'iso-8859-9'  => ['iso-8859-1' => '208-223+240'],
                        'iso-8859-10' => ['iso-8859-4' => '160'],
                        'iso-8859-11' => ['iso-8859-1' => '160'],
                        'iso-8859-13' => ['iso-8859-4' => '160'],
@@ -99,14 +99,15 @@ sub tabinput {
                        'hp-roman8'   => ['iso-8859-1' => '160'],
 
                        'cp1252'      => ['iso-8859-1' => '128-159'],
-                       'cp1250'      => ['iso-8859-2' => '128-191'],
-                       'cp1254'      => ['iso-8859-9' => '128-159'],
-                       'cp874'       => ['iso-8859-11' => '128-159'], # windows-874 actually cp1162
-                       'cp1257'      => ['iso-8859-13' => '128'],
+                       'cp1250'      => ['iso-8859-2' => '128-191', 'cp1252' => '128'],
+                       'cp1254'      => ['iso-8859-9' => '128-159', 'cp1252' => '128-159+208'],
+                       'cp874'       => ['iso-8859-11' => '128-159', 'cp1252' => '128'], # windows-874 actually cp1162
+                       'cp1257'      => ['iso-8859-13' => '128-159+255', 'cp1252' => '128'],
                        'cp1251'      => ['cp1252' => '128'],
                        'cp1253'      => ['cp1252' => '128'],
                        'cp1255'      => ['iso-8859-8' => '128-223', 'cp1252' => '128'],
                        'cp1256'      => ['cp1252' => '128'],
+                       'cp1258'      => ['cp1252' => '128-159+192'],
 
                        'cp850'       => ['cp437' => '144'],
                        'cp860'       => ['cp437' => '128-175'],
@@ -114,14 +115,14 @@ sub tabinput {
                        'cp863'       => ['cp437' => '128-175'],
                        'cp865'       => ['cp437' => '144-175'],
                        'cp852'       => ['cp850' => '128', 'cp437' => '128'],
-                       'cp857'       => ['cp850' => '128-239', 'cp437' => '128'],
+                       'cp857'       => ['cp850' => '128-175+208-239', 'cp437' => '128'],
                        'cp775'       => ['cp850' => '128'],  # partial cp437
-                       'cp866'       => ['cp437' => '128'],
+                       'cp866'       => ['cp437' => '128-175+224'],
                        'cp855'       => ['cp437' => '128'],
                        'cp1006'      => ['cp437' => '128'],
-                       'cp737'       => ['cp437' => '128'],
+                       'cp737'       => ['cp437' => '128-175+224'],
                        'cp869'       => ['cp437' => '128'],
-                       'cp862'       => ['cp437' => '128'],
+                       'cp862'       => ['cp437' => '128-159'],
                        'cp864'       => ['cp437' => '128'],
 
                        'koi8-u'      => ['koi8-r' => 128],
@@ -154,9 +155,24 @@ sub tabinput {
                $visible->{$_} //= 0 for $row{parent};
        }
 
-       if (defined $params and $params =~ m/^ (\d+) (-\d+)? /x) {
-               $row{offset} = $1;
-               $endpoint = -$2 if $2;
+       if (defined $params) {
+               $params =~ m{
+                       \A (?<offset> \d+)
+                       (?: (?: [-] (?<stop> \d+) )? (?: [+] (?<restart> \d+) ) )?
+                       (?: [-] (?<endpoint> \d+) )? \z
+               }x or Alert([
+                       "Unknown range parameters for $input",
+                       "<q>$params</q> is not in format start(-stop)(+restart(-end))",
+               ]);
+
+               $row{offset} = $+{offset};
+               $endpoint = $+{endpoint} if $+{endpoint};
+               if (my $restart = $+{restart}) {
+                       my $skip = int(($+{stop} || $row{offset}) / $row{cols});
+                       for ($skip + 1 .. ($restart / $row{cols}) - 1) {
+                               $row{skip}->{ $_ * $row{cols} - $row{offset} }++;
+                       }
+               }
        }
 
        if ($input =~ /^U([0-9a-f]+)(?:-([0-9a-f]+))?/) {
@@ -164,28 +180,28 @@ sub tabinput {
                my $end = $2 ? hex($2) << 4 : $start + 240;
                $row{table} = join '', map { chr } $start .. $end+15;
                utf8::upgrade($row{table});  # prevent latin1 output
-               $row{len} = length $row{table};
+               $row{endpoint} = $end + 14 - $start;
                $row{set} = sprintf 'Unicode block U+%02Xxx', $start >> 8;
        }
        elsif ($input eq 'U') {
-               $row{len} = 1024;
                $row{set} = 'Unicode planes';
                $row{cell} = do 'charset-ucplanes.inc.pl'
                        or Alert('Table data could not be read', $@ || $!);
                $row{cols} *= 2;
+               $row{endpoint} = 1023 * $row{cell}->{colsize};
        }
        elsif ($row{set} = Encode::resolve_alias($input)) {
                if ($row{set} eq 'Internal') {
-                       $row{len} = $endpoint < 255 ? 640 : 8192;
                        $row{set} = 'Unicode BMP';
                        $row{cell} = do 'charset-unicode.inc.pl'
                                or Alert('Table data could not be read', $@ || $!);
+                       $row{endpoint} = ($endpoint || 8191) * $row{cell}->{colsize};
                }
                elsif ($row{set} eq 'utf-8-strict') {
-                       $row{len} = 256;
                        $row{set} = 'UTF-8';
                        $row{cell} = do 'charset-utf8.inc.pl'
                                or Alert('Table data could not be read', $@ || $!);
+                       $row{endpoint} = 255;
                }
                else {
                        if ($row{set} eq 'MacHebrew') {
@@ -199,7 +215,7 @@ sub tabinput {
                                # substr strings is twice as fast as splitting to an array
                                $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $endpoint);
                        }
-                       $row{len} = $endpoint - $row{offset};
+                       $row{endpoint} = $endpoint - $row{offset};
 
                        if ($row{set} eq 'cp437' and !$row{offset}) {
                                substr($row{table}, 237, 1) = pack 'U*', 0x3D5; # phi sign
@@ -284,7 +300,6 @@ for my $row (@request) {
        my $rowdiv = 16 ** $coldigits;  # row divide for column digits
        $rowdiv = 1 if $rowdiv != $cols * $colsize;  # divide only if all columns are matched
        my $offset = 0;
-       my $endpoint = $offset + $row->{len} * $colsize;
 
        printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
        my $title = $row->{set};
@@ -298,7 +313,12 @@ for my $row (@request) {
                print "\n";
        }
        print '<tbody>';
-       while ($offset < $endpoint - 1) {
+       while ($offset < $row->{endpoint}) {
+               if ($row->{skip}->{$offset}) {
+                       $offset += $cols * $colsize;
+                       next;
+               }
+
                print '<tr><th>';
                {
                        if (my $rowmod = $offset % $rowdiv) {