rename prepareStrMutation to prepareMutation (#17235)

* remove unnecessary when statement

* remove outdated codes

* rename prepareStrMutation to prepareMutation
This commit is contained in:
flywind 2021-03-03 08:08:52 +08:00 • committed by GitHub
commit a04c9d1f62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 10 deletions

View file

@ -676,25 +676,25 @@ moveMem(addr y[0], addr x[0], 3)
``` ```
The program fails because we need to prepare a fresh copy for the variable `y`. The program fails because we need to prepare a fresh copy for the variable `y`.
`prepareStrMutation` should be called before the address operation. `prepareMutation` should be called before the address operation.
```nim ```nim
var x = "abc" var x = "abc"
var y = x var y = x
prepareStrMutation(y) prepareMutation(y)
moveMem(addr y[0], addr x[0], 3) moveMem(addr y[0], addr x[0], 3)
assert y == "abc" assert y == "abc"
``` ```
Now `prepareStrMutation` solves the problem. Now `prepareMutation` solves the problem.
It manually creates a fresh copy and makes the variable `y` mutable. It manually creates a fresh copy and makes the variable `y` mutable.
```nim ```nim
var x = "abc" var x = "abc"
var y = x var y = x
prepareStrMutation(y) prepareMutation(y)
moveMem(addr y[0], addr x[0], 3) moveMem(addr y[0], addr x[0], 3)
moveMem(addr y[0], addr x[0], 3) moveMem(addr y[0], addr x[0], 3)
moveMem(addr y[0], addr x[0], 3) moveMem(addr y[0], addr x[0], 3)

View file

@ -81,7 +81,7 @@ func setSlice*(s: var string, slice: Slice[int]) =
impl() impl()
else: else:
when defined(nimSeqsV2): when defined(nimSeqsV2):
prepareStrMutation(s) prepareMutation(s)
moveMem(addr s[0], addr s[first], last - first + 1) moveMem(addr s[0], addr s[first], last - first + 1)
s.setLen(last - first + 1) s.setLen(last - first + 1)

View file

@ -3122,14 +3122,14 @@ when not defined(createNimHcr) and not defined(nimscript):
include nimhcr include nimhcr
when notJSnotNims and not defined(nimSeqsV2): when notJSnotNims and not defined(nimSeqsV2):
proc prepareStrMutation*(s: var string) {.inline.} = proc prepareMutation*(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 `prepareMutation` before modifying the strings
## via `addr`. ## via `addr`.
runnableExamples("--gc:arc"): runnableExamples("--gc:arc"):
var x = "abc" var x = "abc"
var y = "defgh" var y = "defgh"
prepareStrMutation(y) # without this, you may get a `SIGBUS` or `SIGSEGV` prepareMutation(y) # without this, you may get a `SIGBUS` or `SIGSEGV`
moveMem(addr y[0], addr x[0], x.len) moveMem(addr y[0], addr x[0], x.len)
assert y == "abcgh" assert y == "abcgh"
discard discard

View file

@ -169,9 +169,9 @@ proc nimPrepareStrMutationV2(s: var NimStringV2) {.compilerRtl, inline.} =
if s.p != nil and (s.p.cap and strlitFlag) == strlitFlag: if s.p != nil and (s.p.cap and strlitFlag) == strlitFlag:
nimPrepareStrMutationImpl(s) nimPrepareStrMutationImpl(s)
proc prepareStrMutation*(s: var string) {.inline.} = proc prepareMutation*(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 via `addr`. # `prepareMutation` 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)[])