Advertisement
sashomaga

Clear comments

Feb 1st, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. class Program
  8. {
  9.     static void Main()
  10.     {
  11.  
  12.         int count = int.Parse(Console.ReadLine());
  13.         StringBuilder builder = new StringBuilder();
  14.         string line;
  15.         for (int i = 0; i < count; i++)
  16.         {
  17.             line = Console.ReadLine();
  18.             if (line.Length>0)
  19.             {
  20.                 builder.AppendLine(line);
  21.             }
  22.            
  23.         }
  24.         string content = builder.ToString();
  25.         builder.Clear();
  26.         char nextChar;
  27.         bool inTotalQuote = false;
  28.         bool inQuotes = false;
  29.         bool inComment = false;
  30.         bool inMultiComment = false;
  31.         List<string> rows = new List<string>();
  32.  
  33.         for (int pos = 0; pos < content.Length; pos++)
  34.         {
  35.             nextChar = content[pos];
  36.  
  37.             if (nextChar == '\\' && !inTotalQuote && !inComment && !inMultiComment) //TODO: better
  38.             {
  39.                 if (pos < content.Length -1 && content[pos+1] != '\n')
  40.                 {
  41.                     builder.Append("\\").Append(content[pos + 1]);
  42.                 }
  43.                 pos++;
  44.                 continue;
  45.             }
  46.             if (nextChar == '"')
  47.             {
  48.                 if (inTotalQuote == true) //"" espaces quotes !
  49.                 {
  50.                     inTotalQuote = false;
  51.                 }
  52.                 else if (inQuotes == true)
  53.                 {
  54.                     inQuotes = false;
  55.                 }
  56.                 else if (inQuotes == false)
  57.                 {
  58.                     inQuotes = true;
  59.                 }              
  60.             }
  61.             if (nextChar == '/' && !inQuotes && !inTotalQuote && !inComment && !inMultiComment)
  62.             {
  63.                 if (pos < content.Length)
  64.                 {
  65.                     if (content[pos+1] == '/')
  66.                     {
  67.                         inComment = true;
  68.                         pos++;
  69.                         continue;
  70.                     }
  71.                     if (content[pos+1] == '*')
  72.                     {
  73.                         inMultiComment = true;
  74.                         pos++;
  75.                         continue;
  76.                     }
  77.                 }  
  78.             }
  79.             if (nextChar == '*' && !inQuotes && !inTotalQuote && !inComment)
  80.             {
  81.                  if (pos < content.Length)
  82.                 {
  83.                     if (content[pos+1] == '/')
  84.                     {
  85.                         inMultiComment = false;
  86.                         pos++;
  87.                         continue;
  88.                     }
  89.                 }  
  90.             }
  91.             if (nextChar == '@' && !inQuotes && !inTotalQuote && !inComment && !inMultiComment)
  92.             {
  93.                 if (pos < content.Length)
  94.                 {
  95.                     if (content[pos+1] == '"')
  96.                     {
  97.                         inTotalQuote = true;
  98.                         pos++;
  99.                         builder.Append("@\"");
  100.                         continue;
  101.                     }
  102.                 }  
  103.             }
  104.             if (inComment == false && inMultiComment == false) //append current char
  105.             {
  106.                 if (nextChar == '\n' && inMultiComment == false)
  107.                 {
  108.                     if (!string.IsNullOrWhiteSpace(builder.ToString()))
  109.                     {
  110.                         rows.Add(builder.ToString().TrimEnd('\r', '\n'));
  111.                     }                  
  112.                     builder.Clear();                    
  113.                     inQuotes = false;
  114.                 }
  115.                 else
  116.                 {
  117.                     builder.Append(nextChar);
  118.                 }
  119.                 continue;
  120.             }
  121.             if (nextChar == '\n' && inComment == true)
  122.             {
  123.                 if (!string.IsNullOrWhiteSpace(builder.ToString().TrimEnd('\r','\n')))//builder.Length>0
  124.                 {
  125.                     rows.Add(builder.ToString());
  126.                     builder.Clear();
  127.                     inComment = false;
  128.                     inQuotes = false;
  129.                 }
  130.                 else
  131.                 {
  132.                     builder.Clear();
  133.                     inComment = false;
  134.                     inQuotes = false;
  135.                 }
  136.             }
  137.  
  138.         }
  139.         foreach (var item in rows)
  140.         {        
  141.            
  142.             if (!string.IsNullOrWhiteSpace(item))
  143.             {
  144.                 Console.WriteLine(item);
  145.             }
  146.            
  147.         }
  148.  
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement