Make strutils.join with with generic openarrays

This commit is contained in:
def 2016-03-06 17:53:02 +01:00
commit 78d1b282ca

View file

@ -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"