add overload add(a: var string, b: openArray[char]) (#15951)
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
parent
dd6b0f81ef
commit
0cb02fbbee
5 changed files with 152 additions and 122 deletions
|
|
@ -173,9 +173,10 @@ provided by the operating system.
|
|||
dumping (on select signals) and notifying the parent process about the cause
|
||||
of termination.
|
||||
|
||||
- `hashes.hash` now supports `object`, but can be overloaded.
|
||||
- Added `std/strbasics` for high performance string operations.
|
||||
Added `strip`, `setSlice`, `add(a: var string, b: openArray[char])`.
|
||||
|
||||
- Added `strip` and `setSlice` to `std/strbasics`.
|
||||
- `hashes.hash` now supports `object`, but can be overloaded.
|
||||
|
||||
- Added to `wrapnils` an option-like API via `??.`, `isSome`, `get`.
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
discard """
|
||||
targets: "c cpp js"
|
||||
matrix: "--gc:refc; --gc:arc"
|
||||
"""
|
||||
|
||||
import std/[strbasics, sugar]
|
||||
|
||||
|
||||
proc teststrip() =
|
||||
proc main() =
|
||||
when not defined(gcArc): # pending bug #17173
|
||||
block: # strip
|
||||
var a = " vhellov "
|
||||
strip(a)
|
||||
doAssert a == "vhellov"
|
||||
|
|
@ -105,8 +107,7 @@ proc teststrip() =
|
|||
doAssert a.dup(strip(chars = {'x', 'i'})) == " "
|
||||
doAssert a.dup(strip(chars = {'x', 'i', ' '})).len == 0
|
||||
|
||||
|
||||
block:
|
||||
block: # setSlice
|
||||
var a = "Hello, Nim!"
|
||||
doassert a.dup(setSlice(7 .. 9)) == "Nim"
|
||||
doAssert a.dup(setSlice(0 .. 0)) == "H"
|
||||
|
|
@ -115,12 +116,24 @@ proc teststrip() =
|
|||
doAssert a.dup(setSlice(1 .. 0)).len == 0
|
||||
doAssert a.dup(setSlice(20 .. -1)).len == 0
|
||||
|
||||
|
||||
doAssertRaises(AssertionDefect):
|
||||
discard a.dup(setSlice(-1 .. 1))
|
||||
|
||||
doAssertRaises(AssertionDefect):
|
||||
discard a.dup(setSlice(1 .. 11))
|
||||
|
||||
static: teststrip()
|
||||
teststrip()
|
||||
block: # add
|
||||
var a0 = "hi"
|
||||
var b0 = "foobar"
|
||||
when nimvm:
|
||||
discard # pending bug #15952
|
||||
else:
|
||||
a0.add b0.toOpenArray(1,3)
|
||||
doAssert a0 == "hioob"
|
||||
proc fn(c: openArray[char]): string =
|
||||
result.add c
|
||||
doAssert fn("def") == "def"
|
||||
doAssert fn(['d','\0', 'f'])[2] == 'f'
|
||||
|
||||
static: main()
|
||||
main()
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ proc set_all[T](s: var openArray[T]; val: T) =
|
|||
for i in 0..<s.len:
|
||||
s[i] = val
|
||||
|
||||
proc test() =
|
||||
proc main() =
|
||||
var a0 = "hello_world"
|
||||
var a1 = [1,2,3,4,5,6,7,8,9]
|
||||
var a2 = @[1,2,3,4,5,6,7,8,9]
|
||||
|
|
@ -29,6 +29,5 @@ const constval1 = [1,2,3,4].map(proc(x: int): int = x)
|
|||
doAssert("hello".map(proc(x: char): char = x) == constval0)
|
||||
doAssert([1,2,3,4].map(proc(x: int): int = x) == constval1)
|
||||
|
||||
test()
|
||||
static:
|
||||
test()
|
||||
static: main()
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue