From d932cb1e515a52a8afde33feaf36ad5567e4e7df Mon Sep 17 00:00:00 2001 From: Euan Date: Fri, 24 Jun 2016 10:18:46 +0100 Subject: 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. --- lib/pure/strutils.nim | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'lib/pure') diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index b6edb834c..082f02c07 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,15 @@ 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".} = 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, maxsplit: int = -1): string = ## Splits the string `s` into substrings using a group of separators. @@ -2156,6 +2166,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')) -- cgit 1.4.1-2-gfad0 From 746357404cc2ec93407c04cc1720e17032fc0efd Mon Sep 17 00:00:00 2001 From: Euan Date: Fri, 24 Jun 2016 10:50:26 +0100 Subject: Fixing isNilOrWhitespace for empty/nil strings. `isSpace` returns false for an empty string, which is the opposite of this method. --- lib/pure/strutils.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/pure') diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 082f02c07..51694e6ad 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -331,7 +331,7 @@ proc isNilOrEmpty*(s: string): bool {.noSideEffect, procvar, rtl, extern: "nsuIs ## Checks if `s` is nil or empty. result = len(s) == 0 -proc isNilOrWhitespace*(s: string): bool {.noSideEffect, procvar, rtl, extern: "nsuIsNilOrWhitespace".} = isSpace(s) +proc isNilOrWhitespace*(s: string): bool {.noSideEffect, procvar, rtl, extern: "nsuIsNilOrWhitespace".} = ## Checks if `s` is nil or consists entirely of whitespace characters. ## ## This is an alias to `isSpace`. -- cgit 1.4.1-2-gfad0 From f84911364a8cb6c8737367a2b34a1125f9f02e39 Mon Sep 17 00:00:00 2001 From: Euan Date: Fri, 24 Jun 2016 10:51:13 +0100 Subject: Fixing isNilOrWhitespace to handle empty/nil. --- lib/pure/strutils.nim | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 51694e6ad..7d1b1a3d9 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -333,8 +333,13 @@ proc isNilOrEmpty*(s: string): bool {.noSideEffect, procvar, rtl, extern: "nsuIs proc isNilOrWhitespace*(s: string): bool {.noSideEffect, procvar, rtl, extern: "nsuIsNilOrWhitespace".} = ## Checks if `s` is nil or consists entirely of whitespace characters. - ## - ## This is an alias to `isSpace`. + 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 = -- cgit 1.4.1-2-gfad0