summary refs log tree commit diff stats
path: root/lib/sysstr.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sysstr.nim')
-rw-r--r--lib/sysstr.nim155
1 files changed, 88 insertions, 67 deletions
diff --git a/lib/sysstr.nim b/lib/sysstr.nim
index a3e5ab9f8..921a92ff5 100644
--- a/lib/sysstr.nim
+++ b/lib/sysstr.nim
@@ -16,53 +16,55 @@
 # the programmer may not want
 
 type
-  TStringDesc {.importc, nodecl, final.} = object
-    len, space: int # len and space without counting the terminating zero
-    data: array[0..0, char] # for the '\0' character
+  # len and space without counting the terminating zero:
+  NimStringDesc {.compilerproc, final.} = object of TGenericSeq
+    data: array[0..100_000_000, char] # for the '\0' character
 
-  mstring {.importc: "string".} = ptr TStringDesc
+  NimString = ptr NimStringDesc
 
 # implementation:
 
 proc resize(old: int): int {.inline.} =
-  if old <= 0: return 1
+  if old <= 0: return 4
   elif old < 65536: return old * 2
   else: return old * 3 div 2 # for large arrays * 3/2 is better
 
-proc cmpStrings(a, b: mstring): int {.inline, compilerProc.} =
+proc cmpStrings(a, b: NimString): int {.inline, compilerProc.} =
   if a == b: return 0
   if a == nil: return -1
   if b == nil: return 1
   return c_strcmp(a.data, b.data)
 
-proc eqStrings(a, b: mstring): bool {.inline, compilerProc.} =
+proc eqStrings(a, b: NimString): bool {.inline, compilerProc.} =
   if a == b: return true
   if a == nil or b == nil: return false
   return a.len == b.len and
-    c_memcmp(a.data, b.data, a.len * sizeof(char)) == 0
-
-proc rawNewString(space: int): mstring {.compilerProc.} =
-  result = cast[mstring](newObj(addr(strDesc), sizeof(TStringDesc) + 
-                         space * sizeof(char)))
-  result.len = 0
-  result.space = space
-  result.data[0] = '\0'
-
-proc mnewString(len: int): mstring {.exportc.} =
+    c_memcmp(a.data, b.data, a.len * sizeof(char)) == 0'i32
+
+proc rawNewString(space: int): NimString {.compilerProc.} =
+  var s = space
+  if s < 8: s = 7
+  result = cast[NimString](newObj(addr(strDesc), sizeof(TGenericSeq) +
+                          (s+1) * sizeof(char)))
+  #result.len = 0
+  result.space = s
+  #result.data[0] = '\0'
+
+proc mnewString(len: int): NimString {.exportc.} =
   result = rawNewString(len)
   result.len = len
-  result.data[len] = '\0'
+  #result.data[len] = '\0'
 
-proc toNimStr(str: CString, len: int): mstring {.compilerProc.} =
+proc toNimStr(str: CString, len: int): NimString {.compilerProc.} =
   result = rawNewString(len)
   result.len = len
   c_memcpy(result.data, str, (len+1) * sizeof(Char))
   result.data[len] = '\0' # IO.readline relies on this!
 
-proc cstrToNimstr(str: CString): mstring {.compilerProc.} =
+proc cstrToNimstr(str: CString): NimString {.compilerProc.} =
   return toNimstr(str, c_strlen(str))
 
-proc copyString(src: mstring): mstring {.compilerProc.} =
+proc copyString(src: NimString): NimString {.compilerProc.} =
   if src == nil: return nil
   result = rawNewString(src.space)
   result.len = src.len
@@ -71,9 +73,7 @@ proc copyString(src: mstring): mstring {.compilerProc.} =
 proc hashString(s: string): int {.compilerproc.} =
   # the compiler needs exactly the same hash function!
   # this used to be used for efficient generation of string case statements
-  var
-    h: int
-  h = 0
+  var h = 0
   for i in 0..Len(s)-1:
     h = h +% Ord(s[i])
     h = h +% h shl 10
