From bb570de994cd263b23e01111336a121671e06a3f Mon Sep 17 00:00:00 2001 From: Joey Payne Date: Fri, 9 Oct 2015 22:09:21 -0600 Subject: [PATCH 1/4] Added useful procs to strutils for string testing. Added procs: isAlpha isAlphaNumeric isDigit isSpace isUpper isLower --- lib/pure/strutils.nim | 126 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index eacea72e4..005715a30 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -60,6 +60,132 @@ const ## doAssert "01234".find(invalid) == -1 ## doAssert "01A34".find(invalid) == 2 +proc isAlpha*(c: char): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsAlphaChar".}= + ## Checks whether or not `c` is alphabetical. + ## + ## This checks a-z, A-Z ASCII characters only. + return c in Letters + +proc isAlphaNumeric*(c: char): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsAlphaNumericChar".}= + ## Checks whether or not `c` is alphanumeric. + ## + ## This checks a-z, A-Z, 0-9 ASCII characters only. + return c in Letters and c in Digits + +proc isDigit*(c: char): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsDigitChar".}= + ## Checks whether or not `c` is a number. + ## + ## This checks 0-9 ASCII characters only. + return c in Digits + +proc isSpace*(c: char): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsSpaceChar".}= + ## Checks whether or not `c` is a whitespace character. + return c in Whitespace + +proc isLower*(c: char): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsLowerChar".}= + ## Checks whether or not `c` is a lower case character. + ## + ## This checks ASCII characters only. + return c in {'a'..'z'} + +proc isUpper*(c: char): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsUpperChar".}= + ## Checks whether or not `c` is an upper case character. + ## + ## This checks ASCII characters only. + return c in {'A'..'Z'} + +proc isAlpha*(s: string): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsAlphaStr".}= + ## Checks whether or not `s` is alphabetical. + ## + ## This checks a-z, A-Z ASCII characters only. + ## Returns true if all characters in `s` are + ## alphabetic and there is at least one character + ## in `s`. + if s.len() == 0: + return false + + result = true + for c in s: + result = c.isAlpha() and result + +proc isAlphaNumeric*(s: string): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsAlphaNumericStr".}= + ## Checks whether or not `s` is alphanumeric. + ## + ## This checks a-z, A-Z, 0-9 ASCII characters only. + ## Returns true if all characters in `s` are + ## alpanumeric and there is at least one character + ## in `s`. + if s.len() == 0: + return false + + result = true + for c in s: + result = c.isAlphaNumeric() and result + +proc isDigit*(s: string): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsDigitStr".}= + ## Checks whether or not `s` is a numeric value. + ## + ## This checks 0-9 ASCII characters only. + ## Returns true if all characters in `s` are + ## numeric and there is at least one character + ## in `s`. + if s.len() == 0: + return false + + result = true + for c in s: + result = c.isDigit() and result + +proc isSpace*(s: string): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsSpaceStr".}= + ## Checks whether or not `s` is completely whitespace. + ## + ## Returns true if all characters in `s` are whitespace + ## characters and there is at least one character in `s`. + if s.len() == 0: + return false + + result = true + for c in s: + result = c.isSpace() and result + +proc isLower*(s: string): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsLowerStr".}= + ## Checks whether or not `s` contains all lower case characters. + ## + ## This checks ASCII characters only. + ## Returns true if all characters in `s` are lower case + ## and there is at least one character in `s`. + if s.len() == 0: + return false + + result = true + for c in s: + result = c.isLower() and result + +proc isUpper*(s: string): bool {.noSideEffect, procvar, + rtl, extern: "nsuIsUpperStr".}= + ## Checks whether or not `s` contains all upper case characters. + ## + ## This checks ASCII characters only. + ## Returns true if all characters in `s` are upper case + ## and there is at least one character in `s`. + if s.len() == 0: + return false + + result = true + for c in s: + result = c.isUpper() and result + proc toLower*(c: char): char {.noSideEffect, procvar, rtl, extern: "nsuToLowerChar".} = ## Converts `c` into lower case. From e3bc27ac15ae59630c76f9652db249f05faf8cfb Mon Sep 17 00:00:00 2001 From: Joey Payne Date: Sat, 10 Oct 2015 08:40:31 -0600 Subject: [PATCH 2/4] Changed alphanumeric check to "or" instead of "and". --- lib/pure/strutils.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 005715a30..05862ca87 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -72,7 +72,7 @@ proc isAlphaNumeric*(c: char): bool {.noSideEffect, procvar, ## Checks whether or not `c` is alphanumeric. ## ## This checks a-z, A-Z, 0-9 ASCII characters only. - return c in Letters and c in Digits + return c in Letters or c in Digits proc isDigit*(c: char): bool {.noSideEffect, procvar, rtl, extern: "nsuIsDigitChar".}= From ea9a1e26bee4aad6660d3a3cf027ddefe3312196 Mon Sep 17 00:00:00 2001 From: Joey Payne Date: Sat, 10 Oct 2015 08:55:19 -0600 Subject: [PATCH 3/4] Added tests for string test procs. --- lib/pure/strutils.nim | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 05862ca87..f1f5b8dc8 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -1652,3 +1652,55 @@ when isMainModule: doAssert strip("sfoofoofoos", trailing = false, chars = {'s'}) == "foofoofoos" doAssert " foo\n bar".indent(4, "Q") == "QQQQ foo\nQQQQ bar" + + doAssert isAlpha('r') == true + doAssert isAlpha('A') == true + doAssert isAlpha('$') == false + + doAssert isAlpha("Rasp") == true + doAssert isAlpha("Args") == true + doAssert isAlpha("$Tomato") == false + + doAssert isAlphaNumeric('3') == true + doAssert isAlphaNumeric('R') == true + doAssert isAlphaNumeric('!') == false + + doAssert isAlphaNumeric("34ABc") == true + doAssert isAlphaNumeric("Rad") == true + doAssert isAlphaNumeric("1234") == true + doAssert isAlphaNumeric("@nose") == false + + doAssert isDigit('3') == true + doAssert isDigit('a') == false + doAssert isDigit('%') == false + + doAssert isDigit("12533") == true + doAssert isDigit("12.33") == false + doAssert isDigit("A45b") == false + + doAssert isSpace('\t') == true + doAssert isSpace('\l') == true + doAssert isSpace('A') == false + + doAssert isSpace("\t\l \v\r\f") == true + doAssert isSpace(" ") == true + doAssert isSpace("ABc \td") == false + + doAssert isLower('a') == true + doAssert isLower('z') == true + doAssert isLower('A') == false + doAssert isLower('5') == false + doAssert isLower('&') == false + + doAssert isLower("abcd") == true + doAssert isLower("abCD") == false + doAssert isLower("33aa") == false + + doAssert isUpper('A') == true + doAssert isUpper('b') == false + doAssert isUpper('5') == false + doAssert isUpper('%') == false + + doAssert isUpper("ABC") == true + doAssert isUpper("AAcc") == false + doAssert isUpper("A#$") == false From 2d256a4489a214ee10407890783d329d5f27e374 Mon Sep 17 00:00:00 2001 From: Joey Payne Date: Sat, 10 Oct 2015 09:13:00 -0600 Subject: [PATCH 4/4] Made strutils string test procs less redundant. --- lib/pure/strutils.nim | 80 +++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index f1f5b8dc8..a78fed4b9 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -1653,54 +1653,54 @@ when isMainModule: doAssert " foo\n bar".indent(4, "Q") == "QQQQ foo\nQQQQ bar" - doAssert isAlpha('r') == true - doAssert isAlpha('A') == true - doAssert isAlpha('$') == false + doAssert isAlpha('r') + doAssert isAlpha('A') + doAssert(not isAlpha('$')) - doAssert isAlpha("Rasp") == true - doAssert isAlpha("Args") == true - doAssert isAlpha("$Tomato") == false + doAssert isAlpha("Rasp") + doAssert isAlpha("Args") + doAssert(not isAlpha("$Tomato")) - doAssert isAlphaNumeric('3') == true - doAssert isAlphaNumeric('R') == true - doAssert isAlphaNumeric('!') == false + doAssert isAlphaNumeric('3') + doAssert isAlphaNumeric('R') + doAssert(not isAlphaNumeric('!')) - doAssert isAlphaNumeric("34ABc") == true - doAssert isAlphaNumeric("Rad") == true - doAssert isAlphaNumeric("1234") == true - doAssert isAlphaNumeric("@nose") == false + doAssert isAlphaNumeric("34ABc") + doAssert isAlphaNumeric("Rad") + doAssert isAlphaNumeric("1234") + doAssert(not isAlphaNumeric("@nose")) - doAssert isDigit('3') == true - doAssert isDigit('a') == false - doAssert isDigit('%') == false + doAssert isDigit('3') + doAssert(not isDigit('a')) + doAssert(not isDigit('%')) - doAssert isDigit("12533") == true - doAssert isDigit("12.33") == false - doAssert isDigit("A45b") == false + doAssert isDigit("12533") + doAssert(not isDigit("12.33")) + doAssert(not isDigit("A45b")) - doAssert isSpace('\t') == true - doAssert isSpace('\l') == true - doAssert isSpace('A') == false + doAssert isSpace('\t') + doAssert isSpace('\l') + doAssert(not isSpace('A')) - doAssert isSpace("\t\l \v\r\f") == true - doAssert isSpace(" ") == true - doAssert isSpace("ABc \td") == false + doAssert isSpace("\t\l \v\r\f") + doAssert isSpace(" ") + doAssert(not isSpace("ABc \td")) - doAssert isLower('a') == true - doAssert isLower('z') == true - doAssert isLower('A') == false - doAssert isLower('5') == false - doAssert isLower('&') == false + doAssert isLower('a') + doAssert isLower('z') + doAssert(not isLower('A')) + doAssert(not isLower('5')) + doAssert(not isLower('&')) - doAssert isLower("abcd") == true - doAssert isLower("abCD") == false - doAssert isLower("33aa") == false + doAssert isLower("abcd") + doAssert(not isLower("abCD")) + doAssert(not isLower("33aa")) - doAssert isUpper('A') == true - doAssert isUpper('b') == false - doAssert isUpper('5') == false - doAssert isUpper('%') == false + doAssert isUpper('A') + doAssert(not isUpper('b')) + doAssert(not isUpper('5')) + doAssert(not isUpper('%')) - doAssert isUpper("ABC") == true - doAssert isUpper("AAcc") == false - doAssert isUpper("A#$") == false + doAssert isUpper("ABC") + doAssert(not isUpper("AAcc")) + doAssert(not isUpper("A#$"))