-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathord
executable file
·2712 lines (2361 loc) · 102 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 feature 'unicode_strings';
use Encode qw(decode_utf8);
use utf8;
#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-08-10",
'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 and provides the list of matches in various formats.
For example, information about the "BULLET" character can be requested
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 (case doesn't matter)]
ord bull [the HTML 4 entity name (case I<does> matter)]
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: • • •
A single decimal digit is taken as a request for information on that
graphic character, not on the (C0 control) character (U+0001 to+0009).
Since all decimal digits are also hex digits, you can
describe characters U+1 through U+9 by prefixing "0x"
(or ones from 1-7 by making them octal by prefixing just "0").
NULL is not available because it's a pain.
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 also 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"
ord APL_FUNCTIONAL_SYMBOL_LEFTWARDS_VANE
You can also search for all Unicode characters (or those in a given numeric
range) that have various properties.
For example, whose Unicode name contains a given string or regex, like:
ord -f "APL FUNCTIONAL SYMBOL"
ord --findRegex "LEFT.*QUO"
==Searching for characters==
The search options are I<--findString>,
I<--findRegex>, I<--findBlock>, I<--findCategory>, and I<--findSet>.
All I<--find...> variations ignore case.
By default, they only search the first two Unicode
planes (code points up to 0x1FFFF). Even that takes much of a minute.
To search further or less far, use I<--minU> and/or I<--maxU>.
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 does 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. It 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<--oformat HTML>.
The parallel lists of tokens used in the search are in arrays named
\@leftWords and \@rightWords, and include a variety of left/right, open/close,
start/end, and similar pairings.
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.
==Report formats==
Each identified character generates a report.
I<--oformat> controls the output format produced for those reports.
The default is CONSOLE, which
gives lots of detail (the same as for specific characters requested
on the command line). PLAIN and LITERAL are nice for brevity, and various
others are handy for making strings, regexes, lists, or dicts in programs.
See also my C<CharDisplay.py>, which provides a strftime-like way to
specify custom output formats.
=head1 Options
(prefix 'no' to option name to negate where applicable)
=over
=item * B<--binary>
Show code points in binary.
=item * B<--chart> I<nameOrRange>
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).
The chart is shown in the requested I<--oformat> (default: CONSOLE).
The range can instead be given as two (decimal or hex) integers,
separated by non-word characters (such as "-", ":", "...", ", ", etc.).
I<--c0>, I<--c1>, I<--g0>, I<--g1>, which are just synonyms
for I<--chart> with the corresponding ranges.
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<--cheatsheet>
Show a cheat-sheet of special characters this author thinks most useful.
Each entry display the code point, literal, HTML entity name, a proposed
entity name for cases where HTML lacks any, and the Unicode truename.
[unfinished] -- at the moment, this option produces codepoint 0 and unsupported
entity names for many of the characters. See my C<makeHugeEntitySet.py>
for a proposal for a much more diverse set of entity names.
=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 c0> (U+00 to U+1F).
=item * B<--c1>
Same as I<--chart c1> (U+80 to U+9F).
=item * B<--decimal>
Display code points in decimal (default).
=item * B<--digits>
Show the various sets of Unicode digits.
Some of these are font/style variations, similar to the "MATHEMATICAL" alphabets
listable using I<--math>; others are non-Arabic digits.
=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> [block]
Search for characters in Unicode blocks whose block-names contain [block]
(ignoring case).
=item * B<--findRegex> [regex] or B<--regex> [regex]
Search for characters whose names match the (Perl-style) [regex] (ignoring case).
=item * B<--findSet> [string]
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> [string] or just I<-f> [string]
Searches for characters whose names contain string.
This search also happens for main arguments
if nothing is found another way).
=item * B<--findToken> [string]
Search for characters whose names contain all the space-separate items from
[string], regardless of token order. Thanks for the idea to
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 * B<--g0>
Same as I<--chart g0> (U+20 to U+7F).
=item * B<--g1>
Same as I<--chart g1> (U+A0 to U+FF).
=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<--oFormat> OR B<--listFormat> I<f> OR B<--outFormat>
Choose the output layout for results from the several I<--find...> and
I<--chart> options: CONSOLE, HTML, LITERAL, PERLDICT_NUM,
PLAIN, PYDICT_NUM, PYDICT_CHAR, PYDICT_NUM, PYSTR, REGEX, STRING.
=over
=item * CONSOLE (default): The full display like you get for characters
specifically requested.
=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>
=item * LITERAL: Like PLAIN, but also showing the actual graphic character
(this assumes your terminal or other output mechanism can handle them):
U+002a * ASTERISK
U+0359 ͙ COMBINING ASTERISK BELOW
U+204e ⁎ LOW ASTERISK
=item * PERLDICT_NUM: Hash entries, from hex code point to name.
0x0002a => "ASTERISK",
0x00359 => "COMBINING ASTERISK BELOW",
0x0204e => "LOW ASTERISK",
=item * PLAIN: The hex code point in U+ form, the named or hex XML/HTML
reference to get it, and the Unicode truename, tab-separated:
U+002a ASTERISK
U+0359 COMBINING ASTERISK BELOW
U+204e LOW ASTERISK
=item * PYDICT_CHAR: Dict entries, from unichr(hex code point) to name.
chr(0x0002a): "ASTERISK",
chr(0x00359): "COMBINING ASTERISK BELOW",
chr(0x0204e): "LOW ASTERISK",
=item * PYDICT_NUM: Dict entries, from hex code point to name, like:
0x0002a: "ASTERISK",
0x00359: "COMBINING ASTERISK BELOW",
0x0204e: "LOW ASTERISK",
=item * PYSTR: 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 * 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 (cf showUnicodeCharsInClass.py --bracket).
u"[*.\u204e]"
=item * STRING: A string of just the selected literal characters.
u"* ͙,"
For the PY... variants, you can use ''--pyFunction'' to set what
function is used (for example, ''--pyfunction chr''.
=back
=item * B<--literal>
Include display of the literal character in CONSOLE format (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 in CONSOLE format (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.
According to [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> or B<--maxChar> 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.
B<Note>: Last I checked, the highest "real" Unicode character (that is,
not counting TAGs, VARIATION SELECTORs, and PRIVATE USE), was:
2FA1D;CJK COMPATIBILITY IDEOGRAPH-2FA1D
=item * B<--minU> I<n> or B<--minChar> 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 (0x0FFFF).
Numeric options such as I<--maxU> can be specified in base 8, 10, or 16.
=item * B<--octal>
Display code points in octal in CONSOLE format (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, or quads like those plus leftwards and rightwards arrows.
=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> (not yet supported)
For non-ASCII characters, show how to key it. On windows the general idea
is to hold ALT while typing the decimal code point value.
However, it matters whether you give a leading
zero or not (maybe one is CP1252 and the other is Unicode? Not sure).
I think you can only use the numeric keypad digits.
=item * B<--utf8>
Show a variety of information about the UTF-8 encoding for the character(s)
in CONSOLE format. 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:
\\0 (x00, d00, o00) NULL
\\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<--oformat 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 consider 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. But this does that direction too.
C<showNumberInBases> -- Converts a number to multiple bases.
C<mathAlphanumerics.py> -- Maps Latin, Greek, and/or digits to alternate
forms such as Mathematical double-struck, etc.
C<charNameConvert.py> -- An unfinished package based on
my old friend Sebastian Rahtz's work developing a database of how various
characters are named in various standards.
=head1 To do
=over
=item * Hook up C<charNameConvert.py>.
=item * Integrate with Sebastian Rahtz's extraordinary database
of character names in various standards (cf <charNameConvert.py>).
=item * Pull in --oformat bracket feature as from showCharsInPYDICT_CHARlass.
I.e., extend --oformat REGEX to coalesce contiguous ranges.
=item * Make regular reports respect --oformat, not just --findXXX results.
=item * Tweak --table to accept numeric range like --chart.
=item * Port to Python, using my C<CharDisplay.py> package.
=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 * Use the Unicode database file, F<BidiMirroring.txt>, to implement --pairs,
rather than the present keyword-search approach (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).
=item * Option to search in reverse order
=item * Option to find specifically unassigned code points.
=item * Option to show XML-related information (such as if prohibited,
if allowed as NMCHAR or NMSTARTCHAR).
=item * Hook up https://www.unicode.org/Public/security/8.0.0/confusables.txt.
=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, PYSTR; rename
format PYTHON to PYDICT_NUM. 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-25ff: Add --chart cp1252. Support --listFormat with --chart.
=item * 2024-07-28ff: Add --cheatsheet.
=item * 2024-08-10: Add --findCategory, --findTokens. Clean up --findXXX to
not re-use $target, and to run independently of $targets, not
just as fallback. Add --showCategories. Better names for Python --oformats
and rename listFormat to oformat.
=item * 2024-10-23: Support strings of literal (not all ASCII) chars.
=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, maybe completely option-settable.
#
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", # This seems to just be a cover term?
"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 $cheatsheet = 0;
my $cp1252 = 0;
my $decimal = 1;
my $digits = 0;
my $entities = 1;
my $findBlock = "";
my $findCategory= "";
my $findRegex = "";
my $findSet = "";
my $findString = "";
my $findTokens = "";
my $C0 = 0;
my $C1 = 0;
my $G0 = 0;
my $G1 = 0;
my $hex = 1;
my $iconv = 0;
my $jargon = 1;
my $oFormat = "CONSOLE";
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 $showCategories = 0;
my $typing = 1;
my $table = "";
my $utf8 = 1;
my $verbose = 0;
my @theTokens = (); # Parsed from --findTokens
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"binary!" => \$binary,
"c|chart=s" => \$chart,
"cheatsheet!" => \$cheatsheet,
"cp1252!" => \$cp1252,
"c0" => \$C0,
"c1" => \$C1,
"decimal!" => \$decimal,
"digits!" => \$digits,
"entities!" => \$entities,
"findBlock=s" => \$findBlock,
"findCategory=s" => \$findCategory,
"findRegex|regex=s" => \$findRegex,
"findSet=s" => \$findSet,
"f|findString=s" => \$findString,
"findTokens=s" => \$findTokens,
"g0" => \$G0,
"g1" => \$G1,
"h|help|?" => sub { system "perldoc $0"; },
"hex!" => \$hex,
"iconv!" => \$iconv,
"jargon!" => \$jargon,
"listFormat|oFormat|outputFormat|output-format=s" => \$oFormat,
"literal!" => \$literal,
"long!" => \$long,
"macRoman!" => \$macRoman,
"math!" => \$math,
"maxU|maxChar=o" => \$maxU,
"minU|minChar=o" => \$minU,
"octal!" => \$octal,
"pairs!" => \$pairs,
"pyFunction=s" => \$pyFunction,
"q|quiet!" => \$quiet,
"short!" => \$short,
"showCategories!" => \$showCategories,
"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 ($showCategories) {
print("Unicode character category list:\n");
for my $c (sort keys %unicodeCategories) {
printf(" %-3s %s\n", $c.":", $unicodeCategories{$c});
}
exit;
}
if ($chart eq "cp1252") { $cp1252 = 1; }
if (!$quiet && $findString =~ 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. Switching to --findRegex.\n";
$findRegex = $findString;
$findString = "";
}
if ($findCategory && !exists $unicodeCategories{$findCategory}) {
die "Unknown --findCategory target '$findCategory'.\n";
}
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");
}
$oFormat = uc($oFormat);
my %oFormats = (
"CONSOLE" => 1, # The default for non-chart use
"HTML" => 1,
"LITERAL" => 1,
"PERLDICT_NUM" => 1,
"PLAIN" => 1,
"PYDICT_CHAR" => 1,
"PYDICT_NUM" => 1, # TODO: Better names for these --
"PYSTR" => 1,
"REGEX" => 1,
"STRING" => 1,
);
(defined $oFormats{$oFormat}) || die
"Unknown --oformat '$oFormat'. Known: "
. join(", ", sort keys %oFormats) . "\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,