Skip to content

Commit 005e3fa

Browse files
committed
tests/lapi: fix string_byte_test
The patch fixes the following issues: - `string.byte()` returns the internal numeric codes of the characters, not a single number. - `string.char()` returns `nil` when values `i` or `j` are outside the acceptable range (less than zero and greater than the length of the string). It is not documented.
1 parent 509ef25 commit 005e3fa

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

tests/lapi/string_byte_test.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--[[
1+
--[=[
22
SPDX-License-Identifier: ISC
33
Copyright (c) 2023-2025, Sergey Bronnikov.
44
@@ -7,28 +7,28 @@ https://www.lua.org/manual/5.3/manual.html#6.4
77
88
string.byte gets confused with some out-of-range negative indices,
99
https://www.lua.org/bugs.html#5.1.3-9
10-
]]
1110
12-
-- Synopsis: string.byte(s [, i [, j]])
11+
Synopsis: string.byte(s [, i [, j]])
12+
]=]
1313

1414
local luzer = require("luzer")
1515
local test_lib = require("lib")
1616

17+
local unpack = unpack or table.unpack
18+
1719
local function TestOneInput(buf, _size)
1820
local fdp = luzer.FuzzedDataProvider(buf)
1921
os.setlocale(test_lib.random_locale(fdp), "all")
2022
local str = fdp:consume_string(test_lib.MAX_STR_LEN)
21-
local i = fdp:consume_integer(0, test_lib.MAX_INT)
22-
local j = fdp:consume_integer(0, test_lib.MAX_INT)
23+
local i = fdp:consume_integer(test_lib.MIN_INT, test_lib.MAX_INT)
24+
local j = fdp:consume_integer(test_lib.MIN_INT, test_lib.MAX_INT)
2325
-- `string.byte()` is the same as `str:byte()`.
2426
assert(string.byte(str, i, j) == str:byte(i, j))
25-
local char_code = string.byte(str, i, j)
26-
if char_code then
27-
assert(type(char_code) == "number")
28-
local byte = string.char(char_code)
29-
assert(byte)
30-
assert(byte == str)
31-
end
27+
-- Note, `string.byte()` returns `nil` when values `i` or `j`
28+
-- are outside the acceptable range (less than zero and
29+
-- greater than the length of the string). It is undocumented.
30+
local bytes = string.char(unpack({ string.byte(str, i, j) }))
31+
assert(bytes == string.sub(str, i, j))
3232
end
3333

3434
local args = {

0 commit comments

Comments
 (0)