mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-09 21:28:13 +00:00
add overload add(a: var string, b: openArray[char]) (#15951)
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
@@ -8,9 +8,25 @@
|
||||
#
|
||||
|
||||
## This module provides some high performance string operations.
|
||||
##
|
||||
## Experimental API, subject to change.
|
||||
|
||||
const whitespaces = {' ', '\t', '\v', '\r', '\l', '\f'}
|
||||
|
||||
proc add*(x: var string, y: openArray[char]) =
|
||||
## Concatenates `x` and `y` in place. `y` must not overlap with `x` to
|
||||
## allow future `memcpy` optimizations.
|
||||
# Use `{.noalias.}` ?
|
||||
let n = x.len
|
||||
x.setLen n + y.len
|
||||
# pending https://github.com/nim-lang/Nim/issues/14655#issuecomment-643671397
|
||||
# use x.setLen(n + y.len, isInit = false)
|
||||
var i = 0
|
||||
while i < y.len:
|
||||
x[n + i] = y[i]
|
||||
i.inc
|
||||
# xxx use `nimCopyMem(x[n].addr, y[0].addr, y.len)` after some refactoring
|
||||
|
||||
func stripSlice(s: openArray[char], leading = true, trailing = true, chars: set[char] = whitespaces): Slice[int] =
|
||||
## Returns the slice range of `s` which is stripped `chars`.
|
||||
runnableExamples:
|
||||
|
||||
@@ -1069,15 +1069,16 @@ proc add*(x: var string, y: char) {.magic: "AppendStrCh", noSideEffect.}
|
||||
## tmp.add('a')
|
||||
## tmp.add('b')
|
||||
## assert(tmp == "ab")
|
||||
proc add*(x: var string, y: string) {.magic: "AppendStrStr", noSideEffect.}
|
||||
|
||||
proc add*(x: var string, y: string) {.magic: "AppendStrStr", noSideEffect.} =
|
||||
## Concatenates `x` and `y` in place.
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## var tmp = ""
|
||||
## tmp.add("ab")
|
||||
## tmp.add("cd")
|
||||
## assert(tmp == "abcd")
|
||||
|
||||
## See also `strbasics.add`.
|
||||
runnableExamples:
|
||||
var tmp = ""
|
||||
tmp.add("ab")
|
||||
tmp.add("cd")
|
||||
assert tmp == "abcd"
|
||||
|
||||
type
|
||||
Endianness* = enum ## Type describing the endianness of a processor.
|
||||
|
||||
Reference in New Issue
Block a user