Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions llz4.luau
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ local MATCH_LEN_BITS = 4
local MATCH_LEN_MASK = lshift(1, MATCH_LEN_BITS) - 1


local function checkType(value, typ, argNum, funcName)
local function checkType(value: unknown, typ: string, argNum: number, funcName: string)
local actual = typeof(value)
assert(actual == typ, `bad argument #{argNum} to '{funcName}' ({typ} expected, got {actual})`)
end

local function checkData(data, dataStart, dataLen, funcName)
local function checkData(data: buffer, dataStart: number, dataLen: number, funcName: string)
checkType(data, "buffer", 1, funcName)
checkType(dataStart, "number", 2, funcName)
checkType(dataLen, "number", 3, funcName)
Expand All @@ -32,8 +32,9 @@ local function checkData(data, dataStart, dataLen, funcName)
assert(dataStart + dataLen <= buffer.len(data), "data range not in buffer")
end

local function checkAcceleration(acceleration, argNum, funcName)
local function checkAcceleration(acceleration: number?, argNum: number, funcName: string): number
acceleration = acceleration or 1
assert(acceleration, "luau analysis hint")
checkType(acceleration, "number", argNum, funcName)
assert(acceleration >= 1 and acceleration % 1 == 0, "acceleration must be an integer >= 1")
return acceleration
Expand All @@ -54,7 +55,7 @@ end
@return buffer A buffer containing the compressed data at offset 0.
@return number The length of the compressed data.
]]
local function compressBuffer(data, dataStart, dataLen, acceleration)
local function compressBuffer(data: buffer, dataStart: number, dataLen: number, acceleration: number?): (buffer, number)
checkData(data, dataStart, dataLen, "compressBuffer")
acceleration = checkAcceleration(acceleration, 4, "compressBuffer")

Expand Down Expand Up @@ -192,16 +193,15 @@ end
the cost of compression efficiency.
@return string The compressed data as a string.
]]
local function compressString(str, acceleration)
local function compressString(str: string, acceleration: number?): string
checkType(str, "string", 1, "compressString")
acceleration = checkAcceleration(acceleration, 2, "compressString")

local buf, len = compressBuffer(buffer.fromstring(str), 0, #str, acceleration)
return buffer.readstring(buf, 0, len)
end


local function expandBuffer(out, maxLen)
local function expandBuffer(out: buffer, maxLen: number)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotating the return type as (buffer?, any) seems to appease the linter and is probably good enough

local curLen = buffer.len(out)
if curLen >= maxLen then
return nil, "maximum decompressed length was exceeded"
Expand Down Expand Up @@ -231,12 +231,12 @@ end
@return buffer A buffer containing the decompressed data at offset 0.
@return number The length of the decompressed data.
]]
local function decompressBuffer(data, dataStart, dataLen, decompressedLen)
checkData(data, dataStart, dataLen)
local function decompressBuffer(data: buffer, dataStart: number, dataLen: number, decompressedLen: number?): (buffer, number)
checkData(data, dataStart, dataLen, "decompressBuffer")
decompressedLen = decompressedLen or -DEFAULT_MAX_BUFFER_SIZE
checkType(decompressedLen, "number", 4, "decompressBuffer")

local maxLen = math.abs(decompressedLen)
local maxLen = math.abs(decompressedLen :: number)
local outLen = (decompressedLen >= 0) and decompressedLen or DEFAULT_BUFFER_SIZE
local out, outNext = buffer.create(outLen), 0

Expand Down Expand Up @@ -320,7 +320,7 @@ end
output buffer will be dynamically resized with no upper bound.
@return string The decompressed string.
]]
local function decompressString(str, decompressedLen)
local function decompressString(str: string, decompressedLen: number?): string
checkType(str, "string", 1, "decompressString")
decompressedLen = decompressedLen or -DEFAULT_MAX_BUFFER_SIZE
checkType(decompressedLen, "number", 4, "decompressBuffer")
Expand Down