@@ -91,8 +91,7 @@ proc hashString(s: string): int {.compilerproc.} =
 #  setLength(var s: string, newlen: int)
 #    {.extern: "setLengthStr", noDecl, noSideEffect.}
 
-
-proc copyStrLast(s: mstring, start, last: int): mstring {.exportc.} =
+proc copyStrLast(s: NimString, start, last: int): NimString {.exportc.} =
   var
     len: int
   if start >= s.len: return mnewString(0) # BUGFIX
@@ -105,15 +104,19 @@ proc copyStrLast(s: mstring, start, last: int): mstring {.exportc.} =
   c_memcpy(result.data, addr(s.data[start]), len * sizeof(Char))
   result.data[len] = '\0'
 
-proc copyStr(s: mstring, start: int): mstring {.exportc.} =
+proc copyStr(s: NimString, start: int): NimString {.exportc.} =
   return copyStrLast(s, start, s.len-1)
 
-proc addChar(s: mstring, c: char): mstring {.compilerProc.} =
+proc addChar(s: NimString, c: char): NimString {.compilerProc.} =
   result = s
   if result.len >= result.space:
     result.space = resize(result.space)
-    result = cast[mstring](growObj(result,
-      sizeof(TStringDesc) + result.space * sizeof(char)))
+    result = cast[NimString](growObj(result,
+      sizeof(TGenericSeq) + (result.space+1) * sizeof(char)))
+    #var space = resize(result.space)
+    #result = rawNewString(space)
+    #copyMem(result, s, s.len * sizeof(char) + sizeof(TGenericSeq))
+    #result.space = space
   result.data[result.len] = c
   result.data[result.len+1] = '\0'
   inc(result.len)
@@ -149,27 +152,28 @@ proc addChar(s: mstring, c: char): mstring {.compilerProc.} =
 #   <generated C code>
 #   s = rawNewString(0);
 
-proc resizeString(dest: mstring, addlen: int): mstring {.compilerproc.} =
+proc resizeString(dest: NimString, addlen: int): NimString {.compilerproc.} =
   if dest.len + addLen + 1 <= dest.space: # BUGFIX: this is horrible!
     result = dest
   else: # slow path:
-    var
-      sp = max(resize(dest.space), dest.len + addLen + 1)
-    result = cast[mstring](growObj(dest, sizeof(TStringDesc) + 
-                           sp * sizeof(Char)))
-    # DO NOT UPDATE LEN YET: dest.len = newLen
+    var sp = max(resize(dest.space), dest.len + addLen + 1)
+    result = cast[NimString](growObj(dest, sizeof(TGenericSeq) +
+                           (sp+1) * sizeof(Char)))
     result.space = sp
+    #result = rawNewString(sp)
+    #copyMem(result, dest, dest.len * sizeof(char) + sizeof(TGenericSeq))
+    # DO NOT UPDATE LEN YET: dest.len = newLen
 
-proc appendString(dest, src: mstring) {.compilerproc, inline.} =
+proc appendString(dest, src: NimString) {.compilerproc, inline.} =
   c_memcpy(addr(dest.data[dest.len]), src.data, (src.len + 1) * sizeof(Char))
   inc(dest.len, src.len)
 
-proc appendChar(dest: mstring, c: char) {.compilerproc, inline.} =
+proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} =
   dest.data[dest.len] = c
   dest.data[dest.len+1] = '\0'
   inc(dest.len)
 
-proc setLengthStr(s: mstring, newLen: int): mstring {.compilerProc.} =
+proc setLengthStr(s: NimString, newLen: int): NimString {.compilerProc.} =
   var n = max(newLen, 0)
   if n <= s.space:
     result = s
@@ -187,37 +191,54 @@ proc incrSeq(seq: PGenericSeq, elemSize: int): PGenericSeq {.compilerProc.} =
   #  add seq x  generates:
   #  seq = incrSeq(seq, sizeof(x));
   #  seq[seq->len-1] = x;
