Skip to content

Commit

Permalink
convert tabs to spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Dec 30, 2024
1 parent 9be9665 commit aad0ea8
Showing 1 changed file with 121 additions and 121 deletions.
242 changes: 121 additions & 121 deletions res/modules/bit_converter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,76 +13,76 @@ local MAX_INT64 = 9223372036854775807
local MIN_INT64 = -9223372036854775808

local function maskHighBytes(num)
return bit.band(num, 0xFF)
return bit.band(num, 0xFF)
end

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
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
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
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
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
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
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 = { }
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
bytes[i] = lenBytes[i]
end
for i = 1, #lenBytes do
bytes[i] = lenBytes[i]
end

for i = 1, len do
bytes[#bytes + 1] = string.byte(string.sub(str, i, i))
end
for i = 1, len do
bytes[#bytes + 1] = string.byte(string.sub(str, i, i))
end

return bytes
return bytes
end

function bit_converter.bool_to_byte(bool)
return bool and 1 or 0
return bool and 1 or 0
end

-- Credits to Iryont <https://github.com/iryont/lua-struct>
Expand Down Expand Up @@ -151,148 +151,148 @@ end
--

function bit_converter.float32_to_bytes(float, order)
return fromLE(floatOrDoubleToBytes(float, 'f'), order)
return fromLE(floatOrDoubleToBytes(float, 'f'), order)
end

function bit_converter.float64_to_bytes(float, order)
return fromLE(floatOrDoubleToBytes(float, 'd'), order)
return fromLE(floatOrDoubleToBytes(float, 'd'), order)
end

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)
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)
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)
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)
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
if int > MAX_UINT32 or int < MIN_UINT32 then
error("invalid uint32")
end

return uint32ToBytes(int, order)
return uint32ToBytes(int, order)
end

function bit_converter.uint16_to_bytes(int, order)
if int > MAX_UINT16 or int < MIN_UINT16 then
error("invalid uint16")
end
if int > MAX_UINT16 or int < MIN_UINT16 then
error("invalid uint16")
end

return uint16ToBytes(int, order)
return uint16ToBytes(int, order)
end

function bit_converter.int64_to_bytes(int, order)
if int > MAX_INT64 or int < MIN_INT64 then
error("invalid int64")
end

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)
if int > MAX_INT64 or int < MIN_INT64 then
error("invalid int64")
end

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, 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
error("invalid int32")
end
if int > MAX_INT32 or int < MIN_INT32 then
error("invalid int32")
end

return uint32ToBytes(int + MAX_INT32, order)
return uint32ToBytes(int + MAX_INT32, order)
end

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
error("invalid int16")
end
if int > MAX_INT16 or int < MIN_INT16 then
error("invalid int16")
end

return uint16ToBytes(int + MAX_INT16, order)
return uint16ToBytes(int + MAX_INT16, order)
end

function bit_converter.sint32_to_bytes(int, order)
if int > MAX_INT32 or int < MIN_INT32 then
error("invalid sint32")
end
if int > MAX_INT32 or int < MIN_INT32 then
error("invalid sint32")
end

return uint32ToBytes(int + MAX_UINT32 + 1, order)
return uint32ToBytes(int + MAX_UINT32 + 1, order)
end

function bit_converter.sint16_to_bytes(int, order)
if int > MAX_INT16 or int < MIN_INT16 then
error("invalid sint16")
end
if int > MAX_INT16 or int < MIN_INT16 then
error("invalid sint16")
end

return uint16ToBytes(int + MAX_UINT16 + 1, order)
return uint16ToBytes(int + MAX_UINT16 + 1, order)
end

function bit_converter.bytes_to_float32(bytes, order)
return bytesToFloatOrDouble(toLE(bytes, order), 'f')
return bytesToFloatOrDouble(toLE(bytes, order), 'f')
end

function bit_converter.bytes_to_float64(bytes, order)
return bytesToFloatOrDouble(toLE(bytes, order), 'd')
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)
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)
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 len = bit_converter.bytes_to_uint16({ bytes[1], bytes[2] })

local str = ""
local str = ""

for i = 1, len do
str = str..string.char(bytes[i + 2])
end
for i = 1, len do
str = str..string.char(bytes[i + 2])
end

return str
return str
end

function bit_converter.byte_to_bool(byte)
return byte ~= 0
return byte ~= 0
end

function bit_converter.bytes_to_uint32(bytes, order)
if #bytes < 4 then
error("eof")
end
if #bytes < 4 then
error("eof")
end

bytes = toLE(bytes, order)
bytes = toLE(bytes, order)

return
bit.bor(
Expand All @@ -304,11 +304,11 @@ function bit_converter.bytes_to_uint32(bytes, order)
end

function bit_converter.bytes_to_uint16(bytes, order)
if #bytes < 2 then
error("eof")
end
if #bytes < 2 then
error("eof")
end

bytes = toLE(bytes, order)
bytes = toLE(bytes, order)

return
bit.bor(
Expand All @@ -317,11 +317,11 @@ function bit_converter.bytes_to_uint16(bytes, order)
end

function bit_converter.bytes_to_int64(bytes, order)
if #bytes < 8 then
error("eof")
end
if #bytes < 8 then
error("eof")
end

bytes = toLE(bytes, order)
bytes = toLE(bytes, order)

return
bit.bor(
Expand All @@ -341,25 +341,25 @@ function bit_converter.bytes_to_int64(bytes, order)
end

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
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, order)
on_deprecated_call("bit_converter.bytes_to_int16", "bit_converter.bytes_to_sint16")
return bit_converter.bytes_to_uint16(bytes, order) - MAX_INT16
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)
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

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

return bit_converter

0 comments on commit aad0ea8

Please sign in to comment.