function filter_spec_chars(s,charTypes)
local ss = {}
local k = 1
while true do
if k > #s then break end
local c = string.byte(s,k)
if not c then break end
if c<192 then
if (c>=48 and c<=57) then
if charTypes.num then
table.insert(ss, string.char(c))
end
elseif (c>= 65 and c<=90) or (c>=97 and c<=122) then
if charTypes.char then
table.insert(ss, string.char(c))
end
end
k = k + 1
elseif c<224 then
k = k + 2
elseif c<240 then
if c>=228 and c<=233 then
local c1 = string.byte(s,k+1)
local c2 = string.byte(s,k+2)
if c1 and c2 then
local a1,a2,a3,a4 = 128,191,128,191
if c == 228 then a1 = 184
elseif c == 233 then a2,a4 = 190,c1 ~= 190 and 191 or 165
end
if c1>=a1 and c1<=a2 and c2>=a3 and c2<=a4 then
if charTypes.chs then
table.insert(ss, string.char(c,c1,c2))
end
end
end
end
k = k + 3
elseif c<248 then
k = k + 4
elseif c<252 then
k = k + 5
elseif c<254 then
k = k + 6
end
end
return table.concat(ss)
end
local function main()
print(filter_spec_chars("123你好OI+)*&^#@163.com", {num="數字",char="字母",chs="中文"}))
end
main()
uj5u.com熱心網友回復:
你這種東西不實用,幾個M檔案的過濾會卡死uj5u.com熱心網友回復:
其實也不能這么說,讓lua使用掃描的方法去處理幾M的檔案本身就不合理。一個腳本語言不應該期望和C一樣強大。
我猜樓主是想處理幾十個字符的字串吧
uj5u.com熱心網友回復:
-- RuneLen returns the number of bytes required to encode the rune.-- It returns -1 if the rune is not a valid value to encode in UTF-8.
local surrogateMin = 55296 --0xD800
local surrogateMax =57343 -- 0xDFFF
local rune1Max = 127 --1<<7 - 1
local rune2Max = 2047 -- 1<<11 - 1
local rune3Max =65535 -- 1<<16 - 1
local MaxRune = 1114111-- "\U0010FFFF" -- Maximum valid Unicode code point.
function RuneLen(r)
if r < 0 then
return -1
elseif r <= rune1Max then
return 1
elseif r <= rune2Max then
return 2
elseif surrogateMin <= r and r <= surrogateMax then
return -1
elseif r <= rune3Max then
return 3
elseif r <= MaxRune then
return 4
else
return -1
end
end
local str="a你??b好3??c"
for p, c in utf8.codes(str) do
print("#############################################")
print(p .."=".. c .."("..RuneLen(c)..")")
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/42568.html
標籤:Cocos2d-x
上一篇:新人求教,NGUI問題
下一篇:Pygame寫圖片配對游戲
