Skip to content

Commit

Permalink
Merge pull request #1876 from RickBarretto/split-accepts-char-issue-1874
Browse files Browse the repository at this point in the history
[Collections/split] implement split.by support for `:char`
  • Loading branch information
drkameleon authored Feb 7, 2025
2 parents 1c12b00 + 9a69802 commit a285640
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/library/Collections.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,7 @@ proc defineLibrary*() =
attrs = {
"words" : ({Logical}, "split string by whitespace"),
"lines" : ({Logical}, "split string by lines"),
"by" : ({String, Regex, Block}, "split using given separator"),
"by" : ({String, Regex, Char, Block}, "split using given separator"),
"at" : ({Integer}, "split collection at given position"),
"every" : ({Integer}, "split collection every *n* elements"),
"path" : ({Logical}, "split path components in string")
Expand Down Expand Up @@ -2269,6 +2269,8 @@ proc defineLibrary*() =
elif checkAttr("by"):
if aBy.kind == String:
SetInPlaceAny(newStringBlock(InPlaced.s.split(aBy.s)))
elif aBy.kind == Char:
SetInPlaceAny(newStringBlock(InPlaced.s.split(aBy.c)))
elif aBy.kind == Regex:
SetInPlaceAny(newStringBlock(InPlaced.s.split(aBy.rx)))
else:
Expand Down Expand Up @@ -2330,6 +2332,8 @@ proc defineLibrary*() =
elif checkAttr("by"):
if aBy.kind == String:
push(newStringBlock(x.s.split(aBy.s)))
elif aBy.kind == Char:
push(newStringBlock(x.s.split(aBy.c)))
elif aBy.kind == Regex:
push(newStringBlock(x.s.split(aBy.rx)))
else:
Expand Down
4 changes: 4 additions & 0 deletions tests/unittests/lib.collections.art
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,10 @@ do [
= split.by: {/,/} "Arturo, Nim, Ruby, Python, C, CoffeeScript"
passed

ensure -> ["Arturo" " Nim" " Ruby" " Python", " C", " CoffeeScript"]
= split.by: ',' "Arturo, Nim, Ruby, Python, C, CoffeeScript"
passed

sample: {
Arturo is an independently-developed,
modern programming language,
Expand Down
1 change: 1 addition & 0 deletions tests/unittests/lib.collections.res
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@
[+] passed!
[+] passed!
[+] passed!
[+] passed!

>> split - .by (literal)
[+] passed!
Expand Down

0 comments on commit a285640

Please sign in to comment.