[寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本 *** 作 UTF8字符串长度,UTF8字符串剪裁等

[寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本 *** 作 UTF8字符串长度,UTF8字符串剪裁等,第1张

概述原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net/qq446569365 一个用于UTF8字符串 *** 作的类,功能比较齐全,包括: string.utf8len UTF8字符串长度 string.utf8sub 对UTF8字符串进行切割 string.utf8upper 大写转换 string.utf8lower 小

原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列]

博客地址:http://blog.csdn.net/qq446569365

一个用于UTF8字符串 *** 作的类,功能比较齐全,包括:

string.utf8len UTF8字符串长度

string.utf8sub 对UTF8字符串进行切割

string.utf8upper 大写转换

string.utf8lower 小写转换

string.utf8reverse 字符串逆转


Github下载地址:https://github.com/ArcherPeng/utf8String4LUA


--===========================----===========================---- $ID: utf8.lua 147 2007-01-04 00:57:00Z pasta $---- ProvIDes UTF-8 aware string functions implemented in pure lua:-- * string.utf8len(s)-- * string.utf8sub(s,i,j)-- * string.utf8reverse(s)---- If utf8data.lua (containing the lower<->upper case mapPings) is loaded,these-- additional functions are available:-- * string.utf8upper(s)-- * string.utf8lower(s)---- All functions behave as their non UTF-8 aware counterparts with the exception-- that UTF-8 characters are used instead of bytes for all units.--[[copyright (c) 2006-2007,Kyle SmithAll rights reserved.Redistribution and use in source and binary forms,with or withoutmodification,are permitted provIDed that the following conditions are met:    * Redistributions of source code must retain the above copyright notice,this List of conditions and the following disclaimer.    * Redistributions in binary form must reproduce the above copyright      notice,this List of conditions and the following disclaimer in the      documentation and/or other materials provIDed with the distribution.    * Neither the name of the author nor the names of its contributors may be      used to endorse or promote products derived from this software without      specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE copYRIGHT HolDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES,INCLUDING,BUT NOT liMITED TO,THEIMPLIED WARRANTIES OF MERCHANTABIliTY AND fitness FOR A PARTIculaR PURPOSE AREdisCLaimED. IN NO EVENT SHALL THE copYRIGHT OWNER OR CONTRIBUTORS BE liABLEFOR ANY DIRECT,INDIRECT,INCIDENTAL,SPECIAL,EXEMPLARY,OR CONSEQUENTIALdamAGES (INCLUDING,PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE,DATA,OR PROFITS; OR BUSInesS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF liABIliTY,WHETHER IN CONTRACT,STRICT liABIliTY,OR TORT (INCLUDING NEGliGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBIliTY OF SUCH damAGE.--]]-- ABNF from RFC 3629-- -- UTF8-octets = *( UTF8-char )-- UTF8-char   = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4-- UTF8-1      = %x00-7F-- UTF8-2      = %xC2-DF UTF8-tail-- UTF8-3      = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /--               %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )-- UTF8-4      = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /--               %xF4 %x80-8F 2( UTF8-tail )-- UTF8-tail   = %x80-BF-- -- returns the number of bytes used by the UTF-8 character at byte i in s-- also doubles as a UTF-8 character valIDatorlocal function utf8charbytes (s,i)	-- argument defaults	i = i or 1	-- argument checking	if type(s) ~= "string" then		error("bad argument #1 to 'utf8charbytes' (string expected,got ".. type(s).. ")")	end	if type(i) ~= "number" then		error("bad argument #2 to 'utf8charbytes' (number expected,got ".. type(i).. ")")	end	local c = s:byte(i)	-- determine bytes needed for character,based on RFC 3629	-- valIDate byte 1	if c > 0 and c <= 127 then		-- UTF8-1		return 1	elseif c >= 194 and c <= 223 then		-- UTF8-2		local c2 = s:byte(i + 1)		if not c2 then			error("UTF-8 string terminated early")		end		-- valIDate byte 2		if c2 < 128 or c2 > 191 then			error("InvalID UTF-8 character")		end		return 2	elseif c >= 224 and c <= 239 then		-- UTF8-3		local c2 = s:byte(i + 1)		local c3 = s:byte(i + 2)		if not c2 or not c3 then			error("UTF-8 string terminated early")		end		-- valIDate byte 2		if c == 224 and (c2 < 160 or c2 > 191) then			error("InvalID UTF-8 character")		elseif c == 237 and (c2 < 128 or c2 > 159) then			error("InvalID UTF-8 character")		elseif c2 < 128 or c2 > 191 then			error("InvalID UTF-8 character")		end		-- valIDate byte 3		if c3 < 128 or c3 > 191 then			error("InvalID UTF-8 character")		end		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)		if not c2 or not c3 or not c4 then			error("UTF-8 string terminated early")		end		-- valIDate byte 2		if c == 240 and (c2 < 144 or c2 > 191) then			error("InvalID UTF-8 character")		elseif c == 244 and (c2 < 128 or c2 > 143) then			error("InvalID UTF-8 character")		elseif c2 < 128 or c2 > 191 then			error("InvalID UTF-8 character")		end				-- valIDate byte 3		if c3 < 128 or c3 > 191 then			error("InvalID UTF-8 character")		end		-- valIDate byte 4		if c4 < 128 or c4 > 191 then			error("InvalID UTF-8 character")		end		return 4	else		error("InvalID UTF-8 character")	endend-- returns the number of characters in a UTF-8 stringlocal function utf8len (s)	-- argument checking	if type(s) ~= "string" then		error("bad argument #1 to 'utf8len' (string expected,got ".. type(s).. ")")	end	local pos = 1	local bytes = s:len()	local len = 0	while pos <= bytes and len ~= chars do		local c = s:byte(pos)		len = len + 1		pos = pos + utf8charbytes(s,pos)	end	if chars ~= nil then		return pos - 1	end	return lenend-- install in the string libraryif not string.utf8len then	string.utf8len = utf8lenend-- functions IDentically to string.sub except that i and j are UTF-8 characters-- instead of byteslocal function utf8sub (s,j)	-- argument defaults	j = j or -1	-- argument checking	if type(s) ~= "string" then		error("bad argument #1 to 'utf8sub' (string expected,got ".. type(s).. ")")	end	if type(i) ~= "number" then		error("bad argument #2 to 'utf8sub' (number expected,got ".. type(i).. ")")	end	if type(j) ~= "number" then		error("bad argument #3 to 'utf8sub' (number expected,got ".. type(j).. ")")	end	local pos = 1	local bytes = s:len()	local len = 0	-- only set l if i or j is negative	local l = (i >= 0 and j >= 0) or s:utf8len()	local startChar = (i >= 0) and i or l + i + 1	local endChar   = (j >= 0) and j or l + j + 1	-- can't have start before end!	if startChar > endChar then		return ""	end	-- byte offsets to pass to string.sub	local startByte,endByte = 1,bytes	while pos <= bytes do		len = len + 1		if len == startChar then			startByte = pos		end		pos = pos + utf8charbytes(s,pos)		if len == endChar then			endByte = pos - 1			break		end	end	return s:sub(startByte,endByte)end-- install in the string libraryif not string.utf8sub then	string.utf8sub = utf8subend-- replace UTF-8 characters based on a mapPing tablelocal function utf8replace (s,mapPing)	-- argument checking	if type(s) ~= "string" then		error("bad argument #1 to 'utf8replace' (string expected,got ".. type(s).. ")")	end	if type(mapPing) ~= "table" then		error("bad argument #2 to 'utf8replace' (table expected,got ".. type(mapPing).. ")")	end	local pos = 1	local bytes = s:len()	local charbytes	local newstr = ""	while pos <= bytes do		charbytes = utf8charbytes(s,pos)		local c = s:sub(pos,pos + charbytes - 1)		newstr = newstr .. (mapPing[c] or c)		pos = pos + charbytes	end	return newstrend-- IDentical to string.upper except it kNows about unicode simple case conversionslocal function utf8upper (s)	return utf8replace(s,utf8_lc_uc)end-- install in the string libraryif not string.utf8upper and utf8_lc_uc then	string.utf8upper = utf8upperend-- IDentical to string.lower except it kNows about unicode simple case conversionslocal function utf8lower (s)	return utf8replace(s,utf8_uc_lc)end-- install in the string libraryif not string.utf8lower and utf8_uc_lc then	string.utf8lower = utf8lowerend-- IDentical to string.reverse except that it supports UTF-8local function utf8reverse (s)	-- argument checking	if type(s) ~= "string" then		error("bad argument #1 to 'utf8reverse' (string expected,got ".. type(s).. ")")	end	local bytes = s:len()	local pos = bytes	local charbytes	local newstr = ""	while pos > 0 do		c = s:byte(pos)		while c >= 128 and c <= 191 do			pos = pos - 1			c = s:byte(pos)		end		charbytes = utf8charbytes(s,pos)		newstr = newstr .. s:sub(pos,pos + charbytes - 1)		pos = pos - 1	end	return newstrend-- install in the string libraryif not string.utf8reverse then	string.utf8reverse = utf8reverseend
总结

以上是内存溢出为你收集整理的[寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本 *** 作 UTF8字符串长度,UTF8字符串剪裁等全部内容,希望文章能够帮你解决[寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本 *** 作 UTF8字符串长度,UTF8字符串剪裁等所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1066763.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存