text.split[_unlimited]: Use pattern by default

This commit is contained in:
Lars Mueller 2022-09-28 20:50:25 +02:00
parent 1244f1eba0
commit a5c49754d9
3 changed files with 8 additions and 7 deletions

@ -19,7 +19,7 @@ end
get_extension = split_extension
function split_path(filepath)
return modlib.text.split(filepath, dir_delim)
return modlib.text.split_unlimited(filepath, dir_delim, true)
end
-- concat_path is set by init.lua to avoid code duplication

@ -62,7 +62,7 @@ local warn_parent_leaf = "modlib: setting %s used both as parent setting and as
local function build_tree(dict)
local tree = {}
for key, value in pairs(dict) do
local path = modlib.text.split_unlimited(key, ".")
local path = modlib.text.split_unlimited(key, ".", true)
local subtree = tree
for i = 1, #path - 1 do
local index = tonumber(path[i]) or path[i]

@ -45,24 +45,25 @@ function hexdump(text)
return table.concat(dump)
end
function split(text, delimiter, limit, is_regex)
function split(text, delimiter, limit, plain)
limit = limit or math.huge
local no_regex = not is_regex
local parts = {}
local occurences = 1
local last_index = 1
local index = string.find(text, delimiter, 1, no_regex)
local index = string.find(text, delimiter, 1, plain)
while index and occurences < limit do
table.insert(parts, string.sub(text, last_index, index - 1))
last_index = index + string.len(delimiter)
index = string.find(text, delimiter, index + string.len(delimiter), no_regex)
index = string.find(text, delimiter, index + string.len(delimiter), plain)
occurences = occurences + 1
end
table.insert(parts, string.sub(text, last_index))
return parts
end
function split_without_limit(text, delimiter, is_regex) return split(text, delimiter, nil, is_regex) end
function split_without_limit(text, delimiter, plain)
return split(text, delimiter, nil, plain)
end
split_unlimited = split_without_limit