Skip to content

Commit d16ec3c

Browse files
authored
Merge pull request #1889 from arturo-lang/add-char-support-for-prefix
[Strings] Add Char support for `prefix?`
2 parents e57e8df + 9cfadb0 commit d16ec3c

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/library/Strings.nim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,19 +1109,26 @@ proc defineLibrary*() =
11091109
description = "check if string starts with given prefix",
11101110
args = {
11111111
"string": {String},
1112-
"prefix": {String, Regex}
1112+
"prefix": {String, Regex, Char}
11131113
},
11141114
attrs = NoAttrs,
11151115
returns = {Logical},
11161116
example = """
11171117
prefix? "hello" "he" ; => true
11181118
prefix? "boom" "he" ; => false
1119+
..........
1120+
prefix? "hello" {/\w+/} ; => true
1121+
prefix? "world" {/\d+/} ; => false
1122+
..........
1123+
prefix? "hello" 'h' ; => true
11191124
""":
11201125
#=======================================================
1121-
if yKind==Regex:
1126+
if likely(yKind==String):
1127+
push(newLogical(x.s.startsWith(y.s)))
1128+
elif yKind==Regex:
11221129
push(newLogical(x.s.startsWith(y.rx)))
11231130
else:
1124-
push(newLogical(x.s.startsWith(y.s)))
1131+
push(newLogical(x.s.len > 0 and x.s.runeAtPos(0)==y.c))
11251132
11261133
builtin "suffix?",
11271134
alias = unaliased,

version/build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3341
1+
3342

0 commit comments

Comments
 (0)