Skip to content

Commit eec86c4

Browse files
authored
fixes #1539; prepareMutationAt: cannot pass prepareMutation(s) to var/out T parameter (#1540)
fixes #1539 ref #1536
1 parent cf50d7e commit eec86c4

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

doc/stdlib.dgn.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ Converts a Nim string to a C string.
327327

328328
Prepares a string for mutation. String literals are "copy on write", so you need to call `prepareMutation` before modifying strings via `addr`.
329329

330+
####prepareMutationAt
331+
332+
Prepares the given string for mutation and returns an addressable
333+
reference to the character at index `i`.
330334

331335
####&
332336

lib/std/system/stringimpl.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ proc prepareMutation*(s: var string) =
287287
s.i = EmptyI
288288
s.a = a # also do this for `a == nil`
289289

290+
proc prepareMutationAt*(s: var string; i: int): var char {.requires: (i < len(s) and i >= 0), inline.} =
291+
prepareMutation(s)
292+
result = s.a[i]
293+
290294
proc newString*(len: int): string =
291295
let a = cast[StrData](alloc(len))
292296
if a != nil:

src/nimony/derefs.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ proc trProcDecl(c: var Context; n: var Cursor) =
383383
takeParRi c, n
384384
else:
385385
var body = n
386-
trSons c, n, c.r.returnExpects
386+
tr c, n, c.r.returnExpects
387387
if c.r.dangerousLocations.len > 0:
388388
checkForDangerousLocations c, body
389389
takeParRi c, n

tests/nimony/sysbasics/tstrings.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,8 @@ block: # issue #1444
143143
assert substr("abc", 3, 1) == ""
144144
assert substr("abc", 3, 2) == ""
145145
assert substr("abc", 3, 3) == ""
146+
147+
block:
148+
var s = "12234"
149+
var m = prepareMutationAt(s, 1)
150+
assert m == '2'

0 commit comments

Comments
 (0)