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.
| @@ -705,6 +705,250 @@ | ||
| 705 | 705 | |
| 706 | 706 | (* The module's test: values C's library gives, to within two units in |
| 707 | 707 | the last place, and the edges. *) |
| 708 | +(* Reading a real from text exactly: the F64 nearest to its digits | |
| 709 | + times 10 to its exponent, ties to the even one, as the compiler | |
| 710 | + reads a constant (src/Scan.raz). The number is a fraction of two big | |
| 711 | + integers, num / den, scaled by 2^e until its whole part has 53 or 54 | |
| 712 | + bits; that part and the remainder give the bits and the rounding. *) | |
| 713 | +const | |
| 714 | + MaxDigits = 780; (* significant digits kept; past them, a 1 if any left out isn't 0 *) | |
| 715 | + | |
| 716 | +type | |
| 717 | + Big = record | |
| 718 | + n: INT; (* words in use; 0 is zero *) | |
| 719 | + w: [128]U32; (* least significant first *) | |
| 720 | + end; | |
| 721 | +(* 768 significant digits are as many as can matter to rounding an | |
| 722 | + F64, so the ones past MaxDigits matter only by being 0 or not, which | |
| 723 | + a last digit 1 keeps. Then den is at most 10^(330 + 781), about | |
| 724 | + 2^3691, and num below 10^310; the shift that follows makes the | |
| 725 | + larger at most 54 bits longer than den, and the long division | |
| 726 | + shifts den 53 more: under 3,800 bits, of the 4,096 here. *) | |
| 727 | + | |
| 728 | +(* a := a * m + add *) | |
| 729 | +proc bigMul(var a: Big; m, add: U32); | |
| 730 | +var i: INT; c: U64; | |
| 731 | +begin | |
| 732 | + c := U64(add); | |
| 733 | + for i := 0 to a.n - 1 do | |
| 734 | + c := U64(a.w[i]) * U64(m) + c; | |
| 735 | + a.w[i] := U32(c); | |
| 736 | + c := c shr 32; | |
| 737 | + end; | |
| 738 | + if c # 0 then | |
| 739 | + a.w[a.n] := U32(c); | |
| 740 | + INC(a.n); | |
| 741 | + end; | |
| 742 | +end; | |
| 743 | + | |
| 744 | +proc bigShl(var a: Big; k: INT); | |
| 745 | +var i, ws, bits: INT; | |
| 746 | +begin | |
| 747 | + if a.n = 0 then return; end; | |
| 748 | + ws := k div 32; | |
| 749 | + bits := k mod 32; | |
| 750 | + a.w[a.n + ws] := 0; | |
| 751 | + for i := a.n - 1 to 0 by -1 do | |
| 752 | + if bits > 0 then a.w[i + ws + 1] := a.w[i + ws + 1] | (a.w[i] shr (32 - bits)); end; | |
| 753 | + a.w[i + ws] := a.w[i] shl bits; | |
| 754 | + end; | |
| 755 | + for i := 0 to ws - 1 do a.w[i] := 0; end; | |
| 756 | + INC(a.n, ws + 1); | |
| 757 | + while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; | |
| 758 | +end; | |
| 759 | + | |
| 760 | +proc bigShr1(var a: Big); | |
| 761 | +var i: INT; | |
| 762 | +begin | |
| 763 | + for i := 0 to a.n - 1 do | |
| 764 | + a.w[i] := a.w[i] shr 1; | |
| 765 | + if i + 1 < a.n then a.w[i] := a.w[i] | (a.w[i + 1] shl 31); end; | |
| 766 | + end; | |
| 767 | + while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; | |
| 768 | +end; | |
| 769 | + | |
| 770 | +(* -1, 0 or 1 as a is below, equal to or above b *) | |
| 771 | +proc bigCmp(var a, b: Big): INT; | |
| 772 | +var i: INT; | |
| 773 | +begin | |
| 774 | + if a.n # b.n then | |
| 775 | + if a.n > b.n then return 1; end; | |
| 776 | + return -1; | |
| 777 | + end; | |
| 778 | + for i := a.n - 1 to 0 by -1 do | |
| 779 | + if a.w[i] # b.w[i] then | |
| 780 | + if a.w[i] > b.w[i] then return 1; end; | |
| 781 | + return -1; | |
| 782 | + end; | |
| 783 | + end; | |
| 784 | + return 0; | |
| 785 | +end; | |
| 786 | + | |
| 787 | +(* a := a - b, b not above a *) | |
| 788 | +proc bigSub(var a, b: Big); | |
| 789 | +var i: INT; d: I64; | |
| 790 | +begin | |
| 791 | + d := 0; | |
| 792 | + for i := 0 to a.n - 1 do | |
| 793 | + d := d + I64(a.w[i]); | |
| 794 | + if i < b.n then d := d - I64(b.w[i]); end; | |
| 795 | + a.w[i] := U32(d); | |
| 796 | + if d < 0 then d := -1; else d := 0; end; (* a borrow *) | |
| 797 | + end; | |
| 798 | + while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; | |
| 799 | +end; | |
| 800 | + | |
| 801 | +proc bigBits(var a: Big): INT; | |
| 802 | +var t: U32; bits: INT; | |
| 803 | +begin | |
| 804 | + if a.n = 0 then return 0; end; | |
| 805 | + bits := (a.n - 1) * 32; | |
| 806 | + t := a.w[a.n - 1]; | |
| 807 | + while t # 0 do | |
| 808 | + INC(bits); | |
| 809 | + t := t shr 1; | |
| 810 | + end; | |
| 811 | + return bits; | |
| 812 | +end; | |
| 813 | + | |
| 814 | +(* Whether s[i] is a decimal digit. *) | |
| 815 | +proc digitAt(s: []U8; i: INT): BOOL; | |
| 816 | +begin | |
| 817 | + return (i < LEN(s)) and (s[i] >= '0') and (s[i] <= '9'); | |
| 818 | +end; | |
| 819 | + | |
| 820 | +(* s as a real: a sign if any, digits, a point and digits if any, and E | |
| 821 | + or e, a sign if any and digits if any; nothing else. The F64 nearest | |
| 822 | + it, ties to the even one, and false if s isn't one, or is past the | |
| 823 | + largest F64; one below the smallest is 0. *) | |
| 824 | +proc toReal*(s: []U8): (F64, BOOL); | |
| 825 | +var | |
| 826 | + num, den, d: Big; i, digits, e, ex: INT; neg, eneg, point, half, sticky: BOOL; | |
| 827 | + q, b: U64; x: F64; | |
| 828 | +begin | |
| 829 | + x := 0.0; | |
| 830 | + num.n := 0; | |
| 831 | + den.n := 1; | |
| 832 | + den.w[0] := 1; | |
| 833 | + digits := 0; | |
| 834 | + e := 0; | |
| 835 | + sticky := false; | |
| 836 | + neg := (LEN(s) > 0) and (s[0] = '-'); | |
| 837 | + i := 0; | |
| 838 | + if (LEN(s) > 0) and ((s[0] = '-') or (s[0] = '+')) then i := 1; end; | |
| 839 | + if not digitAt(s, i) then return x, false; end; | |
| 840 | + (* the digits: num, and e, the power of 10 it is to be taken to *) | |
| 841 | + point := false; | |
| 842 | + while digitAt(s, i) or not point and (i < LEN(s)) and (s[i] = '.') and digitAt(s, i + 1) do | |
| 843 | + if s[i] = '.' then | |
| 844 | + point := true; | |
| 845 | + elsif digits < MaxDigits then | |
| 846 | + bigMul(num, 10, U32(s[i] - '0')); | |
| 847 | + if num.n > 0 then INC(digits); end; (* counted from the first that isn't 0 *) | |
| 848 | + if point then DEC(e); end; | |
| 849 | + else | |
| 850 | + (* a digit not kept, which still counts if it is before the point *) | |
| 851 | + if s[i] # '0' then sticky := true; end; | |
| 852 | + if not point then INC(e); end; | |
| 853 | + end; | |
| 854 | + INC(i); | |
| 855 | + end; | |
| 856 | + if sticky then bigMul(num, 10, 1); DEC(e); end; | |
| 857 | + ex := 0; | |
| 858 | + if (i < LEN(s)) and ((s[i] = 'E') or (s[i] = 'e')) then | |
| 859 | + INC(i); | |
| 860 | + eneg := (i < LEN(s)) and (s[i] = '-'); | |
| 861 | + if (i < LEN(s)) and ((s[i] = '+') or (s[i] = '-')) then INC(i); end; | |
| 862 | + if not digitAt(s, i) then return x, false; end; | |
| 863 | + while digitAt(s, i) do | |
| 864 | + if ex < 100000 then ex := ex * 10 + INT(s[i]) - '0'; end; | |
| 865 | + INC(i); | |
| 866 | + end; | |
| 867 | + if eneg then ex := -ex; end; | |
| 868 | + end; | |
| 869 | + if i < LEN(s) then return x, false; end; | |
| 870 | + INC(e, ex); | |
| 871 | + if neg then x := -x; end; | |
| 872 | + if num.n = 0 then return x, true; end; | |
| 873 | + if digits + e > 310 then return x, false; end; (* 10^310 is past every F64 *) | |
| 874 | + if digits + e < -330 then return x, true; end; (* and 10^-330 below half the least *) | |
| 875 | + for i := 1 to e do bigMul(num, 10, 0); end; | |
| 876 | + for i := 1 to -e do bigMul(den, 10, 0); end; | |
| 877 | + (* num / den 2^-e from 2^52 to 2^54, but not past the least exponent *) | |
| 878 | + e := bigBits(num) - bigBits(den) - 53; | |
| 879 | + if e < -1074 then e := -1074; end; | |
| 880 | + if e < 0 then bigShl(num, -e); else bigShl(den, e); end; | |
| 881 | + (* q, its whole part, by long division; num is left the remainder *) | |
| 882 | + d := den; | |
| 883 | + bigShl(d, 53); | |
| 884 | + q := 0; | |
| 885 | + for i := 53 to 0 by -1 do | |
| 886 | + if bigCmp(num, d) >= 0 then | |
| 887 | + bigSub(num, d); | |
| 888 | + q := q | (U64(1) shl i); | |
| 889 | + end; | |
| 890 | + bigShr1(d); | |
| 891 | + end; | |
| 892 | + if q >= U64(1) shl 53 then (* a bit too many: it rounds *) | |
| 893 | + half := (q & 1) # 0; | |
| 894 | + sticky := num.n > 0; | |
| 895 | + q := q shr 1; | |
| 896 | + INC(e); | |
| 897 | + else (* the remainder against half of den *) | |
| 898 | + bigShl(num, 1); | |
| 899 | + i := bigCmp(num, den); | |
| 900 | + half := i >= 0; | |
| 901 | + sticky := i > 0; | |
| 902 | + end; | |
| 903 | + if half and (sticky or ((q & 1) # 0)) then | |
| 904 | + INC(q); | |
| 905 | + if q = U64(1) shl 53 then | |
| 906 | + q := q shr 1; | |
| 907 | + INC(e); | |
| 908 | + end; | |
| 909 | + end; | |
| 910 | + if e > 971 then return x, false; end; | |
| 911 | + if q >= U64(1) shl 52 then | |
| 912 | + b := U64(e + 1075) shl 52 | (q & (U64(1) shl 52 - 1)); | |
| 913 | + else | |
| 914 | + b := q; (* below the least normal, e is -1074 *) | |
| 915 | + end; | |
| 916 | + if neg then b := b | (U64(1) shl 63); end; | |
| 917 | + return fromBits(b), true; | |
| 918 | +end; | |
| 919 | + | |
| 920 | +(* Whether toReal reads s as want, exactly, or as nothing if not ok. *) | |
| 921 | +proc reads(s: []U8; want: F64; ok: BOOL): BOOL; | |
| 922 | +var x: F64; o: BOOL; | |
| 923 | +begin | |
| 924 | + x, o := toReal(s); | |
| 925 | + return (o = ok) and (not ok or (bits(x) = bits(want))); | |
| 926 | +end; | |
| 927 | + | |
| 928 | +proc testToReal; | |
| 929 | +var b: [900]U8; i: INT; | |
| 930 | +begin | |
| 931 | + 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) | |
| 932 | + or not reads("+1.25E+2", 125.0, true) or not reads("0.1", 0.1, true) | |
| 933 | + or not reads("1.7976931348623157E308", 1.7976931348623157E308, true) | |
| 934 | + or not reads("4.9e-324", fromBits(1), true) or not reads("1e-400", 0.0, true) | |
| 935 | + or not reads("-0", -0.0, true) or not reads("9007199254740993", 9007199254740992.0, true) | |
| 936 | + or not reads("1e309", 0.0, false) or not reads("1.8E308", 0.0, false) | |
| 937 | + then Rt.stop("Math.toReal"); end; | |
| 938 | + if not reads("", 0.0, false) or not reads("-", 0.0, false) or not reads("1.", 0.0, false) | |
| 939 | + or not reads(".5", 0.0, false) or not reads("1e", 0.0, false) or not reads("1.5.2", 0.0, false) | |
| 940 | + or not reads(" 1", 0.0, false) or not reads("1x", 0.0, false) or not reads("0x10", 0.0, false) | |
| 941 | + then Rt.stop("Math.toReal: not a real"); end; | |
| 942 | + (* 2^53 + 1, then 801 zeros and a 1: just above the half between two | |
| 943 | + F64s, though the 1 is past the digits kept, so it rounds up *) | |
| 944 | + COPY(b[0..16], "9007199254740993"); | |
| 945 | + for i := 16 to 816 do b[i] := '0'; end; | |
| 946 | + b[817] := '1'; | |
| 947 | + COPY(b[818..823], "e-802"); | |
| 948 | + if not reads(b[0..823], 9007199254740994.0, true) or not reads(b[0..817], 0.0, false) | |
| 949 | + then Rt.stop("Math.toReal: many digits"); end; | |
| 950 | +end; | |
| 951 | + | |
| 708 | 952 | proc near(got, want: F64): BOOL; |
| 709 | 953 | var d: F64; |
| 710 | 954 | begin |
| @@ -762,6 +1006,7 @@ | ||
| 762 | 1006 | or not near(acos(-0.2), 1.7721542475852274) or (acos(1.0) # 0.0) or (acos(-1.0) # pi) |
| 763 | 1007 | or not near(atan(0.5), 0.4636476090008061) or not near(atan(-1.0E10), -1.5707963266948965) |
| 764 | 1008 | or (atan(inf) # pi / 2.0) or not negative(atan(-0.0)) then Rt.stop("Math.asin, acos, atan"); end; |
| 1009 | + testToReal; | |
| 765 | 1010 | end; |
| 766 | 1011 | |
| 767 | 1012 | end Math. |
| @@ -1,8 +1,11 @@ | ||
| 1 | 1 | (* Operations on strings, which are []U8. = and # compare them; this |
| 2 | - module orders, searches and hashes them, reads and writes their | |
| 3 | - UTF-8, reads a number from one, and copies one to keep. *) | |
| 2 | + module orders, searches and hashes them, trims them and takes them | |
| 3 | + apart, changes the case of their ASCII letters, joins them and | |
| 4 | + replaces in them, reads and writes their UTF-8, reads a number from | |
| 5 | + one and writes one, and copies one to keep. A part it gives is a | |
| 6 | + slice of the string it was given, with no memory of its own. *) | |
| 4 | 7 | module Str; |
| 5 | -import Mem, Heap, Rt, Bytes; | |
| 8 | +import Mem, Heap, Rt, Bytes, Buf; | |
| 6 | 9 | |
| 7 | 10 | const least = -9223372036854775807 - 1; (* the smallest I64 *) |
| 8 | 11 | |
| @@ -41,6 +44,24 @@ | ||
| 41 | 44 | return a = b; |
| 42 | 45 | end; |
| 43 | 46 | |
| 47 | +(* c, a small letter if it is an ASCII capital. *) | |
| 48 | +proc small(c: U8): U8; | |
| 49 | +begin | |
| 50 | + if (c >= 'A') and (c <= 'Z') then return c + 32; end; | |
| 51 | + return c; | |
| 52 | +end; | |
| 53 | + | |
| 54 | +(* Whether a and b are the same but for the case of ASCII letters. *) | |
| 55 | +proc eqFold*(a, b: []U8): BOOL; | |
| 56 | +var i: INT; | |
| 57 | +begin | |
| 58 | + if LEN(a) # LEN(b) then return false; end; | |
| 59 | + for i := 0 to LEN(a) - 1 do | |
| 60 | + if small(a[i]) # small(b[i]) then return false; end; | |
| 61 | + end; | |
| 62 | + return true; | |
| 63 | +end; | |
| 64 | + | |
| 44 | 65 | (* FNV-1a, 64 bits. *) |
| 45 | 66 | proc hash*(s: []U8): U64; |
| 46 | 67 | var h: U64; i: INT; |
| @@ -58,12 +79,123 @@ | ||
| 58 | 79 | return Bytes.find(s, sub); |
| 59 | 80 | end; |
| 60 | 81 | |
| 82 | +(* Where sub last starts in s, or -1. *) | |
| 83 | +proc findLast*(s, sub: []U8): INT; | |
| 84 | +var i: INT; | |
| 85 | +begin | |
| 86 | + for i := LEN(s) - LEN(sub) to 0 by -1 do | |
| 87 | + if s[i..i + LEN(sub)] = sub then return i; end; | |
| 88 | + end; | |
| 89 | + return -1; | |
| 90 | +end; | |
| 91 | + | |
| 61 | 92 | (* Whether s starts with p. *) |
| 62 | 93 | proc hasPrefix*(s, p: []U8): BOOL; |
| 63 | 94 | begin |
| 64 | 95 | return (LEN(s) >= LEN(p)) and (s[0..LEN(p)] = p); |
| 65 | 96 | end; |
| 66 | 97 | |
| 98 | +(* Whether s ends with x. *) | |
| 99 | +proc hasSuffix*(s, x: []U8): BOOL; | |
| 100 | +begin | |
| 101 | + return (LEN(s) >= LEN(x)) and (s[LEN(s) - LEN(x)..LEN(s)] = x); | |
| 102 | +end; | |
| 103 | + | |
| 104 | +(* Whether c is a space: a blank, a tab, a line feed, a vertical tab, a | |
| 105 | + form feed or a carriage return. *) | |
| 106 | +proc space(c: U8): BOOL; | |
| 107 | +begin | |
| 108 | + return (c = ' ') or (c >= 9) and (c <= 13); | |
| 109 | +end; | |
| 110 | + | |
| 111 | +(* s without the spaces at its start. *) | |
| 112 | +proc trimLeft*(s: []U8): []U8; | |
| 113 | +var i: INT; | |
| 114 | +begin | |
| 115 | + i := 0; | |
| 116 | + while (i < LEN(s)) and space(s[i]) do INC(i); end; | |
| 117 | + return s[i..LEN(s)]; | |
| 118 | +end; | |
| 119 | + | |
| 120 | +(* s without the spaces at its end. *) | |
| 121 | +proc trimRight*(s: []U8): []U8; | |
| 122 | +var n: INT; | |
| 123 | +begin | |
| 124 | + n := LEN(s); | |
| 125 | + while (n > 0) and space(s[n - 1]) do DEC(n); end; | |
| 126 | + return s[0..n]; | |
| 127 | +end; | |
| 128 | + | |
| 129 | +(* s without the spaces at either end. *) | |
| 130 | +proc trim*(s: []U8): []U8; | |
| 131 | +begin | |
| 132 | + return trimRight(trimLeft(s)); | |
| 133 | +end; | |
| 134 | + | |
| 135 | +(* What comes before the first sep in s, what comes after it, and true; | |
| 136 | + or s, an empty string and false if s has no sep. *) | |
| 137 | +proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); | |
| 138 | +var i: INT; | |
| 139 | +begin | |
| 140 | + i := find(s, sep); | |
| 141 | + if i < 0 then return s, s[LEN(s)..LEN(s)], false; end; | |
| 142 | + return s[0..i], s[i + LEN(sep)..LEN(s)], true; | |
| 143 | +end; | |
| 144 | + | |
| 145 | +(* The first word of s, the bytes after the spaces at its start up to | |
| 146 | + the next space or the end, and the rest of s from there; two empty | |
| 147 | + strings if s holds only spaces. *) | |
| 148 | +proc field*(s: []U8): ([]U8, []U8); | |
| 149 | +var i: INT; | |
| 150 | +begin | |
| 151 | + s := trimLeft(s); | |
| 152 | + i := 0; | |
| 153 | + while (i < LEN(s)) and not space(s[i]) do INC(i); end; | |
| 154 | + return s[0..i], s[i..LEN(s)]; | |
| 155 | +end; | |
| 156 | + | |
| 157 | +(* s's ASCII capitals made small, in place. *) | |
| 158 | +proc lower*(s: []U8); | |
| 159 | +var i: INT; | |
| 160 | +begin | |
| 161 | + for i := 0 to LEN(s) - 1 do s[i] := small(s[i]); end; | |
| 162 | +end; | |
| 163 | + | |
| 164 | +(* s's small ASCII letters made capitals, in place. *) | |
| 165 | +proc upper*(s: []U8); | |
| 166 | +var i: INT; | |
| 167 | +begin | |
| 168 | + for i := 0 to LEN(s) - 1 do | |
| 169 | + if (s[i] >= 'a') and (s[i] <= 'z') then s[i] := s[i] - 32; end; | |
| 170 | + end; | |
| 171 | +end; | |
| 172 | + | |
| 173 | +(* The parts added to b, with sep between each two. *) | |
| 174 | +proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); | |
| 175 | +var i: INT; | |
| 176 | +begin | |
| 177 | + for i := 0 to LEN(parts) - 1 do | |
| 178 | + if i > 0 then Buf.str(b, sep); end; | |
| 179 | + Buf.str(b, parts[i]); | |
| 180 | + end; | |
| 181 | +end; | |
| 182 | + | |
| 183 | +(* s added to b, with new for each old in it, from the left, none | |
| 184 | + overlapping another; s as it is if old is empty. *) | |
| 185 | +proc replace*(b: ^Buf.Buf; s, old, new: []U8); | |
| 186 | +var i: INT; | |
| 187 | +begin | |
| 188 | + i := -1; | |
| 189 | + if LEN(old) > 0 then i := find(s, old); end; | |
| 190 | + while i >= 0 do | |
| 191 | + Buf.str(b, s[0..i]); | |
| 192 | + Buf.str(b, new); | |
| 193 | + s := s[i + LEN(old)..LEN(s)]; | |
| 194 | + i := find(s, old); | |
| 195 | + end; | |
| 196 | + Buf.str(b, s); | |
| 197 | +end; | |
| 198 | + | |
| 67 | 199 | (* c in UTF-8: its bytes, and how many. A number that isn't a |
| 68 | 200 | character, a surrogate or one past 10FFFFH, becomes FFFDH, the |
| 69 | 201 | character that stands for a wrong one. *) |
| @@ -116,6 +248,57 @@ | ||
| 116 | 248 | return c, n; |
| 117 | 249 | end; |
| 118 | 250 | |
| 251 | +(* The character s ends with, and its length in bytes, read as decode | |
| 252 | + reads one: FFFDH and 1 if the last byte doesn't end one rightly; 0 | |
| 253 | + and 0 if s is empty. *) | |
| 254 | +proc decodeLast*(s: []U8): (I32, INT); | |
| 255 | +var c: I32; i, n: INT; | |
| 256 | +begin | |
| 257 | + if LEN(s) = 0 then return 0, 0; end; | |
| 258 | + (* back to the byte that starts it, at most 3 continuations back *) | |
| 259 | + i := LEN(s) - 1; | |
| 260 | + while (i > 0) and (i > LEN(s) - 4) and (s[i] & 0C0H = 80H) do DEC(i); end; | |
| 261 | + c, n := decode(s[i..LEN(s)]); | |
| 262 | + if i + n # LEN(s) then return 0FFFDH, 1; end; | |
| 263 | + return c, n; | |
| 264 | +end; | |
| 265 | + | |
| 266 | +(* The character at i in s, as decode reads it, and where the one after | |
| 267 | + it starts; 0 and -1 if i is at the end or past it. *) | |
| 268 | +proc next*(s: []U8; i: INT): (I32, INT); | |
| 269 | +var c: I32; n: INT; | |
| 270 | +begin | |
| 271 | + if i >= LEN(s) then return 0, -1; end; | |
| 272 | + c, n := decode(s[i..LEN(s)]); | |
| 273 | + return c, i + n; | |
| 274 | +end; | |
| 275 | + | |
| 276 | +(* The characters in s, as decode reads them, each byte that doesn't | |
| 277 | + start one rightly counted as one. *) | |
| 278 | +proc chars*(s: []U8): INT; | |
| 279 | +var i, n: INT; | |
| 280 | +begin | |
| 281 | + _, i := next(s, 0); | |
| 282 | + while i >= 0 do | |
| 283 | + INC(n); | |
| 284 | + _, i := next(s, i); | |
| 285 | + end; | |
| 286 | + return n; | |
| 287 | +end; | |
| 288 | + | |
| 289 | +(* Whether s is UTF-8: decode finds no byte that doesn't start a | |
| 290 | + character rightly. *) | |
| 291 | +proc valid*(s: []U8): BOOL; | |
| 292 | +var c: I32; i, n: INT; | |
| 293 | +begin | |
| 294 | + while i < LEN(s) do | |
| 295 | + c, n := decode(s[i..LEN(s)]); | |
| 296 | + if (c = 0FFFDH) and (n = 1) then return false; end; | |
| 297 | + INC(i, n); | |
| 298 | + end; | |
| 299 | + return true; | |
| 300 | +end; | |
| 301 | + | |
| 119 | 302 | (* s as a decimal number, with a - or + before it if any, and nothing |
| 120 | 303 | else; false if it isn't one, or an I64 can't hold it. *) |
| 121 | 304 | proc toInt*(s: []U8): (I64, BOOL); |
| @@ -139,6 +322,43 @@ | ||
| 139 | 322 | return n, true; |
| 140 | 323 | end; |
| 141 | 324 | |
| 325 | +(* s in hexadecimal, digits only, capitals or not; false if it isn't | |
| 326 | + that, or a U64 can't hold it. *) | |
| 327 | +proc toHex*(s: []U8): (U64, BOOL); | |
| 328 | +var v: U64; d, i: INT; | |
| 329 | +begin | |
| 330 | + if LEN(s) = 0 then return 0, false; end; | |
| 331 | + for i := 0 to LEN(s) - 1 do | |
| 332 | + match s[i] of | |
| 333 | + | '0'..'9': d := INT(s[i]) - '0'; | |
| 334 | + | 'A'..'F': d := INT(s[i]) - 'A' + 10; | |
| 335 | + | 'a'..'f': d := INT(s[i]) - 'a' + 10; | |
| 336 | + | else: return 0, false; | |
| 337 | + end; | |
| 338 | + if v shr 60 # 0 then return 0, false; end; | |
| 339 | + v := v shl 4 | U64(d); | |
| 340 | + end; | |
| 341 | + return v, true; | |
| 342 | +end; | |
| 343 | + | |
| 344 | +(* n in decimal, as Out.int writes it: its bytes, the first of the 20, | |
| 345 | + and how many. *) | |
| 346 | +proc fromInt*(n: I64): ([20]U8, INT); | |
| 347 | +var t, d: [20]U8; u: U64; i: INT; | |
| 348 | +begin | |
| 349 | + (* as a U64, which holds the smallest I64's magnitude *) | |
| 350 | + if n < 0 then u := U64(-(n + 1)) + 1; else u := U64(n); end; | |
| 351 | + i := 20; | |
| 352 | + repeat | |
| 353 | + DEC(i); | |
| 354 | + t[i] := U8(u mod 10) + '0'; | |
| 355 | + u := u div 10; | |
| 356 | + until u = 0; | |
| 357 | + if n < 0 then DEC(i); t[i] := '-'; end; | |
| 358 | + COPY(d[0..20 - i], t[i..20]); | |
| 359 | + return d, 20 - i; | |
| 360 | +end; | |
| 361 | + | |
| 142 | 362 | (* The bytes of s, at most 8, as an unsigned number, the first byte |
| 143 | 363 | the lowest: how a file or a message in little-endian order is read, |
| 144 | 364 | whatever the machine's own order. *) |
| @@ -235,8 +455,68 @@ | ||
| 235 | 455 | b := [0F4H, 90H, 80H, 80H]; |
| 236 | 456 | c, n := decode(b); |
| 237 | 457 | if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decode: past 10FFFFH"); end; |
| 458 | + if not valid(b[1..1]) or valid(b) then Rt.stop("Str.valid"); end; | |
| 459 | + c, n := decodeLast("a€"); | |
| 460 | + if (c # 20ACH) or (n # 3) then Rt.stop("Str.decodeLast: €"); end; | |
| 461 | + c, n := decodeLast(""); | |
| 462 | + if (c # 0) or (n # 0) then Rt.stop("Str.decodeLast: empty"); end; | |
| 463 | + b := ['a', 0E2H, 82H, 0]; | |
| 464 | + c, n := decodeLast(b[0..3]); | |
| 465 | + if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decodeLast: cut short"); end; | |
| 466 | + b := [80H, 80H, 80H, 80H]; | |
| 467 | + c, n := decodeLast(b); | |
| 468 | + if (c # 0FFFDH) or (n # 1) then Rt.stop("Str.decodeLast: continuations"); end; | |
| 469 | + if (chars("héllo😀") # 6) or (chars("") # 0) or (chars(b) # 4) or not valid("héllo😀") | |
| 470 | + then Rt.stop("Str.chars"); end; | |
| 471 | + c, n := next("é!", 0); | |
| 472 | + if (c # 0E9H) or (n # 2) then Rt.stop("Str.next"); end; | |
| 473 | + c, n := next("é!", 2); | |
| 474 | + if (c # '!') or (n # 3) then Rt.stop("Str.next: !"); end; | |
| 475 | + c, n := next("é!", 3); | |
| 476 | + if (c # 0) or (n # -1) then Rt.stop("Str.next: the end"); end; | |
| 238 | 477 | end; |
| 239 | 478 | |
| 479 | +proc testParts; | |
| 480 | +var b: Buf.Buf; w, rest, k, v: []U8; ok: BOOL; up: [5]U8; | |
| 481 | +begin | |
| 482 | + if not eqFold("Hello", "hELLO") or eqFold("a", "b") or eqFold("a", "ab") or not eqFold("", "") | |
| 483 | + or eqFold("@", "`") then Rt.stop("Str.eqFold"); end; | |
| 484 | + if (findLast("abcabc", "bc") # 4) or (findLast("abc", "x") # -1) or (findLast("abc", "") # 3) | |
| 485 | + or (findLast("ab", "abc") # -1) then Rt.stop("Str.findLast"); end; | |
| 486 | + if not hasSuffix("hello", "lo") or hasSuffix("lo", "hello") or not hasSuffix("x", "") | |
| 487 | + then Rt.stop("Str.hasSuffix"); end; | |
| 488 | + if (trim(" \t a b \r\n") # "a b") or (trimLeft(" a ") # "a ") or (trimRight(" a ") # " a") | |
| 489 | + or (trim(" ") # "") or (trim("") # "") then Rt.stop("Str.trim"); end; | |
| 490 | + k, v, ok := cut("key=val=ue", "="); | |
| 491 | + if not ok or (k # "key") or (v # "val=ue") then Rt.stop("Str.cut"); end; | |
| 492 | + k, v, ok := cut("none", "="); | |
| 493 | + if ok or (k # "none") or (v # "") then Rt.stop("Str.cut: none"); end; | |
| 494 | + w, rest := field(" one two\tthree "); | |
| 495 | + if (w # "one") or (rest # " two\tthree ") then Rt.stop("Str.field"); end; | |
| 496 | + w, rest := field(rest); | |
| 497 | + w, rest := field(rest); | |
| 498 | + if w # "three" then Rt.stop("Str.field: three"); end; | |
| 499 | + w, rest := field(rest); | |
| 500 | + if (w # "") or (rest # "") then Rt.stop("Str.field: none"); end; | |
| 501 | + up := "aB-z9"; | |
| 502 | + upper(up); | |
| 503 | + if up # "AB-Z9" then Rt.stop("Str.upper"); end; | |
| 504 | + lower(up); | |
| 505 | + if up # "ab-z9" then Rt.stop("Str.lower"); end; | |
| 506 | + Buf.init(ADR(b), Heap.heap); | |
| 507 | + join(ADR(b), ["a", "b", "c"], ", "); | |
| 508 | + if Buf.bytes(ADR(b)) # "a, b, c" then Rt.stop("Str.join"); end; | |
| 509 | + Buf.reset(ADR(b)); | |
| 510 | + join(ADR(b), [], "-"); | |
| 511 | + replace(ADR(b), "a.b..c", ".", "::"); | |
| 512 | + if Buf.bytes(ADR(b)) # "a::b::::c" then Rt.stop("Str.replace"); end; | |
| 513 | + Buf.reset(ADR(b)); | |
| 514 | + replace(ADR(b), "aaa", "aa", "b"); | |
| 515 | + replace(ADR(b), "xy", "", "-"); | |
| 516 | + if Buf.bytes(ADR(b)) # "baxy" then Rt.stop("Str.replace: overlapping, empty"); end; | |
| 517 | + Buf.done(ADR(b)); | |
| 518 | +end; | |
| 519 | + | |
| 240 | 520 | proc testToInt; |
| 241 | 521 | var n: I64; ok: BOOL; |
| 242 | 522 | begin |
| @@ -264,6 +544,29 @@ | ||
| 264 | 544 | if ok then Rt.stop("Str.toInt: a space"); end; |
| 265 | 545 | end; |
| 266 | 546 | |
| 547 | +proc testNumbers; | |
| 548 | +var u: U64; ok: BOOL; d: [20]U8; n: INT; | |
| 549 | +begin | |
| 550 | + u, ok := toHex("ff"); | |
| 551 | + if not ok or (u # 255) then Rt.stop("Str.toHex: ff"); end; | |
| 552 | + u, ok := toHex("FFFFFFFFFFFFFFFF"); | |
| 553 | + if not ok or (u # 0FFFFFFFFFFFFFFFFH) then Rt.stop("Str.toHex: the largest"); end; | |
| 554 | + u, ok := toHex("10000000000000000"); | |
| 555 | + if ok then Rt.stop("Str.toHex: past the largest"); end; | |
| 556 | + u, ok := toHex("0x1"); | |
| 557 | + if ok then Rt.stop("Str.toHex: 0x"); end; | |
| 558 | + u, ok := toHex(""); | |
| 559 | + if ok then Rt.stop("Str.toHex: empty"); end; | |
| 560 | + d, n := fromInt(0); | |
| 561 | + if d[0..n] # "0" then Rt.stop("Str.fromInt: 0"); end; | |
| 562 | + d, n := fromInt(-1234); | |
| 563 | + if d[0..n] # "-1234" then Rt.stop("Str.fromInt: -1234"); end; | |
| 564 | + d, n := fromInt(least); | |
| 565 | + if d[0..n] # "-9223372036854775808" then Rt.stop("Str.fromInt: the smallest"); end; | |
| 566 | + d, n := fromInt(9223372036854775807); | |
| 567 | + if d[0..n] # "9223372036854775807" then Rt.stop("Str.fromInt: the largest"); end; | |
| 568 | +end; | |
| 569 | + | |
| 267 | 570 | (* The module's test: stops the program if something is wrong. *) |
| 268 | 571 | proc main*; |
| 269 | 572 | var buf: [3]U8; t: []U8; ok: BOOL; |
| @@ -279,8 +582,10 @@ | ||
| 279 | 582 | if (find("hello", "ll") # 2) or (find("hello", "x") # -1) or (find("", "") # 0) |
| 280 | 583 | or (find("ab", "abc") # -1) then Rt.stop("Str.find"); end; |
| 281 | 584 | if not hasPrefix("hello", "he") or hasPrefix("he", "hello") then Rt.stop("Str.hasPrefix"); end; |
| 585 | + testParts; | |
| 282 | 586 | testUtf8; |
| 283 | 587 | testToInt; |
| 588 | + testNumbers; | |
| 284 | 589 | testOrder; |
| 285 | 590 | end; |
| 286 | 591 |
| @@ -1,7 +1,10 @@ | ||
| 1 | 1 | module Str; |
| 2 | 2 | Operations on strings, which are []U8. = and # compare them; this |
| 3 | - module orders, searches and hashes them, reads and writes their | |
| 4 | - UTF-8, reads a number from one, and copies one to keep. | |
| 3 | + module orders, searches and hashes them, trims them and takes them | |
| 4 | + apart, changes the case of their ASCII letters, joins them and | |
| 5 | + replaces in them, reads and writes their UTF-8, reads a number from | |
| 6 | + one and writes one, and copies one to keep. A part it gives is a | |
| 7 | + slice of the string it was given, with no memory of its own. | |
| 5 | 8 | |
| 6 | 9 | proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); |
| 7 | 10 | s's bytes, copied into memory from a, to keep after s's own are |
| @@ -13,15 +16,55 @@ | ||
| 13 | 16 | proc eq*(a, b: []U8): BOOL; |
| 14 | 17 | Whether a and b hold the same bytes: a = b, as a procedure value. |
| 15 | 18 | |
| 19 | +proc eqFold*(a, b: []U8): BOOL; | |
| 20 | + Whether a and b are the same but for the case of ASCII letters. | |
| 21 | + | |
| 16 | 22 | proc hash*(s: []U8): U64; |
| 17 | 23 | FNV-1a, 64 bits. |
| 18 | 24 | |
| 19 | 25 | proc find*(s, sub: []U8): INT; |
| 20 | 26 | Where sub first starts in s, or -1. |
| 21 | 27 | |
| 28 | +proc findLast*(s, sub: []U8): INT; | |
| 29 | + Where sub last starts in s, or -1. | |
| 30 | + | |
| 22 | 31 | proc hasPrefix*(s, p: []U8): BOOL; |
| 23 | 32 | Whether s starts with p. |
| 24 | 33 | |
| 34 | +proc hasSuffix*(s, x: []U8): BOOL; | |
| 35 | + Whether s ends with x. | |
| 36 | + | |
| 37 | +proc trimLeft*(s: []U8): []U8; | |
| 38 | + s without the spaces at its start. | |
| 39 | + | |
| 40 | +proc trimRight*(s: []U8): []U8; | |
| 41 | + s without the spaces at its end. | |
| 42 | + | |
| 43 | +proc trim*(s: []U8): []U8; | |
| 44 | + s without the spaces at either end. | |
| 45 | + | |
| 46 | +proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); | |
| 47 | + What comes before the first sep in s, what comes after it, and true; | |
| 48 | + or s, an empty string and false if s has no sep. | |
| 49 | + | |
| 50 | +proc field*(s: []U8): ([]U8, []U8); | |
| 51 | + The first word of s, the bytes after the spaces at its start up to | |
| 52 | + the next space or the end, and the rest of s from there; two empty | |
| 53 | + strings if s holds only spaces. | |
| 54 | + | |
| 55 | +proc lower*(s: []U8); | |
| 56 | + s's ASCII capitals made small, in place. | |
| 57 | + | |
| 58 | +proc upper*(s: []U8); | |
| 59 | + s's small ASCII letters made capitals, in place. | |
| 60 | + | |
| 61 | +proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); | |
| 62 | + The parts added to b, with sep between each two. | |
| 63 | + | |
| 64 | +proc replace*(b: ^Buf.Buf; s, old, new: []U8); | |
| 65 | + s added to b, with new for each old in it, from the left, none | |
| 66 | + overlapping another; s as it is if old is empty. | |
| 67 | + | |
| 25 | 68 | proc encode*(c: I32): ([4]U8, INT); |
| 26 | 69 | c in UTF-8: its bytes, and how many. A number that isn't a |
| 27 | 70 | character, a surrogate or one past 10FFFFH, becomes FFFDH, the |
| @@ -33,9 +76,34 @@ | ||
| 33 | 76 | or followed by too few continuations, or spelling a surrogate or a |
| 34 | 77 | character in more bytes than it needs; 0 and 0 if s is empty. |
| 35 | 78 | |
| 79 | +proc decodeLast*(s: []U8): (I32, INT); | |
| 80 | + The character s ends with, and its length in bytes, read as decode | |
| 81 | + reads one: FFFDH and 1 if the last byte doesn't end one rightly; 0 | |
| 82 | + and 0 if s is empty. | |
| 83 | + | |
| 84 | +proc next*(s: []U8; i: INT): (I32, INT); | |
| 85 | + The character at i in s, as decode reads it, and where the one after | |
| 86 | + it starts; 0 and -1 if i is at the end or past it. | |
| 87 | + | |
| 88 | +proc chars*(s: []U8): INT; | |
| 89 | + The characters in s, as decode reads them, each byte that doesn't | |
| 90 | + start one rightly counted as one. | |
| 91 | + | |
| 92 | +proc valid*(s: []U8): BOOL; | |
| 93 | + Whether s is UTF-8: decode finds no byte that doesn't start a | |
| 94 | + character rightly. | |
| 95 | + | |
| 36 | 96 | proc toInt*(s: []U8): (I64, BOOL); |
| 37 | 97 | s as a decimal number, with a - or + before it if any, and nothing |
| 38 | 98 | else; false if it isn't one, or an I64 can't hold it. |
| 99 | + | |
| 100 | +proc toHex*(s: []U8): (U64, BOOL); | |
| 101 | + s in hexadecimal, digits only, capitals or not; false if it isn't | |
| 102 | + that, or a U64 can't hold it. | |
| 103 | + | |
| 104 | +proc fromInt*(n: I64): ([20]U8, INT); | |
| 105 | + n in decimal, as Out.int writes it: its bytes, the first of the 20, | |
| 106 | + and how many. | |
| 39 | 107 | |
| 40 | 108 | proc le*(s: []U8): U64; |
| 41 | 109 | The bytes of s, at most 8, as an unsigned number, the first byte |