Created
September 26, 2012 19:00
-
-
Save FGRibreau/3789873 to your computer and use it in GitHub Desktop.
Strpad function in LUA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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