refactor: move reverse_text to strings.c as it's a string operation

Also add tests for reverse_text.

Co-authored-by: Kalle Ranki <kalle.ranki@gmail.com>
This commit is contained in:
Dundar Goc
2022-05-20 11:54:39 +02:00
parent 1d160a76ec
commit 1a0de90068
4 changed files with 80 additions and 25 deletions

View File

@@ -150,3 +150,38 @@ describe('strcase_save()' , function()
eq("a", strcase_save("\xc1\x81", false))
end)
end)
describe("reverse_text", function()
local reverse_text = function(str)
return helpers.internalize(strings.reverse_text(to_cstr(str)))
end
itp("handles empty string", function()
eq("", reverse_text(""))
end)
itp("handles simple cases", function()
eq("a", reverse_text("a"))
eq("ba", reverse_text("ab"))
end)
itp("handles multibyte characters", function()
eq("bα", reverse_text("αb"))
eq("Yötön yö", reverse_text("öy nötöY"))
end)
itp("handles combining chars", function()
local utf8_COMBINING_RING_ABOVE = "\204\138"
local utf8_COMBINING_RING_BELOW = "\204\165"
eq("bba" .. utf8_COMBINING_RING_ABOVE .. utf8_COMBINING_RING_BELOW .. "aa",
reverse_text("aaa" .. utf8_COMBINING_RING_ABOVE .. utf8_COMBINING_RING_BELOW .. "bb"))
end)
itp("treats invalid utf as separate characters", function()
eq("\192ba", reverse_text("ab\192"))
end)
itp("treats an incomplete utf continuation sequence as valid", function()
eq("\194ba", reverse_text("ab\194"))
end)
end)