Skip to content

Commit

Permalink
Merge pull request #273 from hwhw/master
Browse files Browse the repository at this point in the history
rework util.template() a bit
  • Loading branch information
Frenzie committed Nov 19, 2014
2 parents a1a2b6e + 6d4a702 commit 89a3711
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
27 changes: 11 additions & 16 deletions ffi/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -234,29 +234,24 @@ dynamic positioning of place markers. The range of place markers
runs from %1 to %99, but normally no more than two or three should
be required. There are no provisions for escaping place markers.
output = util.template{
output = util.template(
_("Hello %1, welcome to %2."),
name,
company
}
)
This function was inspired by Qt:
http://qt-project.org/doc/qt-4.8/internationalization.html#use-qstring-arg-for-dynamic-text
--]]
function util.template(str, vars)
-- Adapted from MarkEdgar's solution on http://lua-users.org/wiki/StringInterpolation
-- Allow util.template{str, vars} as well as util.template(str, {vars})
if not vars then
vars = str
str = vars[1]
end
return (string.gsub(str, "%%[1-9][0-9]?", -- not regular expressions http://www.lua.org/pil/20.2.html
function(i)
if i ~= nil then
i = string.sub(i, 2) + 1
return vars[i]
end
end))
function util.template(str, ...)
local params = {...}
-- shortcut:
if #params == 0 then return str end
local result = string.gsub(str, "%%([1-9][0-9]?)",
function(i)
return params[tonumber(i)]
end)
return result
end
function util.unichar (value)
Expand Down
36 changes: 18 additions & 18 deletions spec/unit/util_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,59 @@ describe("util module", function()

it("should not affect string without arguments", function()
local str_regular = ("just a string")
local str_template = util.template{
local str_template = util.template(
("just a string")
}
)
assert.are.equal(str_regular, str_template)

local str_regular_marker = ("just a string %1")
local str_template_marker = util.template{
local str_template_marker = util.template(
("just a string %1")
}
)
assert.are.equal(str_regular_marker, str_template_marker)
end)

it("should not replace %0", function()
local str_regular = ("The argument goes %0.")
local str_template = util.template{
local str_template = util.template(
("The argument goes %0."),
"argument"
}
)
assert.are.equal(str_regular, str_template)
end)

it("should replace place markers with arguments", function()
local str_regular = ("The arguments go here and there.")
local str_template = util.template{
local str_template = util.template(
("The arguments go %1 and %2."),
"here",
"there"
}
)
assert.are.equal(str_regular, str_template)
end)

it("should allow dynamic positioning of place markers", function()
local str_regular = ("The arguments go there and here.")
local str_template = util.template{
local str_template = util.template(
("The arguments go %2 and %1."),
"here",
"there"
}
)
assert.are.equal(str_regular, str_template)
end)

it("should replace place markers no matter how many times they appear", function()
local str_regular = ("bark bark bark bark")
local str_template = util.template{
local str_template = util.template(
("%1 %1 %1 %1"),
"bark"
}
)
assert.are.equal(str_regular, str_template)
end)

it("should treat %10 as %10, not %1", function()
local str_regular = ("First: success1; tenth: success10.")
local str_template = util.template{
local str_template = util.template(
("First: %1; tenth: %10."),
"success1",
"success2",
Expand All @@ -69,7 +69,7 @@ describe("util module", function()
"success8",
"success9",
"success10"
}
)
assert.are.equal(str_regular, str_template)
end)

Expand All @@ -79,19 +79,19 @@ describe("util module", function()
for i=1,100,1 do
template_args[i] = "success" .. i .. "-"
end
local str_template = util.template{
local str_template = util.template(
("First: %1; tenth: %10; hundredth: %100."),
unpack(template_args)
}
)
assert.are.equal(str_regular, str_template)
end)

it("should not allow escaping", function()
local str_regular = ("This %argument tried to escape.")
local str_template = util.template{
local str_template = util.template(
("This %%1 tried to escape."),
"argument"
}
)
assert.are.equal(str_regular, str_template)
end)

Expand Down

0 comments on commit 89a3711

Please sign in to comment.