Logo

Programming-Idioms

History of Idiom 169 > diff from v114 to v115

Edit summary for version 115 by abcdefg233:
New Lua implementation by user [abcdefg233]

Version 114

2022-12-12, 10:38:13

Version 115

2024-04-01, 20:13:12

Idiom #169 String length

Assign to the integer n the number of characters of the string s.
Make sure that multibyte characters are properly handled.
n can be different from the number of bytes of s.

Idiom #169 String length

Assign to the integer n the number of characters of the string s.
Make sure that multibyte characters are properly handled.
n can be different from the number of bytes of s.

Variables
n,s
Variables
n,s
Extra Keywords
size characters chars number runes
Extra Keywords
size characters chars number runes
Code
local utf8={}
function utf8.bytes(str,index)
 local byte=string.byte(str,index)
 local ret
 if     byte==nil then ret=0
 elseif byte<128  then ret=1
 elseif byte<192  then ret=2
 elseif byte<224  then ret=3
 else                  ret=4
 end
 return ret
end
function utf8.len(str)
 local count=0
 local fini=#str+1
 local index=1
 while index~=fini do
  count=count+1
  index=index+utf8.bytes(str,index)
 end
 return count
end
local n=utf8.len(s)
Comments bubble
Use for lua version under 5.3. (which doesn't have utf8 lib)