Lua中文语言编程源码-第七节,更改lstrlib.c 标准字符串操作与模式匹配库函数, 使Lua加载中文库关键词(标准字符串操作与模式匹配库相关)

源码已经更新在CSDN的码库里:

复制代码
git clone https://gitcode.com/funsion/CLua.git

在src文件夹下的lstrlib.c 标准字符串操作与模式匹配库函数,表明这个C源文件实现了Lua的标准字符串操作与模式匹配库,即提供了与字符串操作相关的API和功能实现。

增加中文版stringmetamethods 元方法列表,保留英文版stringmetamethods元方法列表。

原始的代码为:
复制代码
static const luaL_Reg stringmetamethods[] = {
  {"__add", arith_add},
  {"__sub", arith_sub},
  {"__mul", arith_mul},
  {"__mod", arith_mod},
  {"__pow", arith_pow},
  {"__div", arith_div},
  {"__idiv", arith_idiv},
  {"__unm", arith_unm},
  {"__index", NULL},  /* placeholder */
  {NULL, NULL}
};
更改成以下代码:
复制代码
/* 定义字符串的元方法列表 */
static const luaL_Reg stringmetamethods[] = {
  {"__add", arith_add},
  {"__加",arith_add},
  {"__sub", arith_sub},
  {"__减",arith_sub},
  {"__mul", arith_mul},
  {"__乘",arith_mul},
  {"__mod", arith_mod},
  {"__取模",arith_mod},
  {"__pow", arith_pow},
  {"__乘方",arith_pow},
  {"__div", arith_div},
  {"__整除",arith_div},
  {"__idiv", arith_idiv},
  {"__向下整除", arith_idiv},
  {"__unm", arith_unm},
  {"__负号", NULL},
  {"__index", NULL}, 
  {"__索引", NULL} ,/* 占位符 */
  {NULL, NULL}
};
原始的代码为:
复制代码
static const luaL_Reg strlib[] = {
  {"byte", str_byte},
  {"char", str_char},
  {"dump", str_dump},
  {"find", str_find},
  {"format", str_format},
  {"gmatch", gmatch},
  {"gsub", str_gsub},
  {"len", str_len},
  {"lower", str_lower},
  {"match", str_match},
  {"rep", str_rep},
  {"reverse", str_reverse},
  {"sub", str_sub},
  {"upper", str_upper},
  {"pack", str_pack},
  {"packsize", str_packsize},
  {"unpack", str_unpack},
  {NULL, NULL}
};

更改成以下代码:

复制代码
/* strlib 结构体定义了字符串库中所有函数的映射关系 */
static const luaL_Reg strlib[] = {
  {"byte", str_byte}, /* byte函数用于提取字符串中的一个或多个字节 */
  {"char", str_char}, /* char函数根据给定的数值创建一个字符串 */
  {"dump", str_dump}, /* dump函数将函数转换为二进制字符串 */
  {"find", str_find}, /* find函数在字符串中查找子字符串 */
  {"format", str_format}, /* format函数根据指定的格式创建一个字符串 */
  {"gmatch", gmatch}, /* gmatch函数返回一个迭代器,用于在字符串中进行全局正则表达式匹配 */
  {"gsub", str_gsub}, /* gsub函数在字符串中替换所有匹配的子串 */
  {"len", str_len}, /* len函数返回字符串的长度 */
  {"lower", str_lower}, /* lower函数将字符串中的大写字母转换为小写字母 */
  {"match", str_match}, /* match函数在字符串中进行模式匹配 */
  {"rep", str_rep}, /* rep函数重复字符串一定次数 */
  {"reverse", str_reverse}, /* reverse函数反转字符串 */
  {"sub", str_sub}, /* sub函数提取字符串的子串 */
  {"upper", str_upper}, /* upper函数将字符串中的小写字母转换为大写字母 */
  {"pack", str_pack}, /* pack函数将数据打包成二进制格式 */
  {"packsize", str_packsize}, /* packsize函数返回给定格式数据打包后的大小 */
  {"unpack", str_unpack}, /* unpack函数从二进制字符串中解包数据 */
/* 中文注释部分为对应的中文函数名,便于中文用户理解 */
  {"字节", str_byte},
  {"字符", str_char},
  {"转储", str_dump},
  {"查找", str_find},
  {"格式", str_format},
  {"匹配", gmatch},
  {"替换", str_gsub},
  {"长度", str_len},
  {"小写", str_lower},
  {"配对", str_match},
  {"重复", str_rep},
  {"倒序", str_reverse},
  {"子串", str_sub},
  {"大写", str_upper},
  {"打包", str_pack},
  {"包尺寸", str_packsize},
  {"拆包", str_unpack},
  {NULL, NULL} /* 结束标志 */
};

为了保证中英文 函数都可以加载,以便你可以复制英文原码来进行更改。所以保留了英文版 函数名列表,这样就能使用两种文的函数。

{"byte", str_byte}, // 字节

{"字节", str_byte}, // 和byte相同,但是使用中文名称

其实它们都是加载同样的库名,算是加载了2次,以Lua内部算法,应该只会加载一次。

更改完之后,同样需要重新编译Lua的源码,实现以上列出的关键词的中文化。

注意,在Window系统下编译Lua, 最好将所有Lua的源码,重新保存成ANSI格式的文件,刚下载的默认的源码会是UTF-8格式的。

这个事情说三遍,

1,不然就会出现,Window下的UTF-8源码可编译,但Shell里的中文输出会乱码。

2,要不然就是Window的ANSI源码不可编译(假如你没做以上步骤),

3,如果是用ANSI格式的源码编译的Lua.exe,对应的,你在Window下写的Lua程序也是需要保存成ANSI格式的。这样就可以在Shell里输出正确的中文显示。

相关推荐
wt_cs3 分钟前
银行回单ocr api集成解析-图像文字识别-文字识别技术
开发语言·python
_WndProc25 分钟前
【Python】Flask网页
开发语言·python·flask
liujing1023292941 分钟前
Day04_刷题niuke20250703
java·开发语言·算法
能工智人小辰1 小时前
二刷 苍穹外卖day10(含bug修改)
java·开发语言
DKPT1 小时前
Java设计模式之结构型模式(外观模式)介绍与说明
java·开发语言·笔记·学习·设计模式
LL.。1 小时前
同步回调和异步回调
开发语言·前端·javascript
0wioiw02 小时前
Python基础(吃洋葱小游戏)
开发语言·python·pygame
栗子~~2 小时前
Python实战- Milvus 向量库 使用相关方法demo
开发语言·python·milvus
狐凄2 小时前
Python实例题:基于 Flask 的在线聊天系统
开发语言·python
狐凄2 小时前
Python实例题:基于 Flask 的任务管理系统
开发语言·python