Skip to content

endsWith, startsWith and indexOf find an empty search string - #24

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-empty-string-search-guards
Oct 5, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
fix-empty-string-search-guards

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Paired with ASDAlexander77/TypeScriptCompiler#511, which makes an empty string falsy (TypeScriptCompiler #509).

endsWith, startsWith and indexOf each began with if (!searchString), a null check written as a truthiness test. While the compiler took "" as true, "" passed the guard and the functions gave what JavaScript gives: endsWith("") and startsWith("") true, indexOf("") the position. With the compiler change, the guard returned false / -1 for "" instead.

The guards are now searchString == null. That is the same test under either compiler, so this PR can merge before or after the compiler one. lastIndexOf has the same guard, but a few lines later it already returns position for "", so it is left alone.

Test

tests/string_empty_search.ts asserts endsWith, startsWith, indexOf, lastIndexOf and includes with "", on a non-empty string and on "".

Built locally (release, gc) with the #511 compiler and run with a release-only copy of tests.ps1: release compile 160/160, release jit 160/160. The debug library was not built.

With the #511 compiler and this repo's main lib.ts, the new test fails with assertion failed: endsWith(''). tests/string_indexOf.ts prints indexOf("") results without asserting them, so it would print -1 without failing.

🤖 Generated with Claude Code

Each began with `if (!searchString)`, a null check written as a
truthiness test. While the compiler took "" as true it let "" through,
and the functions went on to give what JavaScript gives: endsWith("")
and startsWith("") true, indexOf("") the position. With "" falsy
(TypeScriptCompiler #509) the guard returned false / -1 for it.

The guards are now `searchString == null`, which is the same test under
either compiler, so this can land before or after the compiler change.
lastIndexOf has the same guard, but it already returns `position` for
"" a few lines later, so it is left alone.

tests/string_empty_search.ts asserts each case. With the compiler fix
and the old guards, it fails at endsWith('').

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 8f40c4e into main Oct 5, 2026
@ASDAlexander77
ASDAlexander77 deleted the fix-empty-string-search-guards branch October 5, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant