Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class CSharpCleanCode
- {
- static void Main()
- {
- int rows = int.Parse(Console.ReadLine());
- string[] source = new string[rows];
- string code = null;
- string state = null;
- for (int i = 0; i < rows; i++)
- {
- source[i] = Console.ReadLine();
- }
- for (int i = 0; i < source.Length; i++)
- {
- int openQuotes = source[i].IndexOf('"', 0);
- int closeQuotes = 0;
- int lastIndex = openQuotes + 1;
- int lastIndex1 = openQuotes;
- int lastIndex2 = openQuotes;
- if (openQuotes > -1)
- {
- int escape1Quotes = source[i].IndexOf('\"', openQuotes + 1);
- while (escape1Quotes > -1)
- {
- escape1Quotes = source[i].IndexOf('\"', escape1Quotes + 1);
- if (escape1Quotes > -1) { lastIndex1 = escape1Quotes; }
- }
- int escape2Quotes = source[i].IndexOf('@', openQuotes - 1);
- while (escape2Quotes > -1)
- {
- escape2Quotes = source[i].IndexOf(@"""", escape2Quotes + 1);
- if (escape2Quotes > -1) { lastIndex2 = escape2Quotes; }
- }
- }
- if (Math.Max(lastIndex1, lastIndex2) > -1 && Math.Max(lastIndex1, lastIndex2) > openQuotes)
- {
- lastIndex = Math.Max(lastIndex1, lastIndex2);
- }
- closeQuotes = source[i].IndexOf('"', lastIndex);
- int startIndex = 0;
- int endIndex = 0;
- if (openQuotes > -1 && closeQuotes > -1 && code != "quoted")
- {
- code = "was quoted";
- startIndex = openQuotes;
- endIndex = closeQuotes;
- }
- else if (openQuotes > -1)
- {
- if (code != "quoted") { code = "quoted"; }
- else { code = null; }
- }
- else if (closeQuotes > -1)
- {
- if (code != "quoted") { code = "quoted"; }
- else { code = null; }
- }
- else if (openQuotes == -1 && closeQuotes == -1 && code != "quoted")
- {
- code = null;
- }
- int comment = source[i].IndexOf("//", 0);
- if (comment > -1 && code != "was quoted" && code != "quoted")
- {
- source[i] = source[i].Remove(comment);
- }
- else if (comment > -1 && code == "was quoted" && (comment < startIndex || comment > endIndex))
- {
- source[i] = source[i].Remove(comment);
- }
- int openComment = source[i].IndexOf("/*", 0);
- int closeComment = 0;
- if (openComment > -1)
- {
- closeComment = source[i].IndexOf("*/", openComment);
- }
- else
- {
- closeComment = source[i].IndexOf("*/", 0);
- }
- if (closeComment > -1) { closeComment = closeComment + 2; }
- int length = source[i].Length;
- if (openComment > -1 && code != "was quoted" && code != "quoted")
- {
- if (closeComment > -1) { state = null; source[i] = source[i].Remove(openComment, closeComment - openComment); }
- else { state = "comment"; source[i] = source[i].Remove(openComment, source[i].Length - openComment); }
- }
- else if (openComment > -1 && code == "was quoted")
- {
- if (openComment < startIndex || openComment > endIndex)
- {
- if (closeComment > -1) { state = null; source[i] = source[i].Remove(openComment, closeComment - openComment); }
- else { state = "comment"; source[i] = source[i].Remove(openComment, source[i].Length - openComment); }
- }
- if (openComment > startIndex && openComment < endIndex)
- {
- openComment = source[i].IndexOf("/*", endIndex + 1);
- if (openComment > -1)
- {
- closeComment = source[i].IndexOf("*/", openComment);
- }
- else
- {
- closeComment = source[i].IndexOf("*/", endIndex + 1);
- }
- if (closeComment > -1) { closeComment = closeComment + 2; }
- if (closeComment > -1) { state = null; source[i] = source[i].Remove(openComment, closeComment - openComment); }
- else { state = "comment"; source[i] = source[i].Remove(openComment, source[i].Length - openComment); }
- }
- }
- else if (state == "comment" && code != "quoted")
- {
- if (closeComment > -1) { state = null; source[i] = source[i].Remove(0, closeComment); }
- else { state = "comment"; source[i] = source[i].Remove(0, source[i].Length); }
- }
- for (int j = 0; j < source[i].Length; j++)
- {
- if (source[i].Substring(j, 1) != " ")
- {
- Console.WriteLine(source[i]);
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement