(* Operations on strings, which are []const U8. = and # compare them; this module orders, searches and hashes them, trims them and takes them apart, tells an ASCII character's kind and changes a letter's case, joins them and replaces in them, reads and writes their UTF-8, reads a number from one and writes one, and copies one to keep. A part it gives is a slice of the string it was given, with no memory of its own. *) module Str; import Mem, Heap, Rt, Bytes, Buf; import SYSTEM; const least = -9223372036854775807 - 1; (* the smallest I64 *) (* s's bytes, copied into memory from a, to keep after s's own are gone; "" and false if a has no room. *) proc copy*(a: ^Mem.Allocator; s: []const U8): ([]U8, BOOL); var p: ^U8; begin if LEN(s) = 0 then return [], true; end; p := Mem.alloc(a, LEN(s), 1); if p = nil then return [], false; end; COPY(p[0..LEN(s)], s); return p[0..LEN(s)], true; end; (* Gives back to a a copy that copy made from it; "", which copy gives for an empty string, was never a's, and is left alone. *) proc free*(a: ^Mem.Allocator; s: []U8); begin if LEN(s) > 0 then Mem.free(a, SYSTEM.ADR(s[0])); end; end; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte. *) proc cmp*(a, b: []const U8): INT; var i, n: INT; begin n := LEN(a); if LEN(b) < n then n := LEN(b); end; for i := 0 to n - 1 do if a[i] # b[i] then if a[i] < b[i] then return -1; end; return 1; end; end; if LEN(a) < LEN(b) then return -1; end; if LEN(a) > LEN(b) then return 1; end; return 0; end; (* Whether a and b hold the same bytes: a = b, as a procedure value. *) proc eq*(a, b: []const U8): BOOL; begin return a = b; end; (* c, a small letter if it is an ASCII capital. *) proc small(c: U8): U8; begin if (c >= 'A') and (c <= 'Z') then return c + 32; end; return c; end; (* Whether a and b are the same but for the case of ASCII letters. *) proc eqFold*(a, b: []const U8): BOOL; var i: INT; begin if LEN(a) # LEN(b) then return false; end; for i := 0 to LEN(a) - 1 do if small(a[i]) # small(b[i]) then return false; end; end; return true; end; (* FNV-1a, 64 bits. *) proc hash*(s: []const U8): U64; var h: U64; i: INT; begin h := 0CBF29CE484222325H; for i := 0 to LEN(s) - 1 do h := (h xor U64(s[i])) * 100000001B3H; end; return h; end; (* Where sub first starts in s, or -1. *) proc find*(s, sub: []const U8): INT; begin return Bytes.find(s, sub); end; (* Where sub first starts in s at or after from, or -1; from is at most LEN(s). *) proc findFrom*(s, sub: []const U8; from: INT): INT; var i: INT; begin i := Bytes.find(s[from..LEN(s)], sub); if i < 0 then return -1; end; return from + i; end; (* Where sub last starts in s, or -1. *) proc findLast*(s, sub: []const U8): INT; var i: INT; begin for i := LEN(s) - LEN(sub) to 0 by -1 do if s[i..i + LEN(sub)] = sub then return i; end; end; return -1; end; (* Whether s starts with p. *) proc hasPrefix*(s, p: []const U8): BOOL; begin return (LEN(s) >= LEN(p)) and (s[0..LEN(p)] = p); end; (* Whether s ends with x. *) proc hasSuffix*(s, x: []const U8): BOOL; begin return (LEN(s) >= LEN(x)) and (s[LEN(s) - LEN(x)..LEN(s)] = x); end; (* Whether c is a space: a blank, a tab, a line feed, a vertical tab, a form feed or a carriage return. *) proc isSpace*(c: U8): BOOL; begin return (c = ' ') or (c >= 9) and (c <= 13); end; (* Whether c is an ASCII digit, 0 to 9. *) proc isDigit*(c: U8): BOOL; begin return (c >= '0') and (c <= '9'); end; (* Whether c is an ASCII letter, A to Z or a to z. *) proc isLetter*(c: U8): BOOL; begin return (small(c) >= 'a') and (small(c) <= 'z'); end; (* Whether c is printable ASCII and neither a letter, a digit nor the blank: one of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~. *) proc isPunct*(c: U8): BOOL; begin return (c > ' ') and (c < 127) and not isLetter(c) and not isDigit(c); end; (* c's value as a hexadecimal digit, capital or small; -1 if it isn't one. *) proc hexVal*(c: U8): INT; begin match c of | '0'..'9': return INT(c) - '0'; | 'A'..'F': return INT(c) - 'A' + 10; | 'a'..'f': return INT(c) - 'a' + 10; | else: return -1; end; end; (* s without the spaces at its start. *) proc trimLeft*(s: []const U8): []const U8; var i: INT; begin i := 0; while (i < LEN(s)) and isSpace(s[i]) do INC(i); end; return s[i..LEN(s)]; end; (* s without the spaces at its end. *) proc trimRight*(s: []const U8): []const U8; var n: INT; begin n := LEN(s); while (n > 0) and isSpace(s[n - 1]) do DEC(n); end; return s[0..n]; end; (* s without the spaces at either end. *) proc trim*(s: []const U8): []const U8; begin return trimRight(trimLeft(s)); end; (* What comes before the first sep in s, what comes after it, and true; or s, an empty string and false if s has no sep. *) proc cut*(s, sep: []const U8): ([]const U8, []const U8, BOOL); var i: INT; begin i := find(s, sep); if i < 0 then return s, s[LEN(s)..LEN(s)], false; end; return s[0..i], s[i + LEN(sep)..LEN(s)], true; end; (* The first word of s, the bytes after the spaces at its start up to the next space or the end, and the rest of s from there; two empty strings if s holds only spaces. *) proc field*(s: []const U8): ([]const U8, []const U8); var i: INT; begin s := trimLeft(s); i := 0; while (i < LEN(s)) and not isSpace(s[i]) do INC(i); end; return s[0..i], s[i..LEN(s)]; end; (* s's ASCII capitals made small, in place. *) proc lower*(s: []U8); var i: INT; begin for i := 0 to LEN(s) - 1 do s[i] := small(s[i]); end; end; (* s's small ASCII letters made capitals, in place. *) proc upper*(s: []U8); var i: INT; begin for i := 0 to LEN(s) - 1 do if (s[i] >= 'a') and (s[i] <= 'z') then s[i] := s[i] - 32; end; end; end; (* The parts added to b, with sep between each two. *) proc join*(var b: Buf.Buf; parts: [][]const U8; sep: []const U8); var i: INT; begin for i := 0 to LEN(parts) - 1 do if i > 0 then Buf.str(b, sep); end; Buf.str(b, parts[i]); end; end; (* s added to b, with new for each old in it, from the left, none overlapping another; s as it is if old is empty. *) proc replace*(var b: Buf.Buf; s, old, new: []const U8); var i: INT; begin i := -1; if LEN(old) > 0 then i := find(s, old); end; while i >= 0 do Buf.str(b, s[0..i]); Buf.str(b, new); s := s[i + LEN(old)..LEN(s)]; i := find(s, old); end; Buf.str(b, s); end; (* c in UTF-8: its bytes, and how many. A number that isn't a character, a surrogate or one past 10FFFFH, becomes FFFDH, the character that stands for a wrong one. *) proc encode*(c: I32): ([4]U8, INT); var b: [4]U8; begin if (c < 0) or (c > 10FFFFH) or (c >= 0D800H) and (c <= 0DFFFH) then c := 0FFFDH; end; if c < 80H then b[0] := U8(c); return b, 1; elsif c < 800H then b[0] := U8(0C0H + (c shr 6)); b[1] := U8(80H + (c & 3FH)); return b, 2; elsif c < 10000H then b[0] := U8(0E0H + (c shr 12)); b[1] := U8(80H + (c shr 6 & 3FH)); b[2] := U8(80H + (c & 3FH)); return b, 3; end; b[0] := U8(0F0H + (c shr 18)); b[1] := U8(80H + (c shr 12 & 3FH)); b[2] := U8(80H + (c shr 6 & 3FH)); b[3] := U8(80H + (c & 3FH)); return b, 4; end; (* The character s starts with, and its length in bytes: FFFDH and 1 if its first byte doesn't start one rightly, being a continuation, or followed by too few continuations, or spelling a surrogate or a character in more bytes than it needs; 0 and 0 if s is empty. *) proc decode*(s: []const U8): (I32, INT); var c, least: I32; n, i: INT; begin if LEN(s) = 0 then return 0, 0; end; c := I32(s[0]); if c < 80H then return c, 1; elsif c < 0C0H then return 0FFFDH, 1; elsif c < 0E0H then n := 2; c := c & 1FH; least := 80H; elsif c < 0F0H then n := 3; c := c & 0FH; least := 800H; elsif c < 0F8H then n := 4; c := c & 7H; least := 10000H; else return 0FFFDH, 1; end; if LEN(s) < n then return 0FFFDH, 1; end; for i := 1 to n - 1 do if s[i] & 0C0H # 80H then return 0FFFDH, 1; end; c := c shl 6 + I32(s[i] & 3FH); end; if (c < least) or (c > 10FFFFH) or (c >= 0D800H) and (c <= 0DFFFH) then return 0FFFDH, 1; end; return c, n; end; (* The character s ends with, and its length in bytes, read as decode reads one: FFFDH and 1 if the last byte doesn't end one rightly; 0 and 0 if s is empty. *) proc decodeLast*(s: []const U8): (I32, INT); var c: I32; i, n: INT; begin if LEN(s) = 0 then return 0, 0; end; (* back to the byte that starts it, at most 3 continuations back *) i := LEN(s) - 1; while (i > 0) and (i > LEN(s) - 4) and (s[i] & 0C0H = 80H) do DEC(i); end; c, n := decode(s[i..LEN(s)]); if i + n # LEN(s) then return 0FFFDH, 1; end; return c, n; end; (* The character at i in s, as decode reads it, and where the one after it starts; 0 and -1 if i is at the end or past it. *) proc next*(s: []const U8; i: INT): (I32, INT); var c: I32; n: INT; begin if i >= LEN(s) then return 0, -1; end; c, n := decode(s[i..LEN(s)]); return c, i + n; end; (* The characters in s, as decode reads them, each byte that doesn't start one rightly counted as one. *) proc chars*(s: []const U8): INT; var i, n: INT; begin _, i := next(s, 0); while i >= 0 do INC(n); _, i := next(s, i); end; return n; end; (* Whether s is UTF-8: decode finds no byte that doesn't start a character rightly. *) proc valid*(s: []const U8): BOOL; var c: I32; i, n: INT; begin while i < LEN(s) do c, n := decode(s[i..LEN(s)]); if (c = 0FFFDH) and (n = 1) then return false; end; INC(i, n); end; return true; end; (* s as a decimal number, with a - or + before it if any, and nothing else; false if it isn't one, or an I64 can't hold it. *) proc toInt*(s: []const U8): (I64, BOOL); var n, d: I64; i: INT; neg: BOOL; begin neg := (LEN(s) > 0) and (s[0] = '-'); if (LEN(s) > 0) and ((s[0] = '-') or (s[0] = '+')) then i := 1; end; if i = LEN(s) then return 0, false; end; (* the digits are taken off below zero, since the smallest I64 has no positive *) while i < LEN(s) do if (s[i] < '0') or (s[i] > '9') then return 0, false; end; d := I64(s[i] - '0'); if n < (least + d) div 10 then return 0, false; end; n := n * 10 - d; INC(i); end; if not neg then if n = least then return 0, false; end; n := -n; end; return n, true; end; (* s in hexadecimal, digits only, capitals or not; false if it isn't that, or a U64 can't hold it. *) proc toHex*(s: []const U8): (U64, BOOL); var v: U64; d, i: INT; begin if LEN(s) = 0 then return 0, false; end; for i := 0 to LEN(s) - 1 do d := hexVal(s[i]); if d < 0 then return 0, false; end; if v shr 60 # 0 then return 0, false; end; v := v shl 4 | U64(d); end; return v, true; end; const (* The digits of RFC 4648: section 4's base64, and section 5's, which has - and _ for + and / and is safe in a URL or a file name. *) Base64* = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; Base64Url* = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; (* The bytes base64 makes of n bytes: 4 for each 3, or part of 3. *) proc base64Size*(n: INT): INT; begin return (n + 2) div 3 * 4; end; (* s in base64 into out, which holds base64Size(LEN(s)) bytes at least; a shorter out stops the program at the byte it lacks, as any slice does. alphabet holds the 64 digits (Base64 or Base64Url, or one's own). A last group of 1 or 2 bytes is padded with = to four if pad, and else has 2 or 3 digits. The bytes written. *) proc base64With*(s: []const U8; out: []U8; alphabet: []const U8; pad: BOOL): INT; var i, k, v, left: INT; begin k := 0; i := 0; while i < LEN(s) do left := LEN(s) - i; (* the bytes not yet taken; 3 or more fill a group *) v := INT(s[i]) shl 16; if left > 1 then v := v | (INT(s[i + 1]) shl 8); end; if left > 2 then v := v | INT(s[i + 2]); end; out[k] := alphabet[v shr 18]; out[k + 1] := alphabet[(v shr 12) & 63]; INC(k, 2); if left > 1 then out[k] := alphabet[(v shr 6) & 63]; INC(k); elsif pad then out[k] := '='; INC(k); end; if left > 2 then out[k] := alphabet[v & 63]; INC(k); elsif pad then out[k] := '='; INC(k); end; INC(i, 3); end; return k; end; (* s in base64 (RFC 4648, section 4), padded, into out as base64With writes it. *) proc base64*(s: []const U8; out: []U8): INT; begin return base64With(s, out, Base64, true); end; (* c's value as a digit of alphabet, 0 to 63, or -1 if it isn't one; = is -1. *) proc base64Val(c: U8; alphabet: []const U8): INT; var i: INT; begin for i := 0 to 63 do if alphabet[i] = c then return i; end; end; return -1; end; (* The last 2 or 3 digits of an unpadded s, at s, decoded into out: the bytes written, 1 or 2; or -1 if a digit isn't one, or the bits that no byte uses aren't 0. *) proc unbase64Tail(s: []const U8; out: []U8; alphabet: []const U8): INT; var a, b, c: INT; begin a := base64Val(s[0], alphabet); b := base64Val(s[1], alphabet); if (a < 0) or (b < 0) then return -1; end; out[0] := U8(((a shl 6) | b) shr 4); if LEN(s) = 2 then if b & 15 # 0 then return -1; end; (* b's low 4 bits belong to no byte *) return 1; end; c := base64Val(s[2], alphabet); if (c < 0) or (c & 3 # 0) then return -1; end; (* c's low 2 bits belong to no byte *) out[1] := U8((((a shl 12) | (b shl 6) | c) shr 2) & 255); return 2; end; (* s, base64 with the digits of alphabet, decoded into out, which holds (LEN(s) + 3) div 4 * 3 bytes at least; a shorter out stops the program at the byte it lacks, as any slice does. The bytes written, and true; or 0 and false, with out left in no state to use, if s is not base64 as the RFC writes it and in no other way. With pad: its length a multiple of 4, every byte a digit but the last one or two, which may be = (and then both of the last two are, or the last alone). Without: no =, and a length that is not 1 more than a multiple of 4. Either way the bits of the last digit that no byte uses are 0, so a byte string has the one spelling that base64 gives it (RFC 4648, section 3.5 allows a decoder to take that). Line breaks, and padding or its lack, are the caller's to mend first. *) proc unbase64With*(s: []const U8; out: []U8; alphabet: []const U8; pad: BOOL): (INT, BOOL); var i, k, a, b, c, d, v, whole, n: INT; last: BOOL; begin whole := LEN(s); (* the bytes in groups of 4 *) if pad then if LEN(s) mod 4 # 0 then return 0, false; end; else whole := LEN(s) div 4 * 4; if LEN(s) mod 4 = 1 then return 0, false; end; end; k := 0; i := 0; while i < whole do last := i + 4 = LEN(s); (* padding is for the last group only *) a := base64Val(s[i], alphabet); b := base64Val(s[i + 1], alphabet); c := base64Val(s[i + 2], alphabet); d := base64Val(s[i + 3], alphabet); if (a < 0) or (b < 0) then return 0, false; end; v := (a shl 6) | b; out[k] := U8(v shr 4); if c >= 0 then v := (v shl 6) | c; out[k + 1] := U8((v shr 2) & 255); if d >= 0 then out[k + 2] := U8(((v shl 6) | d) & 255); INC(k, 3); elsif pad and last and (s[i + 3] = '=') and (c & 3 = 0) then INC(k, 2); (* "xxx=": c's low 2 bits belong to no byte *) else return 0, false; end; elsif pad and last and (s[i + 2] = '=') and (s[i + 3] = '=') and (b & 15 = 0) then INC(k); (* "xx==": b's low 4 bits belong to no byte *) else return 0, false; end; INC(i, 4); end; if whole < LEN(s) then n := unbase64Tail(s[whole..LEN(s)], out[k..LEN(out)], alphabet); if n < 0 then return 0, false; end; INC(k, n); end; return k, true; end; (* s, base64 (RFC 4648, section 4), padded, decoded into out as unbase64With reads it; out holds LEN(s) div 4 * 3 bytes at least. *) proc unbase64*(s: []const U8; out: []U8): (INT, BOOL); begin return unbase64With(s, out, Base64, true); end; (* s as a decimal number, digits only; false if it isn't one, or a U64 can't hold it. *) proc toU64*(s: []const U8): (U64, BOOL); var v, d: U64; i: INT; begin if LEN(s) = 0 then return 0, false; end; for i := 0 to LEN(s) - 1 do if not isDigit(s[i]) then return 0, false; end; d := U64(s[i] - '0'); if v > (0FFFFFFFFFFFFFFFFH - d) div 10 then return 0, false; end; v := v * 10 + d; end; return v, true; end; (* The bytes at p up to its zero byte, not copied; "" for nil. *) proc fromC*(p: ^U8): []const U8; var n: INT; begin if p = nil then return ""; end; while p[n] # 0 do INC(n); end; return p[0..n]; end; (* s copied into out with a zero byte after it, for C: out holds LEN(s) + 1 bytes at least, or the program stops at the byte it lacks, as any slice does. The pointer C is given, SYSTEM.PTR(out). *) proc toC*(s: []const U8; out: []U8): ^U8; var i: INT; begin for i := 0 to LEN(s) - 1 do out[i] := s[i]; end; out[LEN(s)] := 0; return SYSTEM.PTR(out); end; (* n in decimal, as Out.int writes it: its bytes, the first of the 20, and how many. *) proc fromInt*(n: I64): ([20]U8, INT); var t, d: [20]U8; u: U64; i: INT; begin (* as a U64, which holds the smallest I64's magnitude *) if n < 0 then u := U64(-(n + 1)) + 1; else u := U64(n); end; i := 20; repeat DEC(i); t[i] := U8(u mod 10) + '0'; u := u div 10; until u = 0; if n < 0 then DEC(i); t[i] := '-'; end; COPY(d[0..20 - i], t[i..20]); return d, 20 - i; end; (* The bytes of s, at most 8, as an unsigned number, the first byte the lowest: how a file or a message in little-endian order is read, whatever the machine's own order. *) proc le*(s: []const U8): U64; var v: U64; i: INT; begin if LEN(s) > 8 then Rt.stop("Str.le: more than 8 bytes"); end; for i := LEN(s) - 1 to 0 by -1 do v := v shl 8 | U64(s[i]); end; return v; end; (* As le, the first byte the highest: big-endian, network order. *) proc be*(s: []const U8): U64; var v: U64; i: INT; begin if LEN(s) > 8 then Rt.stop("Str.be: more than 8 bytes"); end; for i := 0 to LEN(s) - 1 do v := v shl 8 | U64(s[i]); end; return v; end; (* v into the bytes of s, at most 8, the lowest first; what doesn't fit is left out. *) proc putLE*(s: []U8; v: U64); var i: INT; begin if LEN(s) > 8 then Rt.stop("Str.putLE: more than 8 bytes"); end; for i := 0 to LEN(s) - 1 do s[i] := U8(v); v := v shr 8; end; end; (* As putLE, the highest first. *) proc putBE*(s: []U8; v: U64); var i: INT; begin if LEN(s) > 8 then Rt.stop("Str.putBE: more than 8 bytes"); end; for i := LEN(s) - 1 to 0 by -1 do s[i] := U8(v); v := v shr 8; end; end; proc testOrder; var b: [8]U8; begin putLE(b[0..4], 12345678H); if (b[0] # 78H) or (b[3] # 12H) or (le(b[0..4]) # 12345678H) or (be(b[0..2]) # 7856H) then Rt.stop("Str.putLE"); end; putBE(b, 0102030405060708H); if (b[0] # 1) or (b[7] # 8) or (be(b) # 0102030405060708H) or (le(b[6..8]) # 0807H) then Rt.stop("Str.putBE"); end; putBE(b[0..2], 0ABCDH); if (be(b[0..2]) # 0ABCDH) or (le(b[0..0]) # 0) then Rt.stop("Str.be"); end; end; proc testUtf8; var b: [4]U8; n: INT; c, i: I32; begin b, n := encode('a'); if (n # 1) or (b[0] # 'a') then Rt.stop("Str.encode: a"); end; b, n := encode(0E9H); if b[0..n] # "é" then Rt.stop("Str.encode: é"); end; b, n := encode(20ACH); if b[0..n] # "€" then Rt.stop("Str.encode: €"); end; b, n := encode(1F600H); if b[0..n] # "😀" then Rt.stop("Str.encode: 😀"); end; b, n := encode(0D800H); if b[0..n] # "\u{FFFD}" then Rt.stop("Str.encode: a surrogate"); end; b, n := encode(110000H); if n # 3 then Rt.stop("Str.encode: past 10FFFFH"); end; (* every character back through decode *) for i := 0 to 10FFFFH do if (i < 0D800H) or (i > 0DFFFH) then b, n := encode(i); c, n := decode(b[0..n]); if c # i then Rt.stop("Str.decode: not what encode wrote"); end; end; end; c, n := decode(""); if (c # 0) or (n # 0) then Rt.stop("Str.decode: empty"); end; c, n := decode("é!"); if (c # 0E9H) or (n # 2) then Rt.stop("Str.decode: é"); end; b := [80H, 'a', 0, 0]; c, n := decode(b[0..2]); if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: a continuation"); end; b := [0E2H, 82H, 'a', 0]; c, n := decode(b[0..3]); if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: too few continuations"); end; c, n := decode(b[0..2]); if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: cut short"); end; b := [0C0H, 0AFH, 0, 0]; c, n := decode(b[0..2]); if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: overlong"); end; b := [0EDH, 0A0H, 80H, 0]; c, n := decode(b[0..3]); if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: a surrogate"); end; b := [0F4H, 90H, 80H, 80H]; c, n := decode(b); if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: past 10FFFFH"); end; if not valid(b[1..1]) or valid(b) then Rt.stop("Str.valid"); end; c, n := decodeLast("a€"); if (c # 20ACH) or (n # 3) then Rt.stop("Str.decodeLast: €"); end; c, n := decodeLast(""); if (c # 0) or (n # 0) then Rt.stop("Str.decodeLast: empty"); end; b := ['a', 0E2H, 82H, 0]; c, n := decodeLast(b[0..3]); if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decodeLast: cut short"); end; b := [80H, 80H, 80H, 80H]; c, n := decodeLast(b); if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decodeLast: continuations"); end; if (chars("héllo😀") # 6) or (chars("") # 0) or (chars(b) # 4) or not valid("héllo😀") then Rt.stop("Str.chars"); end; c, n := next("é!", 0); if (c # 0E9H) or (n # 2) then Rt.stop("Str.next"); end; c, n := next("é!", 2); if (c # '!') or (n # 3) then Rt.stop("Str.next: !"); end; c, n := next("é!", 3); if (c # 0) or (n # -1) then Rt.stop("Str.next: the end"); end; end; proc testParts; var b: Buf.Buf; w, rest, k, v: []const U8; ok: BOOL; up: [5]U8; begin if not eqFold("Hello", "hELLO") or eqFold("a", "b") or eqFold("a", "ab") or not eqFold("", "") or eqFold("@", "`") then Rt.stop("Str.eqFold"); end; if (findLast("abcabc", "bc") # 4) or (findLast("abc", "x") # -1) or (findLast("abc", "") # 3) or (findLast("ab", "abc") # -1) then Rt.stop("Str.findLast"); end; if not hasSuffix("hello", "lo") or hasSuffix("lo", "hello") or not hasSuffix("x", "") then Rt.stop("Str.hasSuffix"); end; if (trim(" \t a b \r\n") # "a b") or (trimLeft(" a ") # "a ") or (trimRight(" a ") # " a") or (trim(" ") # "") or (trim("") # "") then Rt.stop("Str.trim"); end; k, v, ok := cut("key=val=ue", "="); if not ok or (k # "key") or (v # "val=ue") then Rt.stop("Str.cut"); end; k, v, ok := cut("none", "="); if ok or (k # "none") or (v # "") then Rt.stop("Str.cut: none"); end; w, rest := field(" one two\tthree "); if (w # "one") or (rest # " two\tthree ") then Rt.stop("Str.field"); end; w, rest := field(rest); w, rest := field(rest); if w # "three" then Rt.stop("Str.field: three"); end; w, rest := field(rest); if (w # "") or (rest # "") then Rt.stop("Str.field: none"); end; up := "aB-z9"; upper(up); if up # "AB-Z9" then Rt.stop("Str.upper"); end; lower(up); if up # "ab-z9" then Rt.stop("Str.lower"); end; Buf.init(b, Heap.heap); join(b, ["a", "b", "c"], ", "); if Buf.bytes(b) # "a, b, c" then Rt.stop("Str.join"); end; Buf.reset(b); join(b, [], "-"); replace(b, "a.b..c", ".", "::"); if Buf.bytes(b) # "a::b::::c" then Rt.stop("Str.replace"); end; Buf.reset(b); replace(b, "aaa", "aa", "b"); replace(b, "xy", "", "-"); if Buf.bytes(b) # "baxy" then Rt.stop("Str.replace: overlapping, empty"); end; Buf.done(b); end; proc testChars; var c: INT; begin for c := 0 to 255 do if isDigit(U8(c)) # ((c >= 48) and (c <= 57)) then Rt.stop("Str.isDigit"); end; if isLetter(U8(c)) # ((c >= 65) and (c <= 90) or (c >= 97) and (c <= 122)) then Rt.stop("Str.isLetter"); end; if isPunct(U8(c)) # ((c > 32) and (c < 127) and not isLetter(U8(c)) and not isDigit(U8(c))) then Rt.stop("Str.isPunct"); end; end; if not isSpace(' ') or not isSpace(9) or not isSpace(13) or isSpace(14) or isSpace('a') then Rt.stop("Str.isSpace"); end; if isPunct(' ') or not isPunct('~') or not isPunct('!') or isPunct(127) then Rt.stop("Str.isPunct: ends"); end; if (hexVal('0') # 0) or (hexVal('9') # 9) or (hexVal('a') # 10) or (hexVal('F') # 15) or (hexVal('g') # -1) or (hexVal('@') # -1) then Rt.stop("Str.hexVal"); end; end; proc testToInt; var n: I64; ok: BOOL; begin n, ok := toInt("42"); if not ok or (n # 42) then Rt.stop("Str.toInt: 42"); end; n, ok := toInt("-17"); if not ok or (n # -17) then Rt.stop("Str.toInt: -17"); end; n, ok := toInt("+0"); if not ok or (n # 0) then Rt.stop("Str.toInt: +0"); end; n, ok := toInt("9223372036854775807"); if not ok or (n # 9223372036854775807) then Rt.stop("Str.toInt: the largest"); end; n, ok := toInt("-9223372036854775808"); if not ok or (n # least) then Rt.stop("Str.toInt: the smallest"); end; n, ok := toInt("9223372036854775808"); if ok then Rt.stop("Str.toInt: past the largest"); end; n, ok := toInt("-9223372036854775809"); if ok then Rt.stop("Str.toInt: past the smallest"); end; n, ok := toInt(""); if ok then Rt.stop("Str.toInt: empty"); end; n, ok := toInt("-"); if ok then Rt.stop("Str.toInt: a sign alone"); end; n, ok := toInt("12a"); if ok then Rt.stop("Str.toInt: a letter"); end; n, ok := toInt(" 1"); if ok then Rt.stop("Str.toInt: a space"); end; end; proc testNumbers; var u: U64; ok: BOOL; d: [20]U8; n: INT; begin u, ok := toU64("18446744073709551615"); if not ok or (u # 0FFFFFFFFFFFFFFFFH) then Rt.stop("Str.toU64: the largest"); end; u, ok := toU64("18446744073709551616"); if ok then Rt.stop("Str.toU64: past the largest"); end; u, ok := toU64("0042"); if not ok or (u # 42) then Rt.stop("Str.toU64: 0042"); end; u, ok := toU64(""); if ok then Rt.stop("Str.toU64: empty"); end; u, ok := toU64("+1"); if ok then Rt.stop("Str.toU64: +1"); end; u, ok := toU64("-1"); if ok then Rt.stop("Str.toU64: -1"); end; u, ok := toU64("12a"); if ok then Rt.stop("Str.toU64: 12a"); end; u, ok := toHex("ff"); if not ok or (u # 255) then Rt.stop("Str.toHex: ff"); end; u, ok := toHex("FFFFFFFFFFFFFFFF"); if not ok or (u # 0FFFFFFFFFFFFFFFFH) then Rt.stop("Str.toHex: the largest"); end; u, ok := toHex("10000000000000000"); if ok then Rt.stop("Str.toHex: past the largest"); end; u, ok := toHex("0x1"); if ok then Rt.stop("Str.toHex: 0x"); end; u, ok := toHex(""); if ok then Rt.stop("Str.toHex: empty"); end; d, n := fromInt(0); if d[0..n] # "0" then Rt.stop("Str.fromInt: 0"); end; d, n := fromInt(-1234); if d[0..n] # "-1234" then Rt.stop("Str.fromInt: -1234"); end; d, n := fromInt(least); if d[0..n] # "-9223372036854775808" then Rt.stop("Str.fromInt: the smallest"); end; d, n := fromInt(9223372036854775807); if d[0..n] # "9223372036854775807" then Rt.stop("Str.fromInt: the largest"); end; end; (* fromC and toC. *) proc testC; var room: [4]U8; c: [4]U8; p: ^U8; begin if LEN(fromC(nil)) # 0 then Rt.stop("Str.fromC: nil"); end; c := "abc\x00"; if fromC(SYSTEM.ADR(c[0])) # "abc" then Rt.stop("Str.fromC: abc"); end; c[0] := 0; if LEN(fromC(SYSTEM.ADR(c[0]))) # 0 then Rt.stop("Str.fromC: empty"); end; room := "xxxx"; p := toC("abc", room); if (p # SYSTEM.ADR(room[0])) or (room[0..3] # "abc") or (room[3] # 0) then Rt.stop("Str.toC: abc"); end; p := toC("", room); if room[0] # 0 then Rt.stop("Str.toC: empty"); end; end; (* One of the RFC 4648 section 10 vectors: plain codes to coded and back. *) proc testVector(plain, coded: []const U8); var buf: [16]U8; n: INT; ok: BOOL; begin if base64Size(LEN(plain)) # LEN(coded) then Rt.stop("Str.base64Size"); end; n := base64(plain, buf[0..base64Size(LEN(plain))]); if buf[0..n] # coded then Rt.stop("Str.base64: vector"); end; n, ok := unbase64(coded, buf[0..LEN(coded) div 4 * 3]); if not ok or (buf[0..n] # plain) then Rt.stop("Str.unbase64: vector"); end; end; (* s must not decode. *) proc testBad(s: []const U8); var buf: [16]U8; ok: BOOL; begin _, ok := unbase64(s, buf[0..LEN(s) div 4 * 3]); if ok then Rt.stop("Str.unbase64: took a bad string"); end; end; (* Every byte value, in 256 strings of 1 to 256 bytes, goes there and back. *) proc testBase64Round; var plain: [256]U8; back: [258]U8; coded: [344]U8; i, n: INT; ok: BOOL; begin for i := 0 to 255 do plain[i] := U8(i); end; for i := 1 to 256 do n := base64(plain[0..i], coded[0..base64Size(i)]); if n # base64Size(i) then Rt.stop("Str.base64: size"); end; n, ok := unbase64(coded[0..base64Size(i)], back[0..base64Size(i) div 4 * 3]); if not ok or (back[0..n] # plain[0..i]) then Rt.stop("Str.base64: round trip"); end; end; end; (* plain in the other alphabet and unpadded, and back; in the padded form too, with the - and _ of base64url. *) proc testUrlVector(plain, bare, padded: []const U8); var buf: [16]U8; n: INT; ok: BOOL; begin n := base64With(plain, buf[0..base64Size(LEN(plain))], Base64Url, false); if buf[0..n] # bare then Rt.stop("Str.base64With: unpadded"); end; n, ok := unbase64With(bare, buf[0..(LEN(bare) + 3) div 4 * 3], Base64Url, false); if not ok or (buf[0..n] # plain) then Rt.stop("Str.unbase64With: unpadded"); end; n := base64With(plain, buf[0..base64Size(LEN(plain))], Base64Url, true); if buf[0..n] # padded then Rt.stop("Str.base64With: padded"); end; n, ok := unbase64With(padded, buf[0..LEN(padded) div 4 * 3], Base64Url, true); if not ok or (buf[0..n] # plain) then Rt.stop("Str.unbase64With: padded"); end; end; (* s must not decode, unpadded, in base64url's alphabet. *) proc testUrlBad(s: []const U8); var buf: [16]U8; ok: BOOL; begin _, ok := unbase64With(s, buf[0..(LEN(s) + 3) div 4 * 3], Base64Url, false); if ok then Rt.stop("Str.unbase64With: took a bad string"); end; end; (* RFC 4648 section 5's alphabet, padded and not. *) proc testBase64Url; var buf: [8]U8; ok: BOOL; begin testUrlVector("", "", ""); testUrlVector("f", "Zg", "Zg=="); testUrlVector("fo", "Zm8", "Zm8="); testUrlVector("foo", "Zm9v", "Zm9v"); testUrlVector("foob", "Zm9vYg", "Zm9vYg=="); testUrlVector("fooba", "Zm9vYmE", "Zm9vYmE="); testUrlVector("foobar", "Zm9vYmFy", "Zm9vYmFy"); testUrlVector("\xfb\xff", "-_8", "-_8="); testUrlBad("Z"); (* one digit over *) testUrlBad("Zm9vY"); testUrlBad("Zg=="); (* padding, unpadded *) testUrlBad("Zm8="); testUrlBad("Zm+v"); (* base64's digit *) testUrlBad("Zm/v"); testUrlBad("Zh"); (* the low 4 bits of h are not 0 *) testUrlBad("Zm9"); (* the low 2 bits of 9 are not 0 *) testUrlBad("Zm9v\n"); _, ok := unbase64With("Zg", buf[0..3], Base64, true); (* padding left off *) if ok then Rt.stop("Str.unbase64With: padding left off"); end; _, ok := unbase64With("Zm-v", buf[0..3], Base64, true); if ok then Rt.stop("Str.unbase64With: base64url's digit in base64"); end; end; proc testBase64; begin testVector("", ""); testVector("f", "Zg=="); testVector("fo", "Zm8="); testVector("foo", "Zm9v"); testVector("foob", "Zm9vYg=="); testVector("fooba", "Zm9vYmE="); testVector("foobar", "Zm9vYmFy"); testVector("\xfb\xff", "+/8="); testBad("Zg="); (* length not a multiple of 4 *) testBad("Zg"); (* padding left off *) testBad("Zm9v\n"); (* a line break *) testBad("Zm9!"); (* a byte outside the alphabet *) testBad("Zm-v"); (* base64url's digit *) testBad("Zg==Zm9v"); (* padding in the middle *) testBad("Z=9v"); (* = in a group's second place *) testBad("=m9v"); (* = in a group's first place *) testBad("Zm=v"); (* = before a digit *) testBad("Z==="); (* three pads *) testBad("===="); testBad("Zg=A"); (* one pad but a digit last *) testBad("Zh=="); (* the low 4 bits of h are not 0 *) testBad("Zm9="); (* the low 2 bits of 9 are not 0 *) testBad("Zm8=Zm9v"); (* padding, then another group *) testBase64Round; testBase64Url; end; (* The module's test: stops the program if something is wrong. *) proc main*; var buf: [3]U8; t: []U8; ok: BOOL; begin buf := "abc"; t, ok := copy(Heap.heap, buf); buf[0] := 'x'; if not ok or (t # "abc") then Rt.stop("Str.copy"); end; free(Heap.heap, t); t, ok := copy(Heap.heap, ""); free(Heap.heap, t); (* Heap would stop the program at a block it never gave *) if (cmp("a", "b") # -1) or (cmp("b", "a") # 1) or (cmp("ab", "ab") # 0) or (cmp("a", "ab") # -1) or (cmp("", "") # 0) then Rt.stop("Str.cmp"); end; if not eq("x", "x") or eq("x", "y") then Rt.stop("Str.eq"); end; if (hash("") # 0CBF29CE484222325H) or (hash("a") # 0AF63DC4C8601EC8CH) then Rt.stop("Str.hash"); end; if (find("hello", "ll") # 2) or (find("hello", "x") # -1) or (find("", "") # 0) or (find("ab", "abc") # -1) then Rt.stop("Str.find"); end; if not hasPrefix("hello", "he") or hasPrefix("he", "hello") then Rt.stop("Str.hasPrefix"); end; if (findFrom("abcabc", "bc", 1) # 1) or (findFrom("abcabc", "bc", 2) # 4) or (findFrom("abc", "c", 3) # -1) or (findFrom("abc", "", 3) # 3) then Rt.stop("Str.findFrom"); end; testChars; testParts; testUtf8; testToInt; testNumbers; testC; testBase64; testOrder; end; end Str.