Skip to content

Commit c4a61ec

Browse files
committed
Implement stridx()
1 parent cdeb2a6 commit c4a61ec

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/vimscript/expression/evaluate.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,15 +1195,19 @@ export class EvaluationContext {
11951195
}
11961196
case 'str2nr': {
11971197
const [s, _base] = getArgs(1, 2);
1198-
const base = _base ? toInt(_base) : 10
1198+
const base = _base ? toInt(_base) : 10;
11991199
if (![2, 8, 10, 16].includes(base)) {
12001200
throw VimError.fromCode(ErrorCode.InvalidArgument474);
12011201
}
12021202
// TODO: Skip prefixes like 0x
1203-
const parsed = Number.parseInt(toString(s!), base)
1203+
const parsed = Number.parseInt(toString(s!), base);
12041204
return int(isNaN(parsed) ? 0 : parsed);
12051205
}
1206-
// TODO: stridx()
1206+
case 'stridx': {
1207+
const [haystack, needle, start] = getArgs(2, 3);
1208+
1209+
return int(toString(haystack!).indexOf(toString(needle!), start ? toInt(start) : 0));
1210+
}
12071211
case 'string': {
12081212
const [x] = getArgs(1);
12091213
return str(displayValue(x!));

test/vimscript/expression.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,12 @@ suite('Vimscript expressions', () => {
743743
exprTest('str2nr("DEADBEEF", 9)', { error: ErrorCode.InvalidArgument474 });
744744
});
745745

746+
suite('stridx', () => {
747+
exprTest('stridx("0123456789", "6")', { value: int(6) });
748+
exprTest('stridx("0123456789", "456")', { value: int(4) });
749+
exprTest('stridx("0123456789", "X")', { value: int(-1) });
750+
});
751+
746752
suite('string', () => {
747753
exprTest('string("")', { value: str('') });
748754
exprTest('string(123)', { value: str('123') });

0 commit comments

Comments
 (0)