diff options
author | Euan <euantorano@gmail.com> | 2016-06-24 10:18:46 +0100 |
---|---|---|
committer | Euan <euantorano@gmail.com> | 2016-06-24 10:18:46 +0100 |
commit | d932cb1e515a52a8afde33feaf36ad5567e4e7df (patch) | |
tree | 2df2f79922cedeb7f87e97fe7677bd8fa75e4c69 /lib | |
parent | 5f4e98bbc7fb8bdd6e60579b26c0001f33ce4f9c (diff) | |
download | Nim-d932cb1e515a52a8afde33feaf36ad5567e4e7df.tar.gz |
Adding isNilOrEmpty and isNilOrWhitespace
As discussed in #4184, this patch adds `isNilOrEmpty` and `isNilOrWhitespace` to `strutils`. It also modifies the existing `isSpace` proc slightly to exit early rather than looping through all characters in a string.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/strutils.nim | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index b6edb834c..082f02c07 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -160,7 +160,8 @@ proc isSpace*(s: string): bool {.noSideEffect, procvar, result = true for c in s: - result = c.isSpace() and result + if not c.isSpace(): + return false proc isLower*(s: string): bool {.noSideEffect, procvar, rtl, extern: "nsuIsLowerStr".}= @@ -326,6 +327,15 @@ proc toOctal*(c: char): string {.noSideEffect, rtl, extern: "nsuToOctal".} = result[i] = chr(val mod 8 + ord('0')) val = val div 8 +proc isNilOrEmpty*(s: string): bool {.noSideEffect, procvar, rtl, extern: "nsuIsNilOrEmpty".} = + ## Checks if `s` is nil or empty. + result = len(s) == 0 + +proc isNilOrWhitespace*(s: string): bool {.noSideEffect, procvar, rtl, extern: "nsuIsNilOrWhitespace".} = isSpace(s) + ## Checks if `s` is nil or consists entirely of whitespace characters. + ## + ## This is an alias to `isSpace`. + iterator split*(s: string, seps: set[char] = Whitespace, maxsplit: int = -1): string = ## Splits the string `s` into substrings using a group of separators. @@ -2156,6 +2166,17 @@ when isMainModule: doAssert isSpace(" ") doAssert(not isSpace("ABc \td")) + doAssert(isNilOrEmpty("")) + doAssert(isNilOrEmpty(nil)) + doAssert(not isNilOrEmpty("test")) + doAssert(not isNilOrEmpty(" ")) + + doAssert(isNilOrWhitespace("")) + doAssert(isNilOrWhitespace(nil)) + doAssert(isNilOrWhitespace(" ")) + doAssert(isNilOrWhitespace("\t\l \v\r\f")) + doAssert(not isNilOrWhitespace("ABc \td")) + doAssert isLower('a') doAssert isLower('z') doAssert(not isLower('A')) |