PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/MSBuild/Microsoft/Build/Shared/QuotingUtilities.cs

#
C# | 143 lines | 140 code | 3 blank | 0 comment | 10 complexity | f393d24288878a505ff1c0890cc4c0a2 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
  1. namespace Microsoft.Build.Shared
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. internal static class QuotingUtilities
  8. {
  9. private static readonly char[] splitMarker = new char[1];
  10. internal static ArrayList SplitUnquoted(string input, params char[] separator)
  11. {
  12. int num;
  13. return SplitUnquoted(input, 0x7fffffff, false, false, out num, separator);
  14. }
  15. internal static ArrayList SplitUnquoted(string input, int maxSplits, bool keepEmptySplits, bool unquote, out int emptySplits, params char[] separator)
  16. {
  17. Microsoft.Build.Shared.ErrorUtilities.VerifyThrow(maxSplits >= 2, "There is no point calling this method for less than two splits.");
  18. string str = new StringBuilder().Append(separator).ToString();
  19. Microsoft.Build.Shared.ErrorUtilities.VerifyThrow(str.IndexOf('"') == -1, "The double-quote character is not supported as a separator.");
  20. StringBuilder builder = new StringBuilder();
  21. builder.EnsureCapacity(input.Length);
  22. bool flag = false;
  23. int num = 0;
  24. int num2 = 1;
  25. for (int i = 0; (i < input.Length) && (num2 < maxSplits); i++)
  26. {
  27. char ch = input[i];
  28. if (ch != '\0')
  29. {
  30. if (ch != '"')
  31. {
  32. if (ch != '\\')
  33. {
  34. goto Label_00CD;
  35. }
  36. builder.Append('\\');
  37. num++;
  38. }
  39. else
  40. {
  41. builder.Append('"');
  42. if ((num % 2) == 0)
  43. {
  44. if ((flag && (i < (input.Length - 1))) && (input[i + 1] == '"'))
  45. {
  46. builder.Append('"');
  47. i++;
  48. }
  49. flag = !flag;
  50. }
  51. num = 0;
  52. }
  53. }
  54. continue;
  55. Label_00CD:
  56. if (!flag && (((str.Length == 0) && char.IsWhiteSpace(input[i])) || (str.IndexOf(input[i]) != -1)))
  57. {
  58. builder.Append('\0');
  59. if (++num2 == maxSplits)
  60. {
  61. builder.Append(input, i + 1, input.Length - (i + 1));
  62. }
  63. }
  64. else
  65. {
  66. builder.Append(input[i]);
  67. }
  68. num = 0;
  69. }
  70. ArrayList list = new ArrayList();
  71. emptySplits = 0;
  72. foreach (string str2 in builder.ToString().Split(splitMarker, maxSplits))
  73. {
  74. string str3 = unquote ? Unquote(str2) : str2;
  75. if ((str3.Length > 0) || keepEmptySplits)
  76. {
  77. list.Add(str3);
  78. }
  79. else
  80. {
  81. emptySplits++;
  82. }
  83. }
  84. return list;
  85. }
  86. internal static string Unquote(string input)
  87. {
  88. int num;
  89. return Unquote(input, out num);
  90. }
  91. internal static string Unquote(string input, out int doubleQuotesRemoved)
  92. {
  93. StringBuilder builder = new StringBuilder();
  94. builder.EnsureCapacity(input.Length);
  95. bool flag = false;
  96. int repeatCount = 0;
  97. doubleQuotesRemoved = 0;
  98. for (int i = 0; i < input.Length; i++)
  99. {
  100. char ch = input[i];
  101. if (ch != '"')
  102. {
  103. if (ch != '\\')
  104. {
  105. goto Label_008F;
  106. }
  107. repeatCount++;
  108. }
  109. else
  110. {
  111. builder.Append('\\', repeatCount / 2);
  112. if ((repeatCount % 2) == 0)
  113. {
  114. if ((flag && (i < (input.Length - 1))) && (input[i + 1] == '"'))
  115. {
  116. builder.Append('"');
  117. i++;
  118. }
  119. flag = !flag;
  120. doubleQuotesRemoved++;
  121. }
  122. else
  123. {
  124. builder.Append('"');
  125. }
  126. repeatCount = 0;
  127. }
  128. continue;
  129. Label_008F:
  130. builder.Append('\\', repeatCount);
  131. builder.Append(input[i]);
  132. repeatCount = 0;
  133. }
  134. return builder.Append('\\', repeatCount).ToString();
  135. }
  136. }
  137. }