-  result = seq
-  if result.len >= result.space:
-    var
-      s: TAddress
-    result.space = resize(result.space)
-    result = cast[PGenericSeq](growObj(result, elemSize * result.space + 
-                               GenericSeqSize))
-    # set new elements to zero:
-    s = cast[TAddress](result)
-    zeroMem(cast[pointer](s + GenericSeqSize + (result.len * elemSize)),
-      (result.space - result.len) * elemSize)
-    # for i in len .. space-1:
-    #   seq->data[i] = 0
-  inc(result.len)
+  when false:
+    # broken version:
+    result = seq
+    if result.len >= result.space:
+      var s = resize(result.space)
+      result = cast[PGenericSeq](newSeq(extGetCellType(seq), s))
+      genericSeqAssign(result, seq, XXX)
+      #copyMem(result, seq, seq.len * elemSize + GenericSeqSize)
+    inc(result.len)  
+  else:  
+    result = seq
+    if result.len >= result.space:
+      result.space = resize(result.space)
+      result = cast[PGenericSeq](growObj(result, elemSize * result.space +
+                                 GenericSeqSize))
+      # set new elements to zero:
+      #var s = cast[TAddress](result)
+      #zeroMem(cast[pointer](s + GenericSeqSize + (result.len * elemSize)),
+      #  (result.space - result.len) * elemSize)
+      # for i in len .. space-1:
+      #   seq->data[i] = 0
+    inc(result.len)
 
 proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
     compilerProc.} =
-  result = seq
-  if result.space < newLen:
-    var
-      s: TAddress
-    result.space = max(resize(result.space), newLen)
-    result = cast[PGenericSeq](growObj(result, elemSize * result.space + 
-                               GenericSeqSize))
-    # set new elements to zero (needed for GC):
-    s = cast[TAddress](result)
-    zeroMem(cast[pointer](s + GenericSeqSize + (result.len * elemSize)),
-      (result.space - result.len) * elemSize)
-  # Else: We could decref references, if we had type information here :-(
-  #       However, this does not happen often
-  result.len = newLen
+  when false:
+    # broken version:
+    result = seq
+    if result.space < newLen:
+      var s = max(resize(result.space), newLen)
+      result = cast[PGenericSeq](newSeq(extGetCellType(seq), s))
+    result.len = newLen
+  else:
+    result = seq
+    if result.space < newLen:
+      result.space = max(resize(result.space), newLen)
+      result = cast[PGenericSeq](growObj(result, elemSize * result.space +
+                                 GenericSeqSize))
+    elif newLen < result.len:
+      # we need to decref here, otherwise the GC leaks!
+      for i in newLen..result.len-1:
+        forAllChildrenAux(cast[pointer](cast[TAddress](result) +% 
+                          GenericSeqSize +% (i*%elemSize)),
+                          extGetCellType(result).base, waZctDecRef)
+      # and set the memory to nil:
+      zeroMem(cast[pointer](cast[TAddress](result) +% GenericSeqSize +%
+             (newLen*%elemSize)), (result.len-%newLen) *% elemSize)
+    result.len = newLen
 
 # --------------- other string routines ----------------------------------
 proc nimIntToStr(x: int): string {.compilerproc.} =
efbb57d ^
b618bfc ^

efbb57d ^


74f8cd1









fa6faaa ^



61accc5 ^
fa6faaa ^









74f8cd1






36d3d10 ^
74f8cd1


b618bfc ^
74f8cd1

88827db ^
88827db ^
f7b4413 ^
fa6faaa ^

548d59f ^

3b44b98 ^









5a484ef ^



fdd87c5 ^

d388cc1 ^

42526cb ^

2d393bf ^

74f8cd1
88827db ^
74f8cd1

cfb7cff ^
74f8cd1
88827db ^
cfb7cff ^



88827db ^
74f8cd1



bb6e79a ^
74f8cd1


c80bafe ^
74f8cd1



4c19657 ^



a8668ea ^
1445cbc ^
dc05a10 ^
ee3f6e8 ^
a0c66db ^
41bf615 ^
1445cbc ^
74f8cd1

6fc0e72 ^
74f8cd1


da76ec4 ^
74f8cd1


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