Skip to content

Commit

Permalink
ngx
Browse files Browse the repository at this point in the history
  • Loading branch information
spout committed Mar 20, 2017
1 parent 0b2f252 commit 5aeec4b
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 12 deletions.
16 changes: 8 additions & 8 deletions lib/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ tplLib.splitTpl = function (self, template)
local startTag = tools.escapetag(ariesIns.startTag or "<%")
local endTag = tools.escapetag(ariesIns.endTag or "%>")

local luaCodeReg = string.format("%s(.-)%s", startTag, endTag)
local includeReg = string.format("^%s include (.-)%s", startTag, endTag)
local expressReg = string.format("^%s=(.-)%s", startTag, endTag)
local notEscapeReg = string.format("^%s%%-(.-)%s", startTag, endTag)
local luaCodeReg = startTag .. "(.-)" .. endTag -- string.format("%s(.-)%s", startTag, endTag)
local includeReg = "^".. startTag .. " include (.-)" .. endTag -- string.format("^%s include (.-)%s", startTag, endTag)
local expressReg = "^".. startTag .. "=(.-)" .. endTag -- string.format("^%s=(.-)%s", startTag, endTag)
local notEscapeReg = "^".. startTag .. "%-(.-)" .. endTag --string.format("^%s%%-(.-)%s", startTag, endTag)
local position = 1 -- postion表示匹配到的lua表达式位置

-- 返回迭代器,把模版切成不同类型的块
Expand Down Expand Up @@ -194,7 +194,7 @@ tplLib.parseInclude = function (self, template, includeTreeStr, includeTrackTree
-- 此处用于去重 ariesIns.includes 里面相同的模板名字
includes[chunk.text] = true
local str = ariesIns:getInclude(chunk.text) -- 调用用户函数获取块内容
str = str:gsub("\n", "\n" .. chunk.space) -- 塞入include之前的用户前面的空格或者换行,这样渲染出来就不会错位了
str = tools.gsub(str, "\n", "\n" .. chunk.space) -- 塞入include之前的用户前面的空格或者换行,这样渲染出来就不会错位了

-- 第一个参数include的模版字符串
-- 第二个参数 includeTreeStr 这个记录所有主模版一条include分支下面的include模版名称
Expand Down Expand Up @@ -330,11 +330,11 @@ tplLib.compile = function(self, code)

-- 不转义print
local rawPrint = function (text, escape)
local text = string.format("%s", text or "nil")
local text = text or "nil" --string.format("%s", )
if escape then
table.insert(result, string.format("%q", text))
else
table.insert(result, string.format("%s", text))
table.insert(result, text) --string.format("%s", text))
--table.insert(result, tools.trim(string.format("%s", text)))
end
end
Expand All @@ -344,7 +344,7 @@ tplLib.compile = function(self, code)
local text = string.format("%s", text or "nil")

for k, v in pairs(_M._ESCAPE_TABLE) do
text = text:gsub(k, v)
text = tools.gsub(text, k, v)
end
rawPrint(text, escape)
end
Expand Down
222 changes: 222 additions & 0 deletions lib/ngx_split.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
-- I hereby assign copyright in this code to the lua-resty-core project,
-- to be licensed under the same terms as the rest of the code.


local ffi = require 'ffi'
local bit = require "bit"
local base = require "resty.core.base"
local core_regex = require "resty.core.regex"


local C = ffi.C
local sub = string.sub
local type = type
local band = bit.band
local new_tab = base.new_tab
local tostring = tostring
local math_max = math.max
local math_min = math.min
local re_match_compile = core_regex.re_match_compile
local destroy_compiled_regex = core_regex.destroy_compiled_regex


local FLAG_DFA = 0x02
local PCRE_ERROR_NOMATCH = -1
local DEFAULT_SPLIT_RES_SIZE = 4


local split_ctx = new_tab(0, 1)


local _M = { version = base.version }


local function re_split_helper(subj, compiled, compile_once, flags, ctx)
local rc
do
local pos = math_max(ctx.pos - 1, 0)

