53 lines
1.2 KiB
Lua
53 lines
1.2 KiB
Lua
local function remove(str, start, _end)
|
|
-- print(start, _end, str:sub(start, start), str:sub(_end, _end))
|
|
local start_part = str:sub(1, start - 1)
|
|
local end_part = str:sub(_end + 1, -1)
|
|
return start_part .. end_part
|
|
end
|
|
|
|
local function remove_patterns(str, pattern)
|
|
while true do
|
|
local rem_start, rem_end, _ = str:find(pattern)
|
|
if rem_start == nil then
|
|
break
|
|
end
|
|
-- print(str, pattern, rem_start, rem_end)
|
|
str = remove(str, rem_start, rem_end)
|
|
-- print(str)
|
|
end
|
|
|
|
return str
|
|
end
|
|
|
|
local function ispali(str)
|
|
str = str:lower()
|
|
|
|
-- format
|
|
str = remove_patterns(str, "%s")
|
|
str = remove_patterns(str, "%p")
|
|
|
|
for i = 1, utf8.len(str) do
|
|
local idx = utf8.offset(str, i)
|
|
local idx_rev = utf8.offset(str, -i)
|
|
local ch_code = utf8.codepoint(str, idx)
|
|
local ch_code_rev = utf8.codepoint(str, idx_rev)
|
|
if ch_code ~= ch_code_rev then
|
|
return false
|
|
end
|
|
end
|
|
|
|
return true
|
|
end
|
|
|
|
local function test(str)
|
|
print(("%s: %q"):format(str, ispali(str)))
|
|
end
|
|
|
|
test("step ön nö pets")
|
|
test("bänänä")
|
|
|
|
print()
|
|
|
|
test("step. ön nö pets!")
|
|
test("bänänä??")
|