heonian-language-exercises/strings.lua

63 lines
1.6 KiB
Lua

function string.split(str, find, tbl)
split = tbl or {}
local p = 0
local continue = true
while continue do
word = string.sub(str,p,string.find(str,find,p+1,-1))
table.insert(split,word)
if string.find(str,find,p) then
str = string.sub(str,p)
p = string.find(str,find,p)
continue = true
p = p + 1
else
continue = false
end
end
return split
end
function strip_symbols(str)
local symbol_table = "'~()!?:></.,\t"
for i=1, #symbol_table do
while string.find(str, "%"..string.sub(symbol_table,i,i)) do
str = string.gsub(str, "%"..string.sub(symbol_table,i,i),"")
end
end
return str
end
function find_n_replace(str, find,repl, caution)
if string.find(str, find) then
if caution then
if not string.find(str, caution, string.find(str, find)+string.len(find)-string.len(caution)) then -- sometimes you want to avoid certain false positives
str = string.gsub(str,find, repl)
end
else
str = string.gsub(str,find, repl)
end
end
return str
end
function minimum_apostrophes(str)
local restr = ""
for i = 1, #str do
if string.sub(str,i,i) == "'" then -- only makes sense to separate vowels from vowels
if string.find(string.sub(str,i-1,i-1),"[aeiou]") then
if string.find(string.sub(str,i+1,i+1),"[aeiou]") then
restr = restr .. string.sub(str,i,i)
end
else -- only consontant meaningful of checking is n'y opposed to ny
if string.find(string.sub(str,i-1,i-1),"[n]")
and string.find(string.sub(str,i+1,i+1),"[y]") then
restr = restr .. string.sub(str,i,i)
end
end
else
restr = restr .. string.sub(str,i,i)
end
end
return restr
end