This repository was archived by the owner on Nov 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstringx.lua
More file actions
74 lines (50 loc) · 1.35 KB
/
stringx.lua
File metadata and controls
74 lines (50 loc) · 1.35 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
--- xxd style string hex dump
function string:xxd()
local non_print_pattern = '%c' -- or use '%G'
local max_width = 8
local width = 1
local address = 0
local line = {}
local s = (self:gsub('(..)', function (c)
if width == max_width then
line[max_width] = c
address = address + max_width * 2
width = 1
local b1, b2 = c:byte(1, 2)
return ('%02x%02x %s\n'):format(b1, b2,
table.concat(line):gsub(non_print_pattern, '.'))
elseif width == 1 then
line[1] = c
width = 2
return ('%08x: %02x%02x '):format(address, c:byte(1, 2))
else
line[width] = c
width = width + 1
return ('%02x%02x '):format(c:byte(1, 2))
end
end))
if #self % 2 ~= 0 then
s = s:gsub('(.)$', function (c)
return ('%02x'):format(c:byte())
end)
end
if #self == 1 then
s = ('%08x: %02x'):format(0, self:byte())
end
local rm = #self % (max_width * 2)
if rm ~= 0 then
local start_index = 10 + 5 * 8 + 1
local line_width = start_index + 16 + 1
local rc = #s % line_width
rc = start_index - rc
s = s .. (' '):rep(rc)
.. self:sub(-rm, -1):gsub(non_print_pattern, '.') .. '\n'
end
return s
end
function string:from_hex()
return (self:gsub('%X', ''):gsub('(..)', function (bs)
return string.char(tonumber(bs, 16))
end))
end
return string