Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tools): cache lower(unit) for bytes_to_str() #11920

Merged
merged 1 commit into from
Nov 6, 2023
Merged
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
10 changes: 6 additions & 4 deletions kong/tools/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ end
-- bytes_to_str(5497558, "G", 3) -- "5.120 GiB"
--
function _M.bytes_to_str(bytes, unit, scale)
if not unit or unit == "" or lower(unit) == "b" then
local u = lower(unit or "")

if u == "" or u == "b" then
return fmt("%d", bytes)
end

Expand All @@ -87,15 +89,15 @@ function _M.bytes_to_str(bytes, unit, scale)

local fspec = fmt("%%.%df", scale)

if lower(unit) == "k" then
if u == "k" then
return fmt(fspec .. " KiB", bytes / 2^10)
end

if lower(unit) == "m" then
if u == "m" then
return fmt(fspec .. " MiB", bytes / 2^20)
end

if lower(unit) == "g" then
if u == "g" then
return fmt(fspec .. " GiB", bytes / 2^30)
end

Expand Down