Repository navigation
feat(std): add String::toScalars and deprecate the byte-wise toChars - #441
Merged
Merged
Conversation
String::toChars (and toVectorChars, and str's toChars extension) copy one char per byte without decoding, so "é" gives two chars, which the name hides; the regex parser tripped over it. String::toScalars decodes the UTF-8 into Unicode scalars (a malformed byte becomes U+FFFD and advances one byte), and the byte-wise helpers are marked @deprecated, pointing to toScalars or toBytes. The compiler's two callers move off it: the failed-test output limit counts bytes, as the substring it cuts with does, and the language server's leadingChars takes scalars.
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.
String::toChars()returns onecharper byte without decoding, so"é"comes back as twochars. Its doc says so, but the name suggests characters: the std::regex parser used it and split non-ASCII scalars until the tests caught it.What changes
String::toScalars(): Vector<char>decodes the UTF-8 into Unicode scalars. A byte that doesn't start a well-formed sequence decodes as U+FFFD and advances one byte, the same ascharAt, so the result never splits a scalar.toChars,toVectorCharsandstr'stoCharsextension are marked@deprecated. The message points totoScalars()for scalars and totoBytes()for bytes. Their docs now state the byte-wise behavior up front.test_runner: the failed-test output limit usedtoChars().length(), which is the byte count, and cuts withsubstring, which takes byte offsets. It now useslength(), so the behavior is identical.leadingChars: "the firstcountcharacters" now takes scalars, so a completion prefix no longer cuts a non-ASCII character in half.Note
The deprecation lint (A0124) only fires for calls through a path. Instance-method calls like
text.toChars()are a documented gap inignis/analyzer/lint.ign, so existing callers get no warning yet. The attribute still shows in the docs and inignis doc.Evidence
toScalarsDecodesUtf8("añ😀"is 7 bytes and 3 scalars),toScalarsReplacesMalformedBytes,toScalarsOfEmptyIsEmpty.test-std645/645;check-stdandignis checkare clean.--filter completion22,test_runner20,lsp116, all passing.