-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.lua
71 lines (59 loc) · 1.51 KB
/
utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
local M = {}
M.HTTP_CODE = {}
M.HTTP_CODE["200"] = ngx.HTTP_OK
M.HTTP_CODE["202"] = ngx.HTTP_ACCEPTED
M.HTTP_CODE["204"] = ngx.HTTP_NO_CONTENT
M.HTTP_CODE["301"] = ngx.HTTP_MOVED_PERMANENTLY
M.HTTP_CODE["302"] = ngx.HTTP_MOVED_TEMPORARILY
M.HTTP_CODE["400"] = ngx.HTTP_BAD_REQUEST
M.HTTP_CODE["401"] = ngx.HTTP_UNAUTHORIZED
M.HTTP_CODE["401"] = ngx.HTTP_UNAUTHORIZED
M.HTTP_CODE["403"] = ngx.HTTP_FORBIDDEN
M.HTTP_CODE["404"] = ngx.HTTP_NOT_FOUND
M.HTTP_CODE["405"] = ngx.HTTP_NOT_ALLOWED
M.HTTP_CODE["500"] = ngx.HTTP_INTERNAL_SERVER_ERROR
function M.read_file(path)
local file = io.open(path, "r") -- r read mode and b binary mode
if not file then return nil end
io.input(file)
content = io.read("*a")
io.close(file)
return content
end
function M.file_exist(path)
if path == nil then
return nil
end
local f = io.open(path, "r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end
function M.starts_with(str, start)
return str:sub(1, #start) == start
end
function M.ends_with(str, ending)
return ending == "" or str:sub(-#ending) == ending
end
function M.table_len(table)
local count = 0
for k, v in pairs(table) do
count = count + 1
end
return count
end
function M.trim(s)
return s:gsub("^%s+", ""):gsub("%s+$", "")
end
function M.split(str, delimiter)
local result = {}
local pattern = string.format("([^%s]+)", delimiter)
str:gsub(pattern, function(item)
table.insert(result, item)
end)
return result
end
return M