From 40536519d850ab4ed4535b741341efde57b4ff5d Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:28:37 +0100 Subject: [PATCH] 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. --- diff --git a/Makefile b/Makefile index 10fac71..afa952a 100644 --- a/Makefile +++ b/Makefile @@ -106,7 +106,7 @@ # raz-lib(3), the library's manual page, made by tools/Doc.raz from # its sources, in the order of doc/raz-lib.md LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \ - Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt C + Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt Width C man: build/raz-lib.3 diff --git a/README.md b/README.md index 6bf17cb..91efe52 100644 --- a/README.md +++ b/README.md @@ -168,7 +168,7 @@ `Rt` (ending the program, failed checks, signals), `Sys` (system calls), `Out` (buffered output), `C`, `Lock`, `Thread` (threads without C), `Mem`, `Heap`, `Arena`, `Str`, `Bytes`, `Buf`, `Fmt`, `Math`, `Files`, `Opt` (a command -line read through one table), `List(T)`, `Map(K, V; hash, eq)`, +line read through one table), `Width` (the columns of a terminal text takes), `List(T)`, `Map(K, V; hash, eq)`, `Vec(T)` (a growable array) and `Sort(T; less)`. Volume 2 of the report, `doc/raz-lib.md`, gives each one's interface, and `tools/Doc.raz` prints it from the module's file: diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 4351132..729fae8 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -30,6 +30,7 @@ | `Math` | roots, powers, logarithms, rounding and trigonometry, and `pi`, for `F64` | | `Files` | an argument as a string, and a whole file read or written | | `Opt` | a command line read through one table, by `getopt`'s rules with long options; its help and its mistakes | +| `Width` | how many columns of a terminal a character, or a string, takes: 0, 1 or 2, as `wcwidth` says | Lengths, counts, file descriptors, process ids and statuses are `INT`s, and a character is an `I32`. What may pass an `INT` on a small @@ -890,3 +891,25 @@ others are an option with neither a letter nor a long name, a long name with `=` in it, a letter `-`, and `-h` or `--help` in the table, since they are `Opt`'s. + +**Width** says how many columns of a terminal text takes, for a +program with a screen that wraps its lines and cuts them at the +window's edge. + +``` +module Width; +proc width*(c: I32): INT; (* the columns the character c takes: 0, 1 or 2 *) +proc columns*(s: []U8): INT; (* the columns the UTF-8 text s takes *) +``` + +A combining mark, a format character, a control and Hangul's medial +and final jamo take 0 columns; an ideograph, a kana, most emoji and +the rest that Unicode calls East Asian Wide or Fullwidth take 2; +every other character takes 1, the ambiguous ones among them, as most +terminals draw them. These are the C library's `wcwidth`'s answers, +but for a control, which is -1 there. `Width` knows characters, not +clusters: an emoji joined to another by a zero width joiner, or a flag, +counts as its parts. Its tables, about 4 KB, come from Unicode's data, +made again by `tools/Widths.raz` for a new version of Unicode; a +program that imports `Width` and calls neither procedure carries none +of them. diff --git a/doc/raz.md b/doc/raz.md index d3ba311..1a01d23 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -1690,8 +1690,8 @@ language, reals aside, is the core of the library: `Rt`, `Out`, `Mem`, `Arena`, `Lock`, `Str` and `Buf`, and of `Sys` what they need. A program that keeps to the core runs on every target. The rest of the library -(`Heap`, `Fmt`, `List`, `Map`, `Vec`, `Sort`, `Math`, `Files`, `Opt` and the -rest of `Sys`) is the standard library: a target may leave a module +(`Heap`, `Fmt`, `List`, `Map`, `Vec`, `Sort`, `Math`, `Files`, `Opt`, `Width` +and the rest of `Sys`) is the standard library: a target may leave a module out, and one that has it gives what `doc/raz-lib.md` says. `conform/` tests the core only. diff --git a/doc/report-history.md b/doc/report-history.md index b3b3592..c4d86df 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -188,4 +188,6 @@ its type, `Color.red`; two enumerations of a scope may share a name, or one share a name with another declaration, and it is then written with its type (section 5.7). `continue` ends a loop's round (sections - 7.2 and 7.7). It breaks a program that has a name `continue`. + 7.2 and 7.7). It breaks a program that has a name `continue`. The + standard library gains `Width`, the columns of a terminal a character + takes (volume 2). diff --git a/lib/Width.raz b/lib/Width.raz new file mode 100644 index 0000000..a3f5b1f --- /dev/null +++ b/lib/Width.raz @@ -0,0 +1,218 @@ +(* Width: how many columns of a terminal a character takes, so that a + program with a screen can wrap its lines and cut them at the + window's edge. + + A character is 0 columns wide if it is a combining mark (Unicode's + general categories Mn and Me), a format character (Cf) other than + the soft hyphen and the marks set over the digits after them + (Prepended_Concatenation_Mark), a control character (Cc), whose + effect is the program's to decide, one of Hangul's medial and final + jamo, which join the syllable before them (1160H to 11FFH, 0D7B0H to + 0D7FFH), or a Hangul filler (3164H, 0FFA0H). It is 2 if Unicode's + East Asian Width is Wide or Fullwidth, as the ideographs, the kana + and most emoji are. Every other one, the ambiguous ones among them, + is 1, as most terminals take them. These are the answers of glibc's + wcwidth, but for a control, which is -1 there, and a few format + characters glibc counts as 1. + + It knows characters, not clusters: an emoji joined to another by a + zero width joiner, or a flag, two regional indicators, counts as its + parts. The tables below are made from Unicode's data by + tools/Widths.raz, which says how; they take about 4 KB, which a + program that doesn't call width or columns doesn't carry. *) +module Width; +import Rt, Str; + +(* The tables: each a sorted list of ranges, first and last. Made by + tools/Widths.raz: don't edit them, make them again. *) +(* tables *) +(* Unicode 18.0.0 *) +const + Zero: []I32 = [ + 0000000H, 000001FH, 000007FH, 000009FH, 0000300H, 000036FH, 0000483H, 0000489H, + 0000591H, 00005BDH, 00005BFH, 00005BFH, 00005C1H, 00005C2H, 00005C4H, 00005C5H, + 00005C7H, 00005C9H, 0000610H, 000061AH, 000061CH, 000061CH, 000064BH, 000065FH, + 0000670H, 0000670H, 00006D6H, 00006DCH, 00006DFH, 00006E4H, 00006E7H, 00006E8H, + 00006EAH, 00006EDH, 0000711H, 0000711H, 0000730H, 000074AH, 00007A6H, 00007B0H, + 00007EBH, 00007F3H, 00007FDH, 00007FDH, 0000816H, 0000819H, 000081BH, 0000823H, + 0000825H, 0000827H, 0000829H, 000082DH, 0000859H, 000085BH, 0000897H, 000089FH, + 00008CAH, 00008E1H, 00008E3H, 0000902H, 000093AH, 000093AH, 000093CH, 000093CH, + 0000941H, 0000948H, 000094DH, 000094DH, 0000951H, 0000957H, 0000962H, 0000963H, + 0000981H, 0000981H, 00009BCH, 00009BCH, 00009C1H, 00009C4H, 00009CDH, 00009CDH, + 00009E2H, 00009E3H, 00009FEH, 00009FEH, 0000A01H, 0000A02H, 0000A3CH, 0000A3CH, + 0000A41H, 0000A42H, 0000A47H, 0000A48H, 0000A4BH, 0000A4DH, 0000A51H, 0000A51H, + 0000A70H, 0000A71H, 0000A75H, 0000A75H, 0000A81H, 0000A82H, 0000ABCH, 0000ABCH, + 0000AC1H, 0000AC5H, 0000AC7H, 0000AC8H, 0000ACDH, 0000ACDH, 0000AE2H, 0000AE3H, + 0000AFAH, 0000AFFH, 0000B01H, 0000B01H, 0000B3CH, 0000B3CH, 0000B3FH, 0000B3FH, + 0000B41H, 0000B44H, 0000B4DH, 0000B4DH, 0000B53H, 0000B56H, 0000B62H, 0000B63H, + 0000B82H, 0000B82H, 0000BC0H, 0000BC0H, 0000BCDH, 0000BCDH, 0000C00H, 0000C00H, + 0000C04H, 0000C04H, 0000C3CH, 0000C3CH, 0000C3EH, 0000C40H, 0000C46H, 0000C48H, + 0000C4AH, 0000C4DH, 0000C55H, 0000C56H, 0000C62H, 0000C63H, 0000C81H, 0000C81H, + 0000CBCH, 0000CBCH, 0000CBFH, 0000CBFH, 0000CC6H, 0000CC6H, 0000CCCH, 0000CCDH, + 0000CE2H, 0000CE3H, 0000D00H, 0000D01H, 0000D3BH, 0000D3CH, 0000D41H, 0000D44H, + 0000D4DH, 0000D4DH, 0000D62H, 0000D63H, 0000D81H, 0000D81H, 0000DCAH, 0000DCAH, + 0000DD2H, 0000DD4H, 0000DD6H, 0000DD6H, 0000E31H, 0000E31H, 0000E34H, 0000E3AH, + 0000E47H, 0000E4EH, 0000EB1H, 0000EB1H, 0000EB4H, 0000EBCH, 0000EC8H, 0000ECEH, + 0000F18H, 0000F19H, 0000F35H, 0000F35H, 0000F37H, 0000F37H, 0000F39H, 0000F39H, + 0000F71H, 0000F7EH, 0000F80H, 0000F84H, 0000F86H, 0000F87H, 0000F8DH, 0000F97H, + 0000F99H, 0000FBCH, 0000FC6H, 0000FC6H, 000102DH, 0001030H, 0001032H, 0001037H, + 0001039H, 000103AH, 000103DH, 000103EH, 0001058H, 0001059H, 000105EH, 0001060H, + 0001071H, 0001074H, 0001082H, 0001082H, 0001085H, 0001086H, 000108DH, 000108DH, + 000109DH, 000109DH, 0001160H, 00011FFH, 000135DH, 000135FH, 0001712H, 0001714H, + 0001732H, 0001733H, 0001752H, 0001753H, 0001772H, 0001773H, 00017B4H, 00017B5H, + 00017B7H, 00017BDH, 00017C6H, 00017C6H, 00017C9H, 00017D3H, 00017DDH, 00017DDH, + 000180BH, 000180FH, 0001885H, 0001886H, 00018A9H, 00018A9H, 0001920H, 0001922H, + 0001927H, 0001928H, 0001932H, 0001932H, 0001939H, 000193BH, 0001A17H, 0001A18H, + 0001A1BH, 0001A1BH, 0001A56H, 0001A56H, 0001A58H, 0001A5EH, 0001A60H, 0001A60H, + 0001A62H, 0001A62H, 0001A65H, 0001A6CH, 0001A73H, 0001A7CH, 0001A7FH, 0001A7FH, + 0001AB0H, 0001AF0H, 0001B00H, 0001B03H, 0001B34H, 0001B34H, 0001B36H, 0001B3AH, + 0001B3CH, 0001B3CH, 0001B42H, 0001B42H, 0001B6BH, 0001B73H, 0001B80H, 0001B81H, + 0001BA2H, 0001BA5H, 0001BA8H, 0001BA9H, 0001BABH, 0001BADH, 0001BE6H, 0001BE6H, + 0001BE8H, 0001BE9H, 0001BEDH, 0001BEDH, 0001BEFH, 0001BF1H, 0001C2CH, 0001C33H, + 0001C36H, 0001C37H, 0001CD0H, 0001CD2H, 0001CD4H, 0001CE0H, 0001CE2H, 0001CE8H, + 0001CEDH, 0001CEDH, 0001CF4H, 0001CF4H, 0001CF8H, 0001CF9H, 0001DC0H, 0001DFFH, + 000200BH, 000200FH, 000202AH, 000202EH, 0002060H, 0002064H, 0002066H, 000206FH, + 00020D0H, 00020F0H, 0002CEFH, 0002CF1H, 0002D7FH, 0002D7FH, 0002DE0H, 0002DFFH, + 000302AH, 000302DH, 0003099H, 000309AH, 0003164H, 0003164H, 000A66FH, 000A672H, + 000A674H, 000A67DH, 000A69EH, 000A69FH, 000A6F0H, 000A6F1H, 000A802H, 000A802H, + 000A806H, 000A806H, 000A80BH, 000A80BH, 000A825H, 000A826H, 000A82CH, 000A82CH, + 000A8C4H, 000A8C5H, 000A8E0H, 000A8F1H, 000A8FFH, 000A8FFH, 000A926H, 000A92DH, + 000A947H, 000A951H, 000A980H, 000A982H, 000A9B3H, 000A9B3H, 000A9B6H, 000A9B9H, + 000A9BCH, 000A9BDH, 000A9E5H, 000A9E5H, 000AA29H, 000AA2EH, 000AA31H, 000AA32H, + 000AA35H, 000AA36H, 000AA43H, 000AA43H, 000AA4CH, 000AA4CH, 000AA7CH, 000AA7CH, + 000AAB0H, 000AAB0H, 000AAB2H, 000AAB4H, 000AAB7H, 000AAB8H, 000AABEH, 000AABFH, + 000AAC1H, 000AAC1H, 000AAECH, 000AAEDH, 000AAF6H, 000AAF6H, 000ABE5H, 000ABE5H, + 000ABE8H, 000ABE8H, 000ABEDH, 000ABEDH, 000D7B0H, 000D7FFH, 000FB1EH, 000FB1EH, + 000FE00H, 000FE0FH, 000FE20H, 000FE2FH, 000FEFFH, 000FEFFH, 000FFA0H, 000FFA0H, + 000FFF9H, 000FFFBH, 00101FDH, 00101FDH, 00102E0H, 00102E0H, 0010376H, 001037AH, + 0010A01H, 0010A03H, 0010A05H, 0010A06H, 0010A0CH, 0010A0FH, 0010A38H, 0010A3AH, + 0010A3FH, 0010A3FH, 0010AE5H, 0010AE6H, 0010D24H, 0010D27H, 0010D69H, 0010D6DH, + 0010EABH, 0010EACH, 0010ECBH, 0010ECFH, 0010EF0H, 0010EFFH, 0010F46H, 0010F50H, + 0010F82H, 0010F85H, 0011001H, 0011001H, 0011038H, 0011046H, 0011070H, 0011070H, + 0011073H, 0011074H, 001107FH, 0011081H, 00110B3H, 00110B6H, 00110B9H, 00110BAH, + 00110C2H, 00110C2H, 0011100H, 0011102H, 0011127H, 001112BH, 001112DH, 0011134H, + 0011173H, 0011173H, 0011180H, 0011181H, 00111B6H, 00111BEH, 00111C9H, 00111CCH, + 00111CFH, 00111CFH, 001122FH, 0011231H, 0011234H, 0011234H, 0011236H, 0011237H, + 001123EH, 001123EH, 0011241H, 0011241H, 00112DFH, 00112DFH, 00112E3H, 00112EAH, + 0011300H, 0011301H, 001133BH, 001133CH, 0011340H, 0011340H, 0011366H, 001136CH, + 0011370H, 0011374H, 00113BBH, 00113C0H, 00113CEH, 00113CEH, 00113D0H, 00113D0H, + 00113D2H, 00113D2H, 00113E1H, 00113E2H, 0011438H, 001143FH, 0011442H, 0011444H, + 0011446H, 0011446H, 001145EH, 001145EH, 00114B3H, 00114B8H, 00114BAH, 00114BAH, + 00114BFH, 00114C0H, 00114C2H, 00114C3H, 00115B2H, 00115B5H, 00115BCH, 00115BDH, + 00115BFH, 00115C0H, 00115DCH, 00115DDH, 0011633H, 001163AH, 001163DH, 001163DH, + 001163FH, 0011640H, 00116ABH, 00116ABH, 00116ADH, 00116ADH, 00116B0H, 00116B5H, + 00116B7H, 00116B7H, 001171DH, 001171DH, 001171FH, 001171FH, 0011722H, 0011725H, + 0011727H, 001172BH, 001182FH, 0011837H, 0011839H, 001183AH, 001193BH, 001193CH, + 001193EH, 001193EH, 0011943H, 0011943H, 00119D4H, 00119D7H, 00119DAH, 00119DBH, + 00119E0H, 00119E0H, 0011A01H, 0011A0AH, 0011A33H, 0011A38H, 0011A3BH, 0011A3EH, + 0011A47H, 0011A47H, 0011A51H, 0011A56H, 0011A59H, 0011A5BH, 0011A8AH, 0011A96H, + 0011A98H, 0011A99H, 0011B60H, 0011B60H, 0011B62H, 0011B64H, 0011B66H, 0011B66H, + 0011C30H, 0011C36H, 0011C38H, 0011C3DH, 0011C3FH, 0011C3FH, 0011C92H, 0011CA7H, + 0011CAAH, 0011CB0H, 0011CB2H, 0011CB3H, 0011CB5H, 0011CB6H, 0011D31H, 0011D36H, + 0011D3AH, 0011D3AH, 0011D3CH, 0011D3DH, 0011D3FH, 0011D45H, 0011D47H, 0011D47H, + 0011D90H, 0011D91H, 0011D95H, 0011D95H, 0011D97H, 0011D97H, 0011DF0H, 0011DF0H, + 0011EF3H, 0011EF4H, 0011F00H, 0011F01H, 0011F36H, 0011F3AH, 0011F40H, 0011F40H, + 0011F42H, 0011F42H, 0011F5AH, 0011F5AH, 0013430H, 0013440H, 0013447H, 0013455H, + 001611EH, 0016129H, 001612DH, 001612FH, 0016AF0H, 0016AF4H, 0016B30H, 0016B36H, + 0016F4FH, 0016F4FH, 0016F8FH, 0016F92H, 0016FE4H, 0016FE4H, 001BC9DH, 001BC9EH, + 001BCA0H, 001BCA3H, 001CF00H, 001CF2DH, 001CF30H, 001CF46H, 001D127H, 001D128H, + 001D167H, 001D169H, 001D173H, 001D182H, 001D185H, 001D18BH, 001D1AAH, 001D1ADH, + 001D242H, 001D244H, 001D25BH, 001D25CH, 001DA00H, 001DA36H, 001DA3BH, 001DA6CH, + 001DA75H, 001DA75H, 001DA84H, 001DA84H, 001DA9BH, 001DA9FH, 001DAA1H, 001DAAFH, + 001E000H, 001E006H, 001E008H, 001E018H, 001E01BH, 001E021H, 001E023H, 001E024H, + 001E026H, 001E02AH, 001E08FH, 001E08FH, 001E130H, 001E136H, 001E2AEH, 001E2AEH, + 001E2ECH, 001E2EFH, 001E4ECH, 001E4EFH, 001E5EEH, 001E5EFH, 001E6E3H, 001E6E3H, + 001E6E6H, 001E6E6H, 001E6EEH, 001E6EFH, 001E6F5H, 001E6F5H, 001E8D0H, 001E8D6H, + 001E944H, 001E94AH, 00E0001H, 00E0001H, 00E0020H, 00E007FH, 00E0100H, 00E01EFH]; + Wide: []I32 = [ + 0001100H, 000115FH, 000231AH, 000231BH, 0002329H, 000232AH, 00023E9H, 00023ECH, + 00023F0H, 00023F0H, 00023F3H, 00023F3H, 00025FDH, 00025FEH, 0002614H, 0002615H, + 0002630H, 0002637H, 0002648H, 0002653H, 000267FH, 000267FH, 000268AH, 000268FH, + 0002693H, 0002693H, 00026A1H, 00026A1H, 00026AAH, 00026ABH, 00026BDH, 00026BEH, + 00026C4H, 00026C5H, 00026CEH, 00026CEH, 00026D4H, 00026D4H, 00026EAH, 00026EAH, + 00026F2H, 00026F3H, 00026F5H, 00026F5H, 00026FAH, 00026FAH, 00026FDH, 00026FDH, + 0002705H, 0002705H, 000270AH, 000270BH, 0002728H, 0002728H, 000274CH, 000274CH, + 000274EH, 000274EH, 0002753H, 0002755H, 0002757H, 0002757H, 0002795H, 0002797H, + 00027B0H, 00027B0H, 00027BFH, 00027BFH, 0002B1BH, 0002B1CH, 0002B50H, 0002B50H, + 0002B55H, 0002B55H, 0002E80H, 0002E99H, 0002E9BH, 0002EF3H, 0002F00H, 0002FD5H, + 0002FF0H, 0003029H, 000302EH, 000303EH, 0003041H, 0003096H, 000309BH, 00030FFH, + 0003105H, 000312FH, 0003131H, 0003163H, 0003165H, 000318EH, 0003190H, 00031E5H, + 00031EFH, 000321EH, 0003220H, 0003247H, 0003250H, 000A48CH, 000A490H, 000A4C6H, + 000A960H, 000A97CH, 000AC00H, 000D7A3H, 000F900H, 000FAFFH, 000FE10H, 000FE19H, + 000FE30H, 000FE52H, 000FE54H, 000FE66H, 000FE68H, 000FE6BH, 000FF01H, 000FF60H, + 000FFE0H, 000FFE6H, 0016FE0H, 0016FE3H, 0016FF0H, 0016FF6H, 0017000H, 0018CDAH, + 0018CFFH, 0018D20H, 0018D80H, 0018DF2H, 0018E00H, 0019191H, 00191A0H, 00191D2H, + 001AFF0H, 001AFF3H, 001AFF5H, 001AFFBH, 001AFFDH, 001AFFEH, 001B000H, 001B128H, + 001B132H, 001B132H, 001B150H, 001B152H, 001B155H, 001B155H, 001B164H, 001B168H, + 001B170H, 001B2FBH, 001D300H, 001D356H, 001D360H, 001D376H, 001F004H, 001F004H, + 001F0CFH, 001F0CFH, 001F18EH, 001F18EH, 001F191H, 001F19AH, 001F1AEH, 001F1AEH, + 001F200H, 001F202H, 001F210H, 001F23BH, 001F240H, 001F248H, 001F250H, 001F251H, + 001F260H, 001F265H, 001F300H, 001F320H, 001F32DH, 001F335H, 001F337H, 001F37CH, + 001F37EH, 001F393H, 001F3A0H, 001F3CAH, 001F3CFH, 001F3D3H, 001F3E0H, 001F3F0H, + 001F3F4H, 001F3F4H, 001F3F8H, 001F43EH, 001F440H, 001F440H, 001F442H, 001F4FCH, + 001F4FFH, 001F53DH, 001F54BH, 001F54EH, 001F550H, 001F567H, 001F57AH, 001F57AH, + 001F595H, 001F596H, 001F5A4H, 001F5A4H, 001F5FBH, 001F64FH, 001F680H, 001F6C5H, + 001F6CCH, 001F6CCH, 001F6D0H, 001F6D2H, 001F6D5H, 001F6D9H, 001F6DCH, 001F6DFH, + 001F6EBH, 001F6ECH, 001F6F4H, 001F6FCH, 001F7DAH, 001F7DAH, 001F7E0H, 001F7EBH, + 001F7F0H, 001F7F0H, 001F90CH, 001F93AH, 001F93CH, 001F945H, 001F947H, 001F9FFH, + 001FA70H, 001FA7CH, 001FA80H, 001FAC6H, 001FAC8H, 001FAC8H, 001FACCH, 001FADDH, + 001FADFH, 001FAEBH, 001FAEFH, 001FAFAH, 0020000H, 002FFFDH, 0030000H, 003FFFDH]; +(* end of the tables *) + +(* Whether c is in one of the ranges of t, by halving. *) +proc within(t: []I32; c: I32): BOOL; +var lo, hi, m: INT; +begin + lo := 0; + hi := LEN(t) div 2 - 1; + while lo <= hi do + m := (lo + hi) div 2; + if c < t[2 * m] then hi := m - 1; + elsif c > t[2 * m + 1] then lo := m + 1; + else return true; + end; + end; + return false; +end; + +(* The columns the character c takes: 0, 1 or 2. *) +proc width*(c: I32): INT; +begin + if (c >= 32) and (c < 7FH) then return 1; end; (* ASCII, at once *) + if within(Zero, c) then return 0; end; + if within(Wide, c) then return 2; end; + return 1; +end; + +(* The columns the UTF-8 text s takes, as Str.decode reads it. *) +proc columns*(s: []U8): INT; +var c: I32; i, n, w: INT; +begin + i := 0; + w := 0; + while i < LEN(s) do + c, n := Str.decode(s[i..LEN(s)]); + w := w + width(c); + i := i + n; + end; + return w; +end; + +(* The module's test. *) +proc main*; +begin + if (width('a') # 1) or (width(' ') # 1) or (width(0) # 0) or (width(9) # 0) + or (width(7FH) # 0) then Rt.stop("Width.width: ASCII"); end; + if (width(0301H) # 0) or (width(200BH) # 0) or (width(0ADH) # 1) + or (width(1160H) # 0) then Rt.stop("Width.width: none"); end; + if (width(4E2DH) # 2) or (width(3042H) # 2) or (width(0FF21H) # 2) + or (width(01F600H) # 2) or (width(3000H) # 2) then Rt.stop("Width.width: wide"); end; + if (width(0E9H) # 1) or (width(03B1H) # 1) or (width(0FFFDH) # 1) + or (width(10FFFFH) # 1) then Rt.stop("Width.width: one"); end; + if (columns("") # 0) or (columns("abc") # 3) or (columns("e\u{301}") # 1) + or (columns("\u{4E2D}\u{6587}") # 4) or (columns("a\u{1F600}b") # 4) then + Rt.stop("Width.columns"); + end; +end; + +end Width. diff --git a/test/check.sh b/test/check.sh index d1b0e42..e4a2ca2 100755 --- a/test/check.sh +++ b/test/check.sh @@ -252,6 +252,19 @@ if (ulimit -v 60000; $out/lib_Thread.linux-x86_64 fail) > /dev/null 2>&1; then pass=$((pass + 1)) else echo "FAIL Thread under a memory limit"; fail=$((fail + 1)); fi +# a module imported and never called leaves nothing of its own in the +# program, not even Width's tables: the same program, importing it or +# not, makes a file of the same size (the names the trace shows may +# come in another order). Width's own imports, Str and through it Heap, +# run their bodies, as every module does, so both import Str +mkdir -p $out/shake +printf 'module Shake;\nimport Out, Str, Width;\nproc main*;\nbegin\n Out.int(1); Out.ln;\nend;\nend Shake.\n' >$out/shake/Shake.raz +$raz -o $out/shake/with $out/shake/Shake.raz 2>/dev/null +printf 'module Shake;\nimport Out, Str;\nproc main*;\nbegin\n Out.int(1); Out.ln;\nend;\nend Shake.\n' >$out/shake/Shake.raz +$raz -o $out/shake/without $out/shake/Shake.raz +if [ $(wc -c <$out/shake/with) = $(wc -c <$out/shake/without) ]; then pass=$((pass + 1)) +else echo "FAIL an unused import, Width, changes the program"; fail=$((fail + 1)); fi + # the editors' support, headless, where the editors are installed if test/editors.sh; then pass=$((pass + 1)); else fail=$((fail + 1)); fi diff --git a/tools/Widths.raz b/tools/Widths.raz new file mode 100644 index 0000000..3c01ffd --- /dev/null +++ b/tools/Widths.raz @@ -0,0 +1,185 @@ +(* Widths: makes the tables of lib/Width.raz from Unicode's data. + + Widths lib/Width.raz EastAsianWidth.txt UnicodeData.txt PropList.txt + + reads three files of Unicode's character database, which + https://www.unicode.org/Public/UCD/latest/ucd/ has, and writes the + tables of lib/Width.raz again, the lines between (* tables *) and + (* end of the tables *), with the version of Unicode they come from. + The rest of the file stays as it is. Width's opening comment says + what each width is; this program is the same rules, in code. *) +module Widths; +import Out, Rt, Str, Buf, Heap, Files; + +const + Last = 10FFFFH; (* the last code point *) + Start = "(* tables *)\n"; + Stop = "(* end of the tables *)"; + +var + w: [Last + 1]U8; (* each character's width, 1 unless a file says *) + +(* The whole file at path. *) +proc load(path: []U8; b: ^Buf.Buf): []U8; +begin + Buf.init(b, Heap.heap); + if not Files.read(path, b) then + Out.str(path); Out.str(": can't be read\n"); + Rt.exit(1); + end; + return Buf.bytes(b); +end; + +(* s in hexadecimal, a code point; stops the program if it isn't one. *) +proc point(s: []U8): INT; +var v: U64; ok: BOOL; +begin + v, ok := Str.toHex(Str.trim(s)); + if not ok or (v > Last) then + Out.str("not a code point: "); Out.str(s); Out.ln; + Rt.exit(1); + end; + return INT(v); +end; + +(* Sets the width of the characters from lo to hi. *) +proc mark(lo, hi: INT; width: U8); +var c: INT; +begin + for c := lo to hi do w[c] := width; end; +end; + +(* EastAsianWidth.txt: lines of a code point, or a range lo..hi, ";" + and the property; W and F are 2 columns. Its first line names the + file, and so the version. *) +proc eastAsian(text: []U8): []U8; +var line, rest, r, p, lo, hi, version: []U8; ok: BOOL; +begin + line, rest, ok := Str.cut(text, "\n"); + _, version, ok := Str.cut(line, "-"); + version, _, ok := Str.cut(version, ".txt"); + rest := text; + while LEN(rest) > 0 do + line, rest, ok := Str.cut(rest, "\n"); + line, _, ok := Str.cut(line, "#"); + r, p, ok := Str.cut(line, ";"); + if ok then + p := Str.trim(p); + if (p = "W") or (p = "F") then + lo, hi, ok := Str.cut(r, ".."); + if ok then mark(point(lo), point(hi), 2); else mark(point(lo), point(lo), 2); end; + end; + end; + end; + return version; +end; + +(* UnicodeData.txt: lines of fields between ";", the code point, the + name and the general category first. A range is two lines, its + first and last, named "<..., First>" and "<..., Last>". *) +proc categories(text: []U8); +var line, rest, c, name, gc: []U8; ok: BOOL; first, n: INT; +begin + rest := text; + first := -1; + while LEN(rest) > 0 do + line, rest, ok := Str.cut(rest, "\n"); + c, line, ok := Str.cut(line, ";"); + name, line, ok := Str.cut(line, ";"); + gc, line, ok := Str.cut(line, ";"); + if not ok then continue; end; + n := point(c); + if Str.hasSuffix(name, ", First>") then first := n; continue; end; + if not Str.hasSuffix(name, ", Last>") then first := n; end; + if ((gc = "Mn") or (gc = "Me") or (gc = "Cf") or (gc = "Cc")) and (n # 0ADH) then + mark(first, n, 0); (* the soft hyphen is seen: 1 *) + end; + end; +end; + +(* PropList.txt: lines of a code point, or a range lo..hi, ";" and a + property; a Prepended_Concatenation_Mark, a sign set over the digits + after it, is seen, so 1 column, though a format character. *) +proc concatenation(text: []U8); +var line, rest, r, p, lo, hi: []U8; ok: BOOL; +begin + rest := text; + while LEN(rest) > 0 do + line, rest, ok := Str.cut(rest, "\n"); + line, _, ok := Str.cut(line, "#"); + r, p, ok := Str.cut(line, ";"); + if ok and (Str.trim(p) = "Prepended_Concatenation_Mark") then + lo, hi, ok := Str.cut(r, ".."); + if ok then mark(point(lo), point(hi), 1); else mark(point(lo), point(lo), 1); end; + end; + end; +end; + +(* c as a constant of raz: 0, six hexadecimal digits and H. *) +proc hex(b: ^Buf.Buf; c: INT); +const Digits = "0123456789ABCDEF"; +var i: INT; +begin + Buf.add(b, '0'); + for i := 5 to 0 by -1 do Buf.add(b, Digits[(c shr (4 * i)) & 15]); end; + Buf.add(b, 'H'); +end; + +(* The constant name, the ranges of characters whose width is width, + each its first and last, four ranges to a line. *) +proc table(b: ^Buf.Buf; name: []U8; width: U8); +var c, lo, n: INT; +begin + Buf.str(b, " "); Buf.str(b, name); Buf.str(b, ": []I32 = ["); + n := 0; + c := 0; + while c <= Last do + if w[c] # width then INC(c); continue; end; + lo := c; + while (c < Last) and (w[c + 1] = width) do INC(c); end; + if n > 0 then Buf.str(b, ","); end; + if n mod 4 = 0 then Buf.str(b, "\n "); else Buf.str(b, " "); end; + hex(b, lo); Buf.str(b, ", "); hex(b, c); + INC(n); + INC(c); + end; + Buf.str(b, "];\n"); +end; + +proc main*; +var src, eaw, ucd, props, out: Buf.Buf; s, before, after, version: []U8; ok: BOOL; +begin + if LEN(Files.arg(4)) = 0 then + Out.str("usage: Widths lib/Width.raz EastAsianWidth.txt UnicodeData.txt PropList.txt\n"); + Rt.exit(2); + end; + s := load(Files.arg(1), ADR(src)); + before, after, ok := Str.cut(s, Start); + if ok then _, after, ok := Str.cut(after, Stop); end; + if not ok then + Out.str(Files.arg(1)); Out.str(": no (* tables *) and (* end of the tables *)\n"); + Rt.exit(1); + end; + mark(0, Last, 1); + version := eastAsian(load(Files.arg(2), ADR(eaw))); + categories(load(Files.arg(3), ADR(ucd))); + concatenation(load(Files.arg(4), ADR(props))); + mark(1160H, 11FFH, 0); (* Hangul's medial and final jamo *) + mark(0D7B0H, 0D7FFH, 0); + mark(3164H, 3164H, 0); (* the Hangul fillers, which draw nothing *) + mark(0FFA0H, 0FFA0H, 0); + Buf.init(ADR(out), Heap.heap); + Buf.str(ADR(out), before); + Buf.str(ADR(out), Start); + Buf.str(ADR(out), "(* Unicode "); Buf.str(ADR(out), version); Buf.str(ADR(out), " *)\nconst\n"); + table(ADR(out), "Zero", 0); + table(ADR(out), "Wide", 2); + Buf.str(ADR(out), Stop); + Buf.str(ADR(out), after); + if not Files.write(Files.arg(1), Buf.bytes(ADR(out))) then + Out.str(Files.arg(1)); Out.str(": can't be written\n"); + Rt.exit(1); + end; +end; + +end Widths.