<commit_before>local sgsub = string.gsub
local smatch = string.match
local gmatch = string.gmatch
local function tappend(t, v) t[#t+1] = v end

-- pattern或是uri末尾为n个/，均忽略
local _M = {}

-- 去除最后的/                         
function remotelast(s)
    s, _ = sgsub(s, "/*$", "")
    return s
end
-- 
function _M.parse_pattern(path, keys, options)
    path = remotelast(path)

    local new_pattern = sgsub(path, "/:([A-Za-z0-9_]+)", function(m)
        tappend(keys, m)
        return "/([A-Za-z0-9_]+)"
    end)

    -- 以*结尾
    local all_pattern = sgsub(new_pattern, "/(%*)", function(m)
        return "/(.*)" -- "/(.)"
    end)
    return all_pattern
end

function _M.parse_path(uri, pattern, keys)
    uri = remotelast(uri)

    local params = {}
    local isEmpty = true
    local match = { smatch(uri, pattern) } -- param values
    if #match > 0 then -- uri match some route
        for j = 1, #match do
            if match[j] then
                local param_name = keys[j]
                if param_name then
                    params[param_name] = match[j]
                end
            end
        end    
    else

    end
    
    return params
end

function _M.is_match(uri, pattern)
    -- print("======",  uri,pattern)
    local ok = smatch(uri, pattern)
    return ok
end


function test1()
    local keys = {}
    local p = parse_pattern("/foo/:bar/create/:id/done", keys)
    print('pattern: ' .. p)
    for k,v in pairs(keys) do
        print(k .. ' ' .. v)
    end
    print("--------------")
    local params = parse_path("/foo/bar_value/create/123/done", p, keys)
    for k,v in pairs(params) do
        print(k .. ' ' .. v)
    end
end

function test1_1()
    local keys = {}
    local p = parse_pattern("/foo/:bar/create/:id/done/", keys)
    print('pattern: ' .. p)
    for k,v in pairs(keys) do
        print(k .. ' ' .. v)
    end
    print("--------------")
    local params = parse_path("/foo/bar_value/create/123/done", p, keys)
    for k,v in pairs(params) do
        print(k .. ' ' .. v)
    end
end

function test2()
    local keys = {}
    local p = parse_pattern("/foo/:id/*", keys)
    print('pattern: ' .. p)
    for k,v in pairs(keys) do
        print(k .. ' ' .. v)
    end
    print("--------------")
    local params = parse_path("/foo/123/ddfd/mmm", p, keys)
    for k,v in pairs(params) do
        print(k .. ' ' .. v)
    end
end


function _M.test()
    test1()
    print()
    test1_1()
    print()
    test2()
end

return _M
<commit_msg>remove test code in path_to_regexp<commit_after>local sgsub = string.gsub
local smatch = string.match
local gmatch = string.gmatch
local function tappend(t, v) t[#t+1] = v end

-- pattern或是uri末尾为n个/，均忽略
local _M = {}

-- 去除最后的/                         
function remotelast(s)
    s, _ = sgsub(s, "/*$", "")
    return s
end
-- 
function _M.parse_pattern(path, keys, options)
    path = remotelast(path)

    local new_pattern = sgsub(path, "/:([A-Za-z0-9_]+)", function(m)
        tappend(keys, m)
        return "/([A-Za-z0-9_]+)"
    end)

    -- 以*结尾
    local all_pattern = sgsub(new_pattern, "/(%*)", function(m)
        return "/(.*)" -- "/(.)"
    end)
    return all_pattern
end

function _M.parse_path(uri, pattern, keys)
    uri = remotelast(uri)

    local params = {}
    local isEmpty = true
    local match = { smatch(uri, pattern) } -- param values
    if #match > 0 then -- uri match some route
        for j = 1, #match do
            if match[j] then
                local param_name = keys[j]
                if param_name then
                    params[param_name] = match[j]
                end
            end
        end    
    else

    end
    
    return params
end

function _M.is_match(uri, pattern)
    -- print("======",  uri,pattern)
    local ok = smatch(uri, pattern)
    return ok
end


return _M

{'fraction_non_alphanumeric': 0.15561959654178675, 'fraction_numerical': 0.008121561435682474, 'mean_word_length': 1.8663663663663663, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4759196', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1353, 'n_words': 343}