Merge pull request #3946 from def-/strutils-join
Make strutils.join with with generic openarrays
This commit is contained in:
commit
61f4316c2e
1 changed files with 15 additions and 11 deletions
|
|
@ -882,7 +882,7 @@ proc abbrev*(s: string, possibilities: openArray[string]): int =
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
proc join*(a: openArray[string], sep: string): string {.
|
proc join*(a: openArray[string], sep: string = ""): string {.
|
||||||
noSideEffect, rtl, extern: "nsuJoinSep".} =
|
noSideEffect, rtl, extern: "nsuJoinSep".} =
|
||||||
## Concatenates all strings in `a` separating them with `sep`.
|
## Concatenates all strings in `a` separating them with `sep`.
|
||||||
if len(a) > 0:
|
if len(a) > 0:
|
||||||
|
|
@ -896,16 +896,15 @@ proc join*(a: openArray[string], sep: string): string {.
|
||||||
else:
|
else:
|
||||||
result = ""
|
result = ""
|
||||||
|
|
||||||
proc join*(a: openArray[string]): string {.
|
proc join*[T: not string](a: openArray[T], sep: string = ""): string {.
|
||||||
noSideEffect, rtl, extern: "nsuJoin".} =
|
noSideEffect, rtl.} =
|
||||||
## Concatenates all strings in `a`.
|
## Converts all elements in `a` to strings using `$` and concatenates them
|
||||||
if len(a) > 0:
|
## with `sep`.
|
||||||
var L = 0
|
result = ""
|
||||||
for i in 0..high(a): inc(L, a[i].len)
|
for i, x in a:
|
||||||
result = newStringOfCap(L)
|
if i > 0:
|
||||||
for i in 0..high(a): add(result, a[i])
|
add(result, sep)
|
||||||
else:
|
add(result, $x)
|
||||||
result = ""
|
|
||||||
|
|
||||||
type
|
type
|
||||||
SkipTable = array[char, int]
|
SkipTable = array[char, int]
|
||||||
|
|
@ -1721,3 +1720,8 @@ when isMainModule:
|
||||||
doAssert(not isUpper("AAcc"))
|
doAssert(not isUpper("AAcc"))
|
||||||
doAssert(not isUpper("A#$"))
|
doAssert(not isUpper("A#$"))
|
||||||
doAssert(unescape(r"\x013", "", "") == "\x013")
|
doAssert(unescape(r"\x013", "", "") == "\x013")
|
||||||
|
|
||||||
|
doAssert join(["foo", "bar", "baz"]) == "foobarbaz"
|
||||||
|
doAssert join(@["foo", "bar", "baz"], ", ") == "foo, bar, baz"
|
||||||
|
doAssert join([1, 2, 3]) == "123"
|
||||||
|
doAssert join(@[1, 2, 3], ", ") == "1, 2, 3"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue