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.
This commit is contained in:
Viktor Marosvary 2017-10-24 10:22:18 +02:00 • committed by Andreas Rumpf
commit ce04288d64
2 changed files with 23 additions and 2 deletions

View file

@ -138,7 +138,8 @@ proc isAlphaNumeric*(s: string): bool {.noSideEffect, procvar,
result = true result = true
for c in s: for c in s:
result = c.isAlphaNumeric() and result if not c.isAlphaNumeric():
return false
proc isDigit*(s: string): bool {.noSideEffect, procvar, proc isDigit*(s: string): bool {.noSideEffect, procvar,
rtl, extern: "nsuIsDigitStr".}= rtl, extern: "nsuIsDigitStr".}=
@ -153,7 +154,8 @@ proc isDigit*(s: string): bool {.noSideEffect, procvar,
result = true result = true
for c in s: for c in s:
result = c.isDigit() and result if not c.isDigit():
return false
proc isSpaceAscii*(s: string): bool {.noSideEffect, procvar, proc isSpaceAscii*(s: string): bool {.noSideEffect, procvar,
rtl, extern: "nsuIsSpaceAsciiStr".}= rtl, extern: "nsuIsSpaceAsciiStr".}=

View file

@ -64,6 +64,25 @@ proc testDelete =
delete(s, 0, 0) delete(s, 0, 0)
assert s == "1236789ABCDEFG" 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 = proc testFind =
assert "0123456789ABCDEFGH".find('A') == 10 assert "0123456789ABCDEFGH".find('A') == 10
assert "0123456789ABCDEFGH".find('A', 5) == 10 assert "0123456789ABCDEFGH".find('A', 5) == 10