Advertisement
Asinka

Clean Code

Feb 3rd, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4.  
  5. class CSharpCleanCode
  6. {
  7.     static void Main()
  8.     {
  9.         int rows = int.Parse(Console.ReadLine());
  10.         string[] source = new string[rows];
  11.         string code = null;
  12.         string state = null;
  13.  
  14.         for (int i = 0; i < rows; i++)
  15.         {
  16.             source[i] = Console.ReadLine();
  17.  
  18.         }
  19.  
  20.         for (int i = 0; i < source.Length; i++)
  21.         {
  22.             int openQuotes = source[i].IndexOf('"', 0);
  23.             int closeQuotes = 0;
  24.             int lastIndex = openQuotes + 1;
  25.             int lastIndex1 = openQuotes;
  26.             int lastIndex2 = openQuotes;
  27.  
  28.             if (openQuotes > -1)
  29.             {
  30.                 int escape1Quotes = source[i].IndexOf('\"', openQuotes + 1);
  31.                 while (escape1Quotes > -1)
  32.                 {
  33.                     escape1Quotes = source[i].IndexOf('\"', escape1Quotes + 1);
  34.                     if (escape1Quotes > -1) { lastIndex1 = escape1Quotes; }
  35.                 }
  36.  
  37.                 int escape2Quotes = source[i].IndexOf('@', openQuotes - 1);
  38.                 while (escape2Quotes > -1)
  39.                 {
  40.                     escape2Quotes = source[i].IndexOf(@"""", escape2Quotes + 1);
  41.                     if (escape2Quotes > -1) { lastIndex2 = escape2Quotes; }
  42.                 }
  43.             }
  44.             if (Math.Max(lastIndex1, lastIndex2) > -1 && Math.Max(lastIndex1, lastIndex2) > openQuotes)
  45.             {
  46.                 lastIndex = Math.Max(lastIndex1, lastIndex2);
  47.             }
  48.             closeQuotes = source[i].IndexOf('"', lastIndex);
  49.             int startIndex = 0;
  50.             int endIndex = 0;
  51.             if (openQuotes > -1 && closeQuotes > -1 && code != "quoted")
  52.             {
  53.                 code = "was quoted";
  54.                 startIndex = openQuotes;
  55.                 endIndex = closeQuotes;
  56.             }
  57.             else if (openQuotes > -1)
  58.             {
  59.                 if (code != "quoted") { code = "quoted"; }
  60.                 else { code = null; }
  61.             }
  62.             else if (closeQuotes > -1)
  63.             {
  64.                 if (code != "quoted") { code = "quoted"; }
  65.                 else { code = null; }
  66.             }
  67.             else if (openQuotes == -1 && closeQuotes == -1 && code != "quoted")
  68.             {
  69.                 code = null;
  70.             }
  71.  
  72.             int comment = source[i].IndexOf("//", 0);
  73.  
  74.             if (comment > -1 && code != "was quoted" && code != "quoted")
  75.             {
  76.                 source[i] = source[i].Remove(comment);
  77.  
  78.             }
  79.             else if (comment > -1 && code == "was quoted" && (comment < startIndex || comment > endIndex))
  80.             {
  81.                 source[i] = source[i].Remove(comment);
  82.             }
  83.  
  84.             int openComment = source[i].IndexOf("/*", 0);
  85.             int closeComment = 0;
  86.             if (openComment > -1)
  87.             {
  88.                 closeComment = source[i].IndexOf("*/", openComment);
  89.             }
  90.             else
  91.             {
  92.                 closeComment = source[i].IndexOf("*/", 0);
  93.             }
  94.  
  95.             if (closeComment > -1) { closeComment = closeComment + 2; }
  96.             int length = source[i].Length;
  97.  
  98.             if (openComment > -1 && code != "was quoted" && code != "quoted")
  99.             {
  100.                 if (closeComment > -1) { state = null; source[i] = source[i].Remove(openComment, closeComment - openComment); }
  101.                 else { state = "comment"; source[i] = source[i].Remove(openComment, source[i].Length - openComment); }
  102.             }
  103.  
  104.             else if (openComment > -1 && code == "was quoted")
  105.             {
  106.                 if (openComment < startIndex || openComment > endIndex)
  107.                 {
  108.                     if (closeComment > -1) { state = null; source[i] = source[i].Remove(openComment, closeComment - openComment); }
  109.                     else { state = "comment"; source[i] = source[i].Remove(openComment, source[i].Length - openComment); }
  110.                 }
  111.                 if (openComment > startIndex && openComment < endIndex)
  112.                 {
  113.                     openComment = source[i].IndexOf("/*", endIndex + 1);
  114.                     if (openComment > -1)
  115.                     {
  116.                         closeComment = source[i].IndexOf("*/", openComment);
  117.                     }
  118.                     else
  119.                     {
  120.                         closeComment = source[i].IndexOf("*/", endIndex + 1);
  121.                     }
  122.                     if (closeComment > -1) { closeComment = closeComment + 2; }
  123.  
  124.                     if (closeComment > -1) { state = null; source[i] = source[i].Remove(openComment, closeComment - openComment); }
  125.                     else { state = "comment"; source[i] = source[i].Remove(openComment, source[i].Length - openComment); }
  126.                 }
  127.             }
  128.             else if (state == "comment" && code != "quoted")
  129.             {
  130.                 if (closeComment > -1) { state = null; source[i] = source[i].Remove(0, closeComment); }
  131.                 else { state = "comment"; source[i] = source[i].Remove(0, source[i].Length); }
  132.             }
  133.  
  134.             for (int j = 0; j < source[i].Length; j++)
  135.             {
  136.                 if (source[i].Substring(j, 1) != " ")
  137.                 {
  138.                     Console.WriteLine(source[i]);
  139.                     break;
  140.                 }
  141.             }
  142.  
  143.            
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement