Latest post of the previous page:
Дествительно.Только cast ещё нужен.Обновил.
Latest post of the previous page:
Дествительно.Только cast ещё нужен.If this parameter is an empty string the function returns the path to the current executable file.
чиорт, а я такое же два часа искал в интернетах и один час писал на ЛУА.
мне уже хочется прекратиь чег либо писать,пишешь что-нибудь несколько часов, а потом находишь это валяется свободно (рукалица)
Code: Select all
--http://lua-users.org/lists/lua-l/2011-02/msg01534.html
local argify
do
local lpeg = require "lpeg"
local function the (kind,x)
local fmt,y = "expecting %s but got %s",type(x)
assert(y==kind,fmt:format(kind,y))
return x
end
local match,P,C,Ct = lpeg.match,lpeg.P,lpeg.C,lpeg.Ct
local sp,quote,esc = P [[ ]],P [["]],P [[\"]]
local function strip (s) return (s:gsub([[\"]],[["]])) end
local word = C((1-sp-quote)^1)
local quoted = quote*C((esc + (1 - quote))^1)*quote
local item = (quoted/strip)+word
local pat = Ct(item*((sp^1)*item)^0)
argify = function (str) return (the("table",pat:match(str))) end
end
Code: Select all
--http://lua-users.org/lists/lua-l/2011-02/msg01545.html
local function argify(s)
local out = {}
-- see http://www.lua.org/pil/20.4.html for encode/decode
string.gsub(
string.gsub(s, "\\(.)", function (x)
return string.format("\\%03d", string.byte(x))
end),
"[\"]([^\"]*)[\"]",
function(m) return m:gsub(" ", "\\032") end):
gsub("([^%s]+)%s*", function(m)
table.insert(out, (m:gsub("\\(%d%d%d)",
function (d) return string.char(d) end)))
end)
return out
end
Code: Select all
-- adapted from original source by Shmuel
local splitCL_it do
local pattern = [=[
(" ( (?: \\" | [^"] )* ) "?) |
( (?: \\" | [^"\s] )+ ) |
\s+
]=]
splitCL_it = function (str)
local outside = true
local iter = regex.gmatch(str,pattern,"x")
return function ()
local arg, raw
repeat
local origin,a,b = iter()
local match = a or b
if match then
origin = origin or b
match = match:gsub("\\\"", "\"")
if outside then
outside = false
arg = match
raw = origin
else
arg = arg..match
raw = raw..origin
end
else
outside = true
return arg, raw
end
until false
end
end
end
if _cmdline=="" then
print "Iterator splitting commandline into separate arguments."
print "Returns both unquoted and original arguments substrings."
print "Example:"
print("splitCL_it([["..[[1 "2 a" \"3" "b\"]].."]])","=>")
sh.each(splitCL_it([[1 "2 a" \"3" "b\"]]),print)
elseif _cmdline then
sh.each(splitCL_it(_cmdline),print)
else
return splitCL_it
end