Skip to content

Instantly share code, notes, and snippets.

@FGRibreau
Created September 26, 2012 19:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FGRibreau/3789873 to your computer and use it in GitHub Desktop.
Save FGRibreau/3789873 to your computer and use it in GitHub Desktop.
Strpad function in LUA
-- strpad(input, pad_length, [pad_string], [pad_type])
-- (php-style) implemented in LUA (inspired from https://gist.github.com/2625581)
-- @FGRibreau - Francois-Guillaume Ribreau
-- @Redsmin - A full-feature client for Redis http://redsmin.com
local function strpad(input, pad_length, pad_string, pad_type)
local output = input
if not pad_string then pad_string = ' ' end
if not pad_type then pad_type = 'STR_PAD_RIGHT' end
if pad_type == 'STR_PAD_BOTH' then
local j = 0
while string.len(output) < pad_length do
output = j % 2 == 0 and output .. pad_string or pad_string .. output
j = j + 1
end
else
while string.len(output) < pad_length do
output = pad_type == 'STR_PAD_LEFT' and pad_string .. output or output .. pad_string
end
end
return output
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment