public string[] Format(string[] lines) { string[] result = new string[lines.Length]; SpaceParam spBase = new SpaceParam(); for (int i = 0; i < lines.Length; i++) { SpaceParam sp = new SpaceParam(); string line = CheckLine(lines[i], sp); int addRazor = sp.Razor; spBase.Razor += sp.Razor; if (spBase.Razor == 0) { result[i] = lines[i]; continue; } int backcount = 0; if (spBase.MemoLeft > 0 && sp.MemoRight > 0 && sp.FirstMemo) { backcount--; } if (spBase.MemoLeft == 0 && sp.ParenthesisRight > 0 && sp.FirstParenthesis) { backcount--; } if (addRazor > 0) { result[i] = GetSpaces(spBase.Razor * 2 - addRazor + spBase.ParenthesisLeft + spBase.MemoLeft + backcount) + line; } else { result[i] = GetSpaces(spBase.Razor * 2 + spBase.ParenthesisLeft + spBase.MemoLeft + backcount) + line; } if (spBase.MemoLeft == 0) { spBase.MemoLeft += sp.MemoLeft; spBase.ParenthesisLeft += sp.ParenthesisLeft; spBase.ParenthesisLeft -= sp.ParenthesisRight; } spBase.MemoLeft -= sp.MemoRight; if (spBase.ParenthesisLeft < 0) { spBase.Razor += spBase.ParenthesisLeft; if (spBase.Razor < 0) { spBase.Razor = 0; } spBase.ParenthesisLeft = 0; } } return(result); }
string CheckLine(string source, SpaceParam sp) { string txt = source.Trim(); txt = txt.Replace("\\\"", "");//移除引号转义 txt = DelString(txt); int n; n = txt.IndexOf("//"); if (n > -1) { txt = txt.Substring(0, n); } txt = Delstring2(txt); txt = Delstring3(txt); sp.MemoLeft = GetSubCount(txt, "/*"); sp.MemoRight = GetSubCount(txt, "*/"); sp.Razor = GetSubCount(txt, "@("); txt = txt.Replace("@(", ""); sp.ParenthesisLeft = GetSubCount(txt, "("); sp.ParenthesisRight = GetSubCount(txt, ")"); if (sp.ParenthesisRight > 0 && txt.Length > 0 && txt.Substring(0, 1) == ")") { sp.FirstParenthesis = true; } if (sp.MemoRight > 0 && txt.Length > 1 && txt.Substring(0, 2) == "*/") { sp.FirstMemo = true; } return(source.TrimStart(' ')); }