Repository navigation
fix(utf8lib): port the Lua 5.4 utf8 library - #94
Merged
Merged
Conversation
utf8.codes advanced one byte per step, because the iterator treated its 1-based control value as a 0-based offset, so it raised "invalid UTF-8 code" on the first multibyte character. utf8.codepoint ignored negative positions and silently clamped out-of-range ones, utf8.len and utf8.offset clamped instead of raising, utf8.offset accepted a start on a continuation byte, utf8.char encoded surrogates as U+FFFD and rejected values above U+10FFFF, and the lax flag was not supported. utf8lib.go now follows lutf8lib.c: the same decoder (strict and lax), luaO_utf8esc encoding up to 0x7FFFFFFF, u_posrelat position handling, and the same argument errors. The stdlib type signatures declare the optional lax parameter.
skhaz
approved these changes
Nov 15, 2026
skhaz
approved these changes
Nov 15, 2026
This was referenced Nov 15, 2026
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.
Defect
The
utf8library departs from Lua 5.4 in several functions:utf8.codesfails on any string that contains a multibyte character.Cause
utf8CodesIterreturns the 1-based position as the control value, but reads the incoming control value as a 0-based offset. Each step advances one byte and decodes a continuation byte.utf8Codepointneither resolves negative positions nor checks bounds, and it clampsj.utf8Lenandutf8Offsetclamp positions instead of raising, andutf8Offsetresolves negative positions with the wrong formula (len + i + 2).unicode/utf8.EncodeRunereplaces surrogates and values above U+10FFFF with U+FFFD, and the decoder has no lax mode.Fix
utf8lib.gois a port oflutf8lib.c:utf8_decodedecoder, strict (up to U+10FFFF, no surrogates) and lax (up to 0x7FFFFFFF);luaO_utf8escencoding forutf8.char(up to six bytes);u_posrelatposition handling;out of bounds,initial/final position out of bounds,position out of bounds,initial position is a continuation byte,value out of range,invalid UTF-8 code);laxargument oflen,codepointandcodes;utf8.codesiterator from Lua 5.4.6. It skips the continuation bytes of the previous character, rejects a string that starts with a continuation byte, and rejects a stray continuation byte after a character.charpatternis unchanged. The stdlib type signatures declare the optionallaxparameter.OpenUtf8is exported and already inlinit.go'sluaLibs, soOpenLibsopens it. No embedder-facing change.Tests
utf8lib_test.go: 39 result cases and 18 error cases forchar,charpattern,len,codepoint,offsetandcodes, including negative positions, bounds, invalid, truncated, overlong and surrogate sequences, and lax mode. Expected values come from the Lua 5.4 reference interpreter; the twocodescontinuation-byte checks follow 5.4.6. 23 cases fail onmain.compiler/check/tests/regression/utf8_lax_param_test.go: the type checker accepts thelaxargument. It fails onmainwith "too many arguments".Verification
go vet ./...,golangci-lintv2.8.0 with the CI flags,go test ./...(full fixture corpus), the coverage collector run, and the race "other" shard (go test -raceon all non-fixture tests and the non-regression fixture categories) pass. The regression-fixture race shards are left to CI.The runtime picks this up through a go-lua dependency bump after merge.