LuaInterface 支持中文

LuaInterface

        public static int fread(CharPtr ptr, int size, int num, Stream stream){
            int num_bytes = num * size;
            byte[] bytes = new byte[num_bytes];
            try{
                int result = stream.Read(bytes, 0, num_bytes);
                bytes = Encoding.Convert(Encoding.GetEncoding("GB2312"), Encoding.Unicode, bytes, 0, result);
                var chars = Encoding.Unicode.GetChars(bytes);
                for (int i = 0; i < chars.Length; i++){
                    ptr[i] = chars[i];
                }
                return chars.Length / size;
            } catch{
                return 0;
            }
        }
        public static int luaL_loadbuffer(lua_State L, CharPtr buff, uint size,
            CharPtr name){
            LoadS ls = new LoadS();
            ls.s = new CharPtr(buff);
            ls.size = (uint) strlen(ls.s);
            return lua_load(L, getS, ls, name);
        }

 

private static int llex(LexState ls, SemInfo seminfo){
            luaZ_resetbuffer(ls.buff);
            for (;;){
                switch (ls.current){
                    case '\n':
                    case '\r':{
                        inclinenumber(ls);
                        continue;
                    }
                    case '-':{
                        next(ls);
                        if (ls.current != '-') return '-';
                        /* else is a comment */
                        next(ls);
                        if (ls.current == '['){
                            int sep = skip_sep(ls);
                            luaZ_resetbuffer(ls.buff); /* `skip_sep' may dirty the buffer */
                            if (sep >= 0){
                                read_long_string(ls, null, sep); /* long comment */
                                luaZ_resetbuffer(ls.buff);
                                continue;
                            }
                        }

                        /* else short comment */
                        while (!currIsNewline(ls) && ls.current != EOZ)
                            next(ls);
                        continue;
                    }
                    case '[':{
                        int sep = skip_sep(ls);
                        if (sep >= 0){
                            read_long_string(ls, seminfo, sep);
                            return (int) RESERVED.TK_STRING;
                        } else if (sep == -1) return '[';
                        else luaX_lexerror(ls, "invalid long string delimiter", (int) RESERVED.TK_STRING);
                    }
                        break;
                    case '=':{
                        next(ls);
                        if (ls.current != '=') return '=';
                        else{
                            next(ls);
                            return (int) RESERVED.TK_EQ;
                        }
                    }
                    case '<':{
                        next(ls);
                        if (ls.current != '=') return '<';
                        else{
                            next(ls);
                            return (int) RESERVED.TK_LE;
                        }
                    }
                    case '>':{
                        next(ls);
                        if (ls.current != '=') return '>';
                        else{
                            next(ls);
                            return (int) RESERVED.TK_GE;
                        }
                    }
                    case '~':{
                        next(ls);
                        if (ls.current != '=') return '~';
                        else{
                            next(ls);
                            return (int) RESERVED.TK_NE;
                        }
                    }
                    case '"':
                    case '\'':{
                        read_string(ls, ls.current, seminfo);
                        return (int) RESERVED.TK_STRING;
                    }
                    case '.':{
                        save_and_next(ls);
                        if (check_next(ls, ".") != 0){
                            if (check_next(ls, ".") != 0)
                                return (int) RESERVED.TK_DOTS; /* ... */
                            else return (int) RESERVED.TK_CONCAT; /* .. */
                        } else if (!isdigit(ls.current)) return '.';
                        else{
                            read_numeral(ls, seminfo);
                            return (int) RESERVED.TK_NUMBER;
                        }
                    }
                    case EOZ:{
                        return (int) RESERVED.TK_EOS;
                    }
                    default:{
                        if (isspace(ls.current)){
                            lua_assert(!currIsNewline(ls));
                            next(ls);
                            continue;
                        } else if (isdigit(ls.current)){
                            read_numeral(ls, seminfo);
                            return (int) RESERVED.TK_NUMBER;
                        } else if (isalpha(ls.current) || ls.current == '_' || ls.current > 0x80){
                            /* identifier or reserved word */
                            TString ts;
                          
                            do
                            {
                                if (ls.current > 0x80){
                                    save_and_next(ls);
                                    save_and_next(ls);
                                } else
                                    save_and_next(ls);
                            } while (isalnum(ls.current) || ls.current == '_' || ls.current > 0x80);

                            ts = luaX_newstring(ls, luaZ_buffer(ls.buff),
                                luaZ_bufflen(ls.buff));
                            if (ts.tsv.reserved > 0) /* reserved word? */
                                return ts.tsv.reserved - 1 + FIRST_RESERVED;
                            else{
                                seminfo.ts = ts;
                                return (int) RESERVED.TK_NAME;
                            }
                        } else{
                            int c = ls.current;
                            next(ls);
                            return c; /* single-char tokens (+ - / ...) */
                        }
                    }
                }
            }
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值