raz / commit 40536519

commit
40536519d850ab4ed4535b741341efde57b4ff5d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
3e1c4e5b
Width: the columns of a terminal a character takes

width(c) and columns(s): 0 for a combining mark, a format character, a
control and Hangul's medial and final jamo, 2 for East Asian Wide and
Fullwidth, 1 for the rest; glibc's wcwidth but for a control and a few
format characters. Its tables, from Unicode 18.0.0, are made again by
tools/Widths.raz from EastAsianWidth.txt, UnicodeData.txt and
PropList.txt. test/check.sh shows that importing it uncalled adds
nothing to a program.

patch · log

8 files changed, +446 -5

Makefile (history) modified +1 -1

@@ -106,7 +106,7 @@
106106 # raz-lib(3), the library's manual page, made by tools/Doc.raz from
107107 # its sources, in the order of doc/raz-lib.md
108108 LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \
109- Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt C
109+ Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt Width C
110110
111111 man: build/raz-lib.3
112112

README.md (history) modified +1 -1

@@ -168,7 +168,7 @@
168168 `Rt` (ending the program, failed checks, signals), `Sys` (system calls), `Out`
169169 (buffered output), `C`, `Lock`, `Thread` (threads without C), `Mem`,
170170 `Heap`, `Arena`, `Str`, `Bytes`, `Buf`, `Fmt`, `Math`, `Files`, `Opt` (a command
171-line read through one table), `List(T)`, `Map(K, V; hash, eq)`,
171+line read through one table), `Width` (the columns of a terminal text takes), `List(T)`, `Map(K, V; hash, eq)`,
172172 `Vec(T)` (a growable array) and `Sort(T; less)`. Volume 2 of the report, `doc/raz-lib.md`, gives each one's interface,
173173 and `tools/Doc.raz` prints it from the module's file:
174174

doc/raz-lib.md (history) modified +23 -0

@@ -30,6 +30,7 @@
3030 | `Math` | roots, powers, logarithms, rounding and trigonometry, and `pi`, for `F64` |
3131 | `Files` | an argument as a string, and a whole file read or written |
3232 | `Opt` | a command line read through one table, by `getopt`'s rules with long options; its help and its mistakes |
33+| `Width` | how many columns of a terminal a character, or a string, takes: 0, 1 or 2, as `wcwidth` says |
3334
3435 Lengths, counts, file descriptors, process ids and statuses are
3536 `INT`s, and a character is an `I32`. What may pass an `INT` on a small
@@ -890,3 +891,25 @@
890891 others are an option with neither a letter nor a long name, a long
891892 name with `=` in it, a letter `-`, and `-h` or `--help` in the table,
892893 since they are `Opt`'s.
894+
895+**Width** says how many columns of a terminal text takes, for a
896+program with a screen that wraps its lines and cuts them at the
897+window's edge.
898+
899+```
900+module Width;
901+proc width*(c: I32): INT; (* the columns the character c takes: 0, 1 or 2 *)
902+proc columns*(s: []U8): INT; (* the columns the UTF-8 text s takes *)
903+```
904+
905+A combining mark, a format character, a control and Hangul's medial
906+and final jamo take 0 columns; an ideograph, a kana, most emoji and
907+the rest that Unicode calls East Asian Wide or Fullwidth take 2;
908+every other character takes 1, the ambiguous ones among them, as most
909+terminals draw them. These are the C library's `wcwidth`'s answers,
910+but for a control, which is -1 there. `Width` knows characters, not
911+clusters: an emoji joined to another by a zero width joiner, or a flag,
912+counts as its parts. Its tables, about 4 KB, come from Unicode's data,
913+made again by `tools/Widths.raz` for a new version of Unicode; a
914+program that imports `Width` and calls neither procedure carries none
915+of them.

doc/raz.md (history) modified +2 -2

@@ -1690,8 +1690,8 @@
16901690 language, reals aside, is the core of the library: `Rt`, `Out`, `Mem`,
16911691 `Arena`, `Lock`, `Str` and `Buf`, and of `Sys` what they need. A program
16921692 that keeps to the core runs on every target. The rest of the library
1693-(`Heap`, `Fmt`, `List`, `Map`, `Vec`, `Sort`, `Math`, `Files`, `Opt` and the
1694-rest of `Sys`) is the standard library: a target may leave a module
1693+(`Heap`, `Fmt`, `List`, `Map`, `Vec`, `Sort`, `Math`, `Files`, `Opt`, `Width`
1694+and the rest of `Sys`) is the standard library: a target may leave a module
16951695 out, and one that has it gives what `doc/raz-lib.md` says. `conform/` tests
16961696 the core only.
16971697

doc/report-history.md (history) modified +3 -1

