From f7198eabc03263cbc1256f510bd6f6e03486cc39 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 1 Mar 2021 11:32:24 +0100 Subject: [PATCH] fixes #17173 --- changelog.md | 5 ++++- lib/std/strbasics.nim | 4 +++- lib/system.nim | 3 +++ lib/system/strs_v2.nim | 5 +++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 8c6d2b3c4f5f8..3f438905e8d93 100644 --- a/changelog.md +++ b/changelog.md @@ -130,7 +130,7 @@ with other backends. see #9125. Use `-d:nimLegacyJsRound` for previous behavior. - Deprecated `any`. See https://github.com/nim-lang/RFCs/issues/281 -- Added `std/sysrand` module to get random numbers from a secure source +- Added `std/sysrand` module to get random numbers from a secure source provided by the operating system. - Added optional `options` argument to `copyFile`, `copyFileToDir`, and @@ -174,6 +174,9 @@ provided by the operating system. of termination. - Added `strip` and `setSlice` to `std/strbasics`. +- Added `system.prepareStrMutation` for better support of low + level `moveMem`, `copyMem` operations for Orc's copy-on-write string + implementation. - Added to `wrapnils` an option-like API via `??.`, `isSome`, `get`. diff --git a/lib/std/strbasics.nim b/lib/std/strbasics.nim index ce061adca59ac..c9e85e651ea99 100644 --- a/lib/std/strbasics.nim +++ b/lib/std/strbasics.nim @@ -64,11 +64,13 @@ func setSlice*(s: var string, slice: Slice[int]) = when not declared(moveMem): impl() else: + when defined(gcDestructors): + prepareStrMutation(s) moveMem(addr s[0], addr s[first], last - first + 1) s.setLen(last - first + 1) func strip*(a: var string, leading = true, trailing = true, chars: set[char] = whitespaces) {.inline.} = - ## Inplace version of `strip`. Strips leading or + ## Inplace version of `strip`. Strips leading or ## trailing `chars` (default: whitespace characters). ## ## If `leading` is true (default), leading `chars` are stripped. diff --git a/lib/system.nim b/lib/system.nim index b2d46ffb89910..2b601b62f2096 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -3126,3 +3126,6 @@ export io when not defined(createNimHcr) and not defined(nimscript): include nimhcr + +when not defined(gcDestructors): + proc prepareStrMutation*(s: var string) {.inline.} = discard diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim index fe117997bf0a5..f725c106332c9 100644 --- a/lib/system/strs_v2.nim +++ b/lib/system/strs_v2.nim @@ -168,3 +168,8 @@ proc nimPrepareStrMutationImpl(s: var NimStringV2) = proc nimPrepareStrMutationV2(s: var NimStringV2) {.compilerRtl, inline.} = if s.p != nil and (s.p.cap and strlitFlag) == strlitFlag: nimPrepareStrMutationImpl(s) + +proc prepareStrMutation*(s: var string) {.inline.} = + {.cast(noSideEffect).}: + let s = unsafeAddr s + nimPrepareStrMutationV2(cast[ptr NimStringV2](s)[])