Files
rosetta/src/Rosetta/core/Sniff.luau
T

28 lines
822 B
Luau

--!strict
-- core/Sniff: helpers for the formats' sniff functions (which return 0..1 from the first bytes, never consuming and
-- never throwing): magic comparison and the start of a text file without its UTF-8 byte order mark.
local Sniff = {}
type Peeker = { peek: (any, number, number?) -> string }
function Sniff.magic(r: Peeker, s: string, at: number?, score: number?): number
return if r:peek(#s, at) == s then score or 1 else 0
end
function Sniff.dropBom(s: string): string
return if string.sub(s, 1, 3) == "\239\187\191" then string.sub(s, 4) else s
end
function Sniff.text(r: Peeker, n: number, trim: boolean?): string
local s = r:peek(n)
if string.sub(s, 1, 3) == "\239\187\191" then
s = string.sub(s, 4)
end
if trim then
s = string.match(s, "^%s*(.*)$") :: string
end
return s
end
return Sniff