@@ -188,4 +188,6 @@
188188 its type, `Color.red`; two enumerations of a scope may share a name,
189189 or one share a name with another declaration, and it is then written
190190 with its type (section 5.7). `continue` ends a loop's round (sections
191- 7.2 and 7.7). It breaks a program that has a name `continue`.
191+ 7.2 and 7.7). It breaks a program that has a name `continue`. The
192+ standard library gains `Width`, the columns of a terminal a character
193+ takes (volume 2).

lib/Width.raz (history) added +218 -0

@@ -0,0 +1,218 @@
1+(* Width: how many columns of a terminal a character takes, so that a
2+ program with a screen can wrap its lines and cut them at the
3+ window's edge.
4+
5+ A character is 0 columns wide if it is a combining mark (Unicode's
6+ general categories Mn and Me), a format character (Cf) other than
7+ the soft hyphen and the marks set over the digits after them
8+ (Prepended_Concatenation_Mark), a control character (Cc), whose
9+ effect is the program's to decide, one of Hangul's medial and final
10+ jamo, which join the syllable before them (1160H to 11FFH, 0D7B0H to
11+ 0D7FFH), or a Hangul filler (3164H, 0FFA0H). It is 2 if Unicode's
12+ East Asian Width is Wide or Fullwidth, as the ideographs, the kana
13+ and most emoji are. Every other one, the ambiguous ones among them,
14+ is 1, as most terminals take them. These are the answers of glibc's
15+ wcwidth, but for a control, which is -1 there, and a few format
16+ characters glibc counts as 1.
17+
18+ It knows characters, not clusters: an emoji joined to another by a
19+ zero width joiner, or a flag, two regional indicators, counts as its
20+ parts. The tables below are made from Unicode's data by
21+ tools/Widths.raz, which says how; they take about 4 KB, which a
22+ program that doesn't call width or columns doesn't carry. *)
23+module Width;
24+import Rt, Str;
25+
26+(* The tables: each a sorted list of ranges, first and last. Made by
27+ tools/Widths.raz: don't edit them, make them again. *)
28+(* tables *)
29+(* Unicode 18.0.0 *)
30+const
31+ Zero: []I32 = [
32+ 0000000H, 000001FH, 000007FH, 000009FH, 0000300H, 000036FH, 0000483H, 0000489H,
33+ 0000591H, 00005BDH, 00005BFH, 00005BFH, 00005C1H, 00005C2H, 00005C4H, 00005C5H,
34+ 00005C7H, 00005C9H, 0000610H, 000061AH, 000061CH, 000061CH, 000064BH, 000065FH,
35+ 0000670H, 0000670H, 00006D6H, 00006DCH, 00006DFH, 00006E4H, 00006E7H, 00006E8H,
36+ 00006EAH, 00006EDH, 0000711H, 0000711H, 0000730H, 000074AH, 00007A6H, 00007B0H,
37+ 00007EBH, 00007F3H, 00007FDH, 00007FDH, 0000816H, 0000819H, 000081BH, 0000823H,
38+ 0000825H, 0000827H, 0000829H, 000082DH, 0000859H, 000085BH, 0000897H, 000089FH,
39+ 00008CAH, 00008E1H, 00008E3H, 0000902H, 000093AH, 000093AH, 000093CH, 000093CH,
40+ 0000941H, 0000948H, 000094DH, 000094DH, 0000951H, 0000957H, 0000962H, 0000963H,
41+ 0000981H, 0000981H, 00009BCH, 00009BCH, 00009C1H, 00009C4H, 00009CDH, 00009CDH,
42+ 00009E2H, 00009E3H, 00009FEH, 00009FEH, 0000A01H, 0000A02H, 0000A3CH, 0000A3CH,
43+ 0000A41H, 0000A42H, 0000A47H, 0000A48H, 0000A4BH, 0000A4DH, 0000A51H, 0000A51H,
44+ 0000A70H, 0000A71H, 0000A75H, 0000A75H, 0000A81H, 0000A82H, 0000ABCH, 0000ABCH,
45+ 0000AC1H, 0000AC5H, 0000AC7H, 0000AC8H, 0000ACDH, 0000ACDH, 0000AE2H, 0000AE3H,
46+ 0000AFAH, 0000AFFH, 0000B01H, 0000B01H, 0000B3CH, 0000B3CH, 0000B3FH, 0000B3FH,
47+ 0000B41H, 0000B44H, 0000B4DH, 0000B4DH, 0000B53H, 0000B56H, 0000B62H, 0000B63H,
48+ 0000B82H, 0000B82H, 0000BC0H, 0000BC0H, 0000BCDH, 0000BCDH, 0000C00H, 0000C00H,
49+ 0000C04H, 0000C04H, 0000C3CH, 0000C3CH, 0000C3EH, 0000C40H, 0000C46H, 0000C48H,
50+ 0000C4AH, 0000C4DH, 0000C55H, 0000C56H, 0000C62H, 0000C63H, 0000C81H, 0000C81H,
51+ 0000CBCH, 0000CBCH, 0000CBFH, 0000CBFH, 0000CC6H, 0000CC6H, 0000CCCH, 0000CCDH,
52+ 0000CE2H, 0000CE3H, 0000D00H, 0000D01H, 0000D3BH, 0000D3CH, 0000D41H, 0000D44H,
53+ 0000D4DH, 0000D4DH, 0000D62H, 0000D63H, 0000D81H, 0000D81H, 0000DCAH, 0000DCAH,
54+ 0000DD2H, 0000DD4H, 0000DD6H, 0000DD6H, 0000E31H, 0000E31H, 0000E34H, 0000E3AH,
55+ 0000E47H, 0000E4EH, 0000EB1H, 0000EB1H, 0000EB4H, 0000EBCH, 0000EC8H, 0000ECEH,
56+ 0000F18H, 0000F19H, 0000F35H, 0000F35H, 0000F37H, 0000F37H, 0000F39H, 0000F39H,
57+ 0000F71H, 0000F7EH, 0000F80H, 0000F84H, 0000F86H, 0000F87H, 0000F8DH, 0000F97H,
58+ 0000F99H, 0000FBCH, 0000FC6H, 0000FC6H, 000102DH, 0001030H, 0001032H, 0001037H,
59+ 0001039H, 000103AH, 000103DH, 000103EH, 0001058H, 0001059H, 000105EH, 0001060H,
60+ 0001071H, 0001074H, 0001082H, 0001082H, 0001085H, 0001086H, 000108DH, 000108DH,
61+ 000109DH, 000109DH, 0001160H, 00011FFH, 000135DH, 000135FH, 0001712H, 0001714H,
62+ 0001732H, 0001733H, 0001752H, 0001753H, 0001772H, 0001773H, 00017B4H, 00017B5H,
63+ 00017B7H, 00017BDH, 00017C6H, 00017C6H, 00017C9H, 00017D3H, 00017DDH, 00017DDH,
64+ 000180BH, 000180FH, 0001885H, 0001886H, 00018A9H, 00018A9H, 0001920H, 0001922H,
65+ 0001927H, 0001928H, 0001932H, 0001932H, 0001939H, 000193BH, 0001A17H, 0001A18H,
66+ 0001A1BH, 0001A1BH, 0001A56H, 0001A56H, 0001A58H, 0001A5EH, 0001A60H, 0001A60H,
67+ 0001A62H, 0001A62H, 0001A65H, 0001A6CH, 0001A73H, 0001A7CH, 0001A7FH, 0001A7FH,
68+ 0001AB0H, 0001AF0H, 0001B00H, 0001B03H, 0001B34H, 0001B34H, 0001B36H, 0001B3AH,
69+ 0001B3CH, 0001B3CH, 0001B42H, 0001B42H, 0001B6BH, 0001B73H, 0001B80H, 0001B81H,
70+ 0001BA2H, 0001BA5H, 0001BA8H, 0001BA9H, 0001BABH, 0001BADH, 0001BE6H, 0001BE6H,
71+ 0001BE8H, 0001BE9H, 0001BEDH, 0001BEDH, 0001BEFH, 0001BF1H, 0001C2CH, 0001C33H,
72+ 0001C36H, 0001C37H, 0001CD0H, 0001CD2H, 0001CD4H, 0001CE0H, 0001CE2H, 0001CE8H,
73+ 0001CEDH, 0001CEDH, 0001CF4H, 0001CF4H, 0001CF8H, 0001CF9H, 0001DC0H, 0001DFFH,
74+ 000200BH, 000200FH, 000202AH, 000202EH, 0002060H, 0002064H, 0002066H, 000206FH,
75+ 00020D0H, 00020F0H, 0002CEFH, 0002CF1H, 0002D7FH, 0002D7FH, 0002DE0H, 0002DFFH,
76+ 000302AH, 000302DH, 0003099H, 000309AH, 0003164H, 0003164H, 000A66FH, 000A672H,
77+ 000A674H, 000A67DH, 000A69EH, 000A69FH, 000A6F0H, 000A6F1H, 000A802H, 000A802H,
78+ 000A806H, 000A806H, 000A80BH, 000A80BH, 000A825H, 000A826H, 000A82CH, 000A82CH,
79+ 000A8C4H, 000A8C5H, 000A8E0H, 000A8F1H, 000A8FFH, 000A8FFH, 000A926H, 000A92DH,
80+ 000A947H, 000A951H, 000A980H, 000A982H, 000A9B3H, 000A9B3H, 000A9B6H, 000A9B9H,
81+ 000A9BCH, 000A9BDH, 000A9E5H, 000A9E5H, 000AA29H, 000AA2EH, 000AA31H, 000AA32H,
82+ 000AA35H, 000AA36H, 000AA43H, 000AA43H, 000AA4CH, 000AA4CH, 000AA7CH, 000AA7CH,
83+ 000AAB0H, 000AAB0H, 000AAB2H, 000AAB4H, 000AAB7H, 000AAB8H, 000AABEH, 000AABFH,
84+ 000AAC1H, 000AAC1H, 000AAECH, 000AAEDH, 000AAF6H, 000AAF6H, 000ABE5H, 000ABE5H,
85+ 000ABE8H, 000ABE8H, 000ABEDH, 000ABEDH, 000D7B0H, 000D7FFH, 000FB1EH, 000FB1EH,
86+ 000FE00H, 000FE0FH, 000FE20H, 000FE2FH, 000FEFFH, 000FEFFH, 000FFA0H, 000FFA0H,
87+ 000FFF9H, 000FFFBH, 00101FDH, 00101FDH, 00102E0H, 00102E0H, 0010376H, 001037AH,
88+ 0010A01H, 0010A03H, 0010A05H, 0010A06H, 0010A0CH, 0010A0FH, 0010A38H, 0010A3AH,
89+ 0010A3FH, 0010A3FH, 0010AE5H, 0010AE6H, 0010D24H, 0010D27H, 0010D69H, 0010D6DH,
90+ 0010EABH, 0010EACH, 0010ECBH, 0010ECFH, 0010EF0H, 0010EFFH, 0010F46H, 0010F50H,
91+ 0010F82H, 0010F85H, 0011001H, 0011001H, 0011038H, 0011046H, 0011070H, 0011070H,
92+ 0011073H, 0011074H, 001107FH, 0011081H, 00110B3H, 00110B6H, 00110B9H, 00110BAH,
93+ 00110C2H, 00110C2H, 0011100H, 0011102H, 0011127H, 001112BH, 001112DH, 0011134H,
94+ 0011173H, 0011173H, 0011180H, 0011181H, 00111B6H, 00111BEH, 00111C9H, 00111CCH,
95+ 00111CFH, 00111CFH, 001122FH, 0011231H, 0011234H, 0011234H, 0011236H, 0011237H,
96+ 001123EH, 001123EH, 0011241H, 0011241H, 00112DFH, 00112DFH, 00112E3H, 00112EAH,
97+ 0011300H, 0011301H, 001133BH, 001133CH, 0011340H, 0011340H, 0011366H, 001136CH,
98+ 0011370H, 0011374H, 00113BBH, 00113C0H, 00113CEH, 00113CEH, 00113D0H, 00113D0H,
99+ 00113D2H, 00113D2H, 00113E1H, 00113E2H, 0011438H, 001143FH, 0011442H, 0011444H,
100+ 0011446H, 0011446H, 001145EH, 001145EH, 00114B3H, 00114B8H, 00114BAH, 00114BAH,
101+ 00114BFH, 00114C0H, 00114C2H, 00114C3H, 00115B2H, 00115B5H, 00115BCH, 00115BDH,
102+ 00115BFH, 00115C0H, 00115DCH, 00115DDH, 0011633H, 001163AH, 001163DH, 001163DH,
103+ 001163FH, 0011640H, 00116ABH, 00116ABH, 00116ADH, 00116ADH, 00116B0H, 00116B5H,
104+ 00116B7H, 00116B7H, 001171DH, 001171DH, 001171FH, 001171FH, 0011722H, 0011725H,
105+ 0011727H, 001172BH, 001182FH, 0011837H, 0011839H, 001183AH, 001193BH, 001193CH,
106+ 001193EH, 001193EH, 0011943H, 0011943H, 00119D4H, 00119D7H, 00119DAH, 00119DBH,
107+ 00119E0H, 00119E0H, 0011A01H, 0011A0AH, 0011A33H, 0011A38H, 0011A3BH, 0011A3EH,
108+ 0011A47H, 0011A47H, 0011A51H, 0011A56H, 0011A59H, 0011A5BH, 0011A8AH, 0011A96H,
109+ 0011A98H, 0011A99H, 0011B60H, 0011B60H, 0011B62H, 0011B64H, 0011B66H, 0011B66H,
110+ 0011C30H, 0011C36H, 0011C38H, 0011C3DH, 0011C3FH, 0011C3FH, 0011C92H, 0011CA7H,
111+ 0011CAAH, 0011CB0H, 0011CB2H, 0011CB3H, 0011CB5H, 0011CB6H, 0011D31H, 0011D36H,
112+ 0011D3AH, 0011D3AH, 0011D3CH, 0011D3DH, 0011D3FH, 0011D45H, 0011D47H, 0011D47H,
113+ 0011D90H, 0011D91H, 0011D95H, 0011D95H, 0011D97H, 0011D97H, 0011DF0H, 0011DF0H,
114+ 0011EF3H, 0011EF4H, 0011F00H, 0011F01H, 0011F36H, 0011F3AH, 0011F40H, 0011F40H,
115+ 0011F42H, 0011F42H, 0011F5AH, 0011F5AH, 0013430H, 0013440H, 0013447H, 0013455H,
116+ 001611EH, 0016129H, 001612DH, 001612FH, 0016AF0H, 0016AF4H, 0016B30H, 0016B36H,
117+ 0016F4FH, 0016F4FH, 0016F8FH, 0016F92H, 0016FE4H, 0016FE4H, 001BC9DH, 001BC9EH,
118+ 001BCA0H, 001BCA3H, 001CF00H, 001CF2DH, 001CF30H, 001CF46H, 001D127H, 001D128H,
119+ 001D167H, 001D169H, 001D173H, 001D182H, 001D185H, 001D18BH, 001D1AAH, 001D1ADH,
120+ 001D242H, 001D244H, 001D25BH, 001D25CH, 001DA00H, 001DA36H, 001DA3BH, 001DA6CH,
121+ 001DA75H, 001DA75H, 001DA84H, 001DA84H, 001DA9BH, 001DA9FH, 001DAA1H, 001DAAFH,
122+ 001E000H, 001E006H, 001E008H, 001E018H, 001E01BH, 001E021H, 001E023H, 001E024H,
123+ 001E026H, 001E02AH, 001E08FH, 001E08FH, 001E130H, 001E136H, 001E2AEH, 001E2AEH,
124+ 001E2ECH, 001E2EFH, 001E4ECH, 001E4EFH, 001E5EEH, 001E5EFH, 001E6E3H, 001E6E3H,
125+ 001E6E6H, 001E6E6H, 001E6EEH, 001E6EFH, 001E6F5H, 001E6F5H, 001E8D0H, 001E8D6H,
126+ 001E944H, 001E94AH, 00E0001H, 00E0001H, 00E0020H, 00E007FH, 00E0100H, 00E01EFH];
127+ Wide: []I32 = [
128+ 0001100H, 000115FH, 000231AH, 000231BH, 0002329H, 000232AH, 00023E9H, 00023ECH,
129+ 00023F0H, 00023F0H, 00023F3H, 00023F3H, 00025FDH, 00025FEH, 0002614H, 0002615H,
130+ 0002630H, 0002637H, 0002648H, 0002653H, 000267FH, 000267FH, 000268AH, 000268FH,
131+ 0002693H, 0002693H, 00026A1H, 00026A1H, 00026AAH, 00026ABH, 00026BDH, 00026BEH,
132+ 00026C4H, 00026C5H, 00026CEH, 00026CEH, 00026D4H, 00026D4H, 00026EAH, 00026EAH,
133+ 00026F2H, 00026F3H, 00026F5H, 00026F5H, 00026FAH, 00026FAH, 00026FDH, 00026FDH,
134+ 0002705H, 0002705H, 000270AH, 000270BH, 0002728H, 0002728H, 000274CH, 000274CH,
135+ 000274EH, 000274EH, 0002753H, 0002755H, 0002757H, 0002757H, 0002795H, 0002797H,
136+ 00027B0H, 00027B0H, 00027BFH, 00027BFH, 0002B1BH, 0002B1CH, 0002B50H, 0002B50H,
137+ 0002B55H, 0002B55H, 0002E80H, 0002E99H, 0002E9BH, 0002EF3H, 0002F00H, 0002FD5H,
138+ 0002FF0H, 0003029H, 000302EH, 000303EH, 0003041H, 0003096H, 000309BH, 00030FFH,
139+ 0003105H, 000312FH, 0003131H, 0003163H, 0003165H, 000318EH, 0003190H, 00031E5H,
140+ 00031EFH, 000321EH, 0003220H, 0003247H, 0003250H, 000A48CH, 000A490H, 000A4C6H,
141+ 000A960H, 000A97CH, 000AC00H, 000D7A3H, 000F900H, 000FAFFH, 000FE10H, 000FE19H,
142+ 000FE30H, 000FE52H, 000FE54H, 000FE66H, 000FE68H, 000FE6BH, 000FF01H, 000FF60H,
143+ 000FFE0H, 000FFE6H, 0016FE0H, 0016FE3H, 0016FF0H, 0016FF6H, 0017000H, 0018CDAH,
144+ 0018CFFH, 0018D20H, 0018D80H, 0018DF2H, 0018E00H, 0019191H, 00191A0H, 00191D2H,
145+ 001AFF0H, 001AFF3H, 001AFF5H, 001AFFBH, 001AFFDH, 001AFFEH, 001B000H, 001B128H,
146+ 001B132H, 001B132H, 001B150H, 001B152H, 001B155H, 001B155H, 001B164H, 001B168H,
147+ 001B170H, 001B2FBH, 001D300H, 001D356H, 001D360H, 001D376H, 001F004H, 001F004H,
148+ 001F0CFH, 001F0CFH, 001F18EH, 001F18EH, 001F191H, 001F19AH, 001F1AEH, 001F1AEH,
149+ 001F200H, 001F202H, 001F210H, 001F23BH, 001F240H, 001F248H, 001F250H, 001F251H,
150+ 001F260H, 001F265H, 001F300H, 001F320H, 001F32DH, 001F335H, 001F337H, 001F37CH,
151+ 001F37EH, 001F393H, 001F3A0H, 001F3CAH, 001F3CFH, 001F3D3H, 001F3E0H, 001F3F0H,
152+ 001F3F4H, 001F3F4H, 001F3F8H, 001F43EH, 001F440H, 001F440H, 001F442H, 001F4FCH,
153+ 001F4FFH, 001F53DH, 001F54BH, 001F54EH, 001F550H, 001F567H, 001F57AH, 001F57AH,
154+ 001F595H, 001F596H, 001F5A4H, 001F5A4H, 001F5FBH, 001F64FH, 001F680H, 001F6C5H,
155+ 001F6CCH, 001F6CCH, 001F6D0H, 001F6D2H, 001F6D5H, 001F6D9H, 001F6DCH, 001F6DFH,
156+ 001F6EBH, 001F6ECH, 001F6F4H, 001F6FCH, 001F7DAH, 001F7DAH, 001F7E0H, 001F7EBH,
157+ 001F7F0H, 001F7F0H, 001F90CH, 001F93AH, 001F93CH, 001F945H, 001F947H, 001F9FFH,
158+ 001FA70H, 001FA7CH, 001FA80H, 001FAC6H, 001FAC8H, 001FAC8H, 001FACCH, 001FADDH,
159+ 001FADFH, 001FAEBH, 001FAEFH, 001FAFAH, 0020000H, 002FFFDH, 0030000H, 003FFFDH];
160+(* end of the tables *)
161+
162+(* Whether c is in one of the ranges of t, by halving. *)
163+proc within(t: []I32; c: I32): BOOL;
164+var lo, hi, m: INT;
165+begin
166+ lo := 0;
167+ hi := LEN(t) div 2 - 1;
168+ while lo <= hi do
169+ m := (lo + hi) div 2;
170+ if c < t[2 * m] then hi := m - 1;
171+ elsif c > t[2 * m + 1] then lo := m + 1;
172+ else return true;
173+ end;
174+ end;
175+ return false;
176+end;
177+
178+(* The columns the character c takes: 0, 1 or 2. *)
179+proc width*(c: I32): INT;
180+begin
181+ if (c >= 32) and (c < 7FH) then return 1; end; (* ASCII, at once *)
182+ if within(Zero, c) then return 0; end;
183+ if within(Wide, c) then return 2; end;
184+ return 1;
185+end;
186+
187+(* The columns the UTF-8 text s takes, as Str.decode reads it. *)
188+proc columns*(s: []U8): INT;
189+var c: I32; i, n, w: INT;
190+begin
191+ i := 0;
192+ w := 0;
193+ while i < LEN(s) do
194+ c, n := Str.decode(s[i..LEN(s)]);
195+ w := w + width(c);
196+ i := i + n;
197+ end;
198+ return w;
199+end;
200+
201+(* The module's test. *)
202+proc main*;
203+begin
204+ if (width('a') # 1) or (width(' ') # 1) or (width(0) # 0) or (width(9) # 0)
205+ or (width(7FH) # 0) then Rt.stop("Width.width: ASCII"); end;
206+ if (width(0301H) # 0) or (width(200BH) # 0) or (width(0ADH) # 1)
207+ or (width(1160H) # 0) then Rt.stop("Width.width: none"); end;
208+ if (width(4E2DH) # 2) or (width(3042H) # 2) or (width(0FF21H) # 2)
209+ or (width(01F600H) # 2) or (width(3000H) # 2) then Rt.stop("Width.width: wide"); end;
210+ if (width(0E9H) # 1) or (width(03B1H) # 1) or (width(0FFFDH) # 1)
211+ or (width(10FFFFH) # 1) then Rt.stop("Width.width: one"); end;
212+ if (columns("") # 0) or (columns("abc") # 3) or (columns("e\u{301}") # 1)
213+ or (columns("\u{4E2D}\u{6587}") # 4) or (columns("a\u{1F600}b") # 4) then
214+ Rt.stop("Width.columns");
215+ end;
216+end;
217+
218+end Width.

test/check.sh (history) modified +13 -0

@@ -252,6 +252,19 @@
252252 if (ulimit -v 60000; $out/lib_Thread.linux-x86_64 fail) > /dev/null 2>&1; then pass=$((pass + 1))
253253 else echo "FAIL Thread under a memory limit"; fail=$((fail + 1)); fi
254254
255+# a module imported and never called leaves nothing of its own in the
256+# program, not even Width's tables: the same program, importing it or
257+# not, makes a file of the same size (the names the trace shows may
258+# come in another order). Width's own imports, Str and through it Heap,
259+# run their bodies, as every module does, so both import Str
260+mkdir -p $out/shake
261+printf 'module Shake;\nimport Out, Str, Width;\nproc main*;\nbegin\n Out.int(1); Out.ln;\nend;\nend Shake.\n' >$out/shake/Shake.raz
262+$raz -o $out/shake/with $out/shake/Shake.raz 2>/dev/null
263+printf 'module Shake;\nimport Out, Str;\nproc main*;\nbegin\n Out.int(1); Out.ln;\nend;\nend Shake.\n' >$out/shake/Shake.raz
264+$raz -o $out/shake/without $out/shake/Shake.raz
265+if [ $(wc -c <$out/shake/with) = $(wc -c <$out/shake/without) ]; then pass=$((pass + 1))
266+else echo "FAIL an unused import, Width, changes the program"; fail=$((fail + 1)); fi
267+
255268 # the editors' support, headless, where the editors are installed
256269 if test/editors.sh; then pass=$((pass + 1)); else fail=$((fail + 1)); fi
257270

tools/Widths.raz (history) added +185 -0

@@ -0,0 +1,185 @@
1+(* Widths: makes the tables of lib/Width.raz from Unicode's data.
2+
3+ Widths lib/Width.raz EastAsianWidth.txt UnicodeData.txt PropList.txt
4+
5+ reads three files of Unicode's character database, which
6+ https://www.unicode.org/Public/UCD/latest/ucd/ has, and writes the
7+ tables of lib/Width.raz again, the lines between (* tables *) and
8+ (* end of the tables *), with the version of Unicode they come from.
9+ The rest of the file stays as it is. Width's opening comment says
10+ what each width is; this program is the same rules, in code. *)
11+module Widths;
12+import Out, Rt, Str, Buf, Heap, Files;
13+
14+const
15+ Last = 10FFFFH; (* the last code point *)
16+ Start = "(* tables *)\n";
17+ Stop = "(* end of the tables *)";
18+
19+var
20+ w: [Last + 1]U8; (* each character's width, 1 unless a file says *)
21+
22+(* The whole file at path. *)
23+proc load(path: []U8; b: ^Buf.Buf): []U8;
24+begin
25+ Buf.init(b, Heap.heap);
26+ if not Files.read(path, b) then
27+ Out.str(path); Out.str(": can't be read\n");
28+ Rt.exit(1);
29+ end;
30+ return Buf.bytes(b);
31+end;
32+
33+(* s in hexadecimal, a code point; stops the program if it isn't one. *)
34+proc point(s: []U8): INT;
35+var v: U64; ok: BOOL;
36+begin
37+ v, ok := Str.toHex(Str.trim(s));
38+ if not ok or (v > Last) then
39+ Out.str("not a code point: "); Out.str(s); Out.ln;
40+ Rt.exit(1);
41+ end;
42+ return INT(v);
43+end;
44+
45+(* Sets the width of the characters from lo to hi. *)
46+proc mark(lo, hi: INT; width: U8);
47+var c: INT;
48+begin
49+ for c := lo to hi do w[c] := width; end;
50+end;
51+
52+(* EastAsianWidth.txt: lines of a code point, or a range lo..hi, ";"
53+ and the property; W and F are 2 columns. Its first line names the
54+ file, and so the version. *)
55+proc eastAsian(text: []U8): []U8;
56+var line, rest, r, p, lo, hi, version: []U8; ok: BOOL;
57+begin
58+ line, rest, ok := Str.cut(text, "\n");
59+ _, version, ok := Str.cut(line, "-");
60+ version, _, ok := Str.cut(version, ".txt");
61+ rest := text;
62+ while LEN(rest) > 0 do
63+ line, rest, ok := Str.cut(rest, "\n");
64+ line, _, ok := Str.cut(line, "#");
65+ r, p, ok := Str.cut(line, ";");
66+ if ok then
67+ p := Str.trim(p);
68+ if (p = "W") or (p = "F") then
69+ lo, hi, ok := Str.cut(r, "..");
70+ if ok then mark(point(lo), point(hi), 2); else mark(point(lo), point(lo), 2); end;
71+ end;
72+ end;
73+ end;
74+ return version;
75+end;
76+
77+(* UnicodeData.txt: lines of fields between ";", the code point, the
78+ name and the general category first. A range is two lines, its
79+ first and last, named "<..., First>" and "<..., Last>". *)
80+proc categories(text: []U8);
81+var line, rest, c, name, gc: []U8; ok: BOOL; first, n: INT;
82+begin
83+ rest := text;
84+ first := -1;
85+ while LEN(rest) > 0 do
86+ line, rest, ok := Str.cut(rest, "\n");
87+ c, line, ok := Str.cut(line, ";");
88+ name, line, ok := Str.cut(line, ";");
89+ gc, line, ok := Str.cut(line, ";");
90+ if not ok then continue; end;
91+ n := point(c);
92+ if Str.hasSuffix(name, ", First>") then first := n; continue; end;
93+ if not Str.hasSuffix(name, ", Last>") then first := n; end;
94+ if ((gc = "Mn") or (gc = "Me") or (gc = "Cf") or (gc = "Cc")) and (n # 0ADH) then
95+ mark(first, n, 0); (* the soft hyphen is seen: 1 *)
96+ end;
97+ end;
98+end;
99+
100+(* PropList.txt: lines of a code point, or a range lo..hi, ";" and a
101+ property; a Prepended_Concatenation_Mark, a sign set over the digits
102+ after it, is seen, so 1 column, though a format character. *)
103+proc concatenation(text: []U8);
104+var line, rest, r, p, lo, hi: []U8; ok: BOOL;
105+begin
106+ rest := text;
107+ while LEN(rest) > 0 do
108+ line, rest, ok := Str.cut(rest, "\n");
109+ line, _, ok := Str.cut(line, "#");
110+ r, p, ok := Str.cut(line, ";");
111+ if ok and (Str.trim(p) = "Prepended_Concatenation_Mark") then
112+ lo, hi, ok := Str.cut(r, "..");
113+ if ok then mark(point(lo), point(hi), 1); else mark(point(lo), point(lo), 1); end;
114+ end;
115+ end;
116+end;
117+
118+(* c as a constant of raz: 0, six hexadecimal digits and H. *)
119+proc hex(b: ^Buf.Buf; c: INT);
120+const Digits = "0123456789ABCDEF";
121+var i: INT;
122+begin
123+ Buf.add(b, '0');
124+ for i := 5 to 0 by -1 do Buf.add(b, Digits[(c shr (4 * i)) & 15]); end;
125+ Buf.add(b, 'H');
126+end;
127+
128+(* The constant name, the ranges of characters whose width is width,
129+ each its first and last, four ranges to a line. *)
130+proc table(b: ^Buf.Buf; name: []U8; width: U8);
131+var c, lo, n: INT;
132+begin
133+ Buf.str(b, " "); Buf.str(b, name); Buf.str(b, ": []I32 = [");
134+ n := 0;
135+ c := 0;
136+ while c <= Last do
137+ if w[c] # width then INC(c); continue; end;
138+ lo := c;
139+ while (c < Last) and (w[c + 1] = width) do INC(c); end;
140+ if n > 0 then Buf.str(b, ","); end;
141+ if n mod 4 = 0 then Buf.str(b, "\n "); else Buf.str(b, " "); end;
142+ hex(b, lo); Buf.str(b, ", "); hex(b, c);
143+ INC(n);
144+ INC(c);
145+ end;
146+ Buf.str(b, "];\n");
147+end;
148+
149+proc main*;
150+var src, eaw, ucd, props, out: Buf.Buf; s, before, after, version: []U8; ok: BOOL;
151+begin
152+ if LEN(Files.arg(4)) = 0 then
153+ Out.str("usage: Widths lib/Width.raz EastAsianWidth.txt UnicodeData.txt PropList.txt\n");
154+ Rt.exit(2);
155+ end;
156+ s := load(Files.arg(1), ADR(src));
157+ before, after, ok := Str.cut(s, Start);
158+ if ok then _, after, ok := Str.cut(after, Stop); end;
159+ if not ok then
160+ Out.str(Files.arg(1)); Out.str(": no (* tables *) and (* end of the tables *)\n");
161+ Rt.exit(1);
162+ end;
163+ mark(0, Last, 1);
164+ version := eastAsian(load(Files.arg(2), ADR(eaw)));
165+ categories(load(Files.arg(3), ADR(ucd)));
166+ concatenation(load(Files.arg(4), ADR(props)));
167+ mark(1160H, 11FFH, 0); (* Hangul's medial and final jamo *)
168+ mark(0D7B0H, 0D7FFH, 0);
169+ mark(3164H, 3164H, 0); (* the Hangul fillers, which draw nothing *)
170+ mark(0FFA0H, 0FFA0H, 0);
171+ Buf.init(ADR(out), Heap.heap);
172+ Buf.str(ADR(out), before);
173+ Buf.str(ADR(out), Start);
174+ Buf.str(ADR(out), "(* Unicode "); Buf.str(ADR(out), version); Buf.str(ADR(out), " *)\nconst\n");
175+ table(ADR(out), "Zero", 0);
176+ table(ADR(out), "Wide", 2);
177+ Buf.str(ADR(out), Stop);
178+ Buf.str(ADR(out), after);
179+ if not Files.write(Files.arg(1), Buf.bytes(ADR(out))) then
180+ Out.str(Files.arg(1)); Out.str(": can't be written\n");
181+ Rt.exit(1);
182+ end;
183+end;
184+
185+end Widths.