Repository navigation
fix(stringlib): return integers for byte values, positions and counts - #95
Merged
Merged
Conversation
string.byte, string.len, string.find, string.match, string.gmatch and
string.gsub pushed byte values, positions, position captures and
replacement counts as floats, so math.type(string.byte("a")) was
"float". Lua 5.4 returns integers for all of them.
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 string library returns floats where Lua 5.4 returns integers:
Integer-sensitive code (
math.typechecks,//and%on byte values,string.format("%d", ...)paths, integer table keys from position captures) sees the wrong subtype.Cause
strByte,strLen,strFind(plain and pattern, including the empty-pattern case), position captures instrFind,strMatch,strGmatchandstrGsub(function and table replacements), and thestrGsubmatch count pushLNumber.Fix
These sites push
LInteger.string.unpackfloat formats are unchanged.Tests
stringlib_integer_results_test.gochecks the value and theLIntegertype of every result above, plus a__indexlookup with a position-capture key ingsub. Expected values come from the Lua 5.4 reference interpreter. All 16 cases fail onmain.TestGsubMatchesPreviousImplementationandTestGsubSpecialCasesread thegsubcount with anLNumbertype assertion, which encoded the float result. They now assertLInteger.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.