From c290b2c87362f4fdc91e5ff560d05341286d7602 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Wed, 14 Oct 2026 22:38:12 +0000 Subject: [PATCH] Str: trimming, cutting, case, joining, UTF-8 and numbers; Math.toReal Str gains eqFold, findLast, hasSuffix, trim, trimLeft, trimRight, cut, field, lower, upper, join, replace, decodeLast, next, chars, valid, toHex and fromInt; Math gains toReal, the F64 nearest a decimal text, read exactly as the compiler reads a constant, with a sticky last digit past the 780 it keeps (report 2026-10.25, volume 2). Each is tested in its module's main. --- diff --git a/lib/Math.raz b/lib/Math.raz index 95c7bff..1c509ff 100644 --- a/lib/Math.raz +++ b/lib/Math.raz @@ -705,6 +705,250 @@ (* The module's test: values C's library gives, to within two units in the last place, and the edges. *) +(* Reading a real from text exactly: the F64 nearest to its digits + times 10 to its exponent, ties to the even one, as the compiler + reads a constant (src/Scan.raz). The number is a fraction of two big + integers, num / den, scaled by 2^e until its whole part has 53 or 54 + bits; that part and the remainder give the bits and the rounding. *) +const + MaxDigits = 780; (* significant digits kept; past them, a 1 if any left out isn't 0 *) + +type + Big = record + n: INT; (* words in use; 0 is zero *) + w: [128]U32; (* least significant first *) + end; +(* 768 significant digits are as many as can matter to rounding an + F64, so the ones past MaxDigits matter only by being 0 or not, which + a last digit 1 keeps. Then den is at most 10^(330 + 781), about + 2^3691, and num below 10^310; the shift that follows makes the + larger at most 54 bits longer than den, and the long division + shifts den 53 more: under 3,800 bits, of the 4,096 here. *) + +(* a := a * m + add *) +proc bigMul(var a: Big; m, add: U32); +var i: INT; c: U64; +begin + c := U64(add); + for i := 0 to a.n - 1 do + c := U64(a.w[i]) * U64(m) + c; + a.w[i] := U32(c); + c := c shr 32; + end; + if c # 0 then + a.w[a.n] := U32(c); + INC(a.n); + end; +end; + +proc bigShl(var a: Big; k: INT); +var i, ws, bits: INT; +begin + if a.n = 0 then return; end; + ws := k div 32; + bits := k mod 32; + a.w[a.n + ws] := 0; + for i := a.n - 1 to 0 by -1 do + if bits > 0 then a.w[i + ws + 1] := a.w[i + ws + 1] | (a.w[i] shr (32 - bits)); end; + a.w[i + ws] := a.w[i] shl bits; + end; + for i := 0 to ws - 1 do a.w[i] := 0; end; + INC(a.n, ws + 1); + while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; +end; + +proc bigShr1(var a: Big); +var i: INT; +begin + for i := 0 to a.n - 1 do + a.w[i] := a.w[i] shr 1; + if i + 1 < a.n then a.w[i] := a.w[i] | (a.w[i + 1] shl 31); end; + end; + while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; +end; + +(* -1, 0 or 1 as a is below, equal to or above b *) +proc bigCmp(var a, b: Big): INT; +var i: INT; +begin + if a.n # b.n then + if a.n > b.n then return 1; end; + return -1; + end; + for i := a.n - 1 to 0 by -1 do + if a.w[i] # b.w[i] then + if a.w[i] > b.w[i] then return 1; end; + return -1; + end; + end; + return 0; +end; + +(* a := a - b, b not above a *) +proc bigSub(var a, b: Big); +var i: INT; d: I64; +begin + d := 0; + for i := 0 to a.n - 1 do + d := d + I64(a.w[i]); + if i < b.n then d := d - I64(b.w[i]); end; + a.w[i] := U32(d); + if d < 0 then d := -1; else d := 0; end; (* a borrow *) + end; + while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; +end; + +proc bigBits(var a: Big): INT; +var t: U32; bits: INT; +begin + if a.n = 0 then return 0; end; + bits := (a.n - 1) * 32; + t := a.w[a.n - 1]; + while t # 0 do + INC(bits); + t := t shr 1; + end; + return bits; +end; + +(* Whether s[i] is a decimal digit. *) +proc digitAt(s: []U8; i: INT): BOOL; +begin + return (i < LEN(s)) and (s[i] >= '0') and (s[i] <= '9'); +end; + +(* s as a real: a sign if any, digits, a point and digits if any, and E + or e, a sign if any and digits if any; nothing else. The F64 nearest + it, ties to the even one, and false if s isn't one, or is past the + largest F64; one below the smallest is 0. *) +proc toReal*(s: []U8): (F64, BOOL); +var + num, den, d: Big; i, digits, e, ex: INT; neg, eneg, point, half, sticky: BOOL; + q, b: U64; x: F64; +begin + x := 0.0; + num.n := 0; + den.n := 1; + den.w[0] := 1; + digits := 0; + e := 0; + sticky := false; + neg := (LEN(s) > 0) and (s[0] = '-'); + i := 0; + if (LEN(s) > 0) and ((s[0] = '-') or (s[0] = '+')) then i := 1; end; + if not digitAt(s, i) then return x, false; end; + (* the digits: num, and e, the power of 10 it is to be taken to *) + point := false; + while digitAt(s, i) or not point and (i < LEN(s)) and (s[i] = '.') and digitAt(s, i + 1) do + if s[i] = '.' then + point := true; + elsif digits < MaxDigits then + bigMul(num, 10, U32(s[i] - '0')); + if num.n > 0 then INC(digits); end; (* counted from the first that isn't 0 *) + if point then DEC(e); end; + else + (* a digit not kept, which still counts if it is before the point *) + if s[i] # '0' then sticky := true; end; + if not point then INC(e); end; + end; + INC(i); + end; + if sticky then bigMul(num, 10, 1); DEC(e); end; + ex := 0; + if (i < LEN(s)) and ((s[i] = 'E') or (s[i] = 'e')) then + INC(i); + eneg := (i < LEN(s)) and (s[i] = '-'); + if (i < LEN(s)) and ((s[i] = '+') or (s[i] = '-')) then INC(i); end; + if not digitAt(s, i) then return x, false; end; + while digitAt(s, i) do + if ex < 100000 then ex := ex * 10 + INT(s[i]) - '0'; end; + INC(i); + end; + if eneg then ex := -ex; end; + end; + if i < LEN(s) then return x, false; end; + INC(e, ex); + if neg then x := -x; end; + if num.n = 0 then return x, true; end; + if digits + e > 310 then return x, false; end; (* 10^310 is past every F64 *) + if digits + e < -330 then return x, true; end; (* and 10^-330 below half the least *) + for i := 1 to e do bigMul(num, 10, 0); end; + for i := 1 to -e do bigMul(den, 10, 0); end; + (* num / den 2^-e from 2^52 to 2^54, but not past the least exponent *) + e := bigBits(num) - bigBits(den) - 53; + if e < -1074 then e := -1074; end; + if e < 0 then bigShl(num, -e); else bigShl(den, e); end; + (* q, its whole part, by long division; num is left the remainder *) + d := den; + bigShl(d, 53); + q := 0; + for i := 53 to 0 by -1 do + if bigCmp(num, d) >= 0 then + bigSub(num, d); + q := q | (U64(1) shl i); + end; + bigShr1(d); + end; + if q >= U64(1) shl 53 then (* a bit too many: it rounds *) + half := (q & 1) # 0; + sticky := num.n > 0; + q := q shr 1; + INC(e); + else (* the remainder against half of den *) + bigShl(num, 1); + i := bigCmp(num, den); + half := i >= 0; + sticky := i > 0; + end; + if half and (sticky or ((q & 1) # 0)) then + INC(q); + if q = U64(1) shl 53 then + q := q shr 1; + INC(e); + end; + end; + if e > 971 then return x, false; end; + if q >= U64(1) shl 52 then + b := U64(e + 1075) shl 52 | (q & (U64(1) shl 52 - 1)); + else + b := q; (* below the least normal, e is -1074 *) + end; + if neg then b := b | (U64(1) shl 63); end; + return fromBits(b), true; +end; + +(* Whether toReal reads s as want, exactly, or as nothing if not ok. *) +proc reads(s: []U8; want: F64; ok: BOOL): BOOL; +var x: F64; o: BOOL; +begin + x, o := toReal(s); + return (o = ok) and (not ok or (bits(x) = bits(want))); +end; + +proc testToReal; +var b: [900]U8; i: INT; +begin + if not reads("2", 2.0, true) or not reads("-0.5", -0.5, true) or not reads("1.5e-3", 1.5E-3, true) + or not reads("+1.25E+2", 125.0, true) or not reads("0.1", 0.1, true) + or not reads("1.7976931348623157E308", 1.7976931348623157E308, true) + or not reads("4.9e-324", fromBits(1), true) or not reads("1e-400", 0.0, true) + or not reads("-0", -0.0, true) or not reads("9007199254740993", 9007199254740992.0, true) + or not reads("1e309", 0.0, false) or not reads("1.8E308", 0.0, false) + then Rt.stop("Math.toReal"); end; + if not reads("", 0.0, false) or not reads("-", 0.0, false) or not reads("1.", 0.0, false) + or not reads(".5", 0.0, false) or not reads("1e", 0.0, false) or not reads("1.5.2", 0.0, false) + or not reads(" 1", 0.0, false) or not reads("1x", 0.0, false) or not reads("0x10", 0.0, false) + then Rt.stop("Math.toReal: not a real"); end; + (* 2^53 + 1, then 801 zeros and a 1: just above the half between two + F64s, though the 1 is past the digits kept, so it rounds up *) + COPY(b[0..16], "9007199254740993"); + for i := 16 to 816 do b[i] := '0'; end; + b[817] := '1'; + COPY(b[818..823], "e-802"); + if not reads(b[0..823], 9007199254740994.0, true) or not reads(b[0..817], 0.0, false) + then Rt.stop("Math.toReal: many digits"); end; +end; + proc near(got, want: F64): BOOL; var d: F64; begin @@ -762,6 +1006,7 @@ or not near(acos(-0.2), 1.7721542475852274) or (acos(1.0) # 0.0) or (acos(-1.0) # pi) or not near(atan(0.5), 0.4636476090008061) or not near(atan(-1.0E10), -1.5707963266948965) or (atan(inf) # pi / 2.0) or not negative(atan(-0.0)) then Rt.stop("Math.asin, acos, atan"); end; + testToReal; end; end Math. diff --git a/lib/Str.raz b/lib/Str.raz index 425fd87..61ec2fb 100644 --- a/lib/Str.raz +++ b/lib/Str.raz @@ -1,8 +1,11 @@ (* Operations on strings, which are []U8. = and # compare them; this - module orders, searches and hashes them, reads and writes their - UTF-8, reads a number from one, and copies one to keep. *) + module orders, searches and hashes them, trims them and takes them + apart, changes the case of their ASCII letters, 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; +import Mem, Heap, Rt, Bytes, Buf; const least = -9223372036854775807 - 1; (* the smallest I64 *) @@ -41,6 +44,24 @@ 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: []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: []U8): U64; var h: U64; i: INT; @@ -58,12 +79,123 @@ return Bytes.find(s, sub); end; +(* Where sub last starts in s, or -1. *) +proc findLast*(s, sub: []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: []U8): BOOL; begin return (LEN(s) >= LEN(p)) and (s[0..LEN(p)] = p); end; +(* Whether s ends with x. *) +proc hasSuffix*(s, x: []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 space(c: U8): BOOL; +begin + return (c = ' ') or (c >= 9) and (c <= 13); +end; + +(* s without the spaces at its start. *) +proc trimLeft*(s: []U8): []U8; +var i: INT; +begin + i := 0; + while (i < LEN(s)) and space(s[i]) do INC(i); end; + return s[i..LEN(s)]; +end; + +(* s without the spaces at its end. *) +proc trimRight*(s: []U8): []U8; +var n: INT; +begin + n := LEN(s); + while (n > 0) and space(s[n - 1]) do DEC(n); end; + return s[0..n]; +end; + +(* s without the spaces at either end. *) +proc trim*(s: []U8): []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: []U8): ([]U8, []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: []U8): ([]U8, []U8); +var i: INT; +begin + s := trimLeft(s); + i := 0; + while (i < LEN(s)) and not space(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*(b: ^Buf.Buf; parts: [][]U8; sep: []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*(b: ^Buf.Buf; s, old, new: []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. *) @@ -116,6 +248,57 @@ 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: []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: []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: []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: []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: []U8): (I64, BOOL); @@ -139,6 +322,43 @@ 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: []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 + match s[i] of + | '0'..'9': d := INT(s[i]) - '0'; + | 'A'..'F': d := INT(s[i]) - 'A' + 10; + | 'a'..'f': d := INT(s[i]) - 'a' + 10; + | else: 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; + +(* 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. *) @@ -235,8 +455,68 @@ 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: []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(ADR(b), Heap.heap); + join(ADR(b), ["a", "b", "c"], ", "); + if Buf.bytes(ADR(b)) # "a, b, c" then Rt.stop("Str.join"); end; + Buf.reset(ADR(b)); + join(ADR(b), [], "-"); + replace(ADR(b), "a.b..c", ".", "::"); + if Buf.bytes(ADR(b)) # "a::b::::c" then Rt.stop("Str.replace"); end; + Buf.reset(ADR(b)); + replace(ADR(b), "aaa", "aa", "b"); + replace(ADR(b), "xy", "", "-"); + if Buf.bytes(ADR(b)) # "baxy" then Rt.stop("Str.replace: overlapping, empty"); end; + Buf.done(ADR(b)); +end; + proc testToInt; var n: I64; ok: BOOL; begin @@ -264,6 +544,29 @@ 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 := 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; + (* The module's test: stops the program if something is wrong. *) proc main*; var buf: [3]U8; t: []U8; ok: BOOL; @@ -279,8 +582,10 @@ 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; + testParts; testUtf8; testToInt; + testNumbers; testOrder; end; diff --git a/test/doc/Str.expect b/test/doc/Str.expect index 5720a2c..1630d91 100644 --- a/test/doc/Str.expect +++ b/test/doc/Str.expect @@ -1,7 +1,10 @@ module Str; Operations on strings, which are []U8. = and # compare them; this - module orders, searches and hashes them, reads and writes their - UTF-8, reads a number from one, and copies one to keep. + module orders, searches and hashes them, trims them and takes them + apart, changes the case of their ASCII letters, 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. proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); s's bytes, copied into memory from a, to keep after s's own are @@ -13,15 +16,55 @@ proc eq*(a, b: []U8): BOOL; Whether a and b hold the same bytes: a = b, as a procedure value. +proc eqFold*(a, b: []U8): BOOL; + Whether a and b are the same but for the case of ASCII letters. + proc hash*(s: []U8): U64; FNV-1a, 64 bits. proc find*(s, sub: []U8): INT; Where sub first starts in s, or -1. +proc findLast*(s, sub: []U8): INT; + Where sub last starts in s, or -1. + proc hasPrefix*(s, p: []U8): BOOL; Whether s starts with p. +proc hasSuffix*(s, x: []U8): BOOL; + Whether s ends with x. + +proc trimLeft*(s: []U8): []U8; + s without the spaces at its start. + +proc trimRight*(s: []U8): []U8; + s without the spaces at its end. + +proc trim*(s: []U8): []U8; + s without the spaces at either end. + +proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); + 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 field*(s: []U8): ([]U8, []U8); + 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 lower*(s: []U8); + s's ASCII capitals made small, in place. + +proc upper*(s: []U8); + s's small ASCII letters made capitals, in place. + +proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); + The parts added to b, with sep between each two. + +proc replace*(b: ^Buf.Buf; s, old, new: []U8); + 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 encode*(c: I32): ([4]U8, INT); 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 @@ -33,9 +76,34 @@ 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 decodeLast*(s: []U8): (I32, INT); + 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 next*(s: []U8; i: INT): (I32, INT); + 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 chars*(s: []U8): INT; + The characters in s, as decode reads them, each byte that doesn't + start one rightly counted as one. + +proc valid*(s: []U8): BOOL; + Whether s is UTF-8: decode finds no byte that doesn't start a + character rightly. + proc toInt*(s: []U8): (I64, BOOL); 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 toHex*(s: []U8): (U64, BOOL); + s in hexadecimal, digits only, capitals or not; false if it isn't + that, or a U64 can't hold it. + +proc fromInt*(n: I64): ([20]U8, INT); + n in decimal, as Out.int writes it: its bytes, the first of the 20, + and how many. proc le*(s: []U8): U64; The bytes of s, at most 8, as an unsigned number, the first byte