diff options
| author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-02-01 04:04:55 -0800 |
|---|---|---|
| committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-02-01 04:04:55 -0800 |
| commit | 8160ad061328dfd40696b1c36c8aa911062cf8a4 (patch) | |
| tree | 422adf3c58a020bdb01e8a0699fb9f7d083b852f /util.lua | |
| parent | f7e64e9c6a7dd22d16ac15adf4f86714b5b993ef (diff) | |
| download | mpv-iptv-menu-8160ad061328dfd40696b1c36c8aa911062cf8a4.tar.gz mpv-iptv-menu-8160ad061328dfd40696b1c36c8aa911062cf8a4.tar.xz | |
move string seek/transform operations to util
Diffstat (limited to 'util.lua')
| -rw-r--r-- | util.lua | 102 |
1 files changed, 102 insertions, 0 deletions
@@ -125,4 +125,106 @@ function util.stable_kmerge(t, u, k) return res 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 |
