Created
March 24, 2016 09:55
-
-
Save moogoo78/604736ec042cbb2bf1fc to your computer and use it in GitHub Desktop.
count length of multi-byte string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function mb_len(s) | |
local c = 0 -- 判斷字寬, 中文: 2, 西歐字母: 1 | |
local c2 = 0 -- 判斷字元 (中文大部分3byte, TODO) | |
local add_tail = false | |
local r = "" | |
for i in s:gmatch("[\1-\127\194-\244][\128-\191]*") do | |
if string.byte(i) > 127 then | |
c = c + 2 | |
c2 = c2 + 3 | |
else | |
c = c + 1 | |
c2 = c2 + 1 | |
end | |
end | |
return c, c2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment