convert tabs to spaces

This commit is contained in:
MihailRis 2024-12-30 06:23:42 +03:00
parent 9be9665daa
commit aad0ea84eb

View File

@ -13,15 +13,15 @@ local MAX_INT64 = 9223372036854775807
local MIN_INT64 = -9223372036854775808 local MIN_INT64 = -9223372036854775808
local function maskHighBytes(num) local function maskHighBytes(num)
return bit.band(num, 0xFF) return bit.band(num, 0xFF)
end end
local function reverse(tbl) local function reverse(tbl)
for i=1, math.floor(#tbl / 2) do for i=1, math.floor(#tbl / 2) do
local tmp = tbl[i] local tmp = tbl[i]
tbl[i] = tbl[#tbl - i + 1] tbl[i] = tbl[#tbl - i + 1]
tbl[#tbl - i + 1] = tmp tbl[#tbl - i + 1] = tmp
end end
return tbl return tbl
end end
@ -29,60 +29,60 @@ local orders = { "LE", "BE" }
local fromLEConvertors = local fromLEConvertors =
{ {
LE = function(bytes) return bytes end, LE = function(bytes) return bytes end,
BE = function(bytes) return reverse(bytes) end BE = function(bytes) return reverse(bytes) end
} }
local toLEConvertors = local toLEConvertors =
{ {
LE = function(bytes) return bytes end, LE = function(bytes) return bytes end,
BE = function(bytes) return reverse(bytes) end BE = function(bytes) return reverse(bytes) end
} }
bit_converter.default_order = "LE" bit_converter.default_order = "LE"
local function fromLE(bytes, orderTo) local function fromLE(bytes, orderTo)
if orderTo then if orderTo then
bit_converter.validate_order(orderTo) bit_converter.validate_order(orderTo)
return fromLEConvertors[orderTo](bytes) return fromLEConvertors[orderTo](bytes)
else return bytes end else return bytes end
end end
local function toLE(bytes, orderFrom) local function toLE(bytes, orderFrom)
if orderFrom then if orderFrom then
bit_converter.validate_order(orderFrom) bit_converter.validate_order(orderFrom)
return toLEConvertors[orderFrom](bytes) return toLEConvertors[orderFrom](bytes)
else return bytes end else return bytes end
end end
function bit_converter.validate_order(order) function bit_converter.validate_order(order)
if not bit_converter.is_valid_order(order) then if not bit_converter.is_valid_order(order) then
error("invalid order: "..order) error("invalid order: "..order)
end end
end end
function bit_converter.is_valid_order(order) return table.has(orders, order) end function bit_converter.is_valid_order(order) return table.has(orders, order) end
function bit_converter.string_to_bytes(str) function bit_converter.string_to_bytes(str)
local bytes = { } local bytes = { }
local len = string.len(str) local len = string.len(str)
local lenBytes = bit_converter.uint16_to_bytes(len) local lenBytes = bit_converter.uint16_to_bytes(len)
for i = 1, #lenBytes do for i = 1, #lenBytes do
bytes[i] = lenBytes[i] bytes[i] = lenBytes[i]
end end
for i = 1, len do for i = 1, len do
bytes[#bytes + 1] = string.byte(string.sub(str, i, i)) bytes[#bytes + 1] = string.byte(string.sub(str, i, i))
end end
return bytes return bytes
end end
function bit_converter.bool_to_byte(bool) function bit_converter.bool_to_byte(bool)
return bool and 1 or 0 return bool and 1 or 0
end end
-- Credits to Iryont <https://github.com/iryont/lua-struct> -- Credits to Iryont <https://github.com/iryont/lua-struct>
@ -151,148 +151,148 @@ end
-- --
function bit_converter.float32_to_bytes(float, order) function bit_converter.float32_to_bytes(float, order)
return fromLE(floatOrDoubleToBytes(float, 'f'), order) return fromLE(floatOrDoubleToBytes(float, 'f'), order)
end end
function bit_converter.float64_to_bytes(float, order) function bit_converter.float64_to_bytes(float, order)
return fromLE(floatOrDoubleToBytes(float, 'd'), order) return fromLE(floatOrDoubleToBytes(float, 'd'), order)
end end
function bit_converter.single_to_bytes(float, order) function bit_converter.single_to_bytes(float, order)
on_deprecated_call("bit_converter.float_to_bytes", "bit_converter.float32_to_bytes") on_deprecated_call("bit_converter.float_to_bytes", "bit_converter.float32_to_bytes")
return bit_converter.float32_to_bytes(bytes, order) return bit_converter.float32_to_bytes(bytes, order)
end end
function bit_converter.double_to_bytes(double, order) function bit_converter.double_to_bytes(double, order)
on_deprecated_call("bit_converter.double_to_bytes", "bit_converter.float64_to_bytes") on_deprecated_call("bit_converter.double_to_bytes", "bit_converter.float64_to_bytes")
return bit_converter.float64_to_bytes(bytes, order) return bit_converter.float64_to_bytes(bytes, order)
end end
local function uint32ToBytes(int, order) local function uint32ToBytes(int, order)
return fromLE({ return fromLE({
maskHighBytes(bit.rshift(int, 24)), maskHighBytes(bit.rshift(int, 24)),
maskHighBytes(bit.rshift(int, 16)), maskHighBytes(bit.rshift(int, 16)),
maskHighBytes(bit.rshift(int, 8)), maskHighBytes(bit.rshift(int, 8)),
maskHighBytes(int) maskHighBytes(int)
}, order) }, order)
end end
local function uint16ToBytes(int, order) local function uint16ToBytes(int, order)
return fromLE({ return fromLE({
maskHighBytes(bit.rshift(int, 8)), maskHighBytes(bit.rshift(int, 8)),
maskHighBytes(int) maskHighBytes(int)
}, order) }, order)
end end
function bit_converter.uint32_to_bytes(int, order) function bit_converter.uint32_to_bytes(int, order)
if int > MAX_UINT32 or int < MIN_UINT32 then if int > MAX_UINT32 or int < MIN_UINT32 then
error("invalid uint32") error("invalid uint32")
end end
return uint32ToBytes(int, order) return uint32ToBytes(int, order)
end end
function bit_converter.uint16_to_bytes(int, order) function bit_converter.uint16_to_bytes(int, order)
if int > MAX_UINT16 or int < MIN_UINT16 then if int > MAX_UINT16 or int < MIN_UINT16 then
error("invalid uint16") error("invalid uint16")
end end
return uint16ToBytes(int, order) return uint16ToBytes(int, order)
end end
function bit_converter.int64_to_bytes(int, order) function bit_converter.int64_to_bytes(int, order)
if int > MAX_INT64 or int < MIN_INT64 then if int > MAX_INT64 or int < MIN_INT64 then
error("invalid int64") error("invalid int64")
end end
return fromLE({ return fromLE({
maskHighBytes(bit.rshift(int, 56)), maskHighBytes(bit.rshift(int, 56)),
maskHighBytes(bit.rshift(int, 48)), maskHighBytes(bit.rshift(int, 48)),
maskHighBytes(bit.rshift(int, 40)), maskHighBytes(bit.rshift(int, 40)),
maskHighBytes(bit.rshift(int, 32)), maskHighBytes(bit.rshift(int, 32)),
maskHighBytes(bit.rshift(int, 24)), maskHighBytes(bit.rshift(int, 24)),
maskHighBytes(bit.rshift(int, 16)), maskHighBytes(bit.rshift(int, 16)),
maskHighBytes(bit.rshift(int, 8)), maskHighBytes(bit.rshift(int, 8)),
maskHighBytes(int) maskHighBytes(int)
}, order) }, order)
end end
function bit_converter.int32_to_bytes(int, order) function bit_converter.int32_to_bytes(int, order)
on_deprecated_call("bit_converter.int32_to_bytes", "bit_converter.sint32_to_bytes") on_deprecated_call("bit_converter.int32_to_bytes", "bit_converter.sint32_to_bytes")
if int > MAX_INT32 or int < MIN_INT32 then if int > MAX_INT32 or int < MIN_INT32 then
error("invalid int32") error("invalid int32")
end end
return uint32ToBytes(int + MAX_INT32, order) return uint32ToBytes(int + MAX_INT32, order)
end end
function bit_converter.int16_to_bytes(int, order) function bit_converter.int16_to_bytes(int, order)
on_deprecated_call("bit_converter.int32_to_bytes", "bit_converter.sint16_to_bytes") on_deprecated_call("bit_converter.int32_to_bytes", "bit_converter.sint16_to_bytes")
if int > MAX_INT16 or int < MIN_INT16 then if int > MAX_INT16 or int < MIN_INT16 then
error("invalid int16") error("invalid int16")
end end
return uint16ToBytes(int + MAX_INT16, order) return uint16ToBytes(int + MAX_INT16, order)
end end
function bit_converter.sint32_to_bytes(int, order) function bit_converter.sint32_to_bytes(int, order)
if int > MAX_INT32 or int < MIN_INT32 then if int > MAX_INT32 or int < MIN_INT32 then
error("invalid sint32") error("invalid sint32")
end end
return uint32ToBytes(int + MAX_UINT32 + 1, order) return uint32ToBytes(int + MAX_UINT32 + 1, order)
end end
function bit_converter.sint16_to_bytes(int, order) function bit_converter.sint16_to_bytes(int, order)
if int > MAX_INT16 or int < MIN_INT16 then if int > MAX_INT16 or int < MIN_INT16 then
error("invalid sint16") error("invalid sint16")
end end
return uint16ToBytes(int + MAX_UINT16 + 1, order) return uint16ToBytes(int + MAX_UINT16 + 1, order)
end end
function bit_converter.bytes_to_float32(bytes, order) function bit_converter.bytes_to_float32(bytes, order)
return bytesToFloatOrDouble(toLE(bytes, order), 'f') return bytesToFloatOrDouble(toLE(bytes, order), 'f')
end end
function bit_converter.bytes_to_float64(bytes, order) function bit_converter.bytes_to_float64(bytes, order)
return bytesToFloatOrDouble(toLE(bytes, order), 'd') return bytesToFloatOrDouble(toLE(bytes, order), 'd')
end end
function bit_converter.bytes_to_single(bytes, order) function bit_converter.bytes_to_single(bytes, order)
on_deprecated_call("bit_converter.bytes_to_single", "bit_converter.bytes_to_float32") on_deprecated_call("bit_converter.bytes_to_single", "bit_converter.bytes_to_float32")
return bit_converter.bytes_to_float32(bytes, order) return bit_converter.bytes_to_float32(bytes, order)
end end
function bit_converter.bytes_to_double(bytes, order) function bit_converter.bytes_to_double(bytes, order)
on_deprecated_call("bit_converter.bytes_to_double", "bit_converter.bytes_to_float64") on_deprecated_call("bit_converter.bytes_to_double", "bit_converter.bytes_to_float64")
return bit_converter.bytes_to_float64(bytes, order) return bit_converter.bytes_to_float64(bytes, order)
end end
function bit_converter.bytes_to_string(bytes, order) function bit_converter.bytes_to_string(bytes, order)
local len = bit_converter.bytes_to_uint16({ bytes[1], bytes[2] }) local len = bit_converter.bytes_to_uint16({ bytes[1], bytes[2] })
local str = "" local str = ""
for i = 1, len do for i = 1, len do
str = str..string.char(bytes[i + 2]) str = str..string.char(bytes[i + 2])
end end
return str return str
end end
function bit_converter.byte_to_bool(byte) function bit_converter.byte_to_bool(byte)
return byte ~= 0 return byte ~= 0
end end
function bit_converter.bytes_to_uint32(bytes, order) function bit_converter.bytes_to_uint32(bytes, order)
if #bytes < 4 then if #bytes < 4 then
error("eof") error("eof")
end end
bytes = toLE(bytes, order) bytes = toLE(bytes, order)
return return
bit.bor( bit.bor(
@ -304,11 +304,11 @@ function bit_converter.bytes_to_uint32(bytes, order)
end end
function bit_converter.bytes_to_uint16(bytes, order) function bit_converter.bytes_to_uint16(bytes, order)
if #bytes < 2 then if #bytes < 2 then
error("eof") error("eof")
end end
bytes = toLE(bytes, order) bytes = toLE(bytes, order)
return return
bit.bor( bit.bor(
@ -317,11 +317,11 @@ function bit_converter.bytes_to_uint16(bytes, order)
end end
function bit_converter.bytes_to_int64(bytes, order) function bit_converter.bytes_to_int64(bytes, order)
if #bytes < 8 then if #bytes < 8 then
error("eof") error("eof")
end end
bytes = toLE(bytes, order) bytes = toLE(bytes, order)
return return
bit.bor( bit.bor(
@ -341,25 +341,25 @@ function bit_converter.bytes_to_int64(bytes, order)
end end
function bit_converter.bytes_to_int32(bytes, order) function bit_converter.bytes_to_int32(bytes, order)
on_deprecated_call("bit_converter.bytes_to_int32", "bit_converter.bytes_to_sint32") on_deprecated_call("bit_converter.bytes_to_int32", "bit_converter.bytes_to_sint32")
return bit_converter.bytes_to_uint32(bytes, order) - MAX_INT32 return bit_converter.bytes_to_uint32(bytes, order) - MAX_INT32
end end
function bit_converter.bytes_to_int16(bytes, order) function bit_converter.bytes_to_int16(bytes, order)
on_deprecated_call("bit_converter.bytes_to_int16", "bit_converter.bytes_to_sint16") on_deprecated_call("bit_converter.bytes_to_int16", "bit_converter.bytes_to_sint16")
return bit_converter.bytes_to_uint16(bytes, order) - MAX_INT16 return bit_converter.bytes_to_uint16(bytes, order) - MAX_INT16
end end
function bit_converter.bytes_to_sint32(bytes, order) function bit_converter.bytes_to_sint32(bytes, order)
local num = bit_converter.bytes_to_uint32(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) return MIN_INT32 * (bit.band(MAX_INT32 + 1, num) ~= 0 and 1 or 0) + bit.band(MAX_INT32, num)
end end
function bit_converter.bytes_to_sint16(bytes, order) function bit_converter.bytes_to_sint16(bytes, order)
local num = bit_converter.bytes_to_uint16(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) return MIN_INT16 * (bit.band(MAX_INT16 + 1, num) ~= 0 and 1 or 0) + bit.band(MAX_INT16, num)
end end
return bit_converter return bit_converter