Replaced repeatStr() and repeatChar() with more natural versions.
This commit is contained in:
parent
b870744d5d
commit
3546ff8819
2 changed files with 39 additions and 12 deletions
|
|
@ -499,26 +499,47 @@ proc parseEnum*[T: enum](s: string, default: T): T =
|
||||||
return e
|
return e
|
||||||
result = default
|
result = default
|
||||||
|
|
||||||
proc repeatChar*(count: int, c: char = ' '): string {.noSideEffect,
|
proc repeat*(c: char, count: int): string {.noSideEffect,
|
||||||
rtl, extern: "nsuRepeatChar".} =
|
rtl, extern: "nsuRepeatChar".} =
|
||||||
## Returns a string of length `count` consisting only of
|
## Returns a string of length `count` consisting only of
|
||||||
## the character `c`. You can use this proc to left align strings. Example:
|
## the character `c`. You can use this proc to left align strings. Example:
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. 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
|
## let
|
||||||
## width = 15
|
## width = 15
|
||||||
## text1 = "Hello user!"
|
## text1 = "Hello user!"
|
||||||
## text2 = "This is a very long string"
|
## text2 = "This is a very long string"
|
||||||
## echo text1 & repeatChar(max(0, width - text1.len)) & "|"
|
## echo text1 & spaces(max(0, width - text1.len)) & "|"
|
||||||
## echo text2 & repeatChar(max(0, width - text2.len)) & "|"
|
## echo text2 & spaces(max(0, width - text2.len)) & "|"
|
||||||
result = newString(count)
|
|
||||||
for i in 0..count-1: result[i] = c
|
|
||||||
|
|
||||||
proc repeatStr*(count: int, s: string): string {.noSideEffect,
|
proc repeatChar*(count: int, c: char = ' '): string {.deprecated.} = repeat(c, count)
|
||||||
rtl, extern: "nsuRepeatStr".} =
|
## deprecated: use repeat() or spaces()
|
||||||
## Returns `s` concatenated `count` times.
|
|
||||||
result = newStringOfCap(count*s.len)
|
proc repeatStr*(count: int, s: string): string {.deprecated.} = repeat(s, count)
|
||||||
for i in 0..count-1: result.add(s)
|
## deprecated: use repeat(string, count) or string.repeat(count)
|
||||||
|
|
||||||
proc align*(s: string, count: int, padding = ' '): string {.
|
proc align*(s: string, count: int, padding = ' '): string {.
|
||||||
noSideEffect, rtl, extern: "nsuAlignString".} =
|
noSideEffect, rtl, extern: "nsuAlignString".} =
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,13 @@ assert "/1/2/3".rfind('0') == -1
|
||||||
assert(toHex(100i16, 32) == "00000000000000000000000000000064")
|
assert(toHex(100i16, 32) == "00000000000000000000000000000064")
|
||||||
assert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C")
|
assert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C")
|
||||||
|
|
||||||
|
assert(' '.repeat(8)== " ")
|
||||||
|
assert(" ".repeat(8) == " ")
|
||||||
|
assert(spaces(8) == " ")
|
||||||
|
|
||||||
|
assert(' '.repeat(0) == "")
|
||||||
|
assert(" ".repeat(0) == "")
|
||||||
|
assert(spaces(0) == "")
|
||||||
|
|
||||||
main()
|
main()
|
||||||
#OUT ha/home/a1xyz/usr/bin
|
#OUT ha/home/a1xyz/usr/bin
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue