最近领导让我把lua脚本中全部注释去掉, 我去掉以后发现项目的lua脚本大小从22.7M到了19.1M,少了3.6M(15%),项目已经跑了2周,没有收到测试或者程序的错误反馈。(欢迎大家指出错误。)
思路: 先画状态机图,转为代码。
后续优化:
加入对Lua几个用法较少的注释方法的支持;
转换前后行数不变,利于bugly排查问题;
支持\r. \r\n2个换行方法;
忽略lua脚本本身的编码方式,直接string转字符;
状态机图:
上代码:
//去除注释时, 会移除单行注释 --
//多行注释支持 --[[ --]]& --[[]]& --[=[ --]=]& --[=[ ]=] 四种
//对于引号中的 -- 也做了处理
//支持转义字符 \
//行中间 进行块注释
//去注释前后 有效代码的行数不变
public static int handleLuaNotes(string srcFileName, string desFileName)
{
if (!File.Exists(srcFileName))
{
return -1;
}
FileInfo desFile = new FileInfo(desFileName);
if (File.Exists(desFileName))
{
File.Delete(desFileName);
}
string inputstr = File.ReadAllText(srcFileName);
inputstr = inputstr.Replace( "[=[", "[[");
inputstr = inputstr.Replace( "]=]", "]]");
StringBuilder outputstr = new StringBuilder(); //存放一定不是字符串的
StringBuilder temp = new StringBuilder(); //存放那些可能不是注释的字符串
State state = State.status0;
int calNum = 0;
string curHandle = ""; //调试用变量
for (int i = 0; i < inputstr.Length; i++)
{
char c = inputstr[i];
//curHandle = inputstr.Substring(0, i + 1);
switch (state)
{
case State.status0:
if (c == '"')
{
state = State.status10;
outputstr.Append(c);
}
else if (c == '-')
{
state = State.status1;
temp.Append(c);
}
else
{
state = State.status0;
outputstr.Append(c);
}
break;
case State.status1:
if (c == '-')
{
state = State.status2;
}
else
{
state = State.status0;
temp.Append(c);
outputstr.Append(temp);
}
temp.Clear();
break;
case State.status2:
if (c == '[')
{
state = State.status4;
}
else
{
state = State.status3;
}
break;
case State.status3:
if (c == '\r')
{
state = State.status0;
i++;
outputstr.Append(c);
outputstr.Append(inputstr[i]);
}
else if (c == '\n')
{
state = State.status0;
outputstr.Append(c);
}
else
{
state = State.status3;
}
break;
case State.status4:
if (c == '[')
{
calNum = 0;
state = State.status5;
}
else
{
state = State.status3;
}
break;
case State.status5:
if (c == '-')
{
state = State.status6;
}
else if (c == '\r')
{
i++;
outputstr.Append(c);
outputstr.Append(inputstr[i]);
state = State.status5;
}
else if (c == '\n')
{
state = State.status5;
outputstr.Append(c);
}
else if (c == '[')
{
calNum++;
}
else if (c == ']')
{
if (calNum == 0)
state = State.status8;
if (calNum > 0 )
{
calNum--;
}
}
else
{
state = State.status5;
}
break;
case State.status6:
if (c == '-')
{
state = State.status7;
}
else if (c == '[')
{
calNum++;
state = State.status5;
}
else if (c == ']')
{
if (calNum == 0)
state = State.status8;
if (calNum > 0)
{
calNum--;
state = State.status5;
}
}
else
{
state = State.status5;
}
break;
case State.status7:
if (c == '-')
{
state = State.status7;
}
else if (c == ']')
{
state = State.status8;
}
else
{
state = State.status5;
}
break;
case State.status8:
if (c == ']')
{
if (i+1 < inputstr.Length && inputstr[i+1] != ' ' && inputstr[i+1] != '\r' && inputstr[i + 1] != '\n')
{
outputstr.Append(' ');
}
state = State.status0;
}
else
{
state = State.status5;
}
break;
case State.status9:
//这是个逻辑节点 其实不会用到 该9的时候 会直接跳到0
break;
case State.status10:
outputstr.Append(c);
if (c == '"')
{
state = State.status0;
}
else if (c == '\\')
{
state = State.status11;
}
else
{
state = State.status10;
}
break;
case State.status11:
outputstr.Append(c);
state = State.status10;
break;
default:
break;
}
}
File.WriteAllText(desFileName, outputstr.ToString());
return 0;
}
enum State
{
status0 = 0,
status1 = 1,
status2 = 2,
status3 = 3,
status4 = 4,
status5 = 5,
status6,
status7,
status8,
status9,
status10,
status11,
}