From 72394181c7c022f4f2ac51652825fa096c178111 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 27 Dec 2024 09:58:02 +0300 Subject: [PATCH] add unpack function documentation --- doc/en/scripting/builtins/libbyteutil.md | 17 ++++++++--------- doc/ru/scripting/builtins/libbyteutil.md | 6 ++++++ src/logic/scripting/lua/libs/libbyteutil.cpp | 5 +---- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/doc/en/scripting/builtins/libbyteutil.md b/doc/en/scripting/builtins/libbyteutil.md index fce89eae..c0cf72d0 100644 --- a/doc/en/scripting/builtins/libbyteutil.md +++ b/doc/en/scripting/builtins/libbyteutil.md @@ -39,6 +39,12 @@ Value characters describe the type and size. > [!WARNING] > Due to the absence of an integer type in Lua for values `l` and `L`, only an output size of 8 bytes is guaranteed; the value may differ from what is expected. +```lua +byteutil.unpack(format: str, bytes: table|Bytearray) -> ... +``` + +Extracts values ​​from a byte array based on a format string. + Example: ```lua @@ -58,14 +64,7 @@ debug.print(byteutil.tpack('>iBH?', -8, 250, 2019, true)) -- ) local bytes = byteutil.pack('>iBH?', -8, 250, 2019, true) -debug.print(byteutil.unpack('>iBH?', bytes)) +print(byteutil.unpack('>iBH?', bytes)) -- outputs: --- debug.print( --- { --- -8, --- 250, --- 2019, --- true --- } ---) +-- -8 250 2019 true ``` diff --git a/doc/ru/scripting/builtins/libbyteutil.md b/doc/ru/scripting/builtins/libbyteutil.md index e7fb53bd..a5495e6c 100644 --- a/doc/ru/scripting/builtins/libbyteutil.md +++ b/doc/ru/scripting/builtins/libbyteutil.md @@ -40,6 +40,12 @@ byteutil.tpack(format: str, ...) -> table > Из-за отсутствия в Lua целочисленного типа для значений `l` и `L` гарантируется > только выходной размер в 8 байт, значение может отличаться от ожидаемого. +```lua +byteutil.unpack(format: str, bytes: table|Bytearray) -> ... +``` + +Извлекает значения из массива байт, ориентируясь на строку формата. + Пример: ```lua diff --git a/src/logic/scripting/lua/libs/libbyteutil.cpp b/src/logic/scripting/lua/libs/libbyteutil.cpp index c221ad25..a757712f 100644 --- a/src/logic/scripting/lua/libs/libbyteutil.cpp +++ b/src/logic/scripting/lua/libs/libbyteutil.cpp @@ -134,8 +134,6 @@ static int l_unpack(lua::State* L) { ByteReader reader(bytes); bool bigEndian = false; - int index = 1; - lua::createtable(L, count, 0); for (size_t i = 0; format[i]; i++) { switch (format[i]) { case 'b': @@ -185,9 +183,8 @@ static int l_unpack(lua::State* L) { default: continue; } - lua::rawseti(L, index++); } - return 1; + return count; } static int l_pack(lua::State* L) {