Text utility cleanup

This commit is contained in:
Lars Mueller 2020-12-18 12:27:19 +01:00
parent 1e4f795691
commit 8a6c22d107

138
text.lua

@ -1,123 +1,116 @@
-- String helpers - split & trim at end & begin function upper_first(text) return text:sub(1, 1):upper() .. text:sub(2) end
function upper_first(str)
return str:sub(1,1):upper()..str:sub(2) function lower_first(text) return text:sub(1, 1):lower() .. text:sub(2) end
end
function lower_first(str) function starts_with(text, start) return text:sub(1, start:len()) == start end
return str:sub(1,1):lower()..str:sub(2)
end function ends_with(text, suffix) return text:sub(text:len() - suffix:len() + 1) == suffix end
function starts_with(str, start) return str:sub(1, start:len()) == start end
function ends_with(str, suffix) function trim(text, to_remove)
return str:sub(str:len() - suffix:len() + 1) == suffix
end
function trim(str, to_remove)
local j = 1 local j = 1
for i = 1, string.len(str) do for i = 1, string.len(text) do
if str:sub(i, i) ~= to_remove then if text:sub(i, i) ~= to_remove then
j = i j = i
break break
end end
end end
local k = 1 local k = 1
for i = string.len(str), j, -1 do for i = string.len(text), j, -1 do
if str:sub(i, i) ~= to_remove then if text:sub(i, i) ~= to_remove then
k = i k = i
break break
end end
end end
return text:sub(j, k)
return str:sub(j, k)
end end
function trim_begin(str, to_remove) function trim_begin(text, to_remove)
local j = 1 local j = 1
for i = 1, string.len(str) do for i = 1, string.len(text) do
if str:sub(i, i) ~= to_remove then if text:sub(i, i) ~= to_remove then
j = i j = i
break break
end end
end end
return text:sub(j)
return str:sub(j)
end end
trim_left = trim_begin trim_left = trim_begin
function trim_end(str, to_remove) function trim_end(text, to_remove)
local k = 1 local k = 1
for i = string.len(str), 1, -1 do for i = string.len(text), 1, -1 do
if str:sub(i, i) ~= to_remove then if text:sub(i, i) ~= to_remove then
k = i k = i
break break
end end
end end
return str:sub(1, k) return text:sub(1, k)
end end
trim_right = trim_end trim_right = trim_end
function split(str, delim, limit, regex) function split(text, delimiter, limit, is_regex)
if not limit then return split_without_limit(str, delim, regex) end limit = limit or math.huge
local no_regex = not regex local no_regex = not is_regex
local parts = {} local parts = {}
local occurences = 1 local occurences = 1
local last_index = 1 local last_index = 1
local index = string.find(str, delim, 1, no_regex) local index = string.find(text, delimiter, 1, no_regex)
while index and occurences < limit do while index and occurences < limit do
table.insert(parts, string.sub(str, last_index, index - 1)) table.insert(parts, string.sub(text, last_index, index - 1))
last_index = index + string.len(delim) last_index = index + string.len(delimiter)
index = string.find(str, delim, index + string.len(delim), no_regex) index = string.find(text, delimiter, index + string.len(delimiter), no_regex)
occurences = occurences + 1 occurences = occurences + 1
end end
table.insert(parts, string.sub(str, last_index)) table.insert(parts, string.sub(text, last_index))
return parts return parts
end end
function split_without_limit(str, delim, regex) function split_without_limit(text, delimiter, is_regex) return split(text, delimiter, nil, is_regex) end
local no_regex = not regex
local parts = {}
local last_index = 1
local index = string.find(str, delim, 1, no_regex)
while index do
table.insert(parts, string.sub(str, last_index, index - 1))
last_index = index + string.len(delim)
index = string.find(str, delim, index + string.len(delim), no_regex)
end
table.insert(parts, string.sub(str, last_index))
return parts
end
split_unlimited = split_without_limit split_unlimited = split_without_limit
function split_lines(str, limit) function split_lines(text, limit) return modlib.text.split(text, "\r?\n", limit, true) end
modlib.text.split(str, "\r?\n", limit, true)
end
hashtag = string.byte("#") function lines(text) return text:gmatch"[^\r\n]*" end
zero = string.byte("0")
nine = string.byte("9") local hashtag = string.byte"#"
letter_a = string.byte("A") local zero = string.byte"0"
letter_f = string.byte("F") local nine = string.byte"9"
local letter_a = string.byte"A"
local letter_f = string.byte"F"
function is_hexadecimal(byte) function is_hexadecimal(byte)
return (byte >= zero and byte <= nine) or return byte >= zero and byte <= nine or byte >= letter_a and byte <= letter_f
(byte >= letter_a and byte <= letter_f)
end end
magic_chars = { magic_chars = {
"%", "(", ")", ".", "+", "-", "*", "?", "[", "^", "$" --[[,":"]] "%",
"(",
")",
".",
"+",
"-",
"*",
"?",
"[",
"^",
"$"
} }
local magic_charset = {}
for _, magic_char in pairs(magic_chars) do table.insert(magic_charset, "%" .. magic_char) end
magic_charset = "[" .. table.concat(magic_charset) .. "]"
function escape_magic_chars(text) function escape_magic_chars(text) return text:gsub("(" .. magic_charset .. ")", "%%%1") end
for _, magic_char in ipairs(magic_chars) do
text = string.gsub(text, "%" .. magic_char, "%%" .. magic_char)
end
return text
end
function utf8(number) function utf8(number)
if number < 0x007F then return string.char(number) end if number < 0x007F then
if number < 0x00A0 or number > 0x10FFFF then -- Out of range -- Single byte
return string.char(number)
end
if number < 0x00A0 or number > 0x10FFFF then
-- Out of range
return return
end end
local result = "" local result = ""
@ -129,17 +122,17 @@ function utf8(number)
i = i + 1 i = i + 1
if number <= math.pow(2, 8 - i - 2) then break end if number <= math.pow(2, 8 - i - 2) then break end
end end
return string.char(256 - math.pow(2, 8 - i - 1) + number) .. result -- 256 = math.pow(2, 8) return string.char(256 - math.pow(2, 8 - i - 1) + number) .. result
end end
--+ deprecated
function handle_ifdefs(code, vars) function handle_ifdefs(code, vars)
local finalcode = {} local finalcode = {}
local endif local endif
local after_endif = -1 local after_endif = -1
local ifdef_pos, after_ifdef = string.find(code, "--IFDEF", 1, true) local ifdef_pos, after_ifdef = string.find(code, "--IFDEF", 1, true)
while ifdef_pos do while ifdef_pos do
table.insert(finalcode, table.insert(finalcode, string.sub(code, after_endif + 2, ifdef_pos - 1))
string.sub(code, after_endif + 2, ifdef_pos - 1))
local linebreak = string.find(code, "\n", after_ifdef + 1, true) local linebreak = string.find(code, "\n", after_ifdef + 1, true)
local varname = string.sub(code, after_ifdef + 2, linebreak - 1) local varname = string.sub(code, after_ifdef + 2, linebreak - 1)
endif, after_endif = string.find(code, "--ENDIF", linebreak + 1, true) endif, after_endif = string.find(code, "--ENDIF", linebreak + 1, true)
@ -147,8 +140,7 @@ function handle_ifdefs(code, vars)
if vars[varname] then if vars[varname] then
table.insert(finalcode, string.sub(code, linebreak + 1, endif - 1)) table.insert(finalcode, string.sub(code, linebreak + 1, endif - 1))
end end
ifdef_pos, after_ifdef = string.find(code, "--IFDEF", ifdef_pos, after_ifdef = string.find(code, "--IFDEF", after_endif + 1, true)
after_endif + 1, true)
end end
table.insert(finalcode, string.sub(code, after_endif + 2)) table.insert(finalcode, string.sub(code, after_endif + 2))
return table.concat(finalcode, "") return table.concat(finalcode, "")