Guest User

Untitled

a guest
Jun 25th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. public static string[] smartSplit(
  2. string delimitedData,
  3. char delimiter,
  4. char escape) {}
  5.  
  6. def extract(input, delim, escape):
  7. # states
  8. parsing = 0
  9. escaped = 1
  10.  
  11. state = parsing
  12. found = []
  13. parsed = ""
  14.  
  15. for c in input:
  16. if state == parsing:
  17. if c == delim:
  18. found.append(parsed)
  19. parsed = ""
  20. elif c == escape:
  21. state = escaped
  22. else:
  23. parsed += c
  24. else: # state == escaped
  25. parsed += c
  26. state = parsing
  27.  
  28. if parsed:
  29. found.append(parsed)
  30.  
  31. return found
  32.  
  33. void smartSplit(string const& text, char delim, char esc, vector<string>& tokens)
  34. {
  35. enum State { NORMAL, IN_ESC };
  36. State state = NORMAL;
  37. string frag;
  38.  
  39. for (size_t i = 0; i<text.length(); ++i)
  40. {
  41. char c = text[i];
  42. switch (state)
  43. {
  44. case NORMAL:
  45. if (c == delim)
  46. {
  47. if (!frag.empty())
  48. tokens.push_back(frag);
  49. frag.clear();
  50. }
  51. else if (c == esc)
  52. state = IN_ESC;
  53. else
  54. frag.append(1, c);
  55. break;
  56. case IN_ESC:
  57. frag.append(1, c);
  58. state = NORMAL;
  59. break;
  60. }
  61. }
  62. if (!frag.empty())
  63. tokens.push_back(frag);
  64. }
  65.  
  66. state(input) action
  67. ========================
  68. BEGIN(*): token.clear(); state=START;
  69. END(*): return;
  70. *(n): token.emit(); state=END;
  71. START(DELIMITER): ; // NB: the input is *not* added to the token!
  72. START(ESCAPE): state=ESC; // NB: the input is *not* added to the token!
  73. START(*): token.append(input); state=NORM;
  74. NORM(DELIMITER): token.emit(); token.clear(); state=START;
  75. NORM(ESCAPE): state=ESC; // NB: the input is *not* added to the token!
  76. NORM(*): token.append(input);
  77. ESC(*): token.append(input); state=NORM;
  78.  
  79. public static void smartSplit(string text, char delim, char esc, ref List<string> listToBuild)
  80. {
  81. bool currentlyEscaped = false;
  82. StringBuilder fragment = new StringBuilder();
  83.  
  84. for (int i = 0; i < text.Length; i++)
  85. {
  86. char c = text[i];
  87. if (currentlyEscaped)
  88. {
  89. fragment.Append(c);
  90. currentlyEscaped = false;
  91. }
  92. else
  93. {
  94. if (c == delim)
  95. {
  96. if (fragment.Length > 0)
  97. {
  98. listToBuild.Add(fragment.ToString());
  99. fragment.Remove(0, fragment.Length);
  100. }
  101.  
  102. }
  103. else if (c == esc)
  104. currentlyEscaped = true;
  105. else
  106. fragment.Append(c);
  107. }
  108. }
  109.  
  110. if (fragment.Length > 0)
  111. {
  112. listToBuild.Add(fragment.ToString());
  113. }
  114. }
  115.  
  116. private static string[] Split(string input, char delimiter, char escapeChar, bool removeEmpty)
  117. {
  118. if (input == null)
  119. {
  120. return new string[0];
  121. }
  122.  
  123. char[] specialChars = new char[]{delimiter, escapeChar};
  124.  
  125. var tokens = new List<string>();
  126. var token = new StringBuilder();
  127.  
  128. for (int i = 0; i < input.Length; i++)
  129. {
  130. var c = input[i];
  131.  
  132. if (c.Equals(escapeChar))
  133. {
  134. if (i >= input.Length - 1)
  135. {
  136. throw new ArgumentException("Uncompleted escape sequence has been encountered at the end of the input");
  137. }
  138.  
  139. var nextChar = input[i + 1];
  140.  
  141. if (nextChar != escapeChar && nextChar != delimiter)
  142. {
  143. throw new ArgumentException("Unknown escape sequence has been encountered: " + c + nextChar);
  144. }
  145.  
  146. token.Append(nextChar);
  147. i++;
  148. }
  149. else if (c.Equals(delimiter))
  150. {
  151. if (!removeEmpty || token.Length > 0)
  152. {
  153. tokens.Add(token.ToString());
  154. token.Length = 0;
  155. }
  156. }
  157. else
  158. {
  159. var index = input.IndexOfAny(specialChars, i);
  160.  
  161. if (index < 0)
  162. {
  163. token.Append(c);
  164. }
  165. else
  166. {
  167. token.Append(input.Substring(i, index - i));
  168. i = index - 1;
  169. }
  170. }
  171. }
  172.  
  173. if (!removeEmpty || token.Length > 0)
  174. {
  175. tokens.Add(token.ToString());
  176. }
  177.  
  178. return tokens.ToArray();
  179. }
Add Comment
Please, Sign In to add comment