rc = C.ngx_http_lua_ffi_exec_regex(compiled, flags, subj, #subj, pos)
end

if rc == PCRE_ERROR_NOMATCH then
if not compile_once then
destroy_compiled_regex(compiled)
end
return nil, nil, nil
end

if rc < 0 then
if not compile_once then
destroy_compiled_regex(compiled)
end
return nil, nil, nil, "pcre_exec() failed: " .. rc
end

if rc == 0 then
if band(flags, FLAG_DFA) == 0 then
return nil, nil, nil, "capture size too small"
end

rc = 1
end

local caps = compiled.captures
local ncaps = compiled.ncaptures

local from = caps[0] + 1
local to = caps[1]

if from < 0 or to < 0 then
return nil, nil, nil
end

ctx.pos = to + 1

-- retrieve the first sub-match capture if any

if ncaps > 0 and rc > 1 then
return from, to, sub(subj, caps[2] + 1, caps[3])
end

return from, to
end


function _M.split(subj, regex, opts, ctx, max, res)
-- we need to cast this to strings to avoid exceptions when they are
-- something else.
-- needed because of further calls to string.sub in this function.
subj = tostring(subj)

if not ctx then
ctx = split_ctx
ctx.pos = 1 -- set or reset upvalue field

elseif not ctx.pos then
-- ctx provided by user but missing pos field
ctx.pos = 1
end

max = max or 0

if not res then
-- limit the initial arr_n size of res to a reasonable value
-- 0 < narr <= DEFAULT_SPLIT_RES_SIZE
local narr = DEFAULT_SPLIT_RES_SIZE
if max > 0 then
-- the user specified a valid max limiter if max > 0
narr = math_min(narr, max)
end

res = new_tab(narr, 0)

elseif type(res) ~= "table" then
return error("res is not a table", 2)
end

local len = #subj
if ctx.pos > len then
res[1] = nil
return res
end

if regex == "" then
local pos = ctx.pos
local last = len
if max > 0 then
last = math_min(len, pos + max - 1)
end

local res_idx = 1
while pos < last do
res[res_idx] = sub(subj, pos, pos)
res_idx = res_idx + 1
pos = pos + 1
end

res[res_idx] = sub(subj, pos)
res[res_idx + 1] = nil

return res
end

-- compile regex

local compiled, compile_once, flags = re_match_compile(regex, opts)
if compiled == nil then
-- compiled_once holds the error string
return nil, compile_once
end

local sub_idx = ctx.pos
local res_idx = 0

-- splitting: with and without a max limiter

if max > 0 then
local count = 1

while count < max do
local from, to, capture, err = re_split_helper(subj, compiled,
compile_once, flags, ctx)
if err then
return nil, err
end

if not from then
break
end

count = count + 1
res_idx = res_idx + 1
res[res_idx] = sub(subj, sub_idx, from - 1)

if capture then
res_idx = res_idx + 1
res[res_idx] = capture
end

sub_idx = to + 1
end

if count == max then
if not compile_once then
destroy_compiled_regex(compiled)
end
end

else
while true do
local from, to, capture, err = re_split_helper(subj, compiled,
compile_once, flags, ctx)
if err then
return nil, err
end

if not from then
break
end

res_idx = res_idx + 1
res[res_idx] = sub(subj, sub_idx, from - 1)

if capture then
res_idx = res_idx + 1
res[res_idx] = capture
end

sub_idx = to + 1
end
end

-- trailing nil for non-cleared res tables

res[res_idx + 1] = sub(subj, sub_idx)
res[res_idx + 2] = nil

return res
end


return _M
30 changes: 26 additions & 4 deletions lib/tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ local _M = {
-- return "/"

-- end)()

_M.isNgx = not not ngx

_M.splitNgx = function(str, pat)
local splitDict = require("ngx_split")
local split = splitDict.split
return split(str, pat)
end

_M.split = function(str, pat)
if _M.isNgx then -- 如果有ngx
return _M.splitNgx(str, pat)
end

local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
Expand All @@ -36,10 +49,9 @@ end
-- end

_M.trim = function(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end

_M.trim = function(s)
if _M.isNgx then
return ngx.re.gsub(s, "^[ \t\r]*(.-)[ \t\r]*$", "$0", "i")
end
return (s:gsub("^[ \t\r]*(.-)[ \t\r]*$", "%1"))
end

Expand Down Expand Up @@ -97,7 +109,17 @@ end
result @type string -- 转义后的字符串
]]
_M.escapetag = function(str)
if _M.isNgx then
return ngx.re.gsub(str, "([^\w])", "%%$0", "i")
end
return str:gsub("([^%w])","%%%1")
end

_M.gsub = function(str, regx, replace)
if _M.isNgx then
return ngx.re.gsub(str, regx, replace, "i")
end
return str:gsub(regx, replace)
end

return _M
2 changes: 2 additions & 0 deletions tpl/debug.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<% x = "1" %>
<%= x %>

0 comments on commit 5aeec4b

Please sign in to comment.