PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/MSBuild/Microsoft/Build/Shared/EscapingUtilities.cs

#
C# | 81 lines | 74 code | 7 blank | 0 comment | 23 complexity | 977efe0c29cd27cc0a464bc773cea113 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
  1. namespace Microsoft.Build.Shared
  2. {
  3. using System;
  4. using System.Globalization;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. internal static class EscapingUtilities
  8. {
  9. private static char[] charsToEscape = new char[] { '%', '*', '?', '@', '$', '(', ')', ';', '\'' };
  10. internal static bool ContainsEscapedWildcards(string escapedString)
  11. {
  12. if ((-1 == escapedString.IndexOf('%')) || ((-1 == escapedString.IndexOf("%2", StringComparison.Ordinal)) && (-1 == escapedString.IndexOf("%3", StringComparison.Ordinal))))
  13. {
  14. return false;
  15. }
  16. if (((-1 == escapedString.IndexOf("%2a", StringComparison.Ordinal)) && (-1 == escapedString.IndexOf("%2A", StringComparison.Ordinal))) && (-1 == escapedString.IndexOf("%3f", StringComparison.Ordinal)))
  17. {
  18. return (-1 != escapedString.IndexOf("%3F", StringComparison.Ordinal));
  19. }
  20. return true;
  21. }
  22. private static bool ContainsReservedCharacters(string unescapedString)
  23. {
  24. return (-1 != unescapedString.IndexOfAny(charsToEscape));
  25. }
  26. internal static string Escape(string unescapedString)
  27. {
  28. Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(unescapedString, "unescapedString");
  29. if (!ContainsReservedCharacters(unescapedString))
  30. {
  31. return unescapedString;
  32. }
  33. StringBuilder builder = new StringBuilder(unescapedString, unescapedString.Length * 2);
  34. foreach (char ch in charsToEscape)
  35. {
  36. int num = Convert.ToInt32(ch);
  37. string newValue = string.Format(CultureInfo.InvariantCulture, "%{0:x00}", new object[] { num });
  38. builder.Replace(ch.ToString(CultureInfo.InvariantCulture), newValue);
  39. }
  40. return builder.ToString();
  41. }
  42. internal static string UnescapeAll(string escapedString)
  43. {
  44. bool flag;
  45. return UnescapeAll(escapedString, out flag);
  46. }
  47. internal static string UnescapeAll(string escapedString, out bool escapingWasNecessary)
  48. {
  49. Microsoft.Build.Shared.ErrorUtilities.VerifyThrow(escapedString != null, "Null strings not allowed.");
  50. escapingWasNecessary = false;
  51. int index = escapedString.IndexOf('%');
  52. if (index == -1)
  53. {
  54. return escapedString;
  55. }
  56. StringBuilder builder = new StringBuilder(escapedString.Length);
  57. int startIndex = 0;
  58. while (index != -1)
  59. {
  60. if (((index <= (escapedString.Length - 3)) && Uri.IsHexDigit(escapedString[index + 1])) && Uri.IsHexDigit(escapedString[index + 2]))
  61. {
  62. builder.Append(escapedString, startIndex, index - startIndex);
  63. char ch = (char) int.Parse(escapedString.Substring(index + 1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
  64. builder.Append(ch);
  65. startIndex = index + 3;
  66. escapingWasNecessary = true;
  67. }
  68. index = escapedString.IndexOf('%', index + 1);
  69. }
  70. builder.Append(escapedString, startIndex, escapedString.Length - startIndex);
  71. return builder.ToString();
  72. }
  73. }
  74. }