让 luaj 支持中文函数

大家都知道lua的强大,C版的lua有很多人研究 支持中文函数已经很普遍

java版的luaj不支持中文函数

我改了几处源码,已经实现了

 

1 LexState类 添加函数

 

    private boolean isChineseCode(int charint) {

        if(charint==-1){

            return false;

        }

        return ((char) charint > 0x80);

    }

 

 

    public String charToByteToString(){

        byte[] strs = new byte[nbuff];

        for(int i=0;i<nbuff;i++){

            char char_str =buff[i];

            byte byte_str =(byte)char_str;

            strs[i]=byte_str;

        }

        return  new String(strs);

    }

 

 

2修改函数 int llex(SemInfo seminfo) {

 

 

     default: {
                if (isspace(current)) {
                    _assert(!currIsNewline());
                    nextChar();
                    continue;
                } else if (isdigit(current)) {
                    read_numeral(seminfo);
                    return TK_NUMBER;
                } else if (isalpha(current) || current == '_' || isChineseCode(current)) {
                    /* identifier or reserved word */
                    LuaString ts;
                    do {
                        //支持中文
                        if (isChineseCode(current)) {
                            save_and_next();
                            save_and_next();
                            save_and_next(); // 处理了一个中文字符
                        } else {
                            save_and_next(); // 处理英文字符或者下划线
                        }
                    } while (isalnum(current) || current == '_' || isChineseCode(current));
               
                    //这里想办法把中文转换出来
                    String str_s = charToByteToString();
                    //ts = newstring(buff, 0, nbuff);
                    ts = L.newTString(str_s);
                    if (RESERVED.containsKey(ts))
                        return ((Integer) RESERVED.get(ts)).intValue();
                    else {
                        seminfo.ts = ts;
                        return TK_NAME;
                    }
                } else {
                    int c = current;
                    nextChar();
                    return c; /* single-char tokens (+ - / ...) */
                }
            }

 

如果还有问题,请加QQ群79838845 咨询

转载于:https://my.oschina.net/u/1243666/blog/860974

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值