1use <prelude.kav>
2use <bytes.kav> as bytes
3use <mem.kav> as mem
4use <list.kav> as l
5use <str.kav> as str
6
7pub abstract linear datatype Stralloc {
8 | [n: nat] Stralloc(int, int, bytes::Bytes[n]) : Stralloc
9}
10
11/% create a new Stralloc with room for cap bytes %/
12pub fn new(cap: int) -> Stralloc {
13 val m = bytes::alloc(cap)
14 Stralloc(0, cap, m)
15}
16
17/% free a Stralloc %/
18pub fn free(sa: Stralloc) -> one {
19 val ~Stralloc(len, cap, m) = sa
20 bytes::free(m)
21}
22
23/% empty a Stralloc, but keep the memory %/
24pub fn clear(sa: Stralloc) -> Stralloc {
25 val ~Stralloc(len, cap, m) = sa
26 Stralloc(0, cap, m)
27}
28
29/% append one byte to a Stralloc %/
30pub fn catc(sa: Stralloc, c: int) -> Stralloc {
31 val ~Stralloc(len, cap, m) = sa
32 if len < cap then {
33 val m = bytes::set(m, len, c as u8)
34 Stralloc(len + 1, cap, m)
35 }
36 else {
37 val newcap = cap * 2 + 16
38 val m = bytes::realloc(m, newcap)
39 catc(Stralloc(len, newcap, m), c)
40 }
41}
42
43/% return the length of a Stralloc %/
44pub fn len(sa: !Stralloc) -> int {
45 val Stralloc(len, cap, m) = sa
46 len
47}
48
49/% return the address where the Stralloc starts %/
50pub fn addr(sa: !Stralloc) -> ptr {
51 val Stralloc(len, cap, m) = sa
52 bytes::addr(m)
53}
54
55fn catsat(sa: Stralloc, s: cstr, i: int) -> Stralloc {
56 val c = mem::load8(mem::add(s as ptr, i)) as int
57 if c == 0 then sa else catsat(catc(sa, c), s, i + 1)
58}
59
60/% append the cstr s to a Stralloc %/
61pub fn cats(sa: Stralloc, s: cstr) -> Stralloc {
62 catsat(sa, s, 0)
63}
64
65/% return the byte at position i of a Stralloc %/
66pub fn at(sa: !Stralloc, i: int) -> int {
67 val Stralloc(len, cap, m) = sa
68 if i < 0 then -1
69 else if i >= len then -1
70 else bytes::get(m, i) as int
71}
72
73fn findat(sa: !Stralloc, c: int, i: int) -> int {
74 if i >= len(sa) then len(sa)
75 else if at(sa, i) == c then i
76 else findat(sa, c, i + 1)
77}
78
79/%
80search a Stralloc for byte c starting at from and return where it appears.
81if it isn't present, return the length of the Stralloc.
82%/
83pub fn find(sa: !Stralloc, c: int, from: int) -> int {
84 findat(sa, c, from)
85}
86
87fn subat(sa: !Stralloc, out: Stralloc, i: int, to: int) -> Stralloc {
88 if i >= to then out
89 else subat(sa, catc(out, at(sa, i)), i + 1, to)
90}
91
92/%
93Borrow a Stralloc and return a new Stralloc holding the bytes of sa from
94from up to but not including to
95%/
96pub fn sub(sa: !Stralloc, from: int, to: int) -> Stralloc {
97 val to = if to > len(sa) then len(sa) else to
98 val from = if from < 0 then 0 else from
99 if from >= to then new(0)
100 else subat(sa, new(to - from), from, to)
101}
102
103fn isspace(c: int) -> bool {
104 if c == ' ' then true else c == '\t'
105}
106
107fn skipspace(sa: !Stralloc, i: int) -> int {
108 if i >= len(sa) then len(sa)
109 else if isspace(at(sa, i)) then skipspace(sa, i + 1)
110 else i
111}
112
113fn wordend(sa: !Stralloc, i: int) -> int {
114 if i >= len(sa) then len(sa)
115 else if isspace(at(sa, i)) then i
116 else wordend(sa, i + 1)
117}
118
119fn splitat(sa: !Stralloc, i: int) -> [n: nat] l::List[Stralloc, n] {
120 val start = skipspace(sa, i)
121 if start >= len(sa) then l::Nil
122 else {
123 val end = wordend(sa, start)
124 l::Cons(sub(sa, start, end), splitat(sa, end))
125 }
126}
127
128/% split a Stralloc into words at spaces and tabs and return a List of Strallocs %/
129pub fn words(sa: !Stralloc) -> [n: nat] l::List[Stralloc, n] {
130 splitat(sa, 0)
131}
132
133/% free a list of Strallocs and everything in it %/
134pub fn freeall[n: nat](xs: l::List[Stralloc, n]) -> one {
135 case xs of {
136 | ~l::Nil => one
137 | ~l::Cons(x, rest) => {
138 val _ = free(x)
139 freeall(rest)
140 }
141 }
142}
143
144fn cbyte(c: cstr, i: int) -> int {
145 mem::load8(mem::add(c as ptr, i)) as int
146}
147
148fn eqat(a: !Stralloc, b: !Stralloc, i: int) -> bool {
149 if i >= len(a) then true
150 else if at(a, i) != at(b, i) then false
151 else eqat(a, b, i + 1)
152}
153
154/% compare the bytes of two Strallocs %/
155pub fn eq(a: !Stralloc, b: !Stralloc) -> bool {
156 if len(a) != len(b) then false else eqat(a, b, 0)
157}
158
159fn eqsat(sa: !Stralloc, c: cstr, i: int) -> bool {
160 val cb = cbyte(c, i)
161 if i >= len(sa) then cb == 0
162 else if cb == 0 then false
163 else if at(sa, i) != cb then false
164 else eqsat(sa, c, i + 1)
165}
166
167/% compare the bytes of a Stralloc and a cstr %/
168pub fn eqs(sa: !Stralloc, c: cstr) -> bool {
169 eqsat(sa, c, 0)
170}
171
172fn startsat(sa: !Stralloc, c: cstr, i: int) -> bool {
173 val cb = cbyte(c, i)
174 if cb == 0 then true
175 else if i >= len(sa) then false
176 else if at(sa, i) != cb then false
177 else startsat(sa, c, i + 1)
178}
179
180/% check if a Stralloc begins with a cstr %/
181pub fn starts(sa: !Stralloc, c: cstr) -> bool {
182 startsat(sa, c, 0)
183}
184
185fn endsat(sa: !Stralloc, c: cstr, off: int, i: int) -> bool {
186 val cb = cbyte(c, i)
187 if cb == 0 then true
188 else if at(sa, off + i) != cb then false
189 else endsat(sa, c, off, i + 1)
190}
191
192/% check if a Stralloc ends with a cstr %/
193pub fn ends(sa: !Stralloc, c: cstr) -> bool {
194 val n = str::len(c)
195 if n > len(sa) then false else endsat(sa, c, len(sa) - n, 0)
196}
197
198fn catsaat(a: Stralloc, b: !Stralloc, i: int) -> Stralloc {
199 if i >= len(b) then a else catsaat(catc(a, at(b, i)), b, i + 1)
200}
201
202/% append the bytes of b to a %/
203pub fn catsa(a: Stralloc, b: !Stralloc) -> Stralloc {
204 catsaat(a, b, 0)
205}
206
207/% keep only the first n bytes of a Stralloc %/
208pub fn truncate(sa: Stralloc, n: int) -> Stralloc {
209 val ~Stralloc(len, cap, m) = sa
210 if n < 0 then Stralloc(0, cap, m)
211 else if n < len then Stralloc(n, cap, m)
212 else Stralloc(len, cap, m)
213}