Make string-length, substring and translate count characters - #139
Merged
zhengchun merged 1 commit intoSep 15, 2026
Merged
Conversation
XPath 1.0 4.2 defines these in characters; all three index by byte, so
string-length("日本語") is 9 and substring("日本語abc", 2, 2) returns a
byte slice that is not valid UTF-8. stringLengthFunc's own doc comment
already says "number of characters".
The three move together: fixing string-length alone turns the common
idiom substring(s, 1, string-length(s)) from "hello" into a truncated
string, because the length is then in characters but the slice is in
bytes.
normalizespaceFunc already converts to []rune for the same reason.
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.
Follow-up to #138, where I noted this and offered to open it separately.
XPath 1.0 4.2 defines these three in characters; all three index by byte.
stringLengthFunc's own doc comment atfunc.go:575-576already says "number of characters".string-length("héllo")string-length("日本語")substring("héllo",1,3)"hé""hél"substring("日本語abc",2,2)"\x97\xa5"(not valid UTF-8)"本語"translate("abc","ab","áé")"ác""áéc"translate("日本語","日語","ab")"a本b"normalizespaceFunc(func.go:469) already converts to[]runefor exactly this reason — you added that inbe94ad6to close #32. These three are the remaining byte-indexed string functions.substring-before/substring-after(func.go:564-571) are correct as they are, becausestrings.Indexreturns a boundary-aligned offset, so they are not touched.The three have to move together. Fixing
string-lengthalone breaks the common idiom, because the length is then in characters while the slice is still in bytes:string-length("héllo")substring("héllo",1,string-length("héllo"))"héllo"string-lengthfixed alone"héll""héllo""héllo"Verification
Base
865cb21. Assertions were added as rows to the three existing table tests, so the filter was checked by counting=== RUNlines — 3 on every row, never 0.func.gomd58c32ba35…fe065ef3…string-lengthonly675747ca…Test_func_string_lengthsubstringonly05e43b23…Test_func_substringtranslateonly840aa414…Test_func_translateRuneCountInString(v)+1fa195add…Test_func_string_lengthlen(rs)+25f537834…Test_func_substring(on a pre-existing row)start >=29c88b9c…Test_func_substringi+174048c27…Test_func_translateThe three reverts are disjoint, so each part of the change is independently pinned, and the over-corrections fail, so the rows do not stop one short.
The reference column is libxml2 2.14.6 via lxml, checked on 12 classes of input before any assertion was written, so the new rows encode its behaviour rather than my reading of the spec.
Gates, as
.github/workflows/*.ymlwrite them:go test ./...ok;go test -covermode atomic -coverprofile=coverage.out ./...ok, coverage 86.9%;go vet ./...exit 0;gofmt -lclean on both changed files. (gofmt -l .also listsfunc_go110.goandfunc_pre_go110.go; both are already unformatted at865cb21, and neither workflow runs gofmt.) Run with go1.26.3 rather than the 1.16 pinned incoverage.yml;unicode/utf8and[]runeboth long predatego.mod'sgo 1.14.Two costs worth naming rather than hiding:
substringandtranslatenow allocate a[]rune, which I have not benchmarked —be94ad6was explicitly a performance commit, so say the word if you want numbers. And this changes answers for non-ASCII input, so it is a behaviour fix, not a pure internal one.Disclosure: found and written with the help of Claude (an AI assistant). Every number above is from runs on my machine.