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
23 changes: 10 additions & 13 deletions src/prometheus/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
local logger = require("logger");
local bit32 = require("prometheus.bit").bit32;

local MAX_UNPACK_COUNT = 195;
local MAX_UNPACK_COUNT = 4096;

local function lookupify(tb)
local tb2 = {};
Expand Down Expand Up @@ -223,21 +223,18 @@ local function readU32(arr)
return val;
end

local function bytesToString(arr)
local lenght = arr.n or #arr;
-- Adapted from https://github.com/Reselim/Base64/blob/master/Base64.lua#L16
local function bytesToString(values)
local chunks = {}
local lenght = #values

if lenght < MAX_UNPACK_COUNT then
return string.char(table.unpack(arr))
for i = 1, lenght, MAX_UNPACK_COUNT do
chunks[#chunks + 1] = string.char(
unpack(values, i, math.min(i + MAX_UNPACK_COUNT - 1, lenght))
)
end

local str = "";
local overflow = lenght % MAX_UNPACK_COUNT;

for i = 1, (#arr - overflow) / MAX_UNPACK_COUNT do
str = str .. string.char(table.unpack(arr, (i - 1) * MAX_UNPACK_COUNT + 1, i * MAX_UNPACK_COUNT));
end

return str..(overflow > 0 and string.char(table.unpack(arr, lenght - overflow + 1, lenght)) or "");
return table.concat(chunks, "")
end

local function isNaN(n)
Expand Down