summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorViktor Marosvary <viktor.marosvary@gmail.com>2017-10-24 10:22:18 +0200
committerAndreas Rumpf <rumpf_a@web.de>2017-10-24 10:22:18 +0200
commitce04288d6492c36f5021198d9d7fe8a6932959e4 (patch)
tree54ed852e7564b3052e3084de55bc8a4a19a25cb1
parentc05124485962620d8d1198d3914c1e1b7fe1cb1f (diff)
downloadNim-ce04288d6492c36f5021198d9d7fe8a6932959e4.tar.gz
isAlphaNumberic and isDigit improvement + tests (#6579)
if we encounter a character that does not satisfy the proc, we return immediately, without continuing to loop over the rest of the chars in the string.
-rw-r--r--lib/pure/strutils.nim6
-rw-r--r--tests/stdlib/tstrutil.nim19
2 files changed, 23 insertions, 2 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index 3da8094f5..1f56704f7 100644
--- a/lib/pure/strutils.nim
+++ b/lib/pure/strutils.nim
@@ -138,7 +138,8 @@ proc isAlphaNumeric*(s: string): bool {.noSideEffect, procvar,
 
   result = true
   for c in s:
-    result = c.isAlphaNumeric() and result
+    if not c.isAlphaNumeric():
+      return false
 
 proc isDigit*(s: string): bool {.noSideEffect, procvar,
   rtl, extern: "nsuIsDigitStr".}=
@@ -153,7 +154,8 @@ proc isDigit*(s: string): bool {.noSideEffect, procvar,
 
   result = true
   for c in s:
-    result = c.isDigit() and result
+    if not c.isDigit():
+      return false
 
 proc isSpaceAscii*(s: string): bool {.noSideEffect, procvar,
   rtl, extern: "nsuIsSpaceAsciiStr".}=
diff --git a/tests/stdlib/tstrutil.nim b/tests/stdlib/tstrutil.nim
index fef1b38c2..57968df13 100644
--- a/tests/stdlib/tstrutil.nim
+++ b/tests/stdlib/tstrutil.nim
@@ -64,6 +64,25 @@ proc testDelete =
   delete(s, 0, 0)
   assert s == "1236789ABCDEFG"
 
+
+proc testIsAlphaNumeric =
+  assert isAlphaNumeric("abcdABC1234") == true
+  assert isAlphaNumeric("a") == true
+  assert isAlphaNumeric("abcABC?1234") == false
+  assert isAlphaNumeric("abcABC 1234") == false
+  assert isAlphaNumeric(".") == false
+
+testIsAlphaNumeric()
+
+proc testIsDigit =
+  assert isDigit("1") == true
+  assert isDigit("1234") == true
+  assert isDigit("abcABC?1234") == false
+  assert isDigit(".") == false
+  assert isDigit(":") == false
+
+testIsDigit()
+
 proc testFind =
   assert "0123456789ABCDEFGH".find('A') == 10
   assert "0123456789ABCDEFGH".find('A', 5) == 10