I know that Lua just treats strings as a series of bytes, so there is no inherent ability to get the number of characters in a Unicode string, or to iterate through the characters.
Before I get too deep into writing a solution that solves my use case, has anyone already solved this problem? Or is there a Lua library somewhere on the internet that would let me get the individual characters in a Unicode string?
Lua have functions to work with strings:http://lua-users.org/wiki/StringLibraryTutorial . You can get string lenght by sharp: #yourstring
Thanks for trying to be helpful, but the standard Lua string functions don’t work with Unicode strings.
2 Likes
Hi Ragnar, yes I did find that page. I was making my way through it but thought to post here to see if an easier solution exists.
The UTF8 decode function they list there was throwing me errors, so I still need to figure out why. I suspect my character set is multibyte, not UTF8… or something
[Edit] I missed the libraries they list at the end of that page. I’ll take a closer look. Thanks!
[Edit] Thanks everyone. I ended up going with this pure Lua solution: https://github.com/Stepets/utf8.lua/blob/master/utf8.lua
The output is still a mess if I try to use print(), but I can pass a character or substring to gui.get_text_metrics() and get the right results now, which is all I needed
3 Likes