Home > other >  Lua implementation special characters filtering
Lua implementation special characters filtering

Time:09-17

The function filter_spec_chars (s, charTypes)
The local ss={}
The local k=1
While true do
If k & gt; # s then break end
The local c=string. Byte (s, k)
If not then c break end
If cIf (c>=48 and c<=57) then
If charTypes. Num then
Table. The insert (ss, string. Char (c))
End
Elseif (c>=65 and c<=90) or (c>=97 and c<=122) then
If charTypes. Char then
Table. The insert (ss, string. Char (c))
End
End
K=k + 1
Elseif cK=k + 2
Elseif cIf c>=228 and cLocal c1=string, byte (s, k + 1)
The local c2=string. Byte (s, k + 2)
If c1 and c2 then
The local a1, a2, a3, a4=128191128191
If c==228 then a1=184
Elseif c==233 then a2, a4=190, c1 ~=190 and 191 or 165
End
If c1 & gt;=a1 and c1 & lt;=a2 and c2 & gt;=a3 and c2 & lt;=a4 then
If charTypes. CHS then
Table. The insert (ss, string. Char (c, c1, c2))
End
End
End
End
K=k + 3
Elseif cK=k + 4
Elseif cK=k + 5
Elseif cK=k + 6
End
End
Return to the table. The concat (ss)
End

The local function main ()
Print (filter_spec_chars (" hello OI + 123) * & amp; ^ # @ 163. com ", {num="number", char="letters", CHS="Chinese"}))
End
The main ()

CodePudding user response:

You this kind of thing is not practical, several filtering M file will be stuck

CodePudding user response:

reference 1st floor lovesmiles response:
you this kind of thing is not practical, filters will be stuck a few M file


Actually, also can't say that, let the lua using scanning method to deal with a few M file itself is not reasonable, should not expect a script language and C as powerful,
I guess the original poster is to want to deal with dozens of character string

CodePudding user response:

- 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.
The local surrogateMin=55296-0 xd800
The local surrogateMax=57343-0 XDFFF
The local rune1Max=127-1 & lt; <7-1
The local rune2Max=2047-1 & lt; <11-1
The local rune3Max=65535-1 & lt; <16-1
Local MaxRune=1114111 - "\ U0010FFFF" - Maximum valid Unicode code point.
The function RuneLen (r)
If r & lt; 0 then
The return - 1
Elseif r & lt;=rune1Max then
Return 1
Elseif r & lt;=rune2Max then
Return 2
Elseif surrogateMin & lt;=r and r & lt;=surrogateMax then
The return - 1
Elseif r & lt;=rune3Max then
Return 3
Elseif r & lt;=MaxRune then
Return 4
The else
The return - 1
End
End

The local STR="a you?? B good 3?? C
"For p and c in utf8. Codes (STR) do
Print (" # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ")
Print (p.. "=".. C.. "(".. RuneLen (c).. ") ")
end
  • Related