- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstring.lua
More file actions
Latest commit
171 lines (154 loc) · 4.49 KB
/
Copy pathstring.lua
File metadata and controls
171 lines (154 loc) · 4.49 KB
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
--[[
notice that,
while this does override the 'string' and add some extra stuff,
it does not explicitly replace the default string metatable __index
to do that, require 'ext.meta' (or do it yourself)
--]]
localstring= {}
fork,vinpairs(require'string') dostring[k] =vend
localtable=require'ext.table'
-- table.concat(string.split(a,b),b) == a
functionstring.split(s, exp)
exp=expor''
s=tostring(s)
localt=table()
ifs=='' then
-- handle the s='' case
-- otherwise it will return {''}
-- which technically is also an inverse of table.concat
-- but so is an empty table
-- and it's my preference that an empty string and an empty table are inverses of each other.
returnt
elseifexp=='' then
-- handle the exp='' case
fori=1,#sdo
t:insert(s:sub(i,i))
end
else
localsearchpos=1
localstart, fin=s:find(exp, searchpos)
whilestartdo
t:insert(s:sub(searchpos, start-1))
searchpos=fin+1
start, fin=s:find(exp, searchpos)
end
t:insert(s:sub(searchpos))
end
returnt
end
functionstring.trim(s)
returns:match('^%s*(.-)%s*$')
end
-- should this wrap in a table?
functionstring.bytes(s)
returntable{s:byte(1,#s)}
end
string.load=loadorloadstring
--[[
-- drifting further from standards...
-- this string-converts everything concat'd (no more errors, no more print(a,b,c)'s)
getmetatable('').__concat = function(a,b)
return tostring(a)..tostring(b)
end
--]]
-- a C++-ized accessor to subsets
-- indexes are zero-based inclusive
-- sizes are zero-based-exclusive (or one-based-inclusive depending on how you think about it)
-- parameters are (index, size) rather than (start index, end index)
functionstring.csub(d, start, size)
ifnotsizethenreturnstring.sub(d, start+1) end-- til-the-end
returnstring.sub(d, start+1, start+size)
end
--d = string data
--l = length of a column. default 32
--w = hex word size. default 1
--c = extra column space. default 8
functionstring.hexdump(d, l, w, c)
d=tostring(d)
l=tonumber(l)
w=tonumber(w)
c=tonumber(c)
ifnotlorl<1thenl=32end
ifnotworw<1thenw=1end
ifnotcorc<1thenc=8end
locals=table()
localrhs=table()
localcol=0
fori=1,#d,wdo
ifi%l==1then
s:insert(string.format('%.8x ', (i-1)))
rhs=table()
col=1
end
s:insert''
forj=w,1,-1do
locale=i+j-1
localsub=d:sub(e,e)
if#sub>0then
localb=string.byte(sub)
s:insert(string.format('%.2x', b))
rhs:insert(b>=32andsubor'.')
end
end
ifcol%c==0then
s:insert''
end
if (i+w-1) %l==0ori+w>#dthen
s:insert''
s:insert(rhs:concat())
end
if (i+w-1) %l==0then
s:insert'\n'
end
col=col+1
end
returns:concat()
end
-- escape for pattern matching
localescapeFind='[' .. ([[^$()%.[]*+-?]]):gsub('.', '%%%1') ..']'
functionstring.patescape(s)
return (s:gsub(escapeFind, '%%%1'))
end
-- this is a common function, especially as a __concat metamethod
-- it is nearly table.concat, except table.concat errors upon non-string/number instead of calling tostring() automatically
-- (should I change table.concat's default behavior and use that instead? nah, because why require a table creation.)
-- tempted to make this ext.op.concat ... but that's specifically a binary op ... and that shouldn't call tostring() while this should ...
-- maybe I should move this to ext.op as 'tostringconcat' or something?
functionstring.concat(...)
localn=select('#', ...)
ifn==0thenreturnend-- base-case nil or "" ?
locals=tostring((...))
ifn==1thenreturnsend
returns..string.concat(select(2, ...))
end
-- another common __tostring metamethod
-- since luajit doesn't support __name metafield
functionstring:nametostring()
-- NOTICE this will break for anything that overrides its __metatable metafield
localmt=getmetatable(self)
-- invoke a 'rawtostring' call / get the builtin 'tostring' result
setmetatable(self, nil)
locals=tostring(self)
setmetatable(self, mt)
localname=mt.__name
returnnameandtostring(name)..s:sub(6) ors
end
-- I use this too often ....
functionstring.hex(s, uppercase)
localfmt=uppercaseand'%02X' or'%02x'
return (tostring(s):gsub('.', function(c)
returnfmt:format(c:byte())
end))
end
functionstring.unhex(h)
ifbit.band(#h, 1) ==1
orh:find'[^0-9a-fA-F]'
then
returnnil, "string is not hex"
end
returnh:gsub('..', function(d)
returnstring.char(assert(tonumber(d, 16)))
end)
end
-- TODO other encoders?
returnstring