Lua实现 计算 UTF8 字符串的长度
来源:互联网 发布:js foreach return 编辑:程序博客网 时间:2024/06/08 06:33
因为项目需要, 用Lua实现了一个取utf8字符串长度的方法
--此处用空间换时间local gs_utf8_look_for_table ={ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1}function chSize(char) if not char then printInfo("not char") return 0 else return gs_utf8_look_for_table[char] endend-- 计算utf8字符串字符数, 各种字符都按一个字符计算function utf8Len(str) local len = 0 local currentIndex = 1 local count_ = #str while currentIndex <= count_ do local char = string.byte(str, currentIndex) local currentCharIndex_ = chSize(char); currentIndex = currentIndex + currentCharIndex_ len = len +1 end return lenend
后来翻看quickcocos的functions.lua发现人家早就实现了(吐血三升)
--来自quickcocos的 functions.luafunction string.utf8len(input) local len = string.len(input) local left = len local cnt = 0 local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc} while left ~= 0 do local tmp = string.byte(input, -left) local i = #arr while arr[i] do if tmp >= arr[i] then left = left - i break end i = i - 1 end cnt = cnt + 1 end return cntend
2 0
- Lua实现 计算 UTF8 字符串的长度
- lua 计算utf8格式的字符串的长度
- Lua实现计算 UTF8 字符串的长度,每一个中文算一个字符
- Lua 计算 UTF8 字符串的长度,每一个中文算一个字符
- LUA获取utf8字符串长度
- LUA获取utf8字符串长度
- [寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本操作 UTF8字符串长度,UTF8字符串剪裁等
- js计算字符串长度,其中中文是utf8格式的
- 在Lua中计算含中文的字符串的长度
- 在Lua中计算含中文的字符串的长度
- 编程实现计算字符串的长度
- 编程实现计算字符串的长度
- lua中文字符串长度计算和截取
- Lua中含中文字符串长度计算
- 计算字符串的长度
- 计算字符串的长度
- 计算字符串的长度
- 计算字符串的长度
- JAVA设置当时时间大小
- C++第一次实验_C++三角形
- c语言指针占几个字节
- Android 使用SVN
- 无序的有重复数据的list变成有序的无重复数据的list
- Lua实现 计算 UTF8 字符串的长度
- Android 弹无虚发之第四弹:你应该掌握的Notification(Notification的通知栏常驻、Notification的各种样式、Notification点击无效)
- 深入理解Java内存模型之系列篇
- 冒泡排序,改进型冒泡排序,插入排序,二分查找
- CentOS6 client server NTP同步配置
- 第二次c++编程
- Android Library项目导出aar
- c++第一次实验-1
- Missing library: xdoclet-1.2.1.jar.... home directory for XDoclet. 1.2.1