Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Net.Mail;
  5. using System.Text;
  6. using System.Windows.Forms;
  7.  
  8. namespace TableParser
  9. {
  10. public class FieldsParserTask
  11. {
  12. private const char BackSlash = '\\';
  13.  
  14. private static readonly char[] Quotes =
  15. {
  16. '\"', '\''
  17. };
  18.  
  19. private enum TypeOfTocken
  20. {
  21. Simple,
  22. Сomposite
  23. }
  24.  
  25. private enum InstructionsCurrentChar
  26. {
  27. Break,
  28. Shielding,
  29. Append,
  30. Skip,
  31. SkipQuotes,
  32. BreakСomposite
  33. }
  34.  
  35.  
  36. public static List<string> ParseLine(string line)
  37. {
  38. var parsedField = new List<string>();
  39. var currentIndex = 0;
  40.  
  41. while (currentIndex < line.Length)
  42. {
  43. Token currentToken = ReadField(line, currentIndex);
  44. if (IsCorrectToken(currentToken))
  45. parsedField.Add(currentToken.Value);
  46. currentIndex = currentToken.GetIndexNextToToken();
  47. }
  48.  
  49. return parsedField;
  50. }
  51.  
  52.  
  53. private static InstructionsCurrentChar CommandForSimpleToken(char currentChar)
  54. {
  55. if (currentChar == ' ' || Quotes.Contains(currentChar))
  56. return InstructionsCurrentChar.Break;
  57. return InstructionsCurrentChar.Append;
  58. }
  59.  
  60.  
  61. private static InstructionsCurrentChar CommandForСompositeToken(char currentChar, ref char typeQuotes)
  62. {
  63. if (Quotes.Contains(currentChar))
  64. {
  65. if (typeQuotes == '.')
  66. {
  67. typeQuotes = currentChar;
  68. return InstructionsCurrentChar.Skip;
  69. }
  70. if (currentChar == typeQuotes)
  71. return InstructionsCurrentChar.BreakСomposite;
  72. return InstructionsCurrentChar.SkipQuotes;
  73. }
  74. if (currentChar == BackSlash)
  75. return InstructionsCurrentChar.Shielding;
  76. return InstructionsCurrentChar.Append;
  77. }
  78.  
  79.  
  80. private static Token AssemblyToken(string line, int startIndex, TypeOfTocken type)
  81. {
  82. var tokensLength = 0;
  83. var tokensValue = new StringBuilder();
  84. var typeQuotes = '.';
  85. var skipingQuotes = false;
  86. for (var index = startIndex; index < line.Length; index++)
  87. {
  88. tokensLength++;
  89. var command = type == TypeOfTocken.Simple
  90. ? CommandForSimpleToken(line[index])
  91. : CommandForСompositeToken(line[index], ref typeQuotes);
  92. if (command == InstructionsCurrentChar.Append)
  93. tokensValue.Append(line[index]);
  94. else if (command == InstructionsCurrentChar.Shielding)
  95. {
  96. index++;
  97. tokensLength++;
  98. tokensValue.Append(line[index]);
  99. }
  100. else if (command == InstructionsCurrentChar.SkipQuotes)
  101. {
  102. if (skipingQuotes)
  103. {
  104. tokensValue = new StringBuilder(tokensValue.ToString().Substring(1));
  105. }
  106. else
  107. {
  108. tokensValue.Append(line[index]);
  109. }
  110. skipingQuotes = true;
  111. }
  112. else if (command == InstructionsCurrentChar.Skip)
  113. continue;
  114. else
  115. break;
  116. }
  117. return new Token(tokensValue.ToString(), startIndex, tokensLength);
  118. }
  119.  
  120.  
  121. private static Token ReadField(string line, int startIndex)
  122. {
  123. var type = Quotes.Contains(line[startIndex]) ? TypeOfTocken.Сomposite : TypeOfTocken.Simple;
  124. var currentToken = AssemblyToken(line, startIndex, type);
  125. return currentToken;
  126. }
  127.  
  128. private static bool IsCorrectToken(Token token)
  129. {
  130. return token.Value != "" || token.Length == 2;
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement