大家都知道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 咨询