Skip to content

Commit f9de9c0

Browse files
authored
Merge pull request #1892 from arturo-lang/add-char-support-for-whitespace
[Strings] Add Char support for `whitespace?` + minor fixes
2 parents bebc8f6 + 093ee33 commit f9de9c0

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/helpers/strings.nim

+7-1
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,10 @@ func getSimilar*(s: string, options: seq[string]): seq[string] =
218218
levs.sort(cmper)
219219

220220
if levs.len > 3: result = toSeq(levs.keys)[0..2]
221-
else: result = toSeq(levs.keys)
221+
else: result = toSeq(levs.keys)
222+
223+
func isWhitespace*(s: string): bool =
224+
if s.len == 0: return false
225+
for c in s:
226+
if c notin Whitespace: return false
227+
return true

src/library/Strings.nim

+17-4
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ proc defineLibrary*() =
969969
rule = PrefixPrecedence,
970970
description = "check if given character/string is in ASCII",
971971
args = {
972-
"string": {Char,String}
972+
"string": {String,Char}
973973
},
974974
attrs = NoAttrs,
975975
returns = {Logical},
@@ -1011,6 +1011,9 @@ proc defineLibrary*() =
10111011
lower? "X" ; => false
10121012
lower? "Hello World" ; => false
10131013
lower? "hello" ; => true
1014+
..........
1015+
lower? 'a' ; => true
1016+
lower? 'A' ; => false
10141017
""":
10151018
#=======================================================
10161019
if xKind==Char:
@@ -1175,6 +1178,9 @@ proc defineLibrary*() =
11751178
upper? "x" ; => false
11761179
upper? "Hello World" ; => false
11771180
upper? "HELLO" ; => true
1181+
..........
1182+
upper? 'A' ; => true
1183+
upper? 'a' ; => false
11781184
""":
11791185
#=======================================================
11801186
if xKind==Char:
@@ -1196,18 +1202,25 @@ proc defineLibrary*() =
11961202
rule = PrefixPrecedence,
11971203
description = "check if given string consists only of whitespace",
11981204
args = {
1199-
"string": {String}
1205+
"string": {String,Char}
12001206
},
12011207
attrs = NoAttrs,
12021208
returns = {Logical},
12031209
example = """
12041210
whitespace? "hello" ; => false
12051211
whitespace? " " ; => true
12061212
whitespace? "\n \n" ; => true
1213+
whitespace? "" ; => false
1214+
..........
1215+
whitespace? ' ' ; => true
1216+
whitespace? '\n' ; => true
1217+
whitespace? 'a' ; => false
12071218
""":
12081219
#=======================================================
1209-
push(newLogical(x.s.isEmptyOrWhitespace()))
1210-
1220+
if xKind==Char:
1221+
push(newLogical(x.c.isWhitespace()))
1222+
else:
1223+
push(newLogical(x.s.isWhitespace()))
12111224

12121225
#=======================================
12131226
# Add Library

0 commit comments

Comments
 (0)