Merge branch 'main' into syntax-hightlighting
This commit is contained in:
commit
47259d5590
@ -1,93 +1,98 @@
|
||||
# Модуль core:bit_converter
|
||||
|
||||
## Доступные порядки байтов
|
||||
**LE (Little-Endian)**
|
||||
**BE (Big-Endian)**
|
||||
По умолчанию используется **LE**
|
||||
|
||||
## Конвертация значений в байты и обратно
|
||||
|
||||
```lua
|
||||
function bit_converter.string_to_bytes(string: str) -> table
|
||||
function bit_converter.string_to_bytes(str: string) -> table
|
||||
```
|
||||
Конвертирует строку в байты
|
||||
|
||||
```lua
|
||||
function bit_converter.bool_to_byte(boolean: bool) -> integer
|
||||
function bit_converter.bool_to_byte(bool: boolean) -> integer
|
||||
```
|
||||
Конвертирует логический булев в байт
|
||||
|
||||
```lua
|
||||
function bit_converter.single_to_bytes(number: single) -> table
|
||||
function bit_converter.float32_to_bytes(float: number, [опционально] order: string) -> table
|
||||
```
|
||||
Конвертирует плавающее значение одинарной точности в байты
|
||||
|
||||
```lua
|
||||
function bit_converter.double_to_bytes(number: double) -> table
|
||||
function bit_converter.float64_to_bytes(float: number, [опционально] order: string) -> table
|
||||
```
|
||||
Конвертирует плавающее значение двойной точности в байты
|
||||
|
||||
```lua
|
||||
function bit_converter.uint16_to_bytes(integer: int) -> table
|
||||
function bit_converter.uint16_to_bytes(int: integer, [опционально] order: string) -> table
|
||||
```
|
||||
Конвертирует беззнаковое 2-х байтовое целое число в байты
|
||||
|
||||
```lua
|
||||
function bit_converter.uint32_to_bytes(integer: int) -> table
|
||||
function bit_converter.uint32_to_bytes(int: integer, [опционально] order: string) -> table
|
||||
```
|
||||
Конвертирует беззнаковое 4-х байтовое целое число в байты
|
||||
|
||||
```lua
|
||||
function bit_converter.int16_to_bytes(integer: int) -> table
|
||||
function bit_converter.sint16_to_bytes(int: integer, [опционально] order: string) -> table
|
||||
```
|
||||
Конвертирует знаковое 2-х байтовое целое число в байты
|
||||
|
||||
```lua
|
||||
function bit_converter.int32_to_bytes(integer: int) -> table
|
||||
function bit_converter.sint32_to_bytes(int: integer, [опционально] order: string) -> table
|
||||
```
|
||||
Конвертирует знаковое 4-х байтовое целое число в байты
|
||||
|
||||
```lua
|
||||
function bit_converter.int64_to_bytes(integer: int) -> table
|
||||
function bit_converter.int64_to_bytes(int: integer, [опционально] order: string) -> table
|
||||
```
|
||||
Конвертирует знаковое 8-и байтовое целое число в байты
|
||||
|
||||
```lua
|
||||
function bit_converter.bytes_to_string(table: bytes) -> string
|
||||
function bit_converter.bytes_to_string(bytes: table) -> string
|
||||
```
|
||||
Конвертирует массив байтов в строку
|
||||
|
||||
```lua
|
||||
function bit_converter.byte_to_bool(integer: byte) -> boolean
|
||||
function bit_converter.byte_to_bool(byte: integer) -> boolean
|
||||
```
|
||||
Конвертирует байт в логическое булевое значение
|
||||
|
||||
```lua
|
||||
function bit_converter.bytes_to_single(table: bytes) -> number№
|
||||
function bit_converter.bytes_to_float32(bytes: table|Bytearray, [опционально] order: string) -> number
|
||||
```
|
||||
Конвертирует массив байтов в плавающее число одинарной точности
|
||||
|
||||
```lua
|
||||
function bit_converter.bytes_to_double(table: bytes) -> number
|
||||
function bit_converter.bytes_to_float64(bytes: table|Bytearray, [опционально] order: string) -> number
|
||||
```
|
||||
Конвертирует массив байтов в плавающее число двойной точности
|
||||
|
||||
```lua
|
||||
function bit_converter.bytes_to_uint16(table: bytes) -> integer
|
||||
function bit_converter.bytes_to_uint16(bytes: table|Bytearray, [опционально] order: string) -> integer
|
||||
```
|
||||
Конвертирует массив байтов в 2-х байтовое беззнаковое число
|
||||
|
||||
```lua
|
||||
function bit_converter.bytes_to_uint32(table: bytes) -> integer
|
||||
function bit_converter.bytes_to_uint32(bytes: table|Bytearray, [опционально] order: string) -> integer
|
||||
```
|
||||
Конвертирует массив байтов в 4-х байтовое беззнаковое число
|
||||
|
||||
```lua
|
||||
function bit_converter.bytes_to_int16(table: bytes) -> integer
|
||||
function bit_converter.bytes_to_sint16(bytes: table|Bytearray, [опционально] order: string) -> integer
|
||||
```
|
||||
Конвертирует массив байтов в 2-х байтовое знаковое число
|
||||
|
||||
```lua
|
||||
function bit_converter.bytes_to_int32(table: bytes) -> integer
|
||||
function bit_converter.bytes_to_sint32(bytes: table|Bytearray, [опционально] order: string) -> integer
|
||||
```
|
||||
Конвертирует массив байтов в 4-х байтовое знаковое число
|
||||
|
||||
```lua
|
||||
function bit_converter.bytes_to_int64(table: bytes) -> integer
|
||||
function bit_converter.bytes_to_int64(bytes: table|Bytearray, [опционально] order: string) -> integer
|
||||
```
|
||||
Конвертирует массив байтов в 8-х байтовое знаковое число
|
||||
|
||||
@ -4,67 +4,78 @@
|
||||
### Хранит в себе массив байтов и позволяет легко получать или добавлять разные значения
|
||||
|
||||
```lua
|
||||
function data_buffer(bytes)
|
||||
function data_buffer(
|
||||
[опционально] bytes: table,
|
||||
[опционально] order: string,
|
||||
[опционально] useBytearray: boolean
|
||||
)
|
||||
```
|
||||
Создаёт новый экземпляр data_buffer (параметр bytes необязательный)
|
||||
Создаёт новый экземпляр **data_buffer**.
|
||||
Если **useBytearray** равен **true**, то байты буффера будут хранится ввиде **Bytearray**. Это может снизить производительность, но также и уменьшить размер буффера в памяти
|
||||
|
||||
```lua
|
||||
function data_buffer:put_byte(integer: byte)
|
||||
function data_buffer:set_order(order: string)
|
||||
```
|
||||
Задаёт порядок байтов для чисел.
|
||||
Должен равняться одному из перечисленных в [**bit_converter**](core_bit_converter.md)
|
||||
|
||||
```lua
|
||||
function data_buffer:put_byte(byte: integer)
|
||||
```
|
||||
Записывает байт в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_bytes(table: bytes)
|
||||
function data_buffer:put_bytes(bytes: table|Bytearray)
|
||||
```
|
||||
Записывает байты в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_string(string: str)
|
||||
function data_buffer:put_string(str: string)
|
||||
```
|
||||
Конвертирует строку в байты и записывает их в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_bool(boolean: bool)
|
||||
function data_buffer:put_bool(bool: boolean)
|
||||
```
|
||||
Конвертирует булевое значение в байт и записывает его в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_single(number: single)
|
||||
function data_buffer:put_float32(float: number)
|
||||
```
|
||||
Конвертирует плавающее число одинарной точности в байты и записывает их в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_double(number: double)
|
||||
function data_buffer:put_float64(float: number)
|
||||
```
|
||||
Конвертирует плавающее число двойной точности в байты и записывает их в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_uint16(integer: int)
|
||||
function data_buffer:put_uint16(int: integer)
|
||||
```
|
||||
Конвертирует беззнаковое 2-х байтовое число в байты и записывает их в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_uint32(integer: int)
|
||||
function data_buffer:put_uint32(int: integer)
|
||||
```
|
||||
Конвертирует беззнаковое 4-х байтовое число в байты и записывает их в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_int16(integer: int)
|
||||
function data_buffer:put_sint16(int: integer)
|
||||
```
|
||||
Конвертирует знаковое 2-х байтовое число в байты и записывает их в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_int32(integer: int)
|
||||
function data_buffer:put_sint32(int: integer)
|
||||
```
|
||||
Конвертирует знаковое 4-х байтовое число в байты и записывает их в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_int64(integer: int)
|
||||
function data_buffer:put_int64(int: integer)
|
||||
```
|
||||
Конвертирует знаковое 8-и байтовое число в байты и записывает их в буффер
|
||||
|
||||
```lua
|
||||
function data_buffer:put_number(number: num)
|
||||
function data_buffer:put_number(num: number)
|
||||
```
|
||||
Конвертирует любое число в байты и записывает их в буффер;
|
||||
|
||||
@ -73,10 +84,10 @@ function data_buffer:put_number(number: num)
|
||||
zero = 0
|
||||
uint16 = 1
|
||||
uint32 = 2
|
||||
int16 = 3
|
||||
int32 = 4
|
||||
int64 = 5
|
||||
double = 6
|
||||
float64 = 6
|
||||
sint16 = 7
|
||||
sint32 = 8
|
||||
```
|
||||
|
||||
```lua
|
||||
@ -85,9 +96,9 @@ function data_buffer:get_byte() -> integer
|
||||
Возвращает следующий байт из буффера
|
||||
|
||||
```lua
|
||||
function data_buffer:get_bytes(n) -> table
|
||||
function data_buffer:get_bytes(n) -> table|Bytearray
|
||||
```
|
||||
Возвращает n следующих байтов, если n равен nil или не указан, то возвращается массив всех байтов
|
||||
Возвращает **n** следующих байтов, если **n** равен **nil** или не указан, то возвращается массив всех байтов
|
||||
|
||||
```lua
|
||||
function data_buffer:get_string() -> string
|
||||
@ -100,12 +111,12 @@ function data_buffer:get_bool() -> boolean
|
||||
Читает следующий логический булев из буффера
|
||||
|
||||
```lua
|
||||
function data_buffer:get_single() -> number
|
||||
function data_buffer:get_float32() -> number
|
||||
```
|
||||
Читает следующее плавающее число одинарной точности из буффера
|
||||
|
||||
```lua
|
||||
function data_buffer:get_double() -> number
|
||||
function data_buffer:get_float64() -> number
|
||||
```
|
||||
Читает следующее плавающее число двойной точности из буффера
|
||||
|
||||
@ -120,12 +131,12 @@ function data_buffer:get_uint32() -> integer
|
||||
Читает следующее 4-х байтовое беззнаковое целое число из буффера
|
||||
|
||||
```lua
|
||||
function data_buffer:get_int16() -> integer
|
||||
function data_buffer:get_sint16() -> integer
|
||||
```
|
||||
Читает следующее 2-х байтовое знаковое целое число из буффера
|
||||
|
||||
```lua
|
||||
function data_buffer:get_int32() -> integer
|
||||
function data_buffer:get_sint32() -> integer
|
||||
```
|
||||
Читает следующее 4-х байтовое знаковое целое число из буффера
|
||||
|
||||
|
||||
@ -12,17 +12,57 @@ local MIN_INT32 = -2147483648
|
||||
local MAX_INT64 = 9223372036854775807
|
||||
local MIN_INT64 = -9223372036854775808
|
||||
|
||||
local function intToByte(num)
|
||||
local function maskHighBytes(num)
|
||||
return bit.band(num, 0xFF)
|
||||
end
|
||||
|
||||
local function reverse(tab)
|
||||
for i = 1, math.floor(#tab, 2), 1 do
|
||||
tab[i], tab[#tab-i+1] = tab[#tab-i+1], tab[i]
|
||||
end
|
||||
return tab
|
||||
local function reverse(tbl)
|
||||
for i=1, math.floor(#tbl / 2) do
|
||||
local tmp = tbl[i]
|
||||
tbl[i] = tbl[#tbl - i + 1]
|
||||
tbl[#tbl - i + 1] = tmp
|
||||
end
|
||||
return tbl
|
||||
end
|
||||
|
||||
local orders = { "LE", "BE" }
|
||||
|
||||
local fromLEConvertors =
|
||||
{
|
||||
LE = function(bytes) return bytes end,
|
||||
BE = function(bytes) return reverse(bytes) end
|
||||
}
|
||||
|
||||
local toLEConvertors =
|
||||
{
|
||||
LE = function(bytes) return bytes end,
|
||||
BE = function(bytes) return reverse(bytes) end
|
||||
}
|
||||
|
||||
bit_converter.default_order = "LE"
|
||||
|
||||
local function fromLE(bytes, orderTo)
|
||||
if orderTo then
|
||||
bit_converter.validate_order(orderTo)
|
||||
return fromLEConvertors[orderTo](bytes)
|
||||
else return bytes end
|
||||
end
|
||||
|
||||
local function toLE(bytes, orderFrom)
|
||||
if orderFrom then
|
||||
bit_converter.validate_order(orderFrom)
|
||||
return toLEConvertors[orderFrom](bytes)
|
||||
else return bytes end
|
||||
end
|
||||
|
||||
function bit_converter.validate_order(order)
|
||||
if not bit_converter.is_valid_order(order) then
|
||||
error("invalid order: "..order)
|
||||
end
|
||||
end
|
||||
|
||||
function bit_converter.is_valid_order(order) return table.has(orders, order) end
|
||||
|
||||
function bit_converter.string_to_bytes(str)
|
||||
local bytes = { }
|
||||
|
||||
@ -83,19 +123,12 @@ local function floatOrDoubleToBytes(val, opt)
|
||||
bytes[#bytes + 1] = math.floor(sign * 128 + val) % (2 ^ 8)
|
||||
val = math.floor((sign * 128 + val) / (2 ^ 8))
|
||||
|
||||
if not endianness then
|
||||
reverse(bytes)
|
||||
end
|
||||
return bytes
|
||||
end
|
||||
|
||||
local function bytesToFloatOrDouble(bytes, opt)
|
||||
local n = (opt == 'd') and 8 or 4
|
||||
|
||||
if not endianness then
|
||||
reverse(bytes)
|
||||
end
|
||||
|
||||
local sign = 1
|
||||
local mantissa = bytes[n - 1] % ((opt == 'd') and 16 or 128)
|
||||
for i = n - 2, 1, -1 do
|
||||
@ -117,80 +150,128 @@ end
|
||||
|
||||
--
|
||||
|
||||
function bit_converter.single_to_bytes(float)
|
||||
return floatOrDoubleToBytes(float, 'f')
|
||||
function bit_converter.float32_to_bytes(float, order)
|
||||
return fromLE(floatOrDoubleToBytes(float, 'f'), order)
|
||||
end
|
||||
|
||||
function bit_converter.double_to_bytes(double)
|
||||
return floatOrDoubleToBytes(double, 'd')
|
||||
function bit_converter.float64_to_bytes(float, order)
|
||||
return fromLE(floatOrDoubleToBytes(float, 'd'), order)
|
||||
end
|
||||
|
||||
function bit_converter.uint32_to_bytes(int)
|
||||
function bit_converter.single_to_bytes(float, order)
|
||||
on_deprecated_call("bit_converter.float_to_bytes", "bit_converter.float32_to_bytes")
|
||||
return bit_converter.float32_to_bytes(bytes, order)
|
||||
end
|
||||
|
||||
function bit_converter.double_to_bytes(double, order)
|
||||
on_deprecated_call("bit_converter.double_to_bytes", "bit_converter.float64_to_bytes")
|
||||
return bit_converter.float64_to_bytes(bytes, order)
|
||||
end
|
||||
|
||||
local function uint32ToBytes(int, order)
|
||||
return fromLE({
|
||||
maskHighBytes(bit.rshift(int, 24)),
|
||||
maskHighBytes(bit.rshift(int, 16)),
|
||||
maskHighBytes(bit.rshift(int, 8)),
|
||||
maskHighBytes(int)
|
||||
}, order)
|
||||
end
|
||||
|
||||
local function uint16ToBytes(int, order)
|
||||
return fromLE({
|
||||
maskHighBytes(bit.rshift(int, 8)),
|
||||
maskHighBytes(int)
|
||||
}, order)
|
||||
end
|
||||
|
||||
function bit_converter.uint32_to_bytes(int, order)
|
||||
if int > MAX_UINT32 or int < MIN_UINT32 then
|
||||
error("invalid uint32")
|
||||
end
|
||||
|
||||
return {
|
||||
intToByte(bit.rshift(int, 24)),
|
||||
intToByte(bit.rshift(int, 16)),
|
||||
intToByte(bit.rshift(int, 8)),
|
||||
intToByte(int)
|
||||
}
|
||||
return uint32ToBytes(int, order)
|
||||
end
|
||||
|
||||
function bit_converter.uint16_to_bytes(int)
|
||||
function bit_converter.uint16_to_bytes(int, order)
|
||||
if int > MAX_UINT16 or int < MIN_UINT16 then
|
||||
error("invalid uint16")
|
||||
end
|
||||
|
||||
return {
|
||||
intToByte(bit.rshift(int, 8)),
|
||||
intToByte(int)
|
||||
}
|
||||
return uint16ToBytes(int, order)
|
||||
end
|
||||
|
||||
function bit_converter.int64_to_bytes(int)
|
||||
function bit_converter.int64_to_bytes(int, order)
|
||||
if int > MAX_INT64 or int < MIN_INT64 then
|
||||
error("invalid int64")
|
||||
end
|
||||
|
||||
return {
|
||||
intToByte(bit.rshift(int, 56)),
|
||||
intToByte(bit.rshift(int, 48)),
|
||||
intToByte(bit.rshift(int, 40)),
|
||||
intToByte(bit.rshift(int, 32)),
|
||||
intToByte(bit.rshift(int, 24)),
|
||||
intToByte(bit.rshift(int, 16)),
|
||||
intToByte(bit.rshift(int, 8)),
|
||||
intToByte(int)
|
||||
}
|
||||
return fromLE({
|
||||
maskHighBytes(bit.rshift(int, 56)),
|
||||
maskHighBytes(bit.rshift(int, 48)),
|
||||
maskHighBytes(bit.rshift(int, 40)),
|
||||
maskHighBytes(bit.rshift(int, 32)),
|
||||
maskHighBytes(bit.rshift(int, 24)),
|
||||
maskHighBytes(bit.rshift(int, 16)),
|
||||
maskHighBytes(bit.rshift(int, 8)),
|
||||
maskHighBytes(int)
|
||||
}, order)
|
||||
end
|
||||
|
||||
function bit_converter.int32_to_bytes(int)
|
||||
function bit_converter.int32_to_bytes(int, order)
|
||||
on_deprecated_call("bit_converter.int32_to_bytes", "bit_converter.sint32_to_bytes")
|
||||
|
||||
if int > MAX_INT32 or int < MIN_INT32 then
|
||||
error("invalid int32")
|
||||
end
|
||||
|
||||
return bit_converter.uint32_to_bytes(int + MAX_INT32)
|
||||
return uint32ToBytes(int + MAX_INT32, order)
|
||||
end
|
||||
|
||||
function bit_converter.int16_to_bytes(int)
|
||||
function bit_converter.int16_to_bytes(int, order)
|
||||
on_deprecated_call("bit_converter.int32_to_bytes", "bit_converter.sint16_to_bytes")
|
||||
|
||||
if int > MAX_INT16 or int < MIN_INT16 then
|
||||
error("invalid int16")
|
||||
end
|
||||
|
||||
return bit_converter.uint16_to_bytes(int + MAX_INT16)
|
||||
return uint16ToBytes(int + MAX_INT16, order)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_single(bytes)
|
||||
return bytesToFloatOrDouble(bytes, 'f')
|
||||
function bit_converter.sint32_to_bytes(int, order)
|
||||
if int > MAX_INT32 or int < MIN_INT32 then
|
||||
error("invalid sint32")
|
||||
end
|
||||
|
||||
return uint32ToBytes(int + MAX_UINT32 + 1, order)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_double(bytes)
|
||||
return bytesToFloatOrDouble(bytes, 'd')
|
||||
function bit_converter.sint16_to_bytes(int, order)
|
||||
if int > MAX_INT16 or int < MIN_INT16 then
|
||||
error("invalid sint16")
|
||||
end
|
||||
|
||||
return uint16ToBytes(int + MAX_UINT16 + 1, order)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_string(bytes)
|
||||
function bit_converter.bytes_to_float32(bytes, order)
|
||||
return bytesToFloatOrDouble(toLE(bytes, order), 'f')
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_float64(bytes, order)
|
||||
return bytesToFloatOrDouble(toLE(bytes, order), 'd')
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_single(bytes, order)
|
||||
on_deprecated_call("bit_converter.bytes_to_single", "bit_converter.bytes_to_float32")
|
||||
return bit_converter.bytes_to_float32(bytes, order)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_double(bytes, order)
|
||||
on_deprecated_call("bit_converter.bytes_to_double", "bit_converter.bytes_to_float64")
|
||||
return bit_converter.bytes_to_float64(bytes, order)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_string(bytes, order)
|
||||
local len = bit_converter.bytes_to_uint16({ bytes[1], bytes[2] })
|
||||
|
||||
local str = ""
|
||||
@ -206,17 +287,13 @@ function bit_converter.byte_to_bool(byte)
|
||||
return byte ~= 0
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_float(bytes)
|
||||
if #bytes < 8 then
|
||||
error("eof")
|
||||
end
|
||||
error("unsupported operation")
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_uint32(bytes)
|
||||
function bit_converter.bytes_to_uint32(bytes, order)
|
||||
if #bytes < 4 then
|
||||
error("eof")
|
||||
end
|
||||
|
||||
bytes = toLE(bytes, order)
|
||||
|
||||
return
|
||||
bit.bor(
|
||||
bit.bor(
|
||||
@ -226,20 +303,26 @@ function bit_converter.bytes_to_uint32(bytes)
|
||||
bit.lshift(bytes[3], 8)),bytes[4])
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_uint16(bytes)
|
||||
function bit_converter.bytes_to_uint16(bytes, order)
|
||||
if #bytes < 2 then
|
||||
error("eof")
|
||||
end
|
||||
|
||||
bytes = toLE(bytes, order)
|
||||
|
||||
return
|
||||
bit.bor(
|
||||
bit.lshift(bytes[1], 8),
|
||||
bytes[2], 0)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_int64(bytes)
|
||||
function bit_converter.bytes_to_int64(bytes, order)
|
||||
if #bytes < 8 then
|
||||
error("eof")
|
||||
end
|
||||
|
||||
bytes = toLE(bytes, order)
|
||||
|
||||
return
|
||||
bit.bor(
|
||||
bit.bor(
|
||||
@ -257,12 +340,26 @@ function bit_converter.bytes_to_int64(bytes)
|
||||
bit.lshift(bit.band(bytes[7], 0xFF), 8)),bit.band(bytes[8], 0xFF))
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_int32(bytes)
|
||||
return bit_converter.bytes_to_uint32(bytes) - MAX_INT32
|
||||
function bit_converter.bytes_to_int32(bytes, order)
|
||||
on_deprecated_call("bit_converter.bytes_to_int32", "bit_converter.bytes_to_sint32")
|
||||
return bit_converter.bytes_to_uint32(bytes, order) - MAX_INT32
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_int16(bytes)
|
||||
return bit_converter.bytes_to_uint16(bytes) - MAX_INT16
|
||||
function bit_converter.bytes_to_int16(bytes, order)
|
||||
on_deprecated_call("bit_converter.bytes_to_int16", "bit_converter.bytes_to_sint16")
|
||||
return bit_converter.bytes_to_uint16(bytes, order) - MAX_INT16
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_sint32(bytes, order)
|
||||
local num = bit_converter.bytes_to_uint32(bytes, order)
|
||||
|
||||
return MIN_INT32 * (bit.band(MAX_INT32 + 1, num) ~= 0 and 1 or 0) + bit.band(MAX_INT32, num)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_sint16(bytes, order)
|
||||
local num = bit_converter.bytes_to_uint16(bytes, order)
|
||||
|
||||
return MIN_INT16 * (bit.band(MAX_INT16 + 1, num) ~= 0 and 1 or 0) + bit.band(MAX_INT16, num)
|
||||
end
|
||||
|
||||
return bit_converter
|
||||
|
||||
@ -18,22 +18,31 @@ local TYPE_UINT32 = 2
|
||||
local TYPE_INT16 = 3
|
||||
local TYPE_INT32 = 4
|
||||
local TYPE_INT64 = 5
|
||||
local TYPE_DOUBLE = 6
|
||||
local TYPE_FLOAT64 = 6
|
||||
local TYPE_SINT16 = 7
|
||||
local TYPE_SINT32 = 8
|
||||
|
||||
-- Data buffer
|
||||
|
||||
local data_buffer =
|
||||
{
|
||||
__call =
|
||||
function(data_buffer, bytes)
|
||||
return data_buffer:new(bytes)
|
||||
function(data_buffer, ...)
|
||||
return data_buffer:new(...)
|
||||
end
|
||||
}
|
||||
|
||||
function data_buffer:new(bytes)
|
||||
function data_buffer:new(bytes, order, useBytearray)
|
||||
bytes = bytes or { }
|
||||
|
||||
if order then bit_converter.validate_order(order)
|
||||
else order = bit_converter.default_order end
|
||||
|
||||
local obj = {
|
||||
pos = 1,
|
||||
bytes = bytes or { }
|
||||
order = order,
|
||||
useBytearray = useBytearray or false,
|
||||
bytes = useBytearray and Bytearray(bytes) or bytes
|
||||
}
|
||||
|
||||
self.__index = self
|
||||
@ -42,6 +51,13 @@ function data_buffer:new(bytes)
|
||||
return obj
|
||||
end
|
||||
|
||||
function data_buffer:set_order(order)
|
||||
bit_converter.validate_order(order)
|
||||
|
||||
self.order = order
|
||||
self.floatsOrder = order
|
||||
end
|
||||
|
||||
-- Push functions
|
||||
|
||||
function data_buffer:put_byte(byte)
|
||||
@ -49,7 +65,8 @@ function data_buffer:put_byte(byte)
|
||||
error("invalid byte")
|
||||
end
|
||||
|
||||
self.bytes[self.pos] = byte
|
||||
if self.useBytearray then self.bytes:insert(self.pos, byte)
|
||||
else table.insert(self.bytes, self.pos, byte) end
|
||||
|
||||
self.pos = self.pos + 1
|
||||
end
|
||||
@ -61,11 +78,21 @@ function data_buffer:put_bytes(bytes)
|
||||
end
|
||||
|
||||
function data_buffer:put_single(single)
|
||||
self:put_bytes(bit_converter.single_to_bytes(single))
|
||||
on_deprecated_call("data_buffer:put_single", "data_buffer:put_float32")
|
||||
self:put_bytes(bit_converter.single_to_bytes(single, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_double(double)
|
||||
self:put_bytes(bit_converter.double_to_bytes(double))
|
||||
on_deprecated_call("data_buffer:put_single", "data_buffer:put_float64")
|
||||
self:put_bytes(bit_converter.double_to_bytes(double, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_float32(single)
|
||||
self:put_bytes(bit_converter.float32_to_bytes(single, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_float64(float)
|
||||
self:put_bytes(bit_converter.float64_to_bytes(float, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_string(str)
|
||||
@ -77,23 +104,33 @@ function data_buffer:put_bool(bool)
|
||||
end
|
||||
|
||||
function data_buffer:put_uint16(uint16)
|
||||
self:put_bytes(bit_converter.uint16_to_bytes(uint16))
|
||||
self:put_bytes(bit_converter.uint16_to_bytes(uint16, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_uint32(uint32)
|
||||
self:put_bytes(bit_converter.uint32_to_bytes(uint32))
|
||||
self:put_bytes(bit_converter.uint32_to_bytes(uint32, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_int16(int16)
|
||||
self:put_bytes(bit_converter.int16_to_bytes(int16))
|
||||
on_deprecated_call("data_buffer:put_int16", "data_buffer:put_sint16")
|
||||
self:put_bytes(bit_converter.int16_to_bytes(int16, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_int32(int32)
|
||||
self:put_bytes(bit_converter.int32_to_bytes(int32))
|
||||
on_deprecated_call("data_buffer:put_int32", "data_buffer:put_sint32")
|
||||
self:put_bytes(bit_converter.int32_to_bytes(int32, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_sint16(int16)
|
||||
self:put_bytes(bit_converter.sint16_to_bytes(int16, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_sint32(int32)
|
||||
self:put_bytes(bit_converter.sint32_to_bytes(int32, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_int64(int64)
|
||||
self:put_bytes(bit_converter.int64_to_bytes(int64))
|
||||
self:put_bytes(bit_converter.int64_to_bytes(int64, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_number(num)
|
||||
@ -101,8 +138,8 @@ function data_buffer:put_number(num)
|
||||
local type
|
||||
|
||||
if math.floor(num) ~= num then
|
||||
type = TYPE_DOUBLE
|
||||
bytes = bit_converter.double_to_bytes(num)
|
||||
type = TYPE_FLOAT64
|
||||
bytes = bit_converter.float64_to_bytes(num)
|
||||
elseif num == 0 then
|
||||
type = TYPE_ZERO
|
||||
bytes = { }
|
||||
@ -119,11 +156,11 @@ function data_buffer:put_number(num)
|
||||
end
|
||||
elseif num < 0 then
|
||||
if num >= MIN_INT16 then
|
||||
type = TYPE_INT16
|
||||
bytes = bit_converter.int16_to_bytes(num)
|
||||
type = TYPE_SINT16
|
||||
bytes = bit_converter.sint16_to_bytes(num)
|
||||
elseif num >= MIN_INT32 then
|
||||
type = TYPE_INT32
|
||||
bytes = bit_converter.int32_to_bytes(num)
|
||||
type = TYPE_SINT32
|
||||
bytes = bit_converter.sint32_to_bytes(num)
|
||||
elseif num >= MIN_INT64 then
|
||||
type = TYPE_INT64
|
||||
bytes = bit_converter.int64_to_bytes(num)
|
||||
@ -155,9 +192,13 @@ function data_buffer:get_number()
|
||||
return self:get_int16()
|
||||
elseif type == TYPE_INT32 then
|
||||
return self:get_int32()
|
||||
elseif type == TYPE_SINT16 then
|
||||
return self:get_sint16()
|
||||
elseif type == TYPE_SINT32 then
|
||||
return self:get_sint32()
|
||||
elseif type == TYPE_INT64 then
|
||||
return self:get_int64()
|
||||
elseif type == TYPE_DOUBLE then
|
||||
elseif type == TYPE_FLOAT64 then
|
||||
return self:get_double()
|
||||
else
|
||||
error("unknown lua number type: "..type)
|
||||
@ -165,11 +206,21 @@ function data_buffer:get_number()
|
||||
end
|
||||
|
||||
function data_buffer:get_single()
|
||||
return bit_converter.bytes_to_single(self:get_bytes(4))
|
||||
on_deprecated_call("data_buffer:get_single", "data_buffer:get_float32")
|
||||
return bit_converter.bytes_to_single(self:get_bytes(4), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_double()
|
||||
return bit_converter.bytes_to_double(self:get_bytes(8))
|
||||
on_deprecated_call("data_buffer:get_double", "data_buffer:get_float64")
|
||||
return bit_converter.bytes_to_double(self:get_bytes(8), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_float32()
|
||||
return bit_converter.bytes_to_float32(self:get_bytes(4), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_float64()
|
||||
return bit_converter.bytes_to_float64(self:get_bytes(8), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_string()
|
||||
@ -193,23 +244,33 @@ function data_buffer:get_bool()
|
||||
end
|
||||
|
||||
function data_buffer:get_uint16()
|
||||
return bit_converter.bytes_to_uint16(self:get_bytes(2))
|
||||
return bit_converter.bytes_to_uint16(self:get_bytes(2), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_uint32()
|
||||
return bit_converter.bytes_to_uint32(self:get_bytes(4))
|
||||
return bit_converter.bytes_to_uint32(self:get_bytes(4), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_int16()
|
||||
return bit_converter.bytes_to_int16(self:get_bytes(2))
|
||||
on_deprecated_call("data_buffer:get_int16", "data_buffer:get_sint16")
|
||||
return bit_converter.bytes_to_int16(self:get_bytes(2), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_int32()
|
||||
return bit_converter.bytes_to_int32(self:get_bytes(4))
|
||||
on_deprecated_call("data_buffer:get_int32", "data_buffer:get_sint32")
|
||||
return bit_converter.bytes_to_int32(self:get_bytes(4), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_sint16()
|
||||
return bit_converter.bytes_to_sint16(self:get_bytes(2), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_sint32()
|
||||
return bit_converter.bytes_to_sint32(self:get_bytes(4), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_int64()
|
||||
return bit_converter.bytes_to_int64(self:get_bytes(8))
|
||||
return bit_converter.bytes_to_int64(self:get_bytes(8), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:size()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user