This repository has been archived by the owner on Sep 27, 2019. It is now read-only.
forked from phi-gamma/lualibs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lualibs-string.lua
214 lines (177 loc) · 5.73 KB
/
lualibs-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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
if not modules then modules = { } end modules ['l-string'] = {
version = 1.001,
comment = "companion to luat-lib.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local string = string
local sub, gmatch, format, char, byte, rep, lower = string.sub, string.gmatch, string.format, string.char, string.byte, string.rep, string.lower
local lpegmatch, patterns = lpeg.match, lpeg.patterns
local P, S, C, Ct, Cc, Cs = lpeg.P, lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cc, lpeg.Cs
-- Some functions are already defined in l-lpeg and maybe some from here will
-- move there (unless we also expose caches).
-- if not string.split then
--
-- function string.split(str,pattern)
-- local t = { }
-- if #str > 0 then
-- local n = 1
-- for s in gmatch(str..pattern,"(.-)"..pattern) do
-- t[n] = s
-- n = n + 1
-- end
-- end
-- return t
-- end
--
-- end
-- function string.unquoted(str)
-- return (gsub(str,"^([\"\'])(.*)%1$","%2")) -- interesting pattern
-- end
local unquoted = patterns.squote * C(patterns.nosquote) * patterns.squote
+ patterns.dquote * C(patterns.nodquote) * patterns.dquote
function string.unquoted(str)
return lpegmatch(unquoted,str) or str
end
-- print(string.unquoted("test"))
-- print(string.unquoted([["t\"est"]]))
-- print(string.unquoted([["t\"est"x]]))
-- print(string.unquoted("\'test\'"))
-- print(string.unquoted('"test"'))
-- print(string.unquoted('"test"'))
function string.quoted(str)
return format("%q",str) -- always double quote
end
function string.count(str,pattern) -- variant 3
local n = 0
for _ in gmatch(str,pattern) do -- not for utf
n = n + 1
end
return n
end
function string.limit(str,n,sentinel) -- not utf proof
if #str > n then
sentinel = sentinel or "..."
return sub(str,1,(n-#sentinel)) .. sentinel
else
return str
end
end
local stripper = patterns.stripper
local fullstripper = patterns.fullstripper
local collapser = patterns.collapser
local longtostring = patterns.longtostring
function string.strip(str)
return str and lpegmatch(stripper,str) or ""
end
function string.fullstrip(str)
return str and lpegmatch(fullstripper,str) or ""
end
function string.collapsespaces(str)
return str and lpegmatch(collapser,str) or ""
end
function string.longtostring(str)
return str and lpegmatch(longtostring,str) or ""
end
-- function string.is_empty(str)
-- return not find(str,"%S")
-- end
local pattern = P(" ")^0 * P(-1) -- maybe also newlines
-- patterns.onlyspaces = pattern
function string.is_empty(str)
if not str or str == "" then
return true
else
return lpegmatch(pattern,str) and true or false
end
end
-- if not string.escapedpattern then
--
-- local patterns_escapes = {
-- ["%"] = "%%",
-- ["."] = "%.",
-- ["+"] = "%+", ["-"] = "%-", ["*"] = "%*",
-- ["["] = "%[", ["]"] = "%]",
-- ["("] = "%(", [")"] = "%)",
-- -- ["{"] = "%{", ["}"] = "%}"
-- -- ["^"] = "%^", ["$"] = "%$",
-- }
--
-- local simple_escapes = {
-- ["-"] = "%-",
-- ["."] = "%.",
-- ["?"] = ".",
-- ["*"] = ".*",
-- }
--
-- function string.escapedpattern(str,simple)
-- return (gsub(str,".",simple and simple_escapes or patterns_escapes))
-- end
--
-- function string.topattern(str,lowercase,strict)
-- if str == "" then
-- return ".*"
-- else
-- str = gsub(str,".",simple_escapes)
-- if lowercase then
-- str = lower(str)
-- end
-- if strict then
-- return "^" .. str .. "$"
-- else
-- return str
-- end
-- end
-- end
--
-- end
--- needs checking
local anything = patterns.anything
local allescapes = Cc("%") * S(".-+%?()[]*") -- also {} and ^$ ?
local someescapes = Cc("%") * S(".-+%()[]") -- also {} and ^$ ?
local matchescapes = Cc(".") * S("*?") -- wildcard and single match
local pattern_a = Cs ( ( allescapes + anything )^0 )
local pattern_b = Cs ( ( someescapes + matchescapes + anything )^0 )
local pattern_c = Cs ( Cc("^") * ( someescapes + matchescapes + anything )^0 * Cc("$") )
function string.escapedpattern(str,simple)
return lpegmatch(simple and pattern_b or pattern_a,str)
end
function string.topattern(str,lowercase,strict)
if str == "" or type(str) ~= "string" then
return ".*"
elseif strict then
str = lpegmatch(pattern_c,str)
else
str = lpegmatch(pattern_b,str)
end
if lowercase then
return lower(str)
else
return str
end
end
-- print(string.escapedpattern("abc*234",true))
-- print(string.escapedpattern("12+34*.tex",false))
-- print(string.escapedpattern("12+34*.tex",true))
-- print(string.topattern ("12+34*.tex",false,false))
-- print(string.topattern ("12+34*.tex",false,true))
function string.valid(str,default)
return (type(str) == "string" and str ~= "" and str) or default or nil
end
-- handy fallback
string.itself = function(s) return s end
-- also handy (see utf variant)
local pattern_c = Ct( C(1) ^0) -- string and not utf !
local pattern_b = Ct((C(1)/byte)^0)
function string.totable(str,bytes)
return lpegmatch(bytes and pattern_b or pattern_c,str)
end
-- handy from within tex:
local replacer = lpeg.replacer("@","%%") -- Watch the escaped % in lpeg!
function string.tformat(fmt,...)
return format(lpegmatch(replacer,fmt),...)
end
-- obsolete names:
string.quote = string.quoted
string.unquote = string.unquoted