Skip to content

Commit

Permalink
Merge pull request #1889 from arturo-lang/add-char-support-for-prefix
Browse files Browse the repository at this point in the history
[Strings] Add Char support for `prefix?`
  • Loading branch information
drkameleon authored Feb 20, 2025
2 parents e57e8df + 9cfadb0 commit d16ec3c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/library/Strings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1109,19 +1109,26 @@ proc defineLibrary*() =
description = "check if string starts with given prefix",
args = {
"string": {String},
"prefix": {String, Regex}
"prefix": {String, Regex, Char}
},
attrs = NoAttrs,
returns = {Logical},
example = """
prefix? "hello" "he" ; => true
prefix? "boom" "he" ; => false
..........
prefix? "hello" {/\w+/} ; => true
prefix? "world" {/\d+/} ; => false
..........
prefix? "hello" 'h' ; => true
""":
#=======================================================
if yKind==Regex:
if likely(yKind==String):
push(newLogical(x.s.startsWith(y.s)))
elif yKind==Regex:
push(newLogical(x.s.startsWith(y.rx)))
else:
push(newLogical(x.s.startsWith(y.s)))
push(newLogical(x.s.len > 0 and x.s.runeAtPos(0)==y.c))

builtin "suffix?",
alias = unaliased,
Expand Down
2 changes: 1 addition & 1 deletion version/build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3341
3342

0 comments on commit d16ec3c

Please sign in to comment.