raz / sbcl / lib / Str.raz

162 lines · 4.4 KiB · raw · history · last change cfa2bc20

1(* Operations on []U8 strings: comparing, hashing, finding, UTF-8,
2 reading a number, and numbers in a fixed byte order. *)
3module Str;
4import Mem, Rt;
5
6(* -1, 0 or 1 as a sorts before, with or after b, byte by byte *)
7proc cmp*(a, b: []U8): INT;
8var i: INT;
9begin
10 while (i < LEN(a)) and (i < LEN(b)) do
11 if a[i] < b[i] then return -1; end;
12 if a[i] > b[i] then return 1; end;
13 INC(i);
14 end;
15 if LEN(a) < LEN(b) then return -1; end;
16 if LEN(a) > LEN(b) then return 1; end;
17 return 0;
18end;
19
20proc eq*(a, b: []U8): BOOL;
21begin
22 return a = b;
23end;
24
25(* s's bytes in memory from a; false if a has none *)
26proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL);
27var p: ^U8; d: []U8;
28begin
29 p := Mem.alloc(a, LEN(s), 1);
30 if (p = nil) and (LEN(s) > 0) then return "", false; end;
31 d := p[0..LEN(s)];
32 COPY(d, s);
33 return d, true;
34end;
35
36(* FNV-1a, 64 bits *)
37proc hash*(s: []U8): U64;
38var h: U64; i: INT;
39begin
40 h := 0CBF29CE484222325H;
41 for i := 0 to LEN(s) - 1 do
42 h := (h xor U64(s[i])) * 100000001B3H;
43 end;
44 return h;
45end;
46
47proc hasPrefix*(s, p: []U8): BOOL;
48begin
49 return (LEN(p) <= LEN(s)) and (s[0..LEN(p)] = p);
50end;
51
52(* where sub first starts in s, or -1 *)
53proc find*(s, sub: []U8): INT;
54var i: INT;
55begin
56 for i := 0 to LEN(s) - LEN(sub) do
57 if s[i..i + LEN(sub)] = sub then return i; end;
58 end;
59 return -1;
60end;
61
62(* c in UTF-8: its bytes, and how many *)
63proc encode*(c: I32): ([4]U8, INT);
64var b: [4]U8;
65begin
66 if (c < 0) or (c > 10FFFFH) or ((c >= 0D800H) and (c <= 0DFFFH)) then c := 0FFFDH; end;
67 if c < 80H then
68 b[0] := U8(c);
69 return b, 1;
70 elsif c < 800H then
71 b[0] := U8(0C0H | (c shr 6)); b[1] := U8(80H | (c & 3FH));
72 return b, 2;
73 elsif c < 10000H then
74 b[0] := U8(0E0H | (c shr 12)); b[1] := U8(80H | ((c shr 6) & 3FH));
75 b[2] := U8(80H | (c & 3FH));
76 return b, 3;
77 end;
78 b[0] := U8(0F0H | (c shr 18)); b[1] := U8(80H | ((c shr 12) & 3FH));
79 b[2] := U8(80H | ((c shr 6) & 3FH)); b[3] := U8(80H | (c & 3FH));
80 return b, 4;
81end;
82
83(* the character s starts with, and its length in bytes *)
84proc decode*(s: []U8): (I32, INT);
85var c, least: I32; n, i: INT; b: U8;
86begin
87 if LEN(s) = 0 then return 0, 0; end;
88 b := s[0];
89 if b < 80H then return I32(b), 1;
90 elsif (b >= 0C2H) and (b < 0E0H) then n := 2; c := I32(b & 1FH); least := 80H;
91 elsif (b >= 0E0H) and (b < 0F0H) then n := 3; c := I32(b & 0FH); least := 800H;
92 elsif (b >= 0F0H) and (b < 0F5H) then n := 4; c := I32(b & 7H); least := 10000H;
93 else return 0FFFDH, 1;
94 end;
95 if LEN(s) < n then return 0FFFDH, 1; end;
96 for i := 1 to n - 1 do
97 if s[i] & 0C0H # 80H then return 0FFFDH, 1; end;
98 c := (c shl 6) | I32(s[i] & 3FH);
99 end;
100 if (c < least) or (c > 10FFFFH) or ((c >= 0D800H) and (c <= 0DFFFH)) then return 0FFFDH, 1; end;
101 return c, n;
102end;
103
104(* s, in decimal; false if it isn't a number an I64 holds *)
105proc toInt*(s: []U8): (I64, BOOL);
106var i: INT; neg: BOOL; v: U64; d: U64;
107begin
108 if LEN(s) = 0 then return 0, false; end;
109 if (s[0] = '-') or (s[0] = '+') then neg := s[0] = '-'; i := 1; end;
110 if i = LEN(s) then return 0, false; end;
111 while i < LEN(s) do
112 if (s[i] < '0') or (s[i] > '9') then return 0, false; end;
113 d := U64(s[i] - '0');
114 if v > (8000000000000000H - d) div 10 then return 0, false; end;
115 v := v * 10 + d;
116 INC(i);
117 end;
118 if neg then return I64(0 - v), true; end;
119 if v > 7FFFFFFFFFFFFFFFH then return 0, false; end;
120 return I64(v), true;
121end;
122
123proc tooLong(s: []U8);
124begin
125 if LEN(s) > 8 then Rt.stop("Str: more than 8 bytes for a number"); end;
126end;
127
128(* s, at most 8 bytes, the first the lowest *)
129proc le*(s: []U8): U64;
130var v: U64; i: INT;
131begin
132 tooLong(s);
133 for i := LEN(s) - 1 to 0 by -1 do v := (v shl 8) | U64(s[i]); end;
134 return v;
135end;
136
137(* s, at most 8 bytes, the first the highest *)
138proc be*(s: []U8): U64;
139var v: U64; i: INT;
140begin
141 tooLong(s);
142 for i := 0 to LEN(s) - 1 do v := (v shl 8) | U64(s[i]); end;
143 return v;
144end;
145
146(* v into s, the lowest byte first *)
147proc putLE*(s: []U8; v: U64);
148var i: INT;
149begin
150 tooLong(s);
151 for i := 0 to LEN(s) - 1 do s[i] := U8(v & 0FFH); v := v shr 8; end;
152end;
153
154(* v into s, the highest byte first *)
155proc putBE*(s: []U8; v: U64);
156var i: INT;
157begin
158 tooLong(s);
159 for i := LEN(s) - 1 to 0 by -1 do s[i] := U8(v & 0FFH); v := v shr 8; end;
160end;
161
162end Str.