(* 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. Its test uses Check. *) module Width; import Str, Check; (* 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: []const 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: []const 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; (* Checks that every character in cs has the width w. *) proc allWidth(what: []const U8; cs: []const I32; w: INT); var i: INT; begin for i := 0 to LEN(cs) - 1 do Check.int(what, width(cs[i]), w); end; end; (* The module's test. *) proc main*; begin allWidth("Width.width: ASCII 1", [I32('a'), I32(' ')], 1); allWidth("Width.width: ASCII 0", [I32(0), 9, 7FH], 0); allWidth("Width.width: none", [I32(0301H), 200BH, 1160H], 0); Check.int("Width.width: soft hyphen", width(0ADH), 1); allWidth("Width.width: wide", [I32(4E2DH), 3042H, 0FF21H, 01F600H, 3000H], 2); allWidth("Width.width: one", [I32(0E9H), 03B1H, 0FFFDH, 10FFFFH], 1); Check.int("Width.columns empty", columns(""), 0); Check.int("Width.columns abc", columns("abc"), 3); Check.int("Width.columns combining", columns("e\u{301}"), 1); Check.int("Width.columns wide", columns("\u{4E2D}\u{6587}"), 4); Check.int("Width.columns mixed", columns("a\u{1F600}b"), 4); end; end Width.