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.
This commit is contained in:
parent
5f4e98bbc7
commit
d932cb1e51
2 changed files with 33 additions and 1 deletions
|
|
@ -160,7 +160,8 @@ proc isSpace*(s: string): bool {.noSideEffect, procvar,
|
||||||
|
|
||||||
result = true
|
result = true
|
||||||
for c in s:
|
for c in s:
|
||||||
result = c.isSpace() and result
|
if not c.isSpace():
|
||||||
|
return false
|
||||||
|
|
||||||
proc isLower*(s: string): bool {.noSideEffect, procvar,
|
proc isLower*(s: string): bool {.noSideEffect, procvar,
|
||||||
rtl, extern: "nsuIsLowerStr".}=
|
rtl, extern: "nsuIsLowerStr".}=
|
||||||
|
|
@ -326,6 +327,15 @@ proc toOctal*(c: char): string {.noSideEffect, rtl, extern: "nsuToOctal".} =
|
||||||
result[i] = chr(val mod 8 + ord('0'))
|
result[i] = chr(val mod 8 + ord('0'))
|
||||||
val = val div 8
|
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,
|
iterator split*(s: string, seps: set[char] = Whitespace,
|
||||||
maxsplit: int = -1): string =
|
maxsplit: int = -1): string =
|
||||||
## Splits the string `s` into substrings using a group of separators.
|
## Splits the string `s` into substrings using a group of separators.
|
||||||
|
|
@ -2156,6 +2166,17 @@ when isMainModule:
|
||||||
doAssert isSpace(" ")
|
doAssert isSpace(" ")
|
||||||
doAssert(not isSpace("ABc \td"))
|
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('a')
|
||||||
doAssert isLower('z')
|
doAssert isLower('z')
|
||||||
doAssert(not isLower('A'))
|
doAssert(not isLower('A'))
|
||||||
|
|
|
||||||
|
|
@ -95,5 +95,16 @@ assert(' '.repeat(0) == "")
|
||||||
assert(" ".repeat(0) == "")
|
assert(" ".repeat(0) == "")
|
||||||
assert(spaces(0) == "")
|
assert(spaces(0) == "")
|
||||||
|
|
||||||
|
assert(isNilOrEmpty(""))
|
||||||
|
assert(isNilOrEmpty(nil))
|
||||||
|
assert(not isNilOrEmpty("test"))
|
||||||
|
assert(not isNilOrEmpty(" "))
|
||||||
|
|
||||||
|
assert(isNilOrWhitespace(""))
|
||||||
|
assert(isNilOrWhitespace(nil))
|
||||||
|
assert(isNilOrWhitespace(" "))
|
||||||
|
assert(isNilOrWhitespace("\t\l \v\r\f"))
|
||||||
|
assert(not isNilOrWhitespace("ABc \td"))
|
||||||
|
|
||||||
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