Advertisement
krayddy

TableParser

Nov 18th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3.  
  4. namespace TableParser
  5. {
  6. public class FieldsParserTask
  7. {
  8. public static List<string> ParseLine(string line)
  9. {
  10. var list = new List<string>();
  11. if (line.Length == 0)
  12. return list;
  13. var quoteIndex = 0;
  14. var simple = GetFirstFieldType(line);
  15. MainBlock(line, list, quoteIndex, simple);
  16. return list;
  17. }
  18.  
  19. public static void MainBlock(string line, List<string> list, int quoteIndex, bool simple)
  20. {
  21. for (var i = 0; i < line.Length; i++)
  22. {
  23. if (simple)
  24. {
  25. i = GetIndexNextToToken(line, quoteIndex);
  26. if (i == -1)
  27. {
  28. list = AddFieldWithoutClosingQuote(line, list, quoteIndex);
  29. return;
  30. }
  31. else
  32. if (i != line.IndexOfAny(new char[] { '\'', '\"' }))
  33. i += quoteIndex + 1;
  34. simple = false;
  35. list = AddSimpleField(line, list, quoteIndex, i);
  36. quoteIndex = i;
  37. continue;
  38. }
  39. if (!simple)
  40. ComplexField(line, list, ref quoteIndex, ref simple, ref i);
  41. }
  42. }
  43.  
  44. private static void ComplexField(string line, List<string> list, ref int quoteIndex, ref bool simple, ref int i)
  45. {
  46. for (var k = quoteIndex + 1; k < line.Length; k++)
  47. {
  48. if (line[k] == '\\')
  49. {
  50. k++;
  51. continue;
  52. }
  53. if (line[k] == line[quoteIndex])
  54. {
  55. i = k;
  56. list.Add(DeleteSlash(line.Substring(quoteIndex + 1, k - quoteIndex - 1)));
  57. quoteIndex = k;
  58. simple = true;
  59. break;
  60. }
  61. if (k + 1 == line.Length)
  62. {
  63. i = line.Length - 1;
  64. list.Add(DeleteSlash(line.Substring(quoteIndex + 1)));
  65. break;
  66. }
  67. }
  68. }
  69.  
  70. public static List<string> AddSimpleField(string line, List<string> list, int quoteIndex, int i)
  71. {
  72. if (quoteIndex == 0)
  73. list.AddRange(GetSplitedSimpleField(line.Substring(quoteIndex, i - quoteIndex)));
  74. else
  75. list.AddRange(GetSplitedSimpleField(line.Substring(quoteIndex + 1, i - quoteIndex - 1)));
  76. return list;
  77. }
  78.  
  79. public static List<string> AddFieldWithoutClosingQuote(string line, List<string> list, int quoteIndex)
  80. {
  81. if (quoteIndex != 0)
  82. quoteIndex++;
  83. list.AddRange(line.Substring(quoteIndex)
  84. .Split(' ')
  85. .Where(x => x != ""));
  86. return list;
  87. }
  88.  
  89. public static int GetIndexNextToToken(string line, int quoteIndex)
  90. {
  91. var i = 0;
  92. if (quoteIndex != 0)
  93. i = line.Substring(quoteIndex + 1).IndexOfAny(new char[] { '\'', '\"' });
  94. else
  95. i = line.IndexOfAny(new char[] { '\'', '\"' });
  96. return i;
  97. }
  98.  
  99. public static bool GetFirstFieldType(string line)
  100. {
  101. var simple = true;
  102. if (line[0] == '\'' || line[0] == '\"')
  103. simple = false;
  104. return simple;
  105. }
  106.  
  107. public static List<string> GetSplitedSimpleField(string line)
  108. {
  109. return line
  110. .ToString()
  111. .Split(' ')
  112. .Where(x => x != "")
  113. .ToList();
  114. }
  115.  
  116. public static string DeleteSlash(string line)
  117. {
  118. return line
  119. .Replace("\\\\", "\\")
  120. .Replace("\\'", "'")
  121. .Replace("\\\"", "\"");
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement