Repository navigation
endsWith, startsWith and indexOf find an empty search string - #24
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Paired with ASDAlexander77/TypeScriptCompiler#511, which makes an empty string falsy (TypeScriptCompiler #509).
endsWith,startsWithandindexOfeach began withif (!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("")andstartsWith("")true,indexOf("")the position. With the compiler change, the guard returnedfalse/-1for""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.lastIndexOfhas the same guard, but a few lines later it already returnspositionfor"", so it is left alone.Test
tests/string_empty_search.tsassertsendsWith,startsWith,indexOf,lastIndexOfandincludeswith"", 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 withassertion failed: endsWith('').tests/string_indexOf.tsprintsindexOf("")results without asserting them, so it would print-1without failing.🤖 Generated with Claude Code