翻譯器解決了從sql到C#常常加入許多的 雙引號, 簡單的詞法分析可以對sql代碼進行格式化調整,每行長度70左右
format#region format
private string formatString =string.Empty;
private int index = 0;
private StringBuilder sbword = new StringBuilder();
char[] splitc = new char[]{' ','\t','\r','\n'};//',','=',
char[] splituse = new char[]{',','='};
private bool Insplitc(char[] splitc,char c)
{
for(int i=0;i<splitc.Length;i++)
{
if(c==splitc[i])
return true;
}
return false;
}
private char Peek()
{
return Peek(1);
}
private char Peek(int i)
{
if(index+i>=formatString.Length)
return '\0';
return formatString[index+1];
}
/**//// <summary>
/// 返回一個字元組,關鍵字或是 變數 或是字串
/// </summary>
/// <returns></returns>
private string GetAWord()
{
sbword.Remove(0,sbword.Length);
int oldindex = index;
char c;
char pc;
//跳過空白
for(;index<formatString.Length;index++)
{
if(!Insplitc(splitc,formatString[index]))
break;
}
oldindex =index;
for(;index<formatString.Length;index++)
{
pc = Peek();
c= formatString[index];
if(Insplitc(splitc,c))
break;
if(Insplitc(splituse,c))
{
break;
}
}
if(oldindex<index)
return formatString.Substring(oldindex,index-oldindex);
else// if(oldindex==index)
{
if(index<formatString.Length)
return formatString[index++].ToString();
else
return "\n";
}
}
#endregion
下載原始碼