diff options
author | Grzegorz Adam Hankiewicz <gradha@imap.cc> | 2013-07-05 21:37:30 +0200 |
---|---|---|
committer | Grzegorz Adam Hankiewicz <gradha@imap.cc> | 2013-07-05 21:37:30 +0200 |
commit | 65b899030d819c3b91f23d3085b8e6fdf1a133bd (patch) | |
tree | eaecff27b9d5a4b8e5c2d28badb9c6589edfa442 /lib/pure | |
parent | ebd45529c169dbdc6fca919230f5f7f2fbf9b760 (diff) | |
download | Nim-65b899030d819c3b91f23d3085b8e6fdf1a133bd.tar.gz |
Extends align proc with default padding parameter.
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/strutils.nim | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 6f34b1996..e76bfab8b 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -440,16 +440,23 @@ proc repeatStr*(count: int, s: string): string {.noSideEffect, result = newStringOfCap(count*s.len) for i in 0..count-1: result.add(s) -proc align*(s: string, count: int): string {. +proc align*(s: string, count: int, padding = ' '): string {. noSideEffect, rtl, extern: "nsuAlignString".} = - ## Aligns a string `s` with spaces, so that is of length `count`. Spaces are - ## added before `s` resulting in right alignment. If ``s.len >= count``, no - ## spaces are added and `s` is returned unchanged. If you need to left align - ## a string use the repeatChar proc. + ## Aligns a string `s` with `padding`, so that is of length `count`. + ## `padding` characters (by default spaces) are added before `s` resulting in + ## right alignment. If ``s.len >= count``, no spaces are added and `s` is + ## returned unchanged. If you need to left align a string use the + ## ``repeatChar`` proc. Example: + ## + ## .. code-block:: nimrod + ## assert align("abc", 4) == " abc" + ## assert align("a", 0) == "a" + ## assert align("1232", 6) == " 1232" + ## assert align("1232", 6, '#') == "##1232" if s.len < count: result = newString(count) var spaces = count - s.len - for i in 0..spaces-1: result[i] = ' ' + for i in 0..spaces-1: result[i] = padding for i in spaces..count-1: result[i] = s[i-spaces] else: result = s @@ -1211,6 +1218,7 @@ when isMainModule: doAssert align("abc", 4) == " abc" doAssert align("a", 0) == "a" doAssert align("1232", 6) == " 1232" + doAssert align("1232", 6, '#') == "##1232" echo wordWrap(""" this is a long text -- muchlongerthan10chars and here it goes""", 10, false) doAssert formatBiggestFloat(0.00000000001, ffDecimal, 11) == "0.00000000001" |