Replaced repeatStr() and repeatChar() with more natural versions.

This commit is contained in:
Hans Raaf
2015-02-28 18:14:43 +01:00
parent b870744d5d
commit 3546ff8819
2 changed files with 39 additions and 12 deletions

View File

@@ -499,26 +499,47 @@ proc parseEnum*[T: enum](s: string, default: T): T =
return e
result = default
proc repeatChar*(count: int, c: char = ' '): string {.noSideEffect,
proc repeat*(c: char, count: int): string {.noSideEffect,
rtl, extern: "nsuRepeatChar".} =
## Returns a string of length `count` consisting only of
## the character `c`. You can use this proc to left align strings. Example:
##
## .. code-block:: nim
## proc tabexpand(indent: int, text: string, tabsize: int = 4) =
## echo '\t'.repeat(indent div tabsize), ' '.repeat(indent mod tabsize), text
##
## tabexpand(4, "At four")
## tabexpand(5, "At five")
## tabexpand(6, "At six")
result = newString(count)
for i in 0..count-1: result[i] = c
proc repeat*(s: string, n: int): string {.noSideEffect,
rtl, extern: "nsuRepeatStr".} =
## Returns String `s` concatenated `n` times. Example:
##
## .. code-block:: nim
## echo "+++ STOP ".repeat(4), "+++"
result = newStringOfCap(n * s.len)
for i in 1..n: result.add(s)
template spaces*(n: int): string = repeat(' ',n)
## Returns a String with `n` space characters. You can use this proc
## to left align strings. Example:
##
## .. code-block:: nim
## let
## width = 15
## text1 = "Hello user!"
## text2 = "This is a very long string"
## echo text1 & repeatChar(max(0, width - text1.len)) & "|"
## echo text2 & repeatChar(max(0, width - text2.len)) & "|"
result = newString(count)
for i in 0..count-1: result[i] = c
## echo text1 & spaces(max(0, width - text1.len)) & "|"
## echo text2 & spaces(max(0, width - text2.len)) & "|"
proc repeatStr*(count: int, s: string): string {.noSideEffect,
rtl, extern: "nsuRepeatStr".} =
## Returns `s` concatenated `count` times.
result = newStringOfCap(count*s.len)
for i in 0..count-1: result.add(s)
proc repeatChar*(count: int, c: char = ' '): string {.deprecated.} = repeat(c, count)
## deprecated: use repeat() or spaces()
proc repeatStr*(count: int, s: string): string {.deprecated.} = repeat(s, count)
## deprecated: use repeat(string, count) or string.repeat(count)
proc align*(s: string, count: int, padding = ' '): string {.
noSideEffect, rtl, extern: "nsuAlignString".} =

View File

@@ -46,7 +46,13 @@ assert "/1/2/3".rfind('0') == -1
assert(toHex(100i16, 32) == "00000000000000000000000000000064")
assert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C")
assert(' '.repeat(8)== " ")
assert(" ".repeat(8) == " ")
assert(spaces(8) == " ")
assert(' '.repeat(0) == "")
assert(" ".repeat(0) == "")
assert(spaces(0) == "")
main()
#OUT ha/home/a1xyz/usr/bin