Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Strings] Add Char support for whitespace? + minor fixes #1892

Merged
merged 7 commits into from
Feb 22, 2025
8 changes: 7 additions & 1 deletion src/helpers/strings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,10 @@ func getSimilar*(s: string, options: seq[string]): seq[string] =
levs.sort(cmper)

if levs.len > 3: result = toSeq(levs.keys)[0..2]
else: result = toSeq(levs.keys)
else: result = toSeq(levs.keys)

func isWhitespace*(s: string): bool =
if s.len == 0: return false
for c in s:
if c notin Whitespace: return false
return true
21 changes: 17 additions & 4 deletions src/library/Strings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ proc defineLibrary*() =
rule = PrefixPrecedence,
description = "check if given character/string is in ASCII",
args = {
"string": {Char,String}
"string": {String,Char}
},
attrs = NoAttrs,
returns = {Logical},
Expand Down Expand Up @@ -1011,6 +1011,9 @@ proc defineLibrary*() =
lower? "X" ; => false
lower? "Hello World" ; => false
lower? "hello" ; => true
..........
lower? 'a' ; => true
lower? 'A' ; => false
""":
#=======================================================
if xKind==Char:
Expand Down Expand Up @@ -1175,6 +1178,9 @@ proc defineLibrary*() =
upper? "x" ; => false
upper? "Hello World" ; => false
upper? "HELLO" ; => true
..........
upper? 'A' ; => true
upper? 'a' ; => false
""":
#=======================================================
if xKind==Char:
Expand All @@ -1196,18 +1202,25 @@ proc defineLibrary*() =
rule = PrefixPrecedence,
description = "check if given string consists only of whitespace",
args = {
"string": {String}
"string": {String,Char}
},
attrs = NoAttrs,
returns = {Logical},
example = """
whitespace? "hello" ; => false
whitespace? " " ; => true
whitespace? "\n \n" ; => true
whitespace? "" ; => false
..........
whitespace? ' ' ; => true
whitespace? '\n' ; => true
whitespace? 'a' ; => false
""":
#=======================================================
push(newLogical(x.s.isEmptyOrWhitespace()))

if xKind==Char:
push(newLogical(x.c.isWhitespace()))
else:
push(newLogical(x.s.isWhitespace()))

#=======================================
# Add Library
Expand Down
Loading