-
Notifications
You must be signed in to change notification settings - Fork 0
/
ord
executable file
·2197 lines (1896 loc) · 81.4 KB
/
ord
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl -w
#
# ord: Display information about Unicode characters.
# 2007-10: Written by Steven J. DeRose.
#
use strict;
use Getopt::Long;
use charnames ':full';
use Unicode::UCD 'charscript';
use Unicode::UCD 'charblock';
use Unicode::UCD 'charinfo';
use HTML::Entities;
use Encode;
use URI::Escape;
use POSIX;
#use Char::Unicode::Bidi; # ???
our %metadata = (
'title' => "ord",
'description' => "Display information about Unicode characters.",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5.18",
'created' => "2007-10",
'modified' => "2024-06-25",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
=pod
=encoding utf8
=head1 Usage
ord [options] [chars|mnemonics]
Displays Unicode character code point numbers and other information
about a character(s), or searches the Unicode character database for
particular characters are provides the list of matches in useful formats.
For example, the "BULLET" character can be specified in any of these ways:
ord • [the literal character]
ord 0x2022 [the code point in hexadecimal]
ord 8226 [the code point in decimal]
ord 020042 [the code point in octal]
ord BULLET [the Unicode name]
ord bull [the HTML 4 entity name]
ord %e2%80%a2 [UTF-8 byte sequence as escaped for use in URIs]
Any of these produces:
Unicode Name: BULLET
Unicode Category: Po (Punctuation, Other)
Unicode Script: Common
Unicode Block: General Punctuation
Unicode Plane: 0: Basic Multilingual
Literal: •
Bases: o20042 d08226 x02022
Unicode: U+02022, utf8 \xe2\x80\xa2, URI (%e2%80%a2), Python \u2022
Entities: • • •
Control characters can also be specified by "^" plus a letter, or by mnemonic.
For example, LINE FEED may be specified as "^J" or "LF".
Unix jargon names such as "splat" for "*" are recognized.
Full unicode names ignore case, but if there are spaces the name needs to be
quoted, or to use "_" instead of actual space. For example:
ord APL_FUNCTIONAL_SYMBOL_LEFTWARDS_VANE
You can find all characters whose Unicode name contains a given
string or regex, like:
ord --findRegex "LEFT.*QUO"
A single decimal digit is taken as a request for information on that
graphic character, not on the (C0 control) character (U+0000-0x0009).
Since all decimal digits are also hex digits, you can
describe characters U+0 through U+9 by prefixing them with "0x".
==Searching for characters==
To search for all Unicode characters whose names contain a certain
string or regex, that are in a given Unicode block, or that would be matched
by a given regex (for example '\\sLATIN'), use the I<--findString>,
I<--findRegex>, B<--findBlock>, or I<--findSet> option.
All I<--find...> matches ignore case.
You can limit a search to a certain numeric range with I<--minU> and I<--maxU>.
For example: I<--maxU 0xFFFF> to only search the Base Multilingual Plane.
The list resulting from any of these I<--find...> options can be written out
in many formats, including as a string of the literal characters, a detailed
information display as shown above for single characters,
sets of declarations usable in various programming
languages (see B<--listFormat>), etc. By default, it uses a single line
for each found character, like this:
U+02022 • BULLET
See also my C<CharDisplay.py>, which provides a strftime-like way to
specify custom output formats.
I<--findString> searches for a literal match within the character's full name.
To gather up Unicode characters whose formal names include "star"
(ignoring case), say:
ord --findString star
Such a search may not get all and only stars, much less all characters one might
consider using for the purpose one has in mind -- for example:
"*" (asterisk) does not have "star" in its name
Nor do many snowflakes, florettes, and sparkles.
Some characters have "star" in their name but may not be wanted:
U+00001 START OF HEADING
U+00f0c ༌ TIBETAN MARK DELIMITER TSHEG BSTAR
U+029e6 ⧦ GLEICH STARK
U+1f303 🌃 NIGHT WITH STARS
U+1f752 🝒 ALCHEMICAL SYMBOL FOR STARRED TRIDENT
I<--findRegex> searches via regex within the character's full name.
I<--findBlock> searches for characters whose Unicode block name, such as
"General Punctuation", matches the specified regex. Also prints a heading
at the start of each matching block.
I<--findSet> searches for characters which, as literals, match the specified
regex. For example, to find what characters are included by (Perl's)
regex '\\s' shorthand, use:
ord --findSet "\\s"
I<--pairs> is a special "find" that searches for symmetric pairs of characters,
such as brackets, quotes, etc, and writes them out as HTML similar
to I<--listFormat HTML>.
The parellel lists of tokens are in arrays named
\@leftWords and \@rightWords, and include a variety of left/right, open/close,
start/end, and similar pairings.
The search is a bit slow (use -v to get progress
reports as it runs). Also, it doesn't cover characters that contain multiple
such keywords.
If you specify more than one of the I<--findXXX> options, the result is
not defined.
=head1 Options
(prefix 'no' to option name to negate where applicable)
=over
=item * B<--binary>
Show code points in binary.
=item * B<--chart> I<name>
Show info for all the characters in a named range chosen from:
c0, g0, c1, g1, ASCII, Latin1, LatinxA (for Latin Extended A), LatinxB,
IPA, Combining, Greek, Cyrillic, Armenian, Hebrew, Arabic, Syriac,
ControlPics (symbols for control characters), or cp1252 (Windows Latin;
implies --cp1252).
Ask if you want others, or add an entry to '%namedRanges'.
The range can also be given as two (decimal or hex) integers,
separated by non-word characters (such as '-', ':', '...', ', ', etc.).
See also I<--c0>, I<--c1>, I<--g0>, I<--g1>, which are just synonyms
for I<--chart> with the corresponding value.
See also I<--table>, I<--digits>, and I<--math> for other kinds of charts.
Charts are not affected by I<--minU> or I<maxU>.
=item * B<--cp1252>
Assume the input character set is Windows\xAE code page I<cp1252>.
See also I<--macRoman>.
=item * B<--c0>
Same as I<--chart>, but only for C0 range (d0-d31).
=item * B<--c1>
Same as I<--chart>, but only for C1 range (d128-d159).
=item * B<--decimal>
Display code points in decimal (default).
=item * B<--digits>
Show a chart of Unicode digits.
Some of these are font/style variations, similar to the "MATHEMATICAL" alphabets
listable using I<--math>; others are non-Arabic digits.
See also I<--chart>.
=item * B<--entities>
Display HTML special-character entity names (if any), and the
SGML/HTML/XML numeric character references (decimal and hexadecimal).
=item * B<--findBlock>
Like I<--findString> (q.v.), but treats the main/final argument as the name
of a Unicode ''block'' (ignoring case), and returns all
characters assigned to the block(s) that match
(and are in the code point range from I<--min> and I<--max>).
=item * B<--findRegex> or B<--regex>
Like I<--findString> (q.v.), but treats the final
argument as a (Perl-style) regex, and returns any
characters whose names match it (ignoring case).
=item * B<--findSet> searches for characters which, as literals,
match the specified regex.
For example, to find what characters are included by (Perl's)
regex '\\s' shorthand, use:
ord --findSet "\\s"
=item * B<--findString> or just I<-f>
Return all Unicode character names that contain the main argument (this also
happens if nothing is found another way). This is a little slow.
B<Note>: By default this only searches the first two Unicode
planes(code points up to 0x1FFFF).
To search further or less far, use I<--minU> and/or I<--maxU>.
See also I<--listFormat>, I<--findRegex>, I<--findBlock>, and I<--maxU>.
=item * B<--g0>
Same as I<--chart>, but only for G0 range (d32-d127).
=item * B<--g1>
Same as I<--chart>, but only for G1 range (d160-d255).
=item * B<--hex>
Display code points in hexadecimal (default).
=item * B<--iconv>
For code points >= 128, in addition to the usual display
show the nearest ASCII equivalent, as determined by C<iconv> (q.v.).
Default: off. This requires the Perl Text::Iconv package.
B<Note>: C<iconv>'s mapping may or may not be what you want
in a given situation. For example, accented Latin letters
and Mathematical variants of Latin letters reduce to the plain ASCII letters,
Some characters, such as ligatures and the U+24xx "Control pictures", map
to multiple ASCII characters.
But Greek letters do not map at all, even when they are very closely related
typographically, historically, and phonetically (for example,
upper-case alpha (U+0391) vs. A (U+0041)).
Likewise for NO-BREAK SPACE (U+A0).
In addition, not all implementations of C<iconv>
work identically (though perhaps Perl versions always use the same one?
Don't know).
=item * B<--jargon>
Display applicable *nix jargon names (default).
=item * B<--listFormat> I<f> OR B<--outFormat> OR B<--oFormat>
Choose the output layout for results from the several I<--find...> options:
=over
=item * PLAIN (default): The hex code point and the name, tab-separated:
U+002a ASTERISK
U+0359 COMBINING ASTERISK BELOW
U+204e LOW ASTERISK
=item * LITERAL: Like PLAIN, but also showing the actual graphic character.
U+002a * ASTERISK
U+0359 ͙ COMBINING ASTERISK BELOW
U+204e ⁎ LOW ASTERISK
=item * STRING: A string of just the selected literal characters.
u"* ͙,"
=item * REGEX: A regular expression "[..]" construct, that matches just
the selected literal characters. At least as of now, non-ASCII
characters, hyphen, caret, or rsqb, and C0 controls are expressed via
backslashed hex codes. Runs of contiguous code points should be, but are not yet,
coalesced into ranges.
u"[*.\u204e]"
=item * PERL: Hash entries, from hex code point to name.
0x0002a => 'ASTERISK',
0x00359 => 'COMBINING ASTERISK BELOW',
0x0204e => 'LOW ASTERISK',
=item * PYTHONN: Dict entries, from hex code point to name, like:
0x0002a: 'ASTERISK',
0x00359: 'COMBINING ASTERISK BELOW',
0x0204e: 'LOW ASTERISK',
=item * PYTHONC: Dict entries, from unichr(hex code point) to name.
chr(0x0002a): 'ASTERISK',
chr(0x00359): 'COMBINING ASTERISK BELOW',
chr(0x0204e): 'LOW ASTERISK',
=item * PYTHONS: A Python string constructed from the characters, each
expressed like I<chr(0x0ffff)>:
chr(0x0002a) + # 'ASTERISK'
chr(0x00359) + # 'COMBINING ASTERISK BELOW'
chr(0x0204e) + # 'LOW ASTERISK'
=item * PYTHONU: A Python string constructed from the characters, each
expressed like I<u'\uffff'>.
u'\u0002a' + # 'ASTERISK'
u'\u00359' + # 'COMBINING ASTERISK BELOW'
u'\u0204e' + # 'LOW ASTERISK'
=item * INFO: A display like the output for single characters.
=item * HTML: HTML table rows with columns for the actual character,
the code point in hex, and the character name:
<tr>
<td class='char'>*</td>
<td class='hex'>U+002a</td>
<td class='name'>ASTERISK</td>
</tr>
For the ''PYTHON'' variants, you can use ''--pyFunction'' to set what
function is used (for example, ''--pyfunction chr''.
=back
Of course, for other layouts you can choose the nearest of these and
post-process as desired.
=item * B<--literal>
Include display of the literal character in output (default).
You may wish to turn this off (I<--no-literal>) if your output device
can't handle UTF-8 (you may also wish to get a new output device).
=item * B<--long>
Show long names for characters (default).
=item * B<--macRoman>
Assume the input character set is Apple macRoman.
This is also known as IANA character "macintosh"
(with aliases "mac" and "csMacintosh"), and Microsoft code page 10000.
Acc. [http://fileformats.archiveteam.org/wiki/MacRoman],
C<iconv> recognizes "CP10000", "MAC", "MACINTOSH", "MACROMAN", and "CSMACINTOSH".
See also I<--cp1252>.
=item * B<--math>
Show a display with all the mathematical variants of the Latin
and Greek alphabets. See also the I<--chart> option and the
C<mathAlphanumerics> package.
=item * B<--maxU> I<n>
Only search Unicode characters up through code point I<n> when using
any of the several I<--find...> options,
or trying to resolve an unrecognized name. Default: 65535 (0xoFFFF).
Numeric options such as I<--max> can be specified in base 8, 10, or 16.
=item * B<--minU> I<n>
Only search Unicode characters starting from code point I<n> when using
the several I<--find...> options,
or trying to resolve an unrecognized name. Default: 65535 (0xoFFFF).
Numeric options such as I<--max> can be specified in base 8, 10, or 16.
=item * B<--octal>
Display code points in octal (default).
=item * B<--pairs>
Make a valiant attempt to find symmetric pairs of characters, such as ones
whose names differ only in having "left" vs. "right", "open" vs. "close",
"begin" vs. "end", etc. Write them out as pairs. This is presently done
by searching for directional keywords within character names. There is
a Unicode database file, F<BidiMirroring.txt>, which defines such pairings,
but I have not found a convenient interface to it from either Perl or
Python (see "To Do").
Does not do "vertical" pairs, such as U+02191 UPWARDS ARROW vs.
U+02193 DOWNWARDS ARROW.
=item * B<--regex>
Synonym for I<--findRegex>.
=item * B<--short>
Show short names for characters.
=item * B<--table> I<name>
Show a compact table of characters in the named range
(c0, g0, c1, g1, ascii, or latin1).
See also I<--c0>, I<--c1>, I<--g0>, I<--g1>, I<--table>, I<--digits>,
and I<--math> for other kinds of displays.
See also I<--chart> for a different layout.
=item * B<--typing>
For non-ASCII characters, show how to key it (not yet supported, and of course
system-dependent).
=item * B<--utf8>
Show a variety of information about the UTF-8 encoding for the character(s).
Default: on.
B<Note>: To interpret hex input as UTF-8 instead of a codepoint, prefix
each byte with "%" instead of "0x" (see above). You do not need to specify
the I<--utf8> option for that.
=item * B<--version>
Show version/license info and exit.
=back
=head2 Note
You need to backslash and/or quote some characters to use them as arguments:
sp (x20, d32, o40)
\" (x22, d34, o42)
\# (x23, d35, o43)
\& (x26, d38, o46)
\' (x27, d39, o47)
\( (x28, d40, o50)
\) (x29, d41, o51)
\+ (x2b, d43, 053)
(or, you can precede this with '--' (end-of-options)
\; (x3b, d59, o73)
\< (x3c, d60, o74)
\> (x3e, d62, o76)
\\ (x5c, d92, o134)
\` (x60, d96, o140)
\| (x7c, d124, o174)
Some may be difficult or impossible to escape in some shells, such as:
\\t (x09, d09, o11) HT
\\n (x0a, d10, o12) LF (you can put the newline in double quotes)
\\r (x0d, d13, o15) CR (you can put the return in double quotes)
=head1 Known bugs and limitations
Even with a Unicode-enabled terminal, a character > 255
may appear to be length > 1 and so will be taken as a name. But when
the name is not found, the value is printed out anyway.
For C<--pairs>, in addition to a list of keywords the code has a list of character
pairs that can be considered symmetric but whose names don't contain the various
keywords. But they aren't showing up yet.
Reports look a little weird in the case of combining characters. Especially if
you search for just the combining characters, and print them out as
I<--listFormat STRING>.
Does not catch cases with multiple direction keywords, such as:
U+0200e LEFT-TO-RIGHT MARK
U+0200f RIGHT-TO-LEFT MARK
U+021c4 ⇄ RIGHTWARDS ARROW OVER LEFTWARDS ARROW
U+021c6 ⇆ LEFTWARDS ARROW OVER RIGHTWARDS ARROW
U+02966 ⥦ LEFTWARDS HARPOON WITH BARB UP ABOVE RIGHTWARDS HARPOON WITH BARB UP
U+02968 ⥨ RIGHTWARDS HARPOON WITH BARB UP ABOVE LEFTWARDS HARPOON WITH BARB UP
Does not implement vertical keywords (though has a list in the code).
Does not consider clockwise/counterclockwise cases.
Some additional cases should perhaps be discarded, such as BOX DRAWING characters.
=head1 Related commands
C<chr> -- Does the reverse.
C<showNumberInBases> -- Converts a number to multiple bases.
C<mathAlphanumerics.py> -- class to map Latin, Greek, digits to alternate
forms such as Mathematical double-struck, etc.
=head1 To do
=over
=item * Make regular reports respect --outputFormat, not just --findXXX results.
=item * Tweak --table to accept numeric range like --chart.
=item * Port to Python, using my C<CharDisplay.py> package.
=item * Make --listFormat apply to the main list, not just -f.
=item * Extend --listformat REGEX to coalesce contiguous ranges.
=item * Integrate remainder of C<chr> command functionality:
Allow numbers on command line. Compare/sync report format.
=item * Way to print/export remaining Unicode char properties.
=item * Add I<--typing> show how to key on various systems?
=item * Make I<find> on names match regardless of token order. Cf
Joel Kalvesmaki, '''A New \\u: Extending XSLT
Regular Expressions for Unicode.''' Balisage (2020).
[https://www.balisage.net/Proceedings/vol25/html/Kalvesmaki01/BalisageVol25-Kalvesmaki01.html]
=item * Use the Unicode database file, F<BidiMirroring.txt>, to implement --pairs,
rather than the present keyword-search approachs (at least as an option).
So far I have not found a convenient interface to it from either Perl or
Python.
=item * Use Unicode DB NamesList.txt file '=' entries for alternative names
(~5500; not unique).
=back
=head1 History
=over
=item * 2007-11-22 sjd: Accept control-char mnemonics as input. Getopt.
Add binary and long-name output.
=item * 2008-02-14 sjd: Multiple input chars. setupCharacterNames(). Unify $fmt.
Add longNames for G0 and G1. Add C<--g0>, C<--g1>. C<perl -w>.
=item * 2008-09-03 sjd: Move to BSD. Improve doc.
=item * 2008-09-16 sjd: Better handling of Unicode input.
=item * 2010-01-06 sjd: Use 'charnames' to know Unicode names. Add C<--binary>.
Make print utf-8 and actual Unicode character. Format binary better.
=item * 2010-05-03 sjd: perldoc. Unify formatting. Add Unix Jargon names,
rest of short names. Make user use "_" in.
=item * 2011-08-23 sjd: Add options to control each display form separately.
Start C<--cp1252>.
=item * 2011-12-11 sjd: Add utf-8 output. Opt out of longNames (lists of
char names -- now using viacode instead).
=item * 2012-01-10 sjd: Cleanup. Lose internal 'longNames' lists.
=item * 2012-08-15 sjd: sjdUtils, and use getUTF8().
=item * 2013-06-17ff sjd: Add C<--entities>, esp. HTML named ones.
Add showUnicodeInfo().
=item * 2014-09-02: Clean up.
=item * 2015-08-15ff: Add formats PYTHONS, PYTHONU; rename
format PYTHON to PYTHONN. Finish C<--find>. Display control-char mnemonics.
=item * 2016-01-08: Add C<--math> and C<--digits>.
=item * 2016-02-18: Add C<--regex>, C<--listFormat> CHART and HTML.
Warn if env LANG not utf-8. Recognize html entity names, C-J, ^J, etc.
=item * 2016-08-30: Get rid of sjdUtils.pm dependencies.
=item * 2016-09-05: Add C<--iconv> option.
=item * 2018-01-19: Add C<--pairs>.
=item * 2020-01-03: Add C<--minU>.
=item * 2020-03-17: Factor out all the %05x formatting. Add I<--findSet>.
=item * 2020-09-03: Clean up. Add warning for I<--findString> w/ bad chars.
=item * 2020-11-30: Support hex UTF-8 input.
=item * 2021-07-24: Add character categories.
=item * 2021-08-05: Add --table, re-do --chart.
=item * 2021-09-13: Clean up --chart, make it allow explicit int...int ranges,
ignore case for name lookups, have more named ranges.
=item * 2022-07-25: Fix off-by-one on reporting control-key letters for C0.
=item * 2022-10-10: Add names for Unicode categories, from CharDisplay.py.
=item * 2023-02-08: Add REGEX as output format option, though it doesn't yet
coalesce contiguous ranges.
=item * 2023-03-15: Show name and mapping for CP1252 usage of C1 controls.
=item * 2023-07-25: Report private-use chars specially rather than as unassigned.
=item * 2024-02-26: Add reporting for C1 as MacRoman possibility.
=item * 2024-05-22: Support utf-8 as %ffffff, not just %ff%ff%ff.
=item * 2024-06-25: Add --chart cp1252.
=back
=head1 Rights
Copyright 2007-11-22 by Steven J. DeRose. This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.
For further information on this license, see
L<https://creativecommons.org/licenses/by-sa/3.0>.
For the most recent version, see L<http://www.derose.net/steve/utilities> or
L<https://github.com/sderose>.
=cut
###############################################################################
#
my @C0names = ();
my @G0names = ();
my @C1names = ();
my @G1names = ();
my $upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
my $lower = 'abcdefghijklmnopqrstuvwxyz';
my $URIchars = "!\$'()*+-._" . '0123456789' . $upper . $lower;
my $URIspecials = '/%&:;=?@';
# sprintf() formats for code point numbers. TODO: Make adjustable width
#
my $theHexFormat = "0x%05x";
my $theUFormat = "U+%05x";
my $theEntFormat = "&#x%05x;";
my $thePyFormat = "u'\\u%05x'";
my %unixJargon = ();
my %unicodeCategories = (
"C" => "Other (all subtypes)",
"L" => "Letter (all subtypes)",
"M" => "Mark (all subtypes)",
"N" => "Number (all subtypes)",
"P" => "Punctuation (all subtypes)",
"S" => "Symbol (all subtypes)",
"Z" => "Separator (all subtypes)",
"Cc" => "Other, Control",
"Cf" => "Other, Format",
"Cn" => "Other, Not Assigned",
"Co" => "Other, Private Use",
"Cs" => "Other, Surrogate",
"LC" => "Letter, Cased",
"Ll" => "Letter, Lowercase",
"Lm" => "Letter, Modifier",
"Lo" => "Letter, Other",
"Lt" => "Letter, Titlecase",
"Lu" => "Letter, Uppercase",
"Mc" => "Mark, Spacing Combining",
"Me" => "Mark, Enclosing",
"Mn" => "Mark, Nonspacing",
"Nd" => "Number, Decimal Digit",
"Nl" => "Number, Letter",
"No" => "Number, Other",
"Pc" => "Punctuation, Connector",
"Pd" => "Punctuation, Dash",
"Pe" => "Punctuation, Close",
"Pf" => "Punctuation, Final quote",
"Pi" => "Punctuation, Initial quote",
"Po" => "Punctuation, Other",
"Ps" => "Punctuation, Open",
"Sc" => "Symbol, Currency",
"Sk" => "Symbol, Modifier",
"Sm" => "Symbol, Math",
"So" => "Symbol, Other",
"Zl" => "Separator, Line",
"Zp" => "Separator, Paragraph",
"Zs" => "Separator, Space",
);
setupShortCharacterNames();
setupUnixJargon();
my ($mathAlphabets, $mathGreeks, $digitSets, %mathMissing);
setupMathAlphabets();
###############################################################################
# Options
#
my $binary = 0;
my $chart = "";
my $cp1252 = 0;
my $decimal = 1;
my $digits = 0;
my $entities = 1;
my $findBlock = 0;
my $findRegex = 0;
my $findSet = 0;
my $findString = 0;
my $C0 = 0;
my $C1 = 0;
my $G0 = 0;
my $G1 = 0;
my $hex = 1;
my $iconv = 0;
my $jargon = 1;
my $listFormat = "LITERAL";
my $literal = 1;
my $long = 1;
my $macRoman = 0;
my $math = 0;
my $maxU = 0x1FFFF;
my $minU = 1;
my $octal = 1;
my $pairs = 0;
my $pyFunction = 'chr';
my $quiet = 0;
my $short = 0;
my $typing = 1;
my $table = "";
my $utf8 = 1;
my $verbose = 0;
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"binary!" => \$binary,
"c|chart=s" => \$chart,
"cp1252!" => \$cp1252,
"c0" => \$C0,
"c1" => \$C1,
"decimal!" => \$decimal,
"digits!" => \$digits,
"entities!" => \$entities,
"findBlock!" => \$findBlock,
"findRegex|regex!" => \$findRegex,
"findSet!" => \$findSet,
"f|findString!" => \$findString,
"g0" => \$G0,
"g1" => \$G1,
"h|help|?" => sub { system "perldoc $0"; },
"hex!" => \$hex,
"iconv!" => \$iconv,
"jargon!" => \$jargon,
"listFormat|oFormat|outputFormat|output-format=s" => \$listFormat,
"literal!" => \$literal,
"long!" => \$long,
"macRoman!" => \$macRoman,
"math!" => \$math,
"maxU=o" => \$maxU,
"minU=o" => \$minU,
"octal!" => \$octal,
"pairs!" => \$pairs,
"pyFunction=s" => \$pyFunction,
"q|quiet!" => \$quiet,
"short!" => \$short,
"table=s" => \$table,
"typing!" => \$typing,
"utf8!" => \$utf8,
"v|verbose+" => \$verbose,
"version" => sub {
warn "Version of $VERSION_DATE, by Steven J. DeRose.\n";
exit;
},
);
($result) || die "Bad options.\n";
if (!$quiet && $findString && $ARGV[0] =~ m/[^A-Za-z 0-9]/) {
# ()., also occur in my Charsets/Unicode/metaUnicode/NamesList.txt, but
# I think they're only from synonym / alt name entries?
warn "\nWARNING: --findString for '" . $ARGV[0] .
"' has characters not used in Unicode names. Use --findRegex?\n";
}
if ($chart eq "cp1252") { $cp1252 = 1; }
my $iconvConverter = undef;
if ($iconv) {
(sjdUtils::try_module("Text::Iconv")) || die
"CPAN module TextIconv not found. Install it if you want --iconv.\n";
$iconvConverter = Text::Iconv->new("utf8", "ascii//TRANSLIT");
}
$listFormat = uc($listFormat);
my %listFormats = (
"HTML" => 1,
"INFO" => 1,
"LITERAL" => 1,
"PERL" => 1,
"PLAIN" => 1,
"PYTHONN" => 1,
"PYTHONS" => 1,
"PYTHONU" => 1,
"PYTHONC" => 1,
"STRING" => 1,
"REGEX" => 1,
);
(defined $listFormats{$listFormat}) || die
"Unknown --listFormat '$listFormat'. Known: "
. join(", ", sort keys %listFormats) . "\n";
($minU < $maxU) || die
"--minU ($minU)must be less than --maxU ($maxU).\n";
###############################################################################
#
sub showDigits {
my $prevName = "";
for (my $i=0; $i<scalar(@{$digitSets}); $i++) {
my $foo = $digitSets->[$i];
my $cpA = $foo->[2];
my $name = $foo->[0];
printf("******* %s (%s...):\n", uc($name), uDisp($cpA));
my $msg = "";
my $howMany = $foo->[4] - $foo->[3] + 1;
if ($foo->[3] > 0) { $msg .= " " x $foo->[3]; }
for (my $n=0; $n<$howMany; $n++) {
$msg .= sprintf("%3s", chr($cpA+$n));
}
print($msg . "\n");
$prevName = $name;
}
}
sub showMathAlphabets {
print("\nLatin:\n");
for (my $i=0; $i<scalar(@{$mathAlphabets}); $i++) {
my $foo = $mathAlphabets->[$i];
my $cpA = $foo->[2];
printf("******* %s (%s...):\n", uc($foo->[0]), uDisp($cpA));
my $msg = my $extra = "";
for (my $cp=$cpA; $cp<$cpA+26; $cp++) {
if ($mathMissing{$cp}) {
$msg .= ' -';
$extra .= sprintf("%s is %s, ",
chr($mathMissing{$cp}), uDisp($mathMissing{$cp}));
}
else { $msg .= sprintf("%3s", chr($cp)); }
}
print($msg . "\n");
if ($extra) { print(" Also: $extra.\n"); }
}
print("\nGreek:\n");
for (my $i=0; $i<scalar(@{$mathGreeks}); $i++) {
my $foo = $mathGreeks->[$i];
my $cpA = $foo->[2];
printf("******* %s (%s...):\n", uc($foo->[0]), uDisp($cpA));
my $msg = "";
for (my $cp=$cpA; $cp<$cpA+25; $cp++) {
$msg .= sprintf("%3s", chr($cp));
}
print($msg . "\n");
}
}
###############################################################################
#
# Try to locate characters that come in symmetric pairs, such as:
# left/right up/down open/close increment/decrement? contains/contained
# Not trying:
# intersection/union all/exists, lt/gt,
#
my @leftWords = (
'LEFT', 'LEFTWARDS', 'LEFT-POINTING', 'LEFT-FACING',
'RIGHT-TO-LEFT', 'LEFT-HANDED', 'LEFT-SIDE',
'OPEN', 'BEGIN', 'START',
'LESS-THAN', 'PRECEDES', 'SUBSET', 'IMAGE', 'ELEMENT OF');
my @rightWords = (
'RIGHT', 'RIGHTWARDS', 'RIGHT-POINTING', 'RIGHT-FACING',
'LEFT-TO-RIGHT', 'RIGHT-HANDED', 'RIGHT-SIDE',
'CLOSE', 'END', 'END',
'GREATER-THAN', 'SUCCEEDS', 'SUPERSET', 'ORIGINAL', 'CONTAINS');
(scalar @leftWords == scalar @rightWords) || die "leftWords/rightWords mismatch.\n";
my @upWords = (
'UP', 'UPWARDS', 'UP-POINTING', 'UP-FACING',
'DOWN-TO-UP', 'RISING',
);
my @downWords = (
'DOWN', 'DOWNWARDS', 'DOWN-POINTING', 'DOWN-FACING',
'UP-TO-DOWN', 'FALLING',
);
(scalar @upWords == scalar @downWords) || die "upWords/downWords mismatch.\n";
# List cases we know we don't catch...
my %otherPairs = (
# Dingbats
0x275B => 0x275C, # squo ornament
0x275D => 0x275E, # dquo ornament
0x275F => 0x2760, # low comma ornament
0x27C8 => 0x27C9, # rsol subset / superset sol
0x27D3 => 0x27D4, # lower right / upper left corner (box drawing?)
# Dominos and mah jong tiles? Braille patterns?
# Emoticons? smile/frown
# Alchemical aqua fortis vs. regis?
# Punctuation (prime vs. reversed)
0x2032 => 0x2035,
0x2033 => 0x2036,
0x2034 => 0x2037,
# 2057 => ???
# Logical/math inversion
0x2200 => 0x2203,
0x2206 => 0x2206,
0x2208 => 0x2209,
0x220A => 0x220D,
0x220B => 0x220C,
0x22B2 => 0x22B3,
0x22B4 => 0x22B5,
# Controls
0x0001 => 0x0003,
0x0002 => 0x0002,
0x0005 => 0x0006,
0x000E => 0x000F,
0x2401 => 0x2403,
0x2402 => 0x2402,
0x2405 => 0x2406,
0x240E => 0x240F,
0x10140 => 0x10141,
);
if ($verbose > 1) {
while ((my $key, my $value) = each (%otherPairs)) {
warn sprintf("otherPairs: $theHexFormat vs. $theHexFormat\n", $key, $value);
}
}
# Exclude a handful of spurious matches
my %lame = (
0x0FD5=>1, 0x0FD6=>1, 0x0FD7=>1, 0x0FD8=>1, # Tobetan svasti signs
);
sub searchForSymmetricPairs {
print(qq@<html>
<head>
<style type="text/css">
td.char { text-align:center; font-size:larger; }
</style>
</head>
<body>
<h1>Table of Unicode symmetric pairs (approximate)</h1>
<p>(generated by <i>ord</i>, by Steven J. DeRose)</p>
<table border="border">
@);
# Could do "-" special, but want to avoid "OPEN-HEADED ARROW", etc.
my $allExpr = join("|", @leftWords) .'|' . join("|", @rightWords);
my $leftExpr = join("|", @leftWords);
my $rightExpr = join("|", @rightWords);
($verbose > 1) && warn sprintf(
"Regexes for search:\n %s\n %s\n", $leftExpr, $rightExpr);
my %pairMap = ();
for (my $i=0; $i<scalar(@leftWords); $i++) {
$pairMap{$leftWords[$i]} = $rightWords[$i];
($verbose > 1) && warn sprintf("pairing keyword '%s' with '%s'\n",
$leftWords[$i], $pairMap{$leftWords[$i]});
}
# Gather characters that have any of the directional keywords as name tokens.
my $nCandidates = 0;
my @candidates = ();
my %candidateIndex = ();
for (my $i=$minU; $i<=$maxU; $i++) {
($verbose && $i % 1<<12 == 0) && warn
sprintf("At code point %s, %d candidates\n", hexDisp($i), $nCandidates);
if (exists $lame{$i}) { next; }
my $iname = charnames::viacode($i);
if (!$iname) {
$iname = "NONE";
#warn sprintf("Cannot get name for %s.\n", uDisp($i));
next;
}
# TODO: This isn't working...
if (exists $otherPairs{$i} || ($iname =~ m/\b($allExpr)\b/)) {
($verbose) && warn sprintf(" Candidate %s: %s\n", hexDisp($i), $iname);
push(@candidates, [$i, $iname]);
$candidateIndex{$iname} = $i;