Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/stdlib.dgn.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ Converts a Nim string to a C string.

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

####prepareMutationAt

Prepares the given string for mutation and returns an addressable
reference to the character at index `i`.

####&

Expand Down
4 changes: 4 additions & 0 deletions lib/std/system/stringimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ proc prepareMutation*(s: var string) =
s.i = EmptyI
s.a = a # also do this for `a == nil`

proc prepareMutationAt*(s: var string; i: int): var char {.requires: (i < len(s) and i >= 0), inline.} =
prepareMutation(s)
result = s.a[i]

proc newString*(len: int): string =
let a = cast[StrData](alloc(len))
if a != nil:
Expand Down
2 changes: 1 addition & 1 deletion src/nimony/derefs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ proc trProcDecl(c: var Context; n: var Cursor) =
takeParRi c, n
else:
var body = n
trSons c, n, c.r.returnExpects
tr c, n, c.r.returnExpects
if c.r.dangerousLocations.len > 0:
checkForDangerousLocations c, body
takeParRi c, n
Expand Down
5 changes: 5 additions & 0 deletions tests/nimony/sysbasics/tstrings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,8 @@ block: # issue #1444
assert substr("abc", 3, 1) == ""
assert substr("abc", 3, 2) == ""
assert substr("abc", 3, 3) == ""

block:
var s = "12234"
var m = prepareMutationAt(s, 1)
assert m == '2'
Loading