-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.lua
163 lines (140 loc) · 3.14 KB
/
string.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
local wrap, yield = coroutine.wrap, coroutine.yield
local format = string.format
local common = require 'lithium.common'
local lstring = setmetatable({
quote = common.quote,
pretty = common.pretty,
}, {__index = string})
local function delimIterator(str, delim, plain)
yield()
local i, strlen = 1, #str
while true do
local start, stop = str:find(delim, i, plain)
if start then
if stop < start then
stop = stop + 1
start = start + 1
end
yield(str:sub(i, start - 1))
i = stop + 1
if i > strlen then
if start <= stop then
yield('')
end
break
end
else
yield(str:sub(i, strlen))
break
end
end
while true do
yield(nil)
end
end
function lstring.delim(str, delim, isPattern)
local iter = wrap(delimIterator)
iter(str, delim, not isPattern)
return iter
end
function lstring.lines(str)
return lstring.delim(str, '\r?\n', true)
end
function lstring.lineAt(str, i, newlinePattern)
if newlinePattern == nil then
newlinePattern = '\n'
end
local line = 1
for _ in str:sub(1, i - 1):gmatch(newlinePattern) do
line = line + 1
end
return line
end
function lstring.positionAt(str, i, newlinePattern)
if newlinePattern == nil then
newlinePattern = '\n'
end
if i > #str then
return nil, 'index is out of range'
end
local line = 1
local j = 1
while j < i do
local start, stop = str:find(newlinePattern, j)
if not start or stop >= i then
break
end
if stop < j then
return nil, 'newline pattern matches empty string'
end
j = stop + 1
line = line + 1
end
return line, i - j + 1
end
function lstring.split(...)
return common.array(lstring.delim(...))
end
function lstring.startsWith(str, prefix)
return prefix == str:sub(1, #prefix)
end
function lstring.endsWith(str, suffix)
return suffix == str:sub(-#suffix, -1)
end
function lstring.contains(str, substr)
return not not str:find(substr, 1, true)
end
function lstring.trimLeft(str)
return (str:gsub('^%s+', '', 1))
end
function lstring.trimRight(str)
return (str:gsub('%s+$', '', 1))
end
function lstring.trim(str)
return lstring.trimLeft(lstring.trimRight(str))
end
function lstring.trimNonEmpty(str)
str = lstring.trim(str)
if str == '' then
return nil
end
return str
end
local string_format = string.format
local function lstring_format(format_, t)
return (format_:gsub('%b{}', function(key)
key = key:sub(2, -2)
local value = t[tonumber(key) or key]
if value ~= nil then
return tostring(value)
end
return string_format('{%s}', lstring_format(key, t))
end))
end
lstring.format = lstring_format
local function sepList(sep, lastSep, ...)
assert(sep ~= nil)
lastSep = lastSep or sep
local count = select('#', ...)
assert(count > 0)
if count == 1 then
return (...)
end
local list = {...}
local last = table.remove(list)
return format('%s%s%s', table.concat(list, sep), lastSep, last)
end
lstring.sepList = sepList
function lstring.orList(...)
return sepList(', ', ' or ', ...)
end
function lstring.norList(...)
return sepList(', ', ' nor ', ...)
end
function lstring.andList(...)
return sepList(', ', ' and ', ...)
end
function lstring.commaList(lastSep, ...)
return sepList(', ', lastSep, ...)
end
return lstring