- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
utf8 = {}
function utf8.charbytes(s, i)
-- argument defaults
i = i or 1
local c = string.byte(s, i)
-- determine bytes needed for character, based on RFC 3629
if c > 0 and c <= 127 then
-- UTF8-1
return 1
elseif c >= 194 and c <= 223 then
-- UTF8-2
local c2 = string.byte(s, i + 1)
return 2
elseif c >= 224 and c <= 239 then
-- UTF8-3
local c2 = s:byte(i + 1)
local c3 = s:byte(i + 2)
return 3
elseif c >= 240 and c <= 244 then
-- UTF8-4
local c2 = s:byte(i + 1)
local c3 = s:byte(i + 2)
local c4 = s:byte(i + 3)
return 4
end
end
function utf8.len(s)
local pos = 1
local bytes = string.len(s)
local len = 0
while pos <= bytes and len ~= chars do
local c = string.byte(s,pos)
len = len + 1
pos = pos + utf8.charbytes(s, pos)
end
if chars ~= nil then
return pos - 1
end
return len
end
print(utf8.len("Аутизм")) --6
local mt = {__len = function(self)
local k=0
for _,v in pairs(self) do
k = k+1;
if k > 1 then print(1) return #self end
end
if k==1 and type(self[1])=="string" then
print(utf8.len(self[k]))
end
end};
setmetatable(_G,{
__index = function(self, key) return setmetatable({key},mt) end}
)
t=(#Аутизм) --12
3oJIoTou_xyu 22.05.2019 13:40 # +1
guest8 22.05.2019 14:50 # −999
Orange_Ikarus 22.05.2019 19:32 # +1
TOPT 24.05.2019 04:28 # +1
Rooster 22.05.2019 13:45 # +1
guest8 29.05.2019 02:47 # −999
unicorn 22.05.2019 20:27 # 0