Merge pull request #4408 from euantorano/feature-strutils-isNilOrEmpty

Adding isNilOrEmpty and isNilOrWhitespace
This commit is contained in:
Andreas Rumpf
2016-06-28 18:57:03 +02:00
committed by GitHub

View File

@@ -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,20 @@ 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".} =
## Checks if `s` is nil or consists entirely of whitespace characters.
if len(s) == 0:
return true
result = true
for c in s:
if not c.isSpace():
return false
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 +2171,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'))