StringSplitter.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace Caliburn.Micro.Core {
  4. /// <summary>
  5. /// Helper class when splitting strings
  6. /// </summary>
  7. public static class StringSplitter {
  8. /// <summary>
  9. /// Splits a string with a chosen separator.
  10. /// If a substring is contained in [...] it will not be splitted.
  11. /// </summary>
  12. /// <param name="message">The message to split</param>
  13. /// <param name="separator">The separator to use when splitting</param>
  14. /// <returns></returns>
  15. public static string[] Split(string message, char separator)
  16. {
  17. //Splits a string using the specified separator, if it is found outside of relevant places
  18. //delimited by [ and ]
  19. string str;
  20. var list = new List<string>();
  21. var builder = new StringBuilder();
  22. int squareBrackets = 0;
  23. #if WinRT
  24. foreach (var current in message.ToCharArray())
  25. {
  26. #else
  27. foreach(var current in message) {
  28. #endif
  29. //Square brackets are used as delimiters, so only separators outside them count...
  30. if (current == '[')
  31. {
  32. squareBrackets++;
  33. }
  34. else if (current == ']')
  35. {
  36. squareBrackets--;
  37. }
  38. else if (current == separator)
  39. {
  40. if (squareBrackets == 0)
  41. {
  42. str = builder.ToString();
  43. if (!string.IsNullOrEmpty(str))
  44. list.Add(builder.ToString());
  45. builder.Length = 0;
  46. continue;
  47. }
  48. }
  49. builder.Append(current);
  50. }
  51. str = builder.ToString();
  52. if (!string.IsNullOrEmpty(str))
  53. {
  54. list.Add(builder.ToString());
  55. }
  56. return list.ToArray();
  57. }
  58. /// <summary>
  59. /// Splits a string with , as separator.
  60. /// Does not split within {},[],()
  61. /// </summary>
  62. /// <param name="parameters">The string to split</param>
  63. /// <returns></returns>
  64. public static string[] SplitParameters(string parameters)
  65. {
  66. //Splits parameter string taking into account brackets...
  67. var list = new List<string>();
  68. var builder = new StringBuilder();
  69. bool isInString = false;
  70. int curlyBrackets = 0;
  71. int squareBrackets = 0;
  72. int roundBrackets = 0;
  73. for (int i = 0; i < parameters.Length; i++)
  74. {
  75. var current = parameters[i];
  76. if (current == '"')
  77. {
  78. if (i == 0 || parameters[i - 1] != '\\')
  79. {
  80. isInString = !isInString;
  81. }
  82. }
  83. if (!isInString)
  84. {
  85. switch (current)
  86. {
  87. case '{':
  88. curlyBrackets++;
  89. break;
  90. case '}':
  91. curlyBrackets--;
  92. break;
  93. case '[':
  94. squareBrackets++;
  95. break;
  96. case ']':
  97. squareBrackets--;
  98. break;
  99. case '(':
  100. roundBrackets++;
  101. break;
  102. case ')':
  103. roundBrackets--;
  104. break;
  105. default:
  106. if (current == ',' && roundBrackets == 0 && squareBrackets == 0 && curlyBrackets == 0)
  107. {
  108. //The only commas to be considered as parameter separators are outside:
  109. //- Strings
  110. //- Square brackets (to ignore indexers)
  111. //- Parantheses (to ignore method invocations)
  112. //- Curly brackets (to ignore initializers and Bindings)
  113. list.Add(builder.ToString());
  114. builder.Length = 0;
  115. continue;
  116. }
  117. break;
  118. }
  119. }
  120. builder.Append(current);
  121. }
  122. list.Add(builder.ToString());
  123. return list.ToArray();
  124. }
  125. }
  126. }