Advertisement
psotirov

Fake Text Markup Language

Feb 11th, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.90 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class Problem4
  5. {
  6.     static void Main()
  7.     {
  8.         // temporary console input redirection
  9.         // Console.SetIn(new System.IO.StreamReader("test.002.in.txt")); (!!! thanks to "jasssonpet" for the idea !!!)
  10.         StringBuilder input = new StringBuilder(); // holds all input lines (including \n characters)
  11.  
  12.         int lines = int.Parse(Console.ReadLine());
  13.         for (int i = 0; i < lines; i++)
  14.         {
  15.             input.Append(Console.ReadLine() + "\n");
  16.         }
  17.  
  18.         // calls evaluation method and prints the output result (OBVIOUSLY)
  19.         // but here the method is invoked withour any surrounding tags, i.e. pure text
  20.         Console.Write(Evaluate(input).ToString());
  21.         //Console.ReadLine();
  22.     }
  23.  
  24.     static StringBuilder Evaluate(StringBuilder buffer, string tag = "")
  25.     {
  26.         // the method walks through "buffer" character by character
  27.         // and prints any of them to the output in accordance to the type of surrounding pair of tags
  28.         // if a pair of tags are found inside the buffer
  29.         // the method calls itself with the text inside those tags (using recursion)
  30.         // then the result is "replaced" within the buffer.
  31.  
  32.         StringBuilder result = new StringBuilder();
  33.         if (tag == "del") return result; // if surroundng tag is del - returns empty result
  34.         bool isTag = false; // indicates if we are reading the tag name
  35.         bool inTag = false; // indicates if we are reading the text between pair of tags
  36.         StringBuilder anyTag = new StringBuilder(); // holds the tag name of any found tag in the text
  37.         StringBuilder openTag = new StringBuilder(); // holds the tag name of the relevant opening tag
  38.         StringBuilder toEvaluate = new StringBuilder(); // holds the text between a pair of tags
  39.         int nestedEqualTags = 0; // counts how many equal opening tags are found (the same number of closing tags we must have to make a pair)
  40.  
  41.         for (int i = 0; i < buffer.Length; i++)
  42.         {
  43.             char symbol = buffer[i];
  44.             if (!isTag && symbol == '<') // tag start
  45.             {
  46.                 isTag = true;
  47.                 continue;
  48.             }
  49.  
  50.             if (isTag && symbol == '>') // tag end
  51.             {
  52.                 isTag = false;
  53.                 if (openTag.Length == 0) // we don't have any opening tags up to the moment
  54.                 {
  55.                     openTag.Append(anyTag);
  56.                     inTag = true;
  57.                     anyTag.Clear();
  58.                 }
  59.                 else
  60.                 {
  61.                     if (openTag.Equals(anyTag)) nestedEqualTags++; // a new equal opening tag has been found - counts it
  62.                     if (anyTag[0] == '/' && openTag.ToString() == anyTag.ToString(1, anyTag.Length - 1) && nestedEqualTags == 0)
  63.                     // the closing tag that makes pair with the opening tag has been found
  64.                     {
  65.                         inTag = false;
  66.                         // we already have a pair of tags inside the buffer - evaluates it
  67.                         toEvaluate = Evaluate(toEvaluate, openTag.ToString());
  68.                         // and then saves the result into buffer but after current position
  69.                         if (i == buffer.Length - 1) // if end of buffer has been found
  70.                         {
  71.                             buffer.Append(toEvaluate); // appends it to the end of buffer
  72.                         }
  73.                         else
  74.                         {
  75.                             buffer.Insert(i+1, toEvaluate); // otherwise inserts it
  76.                         }
  77.                         anyTag.Clear();
  78.                         openTag.Clear();
  79.                         toEvaluate.Clear(); // clears all tag holders
  80.                     }
  81.                     else // we don't have a pair of tags or the pair is not the most outer one
  82.                     {
  83.                         // if we have more than one equal opening tags - counts the inner pair of them
  84.                         if (anyTag[0] == '/' && openTag.ToString() == anyTag.ToString(1, anyTag.Length - 1)) nestedEqualTags--;
  85.                         // since this is not the most outer tag puts it to the evaluation buffer
  86.                         toEvaluate.Append("<" + anyTag + ">");
  87.                         anyTag.Clear(); // clears the tag appended already
  88.                     }
  89.                 }
  90.                 continue; // goes to the next buffer position
  91.             }
  92.  
  93.             if (!isTag && !inTag) // standard output
  94.             {
  95.                 switch (tag) // do the job of the corresponding tag  
  96.                 {
  97.                     case "upper":
  98.                         result.Append(char.ToUpper(symbol));
  99.                         break;
  100.                     case "lower":
  101.                         result.Append(char.ToLower(symbol));
  102.                         break;
  103.                     case "toggle":
  104.                         if (Char.IsUpper(symbol)) result.Append(char.ToLower(symbol)); // toggles the case of the character
  105.                         else result.Append(char.ToUpper(symbol));
  106.                         break;
  107.                     case "rev":
  108.                         result.Insert(0, symbol); // text reversing is implemented through insert at the current buffer begining
  109.                         break;
  110.                     default:
  111.                         result.Append(symbol); // normal text is just appended to the end of the current buffer
  112.                         break;
  113.                 }
  114.             }
  115.             else if (isTag) //we are reading tag name
  116.             {
  117.                 anyTag.Append(symbol);
  118.             }
  119.             else // we are in nested tag - just append to the evaluation buffer for further processing
  120.             {
  121.                 toEvaluate.Append(symbol);
  122.             }
  123.  
  124.         }
  125.  
  126.         return result;
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement