add runnableExamples for prepareStrMutation (#17227)
* Update lib/system.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
This commit is contained in:
parent
5628cd5de1
commit
dfa0a6b4a6
2 changed files with 9 additions and 2 deletions
|
|
@ -3124,5 +3124,12 @@ when not defined(createNimHcr) and not defined(nimscript):
|
||||||
when notJSnotNims and not defined(nimSeqsV2):
|
when notJSnotNims and not defined(nimSeqsV2):
|
||||||
proc prepareStrMutation*(s: var string) {.inline.} =
|
proc prepareStrMutation*(s: var string) {.inline.} =
|
||||||
## String literals (e.g. "abc", etc) in the ARC/ORC mode are "copy on write",
|
## String literals (e.g. "abc", etc) in the ARC/ORC mode are "copy on write",
|
||||||
## therefore you should call `prepareStrMutation` before modifying the strings.
|
## therefore you should call `prepareStrMutation` before modifying the strings
|
||||||
|
## via `addr`.
|
||||||
|
runnableExamples("--gc:arc"):
|
||||||
|
var x = "abc"
|
||||||
|
var y = "defgh"
|
||||||
|
prepareStrMutation(y) # without this, you may get a `SIGBUS` or `SIGSEGV`
|
||||||
|
moveMem(addr y[0], addr x[0], x.len)
|
||||||
|
assert y == "abcgh"
|
||||||
discard
|
discard
|
||||||
|
|
|
||||||
|
|
@ -171,7 +171,7 @@ proc nimPrepareStrMutationV2(s: var NimStringV2) {.compilerRtl, inline.} =
|
||||||
|
|
||||||
proc prepareStrMutation*(s: var string) {.inline.} =
|
proc prepareStrMutation*(s: var string) {.inline.} =
|
||||||
# string literals are "copy on write", so you need to call
|
# string literals are "copy on write", so you need to call
|
||||||
# `prepareStrMutation` before modifying the strings.
|
# `prepareStrMutation` before modifying the strings via `addr`.
|
||||||
{.cast(noSideEffect).}:
|
{.cast(noSideEffect).}:
|
||||||
let s = unsafeAddr s
|
let s = unsafeAddr s
|
||||||
nimPrepareStrMutationV2(cast[ptr NimStringV2](s)[])
|
nimPrepareStrMutationV2(cast[ptr NimStringV2](s)[])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue