5 use open IO => ':utf8';
9 $header{content_type} = 'text/html; charset=utf-8';
11 :><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
12 "http://www.w3.org/TR/html4/loose.dtd">
16 <title>charset cheat sheet</title>
17 <meta http-equiv="content-type" content="utf-8">
18 <link rel="stylesheet" type="text/css" media="all" href="/base.css">
22 <h1>Character encoding</h1>
25 my $diinfo = do 'digraphs.inc.pl';
26 my %di = map { $diinfo->{$_}->[0] => $_ } grep { ref $diinfo->{$_} }
29 use Encode qw(decode);
30 # generate character table(s)
31 # (~16x faster than decoding in loop;
32 # substr strings is twice as fast as splitting to an array)
33 my @request = ('iso-8859-1', 'cp437');
34 my @tables = map { decode($_, pack 'C*', 0..255) } @request;
35 my $NOCHAR = chr 0xFFFD;
37 for my $cp437 (grep {$request[$_] eq 'cp437'} 0 .. $#request) {
38 substr($tables[$cp437], 237, 1) = pack 'U*', 0x3D5; # phi sign
39 substr($tables[$cp437], 0, 32) = pack 'U*', map {hex} qw(
40 2007 263A 263B 2665 2666 2663 2660 2022 25D8 25CB 25D9 2642 2640 266A 266B 263C
41 25BA 25C4 2195 203C 00B6 00A7 25AC 21A8 2191 2193 2192 2190 221F 2194 25B2 25BC
55 my @nibble = (0..9, 'A'..'F');
56 for my $tablenum (0 .. $#tables) {
57 print '<li><table class="glyphs">';
58 printf '<caption>%s</caption>', $request[$tablenum];
60 for my $section (qw{thead}) {
61 print "<$section><tr><th>↱";
62 print '<th>', $_ for @nibble;
66 for my $msb (0 .. $#nibble) {
67 print '<tr><th>', $nibble[$msb];
68 for my $lsb (0 .. $#nibble) {
69 my $glyph = substr $tables[$tablenum], ($msb<<4) + $lsb, 1;
70 if ($glyph eq $NOCHAR) {
74 my $info = [ord $glyph];
75 if (defined (my $mnem = $di{ord $glyph})) {
76 $info = $diinfo->{$mnem};
78 my ($codepoint, $name, $prop, $script, $string) = @$info;
80 $glyph = quote($string || $glyph);
81 my $desc = sprintf 'U+%04X%s', $codepoint, $name && " ($name)";
82 my @class = ('X', grep {$_} $prop, $script);
84 $glyph = "<span>$glyph</span>" if $prop eq 'Zs';
86 printf "\n".'<td class="%s" title="%s">%s',
87 join(' ', @class), quote($desc), $glyph;
95 print '<li><table class="glyphs"><caption>UTF-8</caption><col>';
96 for my $section (qw{thead}) {
97 print "<$section><tr><th>↱";
98 print '<th>', $_ for @nibble;
102 print '<tr rowspan="8">';
103 for my $msb (0 .. $#nibble) {
104 print '<tr><th>', $nibble[$msb];
105 for my $lsb (0 .. $#nibble) {
106 my $value = ($msb<<4) + $lsb;
107 if ($value <= 0x7F) {
108 print '<td rowspan="8" colspan="16" class="X di-a"',
109 ' title="U+0000 – U+007F">Single byte ASCII'
112 elsif ($value <= 0xBF) {
113 print '<td rowspan="4" colspan="16" class="X di-d"',
114 '>Multi-byte continuation'
117 elsif ($value <= 0xC1) {
118 print '<td colspan="2" class="X di-b" style="border-right:none; border-bottom:none"',
119 ' title="U+0000 – U+007F">(Overl.)'
122 elsif ($value <= 0xDF) {
123 print '<td rowspan="2" colspan="14" class="X di-prop" style="border-left:none"',
124 ' title="U+0080 – U+03FF">2-byte sequence start'
126 print '<td rowspan="1" colspan="16" class="X di-prop" style="border-top:none"',
127 ' title="U+0400 – U+07FF">'
130 elsif ($value <= 0xEF) {
131 print '<td colspan="16" class="X di-prop"',
132 ' title="U+0800 – U+FFFF">3-byte sequence start'
135 elsif ($value <= 0xF4) {
136 print '<td colspan="5" class="X di-prop" style="border-right:none"',
137 ' title="U+1·0000 – U+10·FFFF">4-byte sequence'
140 elsif ($value <= 0xF7) {
141 print '<td colspan="3" class="X di-b" style="border-left:none"',
142 ' title="U+11·0000 – U+1FF·FFFF">(Overflow)'
145 elsif ($value <= 0xFB) {
146 print '<td colspan="4" class="X di-b"',
147 ' title="U+200·0000 – U+3FFF·FFFF">5-byte'
150 elsif ($value <= 0xFD) {
151 print '<td colspan="2" class="X di-b"',
152 ' title="U+4000·0000 – 7FFFF·FFFF">6-byte'
155 elsif ($value <= 0xFF) {
156 print '<td colspan="2" class="di-invalid">Invalid'
160 print "\n".'<td class="X">?';
174 <a href="http://sheet.shiar.nl/" rel="home">sheet.shiar.nl</a>/charset
175 <a href="git://git.shiar.nl/sheet" rel="vcs-git" title="Git repository"><:= "v$VERSION" :></a>
176 created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
177 <a title="Licensed under the GNU Affero General Public License, version 3" rel="copyright"
178 href="http://www.fsf.org/licensing/licenses/agpl-3.0.html">AGPLv3</a> •
180 use Time::Format qw(time_format);
181 print time_format('yyyy-mm-dd', (stat $ENV{SCRIPT_FILENAME})[9]);