summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAmjad Ben Hedhili <amjadhedhili@outlook.com>2023-09-13 19:43:25 +0100
committerGitHub <noreply@github.com>2023-09-13 20:43:25 +0200
commit325341866f6b82cba5d81db8e39ca98b0d96fd4d (patch)
treec1c65237cddd8c1f4d9a143f830e54eb9f122aff
parent8f5b90f886501862bd27d4e0e8244e2f7f0ebc2a (diff)
downloadNim-325341866f6b82cba5d81db8e39ca98b0d96fd4d.tar.gz
Make capacity work with refc [backport] (#22697)
followup of #19771.
-rw-r--r--lib/system/seqs_v2.nim7
-rw-r--r--lib/system/strs_v2.nim8
-rw-r--r--lib/system/sysstr.nim22
3 files changed, 26 insertions, 11 deletions
diff --git a/lib/system/seqs_v2.nim b/lib/system/seqs_v2.nim
index c3e48019c..9cf403229 100644
--- a/lib/system/seqs_v2.nim
+++ b/lib/system/seqs_v2.nim
@@ -183,8 +183,6 @@ proc newSeq[T](s: var seq[T], len: Natural) =
 proc sameSeqPayload(x: pointer, y: pointer): bool {.compilerproc, inline.} =
   result = cast[ptr NimRawSeq](x)[].p == cast[ptr NimRawSeq](y)[].p
 
-template capacityImpl(sek: NimSeqV2): int =
-  if sek.p != nil: (sek.p.cap and not strlitFlag) else: 0
 
 func capacity*[T](self: seq[T]): int {.inline.} =
   ## Returns the current capacity of the seq.
@@ -194,9 +192,8 @@ func capacity*[T](self: seq[T]): int {.inline.} =
     lst.add "Nim"
     assert lst.capacity == 42
 
-  {.cast(noSideEffect).}:
-    let sek = unsafeAddr self
-    result = capacityImpl(cast[ptr NimSeqV2[T]](sek)[])
+  let sek = cast[ptr NimSeqV2[T]](unsafeAddr self)
+  result = if sek.p != nil: (sek.p.cap and not strlitFlag) else: 0
 
 
 {.pop.}  # See https://github.com/nim-lang/Nim/issues/21401
diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim
index e161172b2..a9a104d3d 100644
--- a/lib/system/strs_v2.nim
+++ b/lib/system/strs_v2.nim
@@ -202,9 +202,6 @@ proc prepareMutation*(s: var string) {.inline.} =
     nimPrepareStrMutationV2(cast[ptr NimStringV2](s)[])
 
 
-template capacityImpl(str: NimStringV2): int =
-  if str.p != nil: str.p.cap else: 0
-
 func capacity*(self: string): int {.inline.} =
   ## Returns the current capacity of the string.
   # See https://github.com/nim-lang/RFCs/issues/460
@@ -213,6 +210,5 @@ func capacity*(self: string): int {.inline.} =
     str.add "Nim"
     assert str.capacity == 42
 
-  {.cast(noSideEffect).}:
-    let str = unsafeAddr self
-    result = capacityImpl(cast[ptr NimStringV2](str)[])
+  let str = cast[ptr NimStringV2](unsafeAddr self)
+  result = if str.p != nil: str.p.cap else: 0
diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim
index b8dc7101d..4acf1780b 100644
--- a/lib/system/sysstr.nim
+++ b/lib/system/sysstr.nim
@@ -340,3 +340,25 @@ proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {.
       result = s
       zeroMem(dataPointer(result, elemAlign, elemSize, result.len), (newLen-%result.len) *% elemSize)
     result.len = newLen
+
+func capacity*(self: string): int {.inline.} =
+  ## Returns the current capacity of the string.
+  # See https://github.com/nim-lang/RFCs/issues/460
+  runnableExamples:
+    var str = newStringOfCap(cap = 42)
+    str.add "Nim"
+    assert str.capacity == 42
+
+  let str = cast[NimString](self)
+  result = if str != nil: str.reserved else: 0
+
+func capacity*[T](self: seq[T]): int {.inline.} =
+  ## Returns the current capacity of the seq.
+  # See https://github.com/nim-lang/RFCs/issues/460
+  runnableExamples:
+    var lst = newSeqOfCap[string](cap = 42)
+    lst.add "Nim"
+    assert lst.capacity == 42
+
+  let sek = cast[PGenericSeq](self)
+  result = if sek != nil: (sek.reserved and not strlitFlag) else: 0
f63bc23da2e1'>^
ef2b1e37 ^
2ed2d4f7 ^




b7b8f3a0 ^




c8f870cb ^



ef2b1e37 ^















81f5ac9e ^
efd7c0c1 ^






cbcd3325 ^






81f5ac9e ^
cbcd3325 ^
d3bcb234 ^

cbcd3325 ^
81f5ac9e ^











cbcd3325 ^
90018274 ^




cbcd3325 ^
95e021ae ^

c928a9eb ^
95e021ae ^
cbcd3325 ^
95e021ae ^















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