[寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本操作 UTF8字符串长度,UTF8字符串剪裁等
原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的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 Smith
All rights reserved. Redistribution and use in source and binary forms, with or without
modification, 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, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF 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 validator
local 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")
end
end -- returns the number of characters in a UTF-8 string
local 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 len
end -- install in the string library
if not string.utf8len then
string.utf8len = utf8len
end -- functions identically to string.sub except that i and j are UTF-8 characters
-- instead of bytes
local function utf8sub (s, i, 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 library
if not string.utf8sub then
string.utf8sub = utf8sub
end -- replace UTF-8 characters based on a mapping table
local 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 newstr
end -- identical to string.upper except it knows about unicode simple case conversions
local function utf8upper (s)
return utf8replace(s, utf8_lc_uc)
end -- install in the string library
if not string.utf8upper and utf8_lc_uc then
string.utf8upper = utf8upper
end -- identical to string.lower except it knows about unicode simple case conversions
local function utf8lower (s)
return utf8replace(s, utf8_uc_lc)
end -- install in the string library
if not string.utf8lower and utf8_uc_lc then
string.utf8lower = utf8lower
end -- identical to string.reverse except that it supports UTF-8
local 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 newstr
end -- install in the string library
if not string.utf8reverse then
string.utf8reverse = utf8reverse
end
[寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本操作 UTF8字符串长度,UTF8字符串剪裁等的更多相关文章
- [寒江孤叶丶的Cocos2d-x之旅_32]微信输入框风格的IOS平台的EditBox
原创文章,欢迎转载.转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net/qq446569365 偶然看到一个游戏注冊账号时候,输入框是弹 ...
- [寒江孤叶丶的Cocos2d-x之旅_33]RichTextEx一款通过HTML标签控制文字样式的富文本控件
RichTextEx一款通过HTML标签控制文字样式的富文本控件 原创文章,欢迎转载.转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net ...
- ERROR: Symbol file could not be found 寒江孤钓<<windows 内核安全编程>> 学习笔记
手动下载了Symbols,设置好了Symbols File Path,串口连接上了以后,出现ERROR: Symbol file could not be found, 并且会一直不停的出现windb ...
- Debuggee not connected 寒江孤钓<<windows 内核安全编程>> 学习笔记
双机联调出现的问题 真机系统win7 虚拟机系统xp 安装书中的配置一步一步走,发现最后启动系统后,windbg一直显示 Opened \\.\pipe\com_1Waiting to reconne ...
- 指定的服务已标记为删除 寒江孤钓<<windows 内核安全编程>> 学习笔记
运行cmd:"sc delete first" 删除我们的服务之后, 再次创建这个服务的时候出现 "指定的服务已标记为删除"的错误, 原因是我们删除服务之前没有 ...
- 删除自定义服务 寒江孤钓<<windows 内核安全编程>> 学习笔记
书中第一章 成功启动first服务之后, 发现书中并没有介绍删除服务的方式, SRVINSTW工具中的移除服务功能,也无法找到我们要删除的服务, 于是,搜素了下,发现解决方法如下: 在cmd中输入 & ...
- 发生系统错误 1275.此驱动程序被阻止加载 寒江孤钓<<windows 内核安全编程>> 学习笔记
安装书中第一章成功安装first服务之后,在cmd窗口使用命令行 "net start first" 时, 出现 "发生系统错误 1275.此驱动程序被阻止加载" ...
- 无法编译出.sys文件 寒江孤钓<<windows 内核安全编程>> 学习笔记
系统环境:win7 编译环境:Windows Win7 IA-64 Checked Build Environment 按照书中所说的步骤,出现如下问题 后来直接使用光盘源码,编译成功,于是对照源文件 ...
- Cocos2D iOS之旅:如何写一个敲地鼠游戏(十一):完善游戏逻辑
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
随机推荐
- OpenJDK源码研究笔记(十一):浅析Javac编译过程中的抽象语法树(IfElse,While,Switch等语句的抽象和封装)
浅析OpenJDK源码编译器Javac的语法树包com.sun.source.tree. 抽象语法树,是编译原理中的经典问题,有点难,本文只是随便写写. 0.赋值语句 public interface ...
- Java 实现有序链表
有序链表: 按关键值排序. 删除链头时,就删除最小(/最大)的值,插入时,搜索插入的位置. 插入时须要比較O(N),平均O(N/2),删除最小(/最大)的在链头的数据时效率为O(1), 假设一个应用须 ...
- HDU 5281 Senior's Gun 杀怪
题意:给出n把枪和m个怪.每把枪有一个攻击力,每一个怪有一个防御力.假设某把枪的攻击力不小于某个怪的防御力则能将怪秒杀,否则无法杀死.一把枪最多仅仅能杀一个怪,不能用多把枪杀同一个怪.每杀一次怪能够得 ...
- LinkedIn微服务框架rest.li
linkedin/rest.li https://github.com/linkedin/rest.li LinkedIn微服务框架rest.li摘要:Rest.li是一款REST+JSON框架,使 ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(5)-AsyncTask异步任务
说明:本文的文字和代码,主要来自于网上的2篇文章. 第4篇的时候,提到了异步任务AsyncTask. 网上找了2篇文章学习下,copy网友的代码,稍微改了几个字,运行成功了. 在开发Android移动 ...
- echarts插件-从后台请求的数据在页面显示空白的问题
最近的项目里面关于统计图方面的问题,有涉及到很多,也在博客里面更新了自己所遇到的问题,开发过程中会遇到很多问题,解决技术问题的方法也有千千万 图片.png 在百度上百度了一下,发现了问题所在之处,不得 ...
- STM32之串口IAP更新升级
一.IAP简介 IAP是应用编程,目的是为了在产品发布后可以方便地通过预留的通信口对产品中的固件程序进行更新升级,后续产品发布后,更新程序我只需要把.bin文件通过串口发送给芯片就可以执行更 新,很方 ...
- 【Henu ACM Round #12 D】 Longest Subsequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 记录每个数字出现的次数cnt[x]; (大于1e6的直接忽略) 另外用一个数组z[1e6] 然后for枚举x 第二层for枚举x的倍 ...
- WPF转换器
1. 前文 在普遍的也业务系统中, 数据要驱动到操作的用户界面, 它实际储存的方式和表达方式会多种多样, 数据库存储的数字 0或1, 在界面用户看到显示只是 成功或失败, 或者存储的字符.或更多的格式 ...
- JS-网页中分页栏
原理 三部分 我给分页栏分成了3部分 上一页:调用prePage()函数 下一页:调用nextPage()函数 带有数字标识的部,调用skipPage()函数 prePage函数 function p ...