summaryrefslogtreecommitdiff
path: root/util.lua
blob: ca693ba4f30a5eddcac024f652e79af4e5b026de (plain) (blame)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
-- Copyright 2025 David Vazgenovich Shakaryan

local mp_utils = require('mp.utils')

local util = {}

function util.copy_table(t)
	local u = {}
	for k, v in pairs(t) do
		u[k] = v
	end
	return u
end

function util.reverse(t)
	for i = 1, #t/2 do
		t[i], t[#t-i+1] = t[#t-i+1], t[i]
	end
end

function util.strip(str)
	return (str:gsub('^%s*(.-)%s*$', '%1'))
end

function util.strip_ne(str)
	if str == nil then
		return nil
	end
	local str = util.strip(tostring(str))
	return str ~= '' and str or nil
end

-- skips over utf8 continuation bytes (10xxxxxx)
-- valid positions range from 1 to #str + 1 (*after* the last byte)
function util.utf8_seek(str, pos, n)
	local step = n > 0 and 1 or -1
	local test = n > 0
		and function() return pos > #str end
		or function() return pos <= 1 end

	while n ~= 0 and not test() do
		repeat
			pos = pos + step
		until test() or bit.band(str:byte(pos), 0xc0) ~= 0x80

		n = n - step
	end
	return pos
end

-- wraps string into a table of strings. spaces are not removed, resulting in
-- width-1 visible chars; newlines and end of string are handled similarly for
-- consistency. words longer than width are not broken. optional cont_width can
-- be specified to use a different width for continuation lines.
function util.wrap(str, width, cont_width)
	local t = {}
	local start, stop = 0, 0
	while stop < #str do
		local i = str:find('[ \n]', stop + 1) or #str + 1
		if i - start >= width then
			t[#t+1] = str:sub(start, stop)
			start = stop + 1

			if cont_width then
				width = cont_width
			end
		end
		stop = i

		if str:byte(stop) == 10 or stop >= #str then
			t[#t+1] = str:sub(start, stop - 1) .. ' '
			start = stop + 1
		end
	end
	return t
end

function util.read_json_file(path)
	local f = io.open(path, 'r')
	if not f then
		return {}
	end
	local json = f:read('*all')
	f:close()
	return mp_utils.parse_json(json)
end

function util.write_json_file(path, data)
	local f = io.open(path, 'w')
	f:write(mp_utils.format_json(data), '\n')
	f:close()
end

-- merges tables `t' and `u', using key `k' for identity. the order from `u' is
-- always respected, preserving the relative order from `t' when possible.
function util.stable_kmerge(t, u, k)
	local pos = {}
	for i, v in ipairs(u) do
		pos[v[k]] = i
	end

	local res = {}
	local seen = {}
	local function append(v)
		if not seen[v[k]] then
			res[#res+1] = v
			seen[v[k]] = true
		end
	end

	local ind = 1
	for _, v in ipairs(t) do
		if pos[v[k]] then
			while ind <= pos[v[k]] do
				append(u[ind])
				ind = ind + 1
			end
		end
		append(v)
	end
	for i = ind, #u do
		append(u[i])
	end

	return res
end

function util.flatten_table(src, dst, prefix)
	local dst = dst or {}
	for k, v in pairs(src) do
		local k = prefix and prefix .. '.' .. k or k
		if type(v) == 'table' then
			util.flatten_table(v, dst, k)
		else
			dst[k] = v
		end
	end
	return dst
end

function util.unflatten_table(src, dst)
	local dst = dst or {}
	for k, v in pairs(src) do
		local t = dst
		local prev
		for curr in (k .. '.'):gmatch('(.-)%.') do
			if prev then
				t[prev] = t[prev] or {}
				t = t[prev]
			end
			prev = curr
		end
		t[prev] = v
	end
	return dst
end

function util.find_matches(str, substr, case_sensitive)
	if not str then
		return
	end

	if not case_sensitive then
		str = str:lower()
	end

	local matches
	local i, j = 0, 0
	while true do
		i, j = str:find(substr, j + 1, true)
		-- j < i avoids infinite loop on empty substr
		if not i or j < i then
			break
		end
		matches = matches or {}
		matches[#matches+1] = {start = i, stop = j}
	end
	return matches or i and {}
end

function util.str_seek_prev_char(str, pos)
	return util.utf8_seek(str, pos, -1)
end

function util.str_seek_next_char(str, pos)
	return util.utf8_seek(str, pos, 1)
end

function util.str_seek_prev_word(str, pos)
	return str:sub(1, pos - 1):match('()%S*%s*$')
end

function util.str_seek_next_word(str, pos)
	return str:match('%s*%S*()', pos)
end

function util.str_insert_char(str, pos, ch)
	return str:sub(1, pos - 1) .. ch .. str:sub(pos), pos + #ch
end

function util.str_del_prev_char(str, pos)
	if pos <= 1 then
		return
	end

	local npos = util.utf8_seek(str, pos, -1)
	return str:sub(1, npos - 1) .. str:sub(pos), npos
end

function util.str_del_next_char(str, pos)
	if pos > #str then
		return
	end

	return str:sub(1, pos - 1) .. str:sub(util.utf8_seek(str, pos, 1)), pos
end

function util.str_del_prev_word(str, pos)
	if pos <= 1 then
		return
	end

	local npos = str:sub(1, pos - 1):match('()%S*%s*$')
	return str:sub(1, npos - 1) .. str:sub(pos), npos
end

function util.str_del_next_word(str, pos)
	if pos > #str then
		return
	end

	return str:sub(1, pos - 1) .. str:sub(str:match('%s*%S*()', pos)), pos
end

function util.str_del_to_start(str, pos)
	if pos <= 1 then
		return
	end

	return str:sub(pos), 1
end

function util.str_del_to_end(str, pos)
	if pos > #str then
		return
	end

	return str:sub(1, pos - 1), pos
end

function util.str_transpose_chars(str, pos)
	if pos <= 1 then
		return
	end

	local npos = util.utf8_seek(str, pos, 1)
	local cp2 = util.utf8_seek(str, npos, -1)
	if cp2 <= 1 then
		return
	end
	local cp1 = util.utf8_seek(str, cp2, -1)

	return str:sub(1, cp1 - 1) .. str:sub(cp2, npos - 1) ..
			str:sub(cp1, cp2 - 1) .. str:sub(npos),
		npos
end

function util.str_transpose_words(str, pos)
	if pos <= 1 then
		return
	end

	local npos = str:match('%s*%S*()', pos)
	local pre, w1, sp, w2 = str:sub(1, npos - 1):match(
		'^(.-)(%S+)(%s+)(%S+%s*)$')
	if not pre then
		return
	end

	return pre .. w2 .. sp .. w1 .. str:sub(npos), npos
end

return util