Number of characters in utf8 string

Place to get help with not working mods / modding interface.
Post Reply
User avatar
moon69
Fast Inserter
Fast Inserter
Posts: 181
Joined: Sun Sep 18, 2016 6:53 pm
Contact:

Number of characters in utf8 string

Post by moon69 »

Any suggestions on how to get the number of displayed characters in a lua string that may contain utf8 chars?

#str & string.len(str) return the number of bytes rather than number of characters.

There is the utf8 string module in lua 5.3, but it's not found in game (not sure if Factorio is still on lua 5.2.x or if it's disabled?).

I'm trying to force some display padding and sorting behaviours.

Qon
Smart Inserter
Smart Inserter
Posts: 2119
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: Number of characters in utf8 string

Post by Qon »

My first thought is to convert the string to . or A or some other known len() character with Lua patterns. Replace every character with the same and then measure length. Would be trivial in regex, but patterns must operate on the character level. Or look in documentation for a proper character length function if you don't want to make messy and bad code ;) .

Post Reply

Return to “Modding help”