Skip to content

Commit bc9230f

Browse files
committed
Backport PUC Lua 5.4's string.{pack,unpack,packsize}
1 parent 7579b27 commit bc9230f

File tree

9 files changed

+567
-0
lines changed

9 files changed

+567
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ find_package(Lua REQUIRED)
288288
if(NOT USE_LUAJIT)
289289
add_subdirectory(lib/bitop)
290290
endif()
291+
add_subdirectory(lib/lstrpack)
291292
add_subdirectory(lib/sha256)
292293

293294
if(BUILD_UNITTESTS OR BUILD_BENCHMARKS)

doc/lua_api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4228,6 +4228,11 @@ Helper functions
42284228
* e.g. `"a,b":split","` returns `{"a","b"}`
42294229
* `string:trim()`: returns the string without whitespace pre- and suffixes
42304230
* e.g. `"\n \t\tfoo bar\t ":trim()` returns `"foo bar"`
4231+
* Utilities for working with binary data:
4232+
* `string.pack(fmt, ...)`
4233+
* `string.unpack(fmt, s, [pos])`
4234+
* `string.packsize(fmt)`
4235+
* Backported from Lua 5.4, see https://www.lua.org/manual/5.4/manual.html#6.4.2
42314236
* `core.wrap_text(str, limit, as_table)`: returns a string or table
42324237
* Adds newlines to the string to keep it within the specified character
42334238
limit

games/devtest/mods/unittests/misc.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,10 @@ local function test_sandbox()
363363
assert(debug.getinfo(function() end, "f").func ~= nil)
364364
end
365365
unittests.register("test_sandbox", test_sandbox)
366+
367+
local function test_str_pack_unpack()
368+
local fmt = "d"
369+
assert(fmt:packsize() == 8)
370+
assert(fmt:unpack(fmt:pack(42.3)) == 42.3)
371+
end
372+
unittests.register("test_str_pack_unpack", test_str_pack_unpack)

lib/lstrpack/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add_library(lstrpack STATIC lstrpack.c)
2+
target_include_directories(lstrpack
3+
PUBLIC
4+
${CMAKE_CURRENT_SOURCE_DIR}
5+
PRIVATE
6+
${LUA_INCLUDE_DIR}
7+
)

lib/lstrpack/LICENSE.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (C) 1994-2025 Lua.org, PUC-Rio.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)