-
Notifications
You must be signed in to change notification settings - Fork 3
/
ssostrings.nim
306 lines (257 loc) · 8.09 KB
/
ssostrings.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import std/hashes
when defined(nimPreviewSlimSystem):
import std/assertions
when cpuEndian == littleEndian:
const
strLongFlag = 1
else:
const
strLongFlag = low(int)
type
String* = object # LongString
cap, len: int
p: ptr UncheckedArray[char]
template contentSize(cap): int = cap + 1
template isLong(s): bool = (s.cap and strLongFlag) == strLongFlag
template frees(s) =
if isLong(s) and s.p != nil:
when compileOption("threads"):
deallocShared(s.p)
else:
dealloc(s.p)
const
strMinCap = max(2, sizeof(String) - 1) - 1
type
ShortString = object
len: int8
data: array[strMinCap + 1, char]
static: assert sizeof(ShortString) == sizeof(String)
template short(s: String): untyped = cast[ptr ShortString](addr s)
template data(s: String): untyped =
if isLong(s): s.p else: cast[ptr UncheckedArray[char]](addr s.short.data[0])
template shortLen(s): int =
when cpuEndian == littleEndian:
s.short.len shr 1
else:
s.short.len
template setShortLen(s, n) =
when cpuEndian == littleEndian:
s.short.len = int8(n) shl 1
else:
s.short.len = int8(n)
template longCap(s): int =
when cpuEndian == littleEndian:
int(uint(s.cap) shr 1)
else:
s.cap and not strLongFlag
template setLongCap(s, n) =
when cpuEndian == littleEndian:
s.cap = n shl 1 or strLongFlag
else:
s.cap = n or strLongFlag
proc `=destroy`*(x: String) =
frees(x)
proc `=wasMoved`*(x: var String) =
x.cap = 0
template dups(a, b) =
if isLong(b):
when compileOption("threads"):
a.p = cast[ptr UncheckedArray[char]](allocShared(contentSize(b.len)))
else:
a.p = cast[ptr UncheckedArray[char]](alloc(contentSize(b.len)))
a.len = b.len
a.setLongCap b.len
copyMem(a.p, b.p, contentSize(a.len))
else:
copyMem(addr a, addr b, sizeof(String))
proc `=dup`*(b: String): String =
dups(result, b)
proc `=copy`*(a: var String, b: String) =
if isLong(a):
if isLong(b) and a.p == b.p: return
`=destroy`(a)
`=wasMoved`(a)
dups(a, b)
proc resize(old: int): int {.inline.} =
if old <= 0: result = 4
elif old <= high(int16): result = old * 2
else: result = old div 2 + old # for large arrays * 3/2 is better
proc len*(s: String): int {.inline.} =
if isLong(s): s.len else: s.shortLen
proc high*(s: String): int {.inline.} = len(s)-1
proc low*(s: String): int {.inline.} = 0
proc prepareAdd(s: var String; addLen: int) =
let newLen = len(s) + addLen
if isLong(s):
let oldCap = s.longCap
if newLen > oldCap:
let newCap = max(newLen, resize(oldCap))
when compileOption("threads"):
s.p = cast[ptr UncheckedArray[char]](
reallocShared0(s.p, contentSize(oldCap), contentSize(newCap)))
else:
s.p = cast[ptr UncheckedArray[char]](
realloc0(s.p, contentSize(oldCap), contentSize(newCap)))
s.setLongCap newCap
elif newLen > strMinCap:
when compileOption("threads"):
let p = cast[ptr UncheckedArray[char]](allocShared0(contentSize(newLen)))
else:
let p = cast[ptr UncheckedArray[char]](alloc0(contentSize(newLen)))
let oldLen = s.shortLen
if oldLen > 0:
# we are about to append, so there is no need to copy the \0 terminator:
copyMem(p, addr s.short.data[0], min(oldLen, newLen))
s.len = oldLen
s.p = p
s.setLongCap newLen
proc add*(s: var String; c: char) {.inline.} =
let len = len(s)
prepareAdd(s, 1)
s.data[len] = c
s.data[len+1] = '\0'
if isLong(s):
inc s.len
else:
s.setShortLen len+1
proc add*(dest: var String; src: String) {.inline.} =
let srcLen = len(src)
if srcLen > 0:
let destLen = len(dest)
prepareAdd(dest, srcLen)
# also copy the \0 terminator:
copyMem(addr dest.data[destLen], src.data, srcLen+1)
if isLong(dest):
inc dest.len, srcLen
else:
dest.setShortLen destLen+srcLen
proc cstrToStr(str: cstring, len: int): String =
if len <= 0:
result = default(String)
else:
if len > strMinCap:
when compileOption("threads"):
let p = cast[ptr UncheckedArray[char]](allocShared(contentSize(len)))
else:
let p = cast[ptr UncheckedArray[char]](alloc(contentSize(len)))
result = String(p: p, len: len)
result.setLongCap len
else:
result = default(String)
result.setShortLen len
copyMem(result.data, str, len+1)
proc toStr*(str: cstring): String {.inline.} =
if str == nil: cstrToStr(str, 0)
else: cstrToStr(str, str.len)
proc toStr*(str: string): String {.inline.} =
cstrToStr(str.cstring, str.len)
proc toCStr*(s: ptr String): cstring {.inline.} =
result = cast[cstring](s[].data)
template toCStr*(s: String): cstring = toCStr(addr s)
proc toNimStr*(s: String): string =
result = newStringUninit(len(s))
copyMem(cstring(result), toCStr(s), result.len)
proc initStringOfCap*(space: Natural): String =
# this is also 'system.newStringOfCap'.
if space <= 0:
result = default(String)
else:
if space > strMinCap:
when compileOption("threads"):
let p = cast[ptr UncheckedArray[char]](allocShared0(contentSize(space)))
else:
let p = cast[ptr UncheckedArray[char]](alloc0(contentSize(space)))
result = String(p: p)
result.setLongCap space
else:
result = default(String)
proc initString*(len: Natural): String =
if len <= 0:
result = default(String)
else:
if len > strMinCap:
when compileOption("threads"):
let p = cast[ptr UncheckedArray[char]](allocShared0(contentSize(len)))
else:
let p = cast[ptr UncheckedArray[char]](alloc0(contentSize(len)))
result = String(p: p, len: len)
result.setLongCap len
else:
result = default(String)
result.setShortLen len
proc setLen*(s: var String, newLen: Natural) =
if newLen == 0:
discard "do not free the buffer here, pattern 's.setLen 0' is common for avoiding allocations"
else:
let oldLen = len(s)
if newLen > oldLen:
prepareAdd(s, newLen - oldLen)
s.data[newLen] = '\0'
if isLong(s):
s.len = newLen
else:
s.setShortLen newLen
# Comparisons
proc `==`*(a, b: String): bool =
let aLen = len(a)
if aLen != len(b):
result = false
else: result = equalMem(a.data, b.data, aLen)
proc cmp*(a, b: String): int =
let aLen = len(a)
let bLen = len(b)
result = cmpMem(a.data, b.data, min(aLen, bLen))
if result == 0:
if aLen < bLen:
result = -1
elif aLen > bLen:
result = 1
proc `<=`*(a, b: String): bool {.inline.} = cmp(a, b) <= 0
proc `<`*(a, b: String): bool {.inline.} = cmp(a, b) < 0
proc raiseIndexDefect(i, n: int) {.noinline, noreturn.} =
raise newException(IndexDefect, "index " & $i & " not in 0 .. " & $n)
template checkBounds(i, n) =
when compileOption("boundChecks"):
{.line.}:
if i < 0 or i >= n:
raiseIndexDefect(i, n-1)
proc `[]`*(x: String; i: int): char {.inline.} =
checkBounds(i, len(x))
x.data[i]
proc `[]`*(x: var String; i: int): var char {.inline.} =
checkBounds(i, len(x))
x.data[i]
proc `[]=`*(x: var String; i: int; val: char) {.inline.} =
checkBounds(i, len(x))
x.data[i] = val
proc `[]`*(x: String; i: BackwardsIndex): char {.inline.} =
checkBounds(len(x) - i.int, len(x))
x.data[len(x) - i.int]
proc `[]`*(x: var String; i: BackwardsIndex): var char {.inline.} =
checkBounds(len(x) - i.int, len(x))
x.data[len(x) - i.int]
proc `[]=`*(x: var String; i: BackwardsIndex; val: char) {.inline.} =
checkBounds(len(x) - i.int, len(x))
x.data[len(x) - i.int] = val
iterator items*(a: String): char {.inline.} =
var i = 0
let L = len(a)
while i < L:
yield a[i]
inc(i)
assert(len(a) == L, "the length of the string changed while iterating over it")
iterator mitems*(a: var String): var char {.inline.} =
var i = 0
let L = len(a)
while i < L:
yield a[i]
inc(i)
assert(len(a) == L, "the length of the string changed while iterating over it")
template toOpenArray*(s: String; first, last: int): untyped =
rangeCheck(first <= last)
checkBounds(last, len(s))
toOpenArray(toCStr(addr s), first, last)
template toOpenArray*(s: String): untyped =
toOpenArray(toCStr(addr s), 0, s.high)
proc hash*(x: String): Hash =
hash(toOpenArray(x))