summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-06-28 18:57:03 +0200
committerGitHub <noreply@github.com>2016-06-28 18:57:03 +0200
commitd8fda3e4bb533f75473bb7abcbe5f31a02699850 (patch)
tree71f23f86ebdca5b98e7f5d234cc7115b47ec47f3 /lib
parentf7f0cff8b3544694593d36f880d24f7e9f87593f (diff)
parente1b7e38674ca8d3c32c88bd42599eafec14dd953 (diff)
downloadNim-d8fda3e4bb533f75473bb7abcbe5f31a02699850.tar.gz
Merge pull request #4408 from euantorano/feature-strutils-isNilOrEmpty
Adding isNilOrEmpty and isNilOrWhitespace
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/strutils.nim28
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index b6edb834c..7d1b1a3d9 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,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'))