Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PackUriParser.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace System.IO.Packaging
  7. {
  8. class PackUriParser : GenericUriParser
  9. {
  10. const string SchemaName = "pack";
  11. StringBuilder builder = new StringBuilder();
  12. public PackUriParser()
  13. : base(GenericUriParserOptions.Default)
  14. {
  15. }
  16. protected override string GetComponents(Uri uri, UriComponents components, UriFormat format)
  17. {
  18. string s = uri.OriginalString;
  19. builder.Remove(0, builder.Length);
  20. if ((components & UriComponents.Scheme) == UriComponents.Scheme)
  21. {
  22. int start = 0;
  23. int end = s.IndexOf(':');
  24. builder.Append(s, start, end - start);
  25. }
  26. if ((components & UriComponents.Host) == UriComponents.Host)
  27. {
  28. // Skip past pack://
  29. int start = 7;
  30. int end = s.IndexOf('/', start);
  31. if (end == -1)
  32. end = s.Length;
  33. if (builder.Length > 0)
  34. builder.Append("://");
  35. builder.Append(s, start, end - start);
  36. }
  37. // Port is always -1, so i think i can ignore both Port and StrongPort
  38. // Normally they'd get parsed here
  39. if ((components & UriComponents.Path) == UriComponents.Path)
  40. {
  41. // Skip past pack://
  42. int start = s.IndexOf('/', 7);
  43. int end = s.IndexOf('?');
  44. if (end == -1)
  45. end = s.IndexOf('#');
  46. if (end == -1)
  47. end = s.Length;
  48. if ((components & UriComponents.KeepDelimiter) != UriComponents.KeepDelimiter &&
  49. builder.Length == 0)
  50. start++;
  51. if (start > 0) builder.Append(s, start, end - start);
  52. }
  53. if ((components & UriComponents.Query) == UriComponents.Query)
  54. {
  55. int index = s.IndexOf('?');
  56. if (index != -1)
  57. {
  58. if ((components & UriComponents.KeepDelimiter) != UriComponents.KeepDelimiter &&
  59. builder.Length == 0)
  60. index++;
  61. int fragIndex = s.IndexOf('#');
  62. int end = fragIndex == -1 ? s.Length : fragIndex;
  63. builder.Append(s, index, end - index);
  64. }
  65. }
  66. if ((components & UriComponents.Fragment) == UriComponents.Fragment)
  67. {
  68. int index = s.IndexOf('#');
  69. if (index != -1)
  70. {
  71. if ((components & UriComponents.KeepDelimiter) != UriComponents.KeepDelimiter &&
  72. builder.Length == 0)
  73. index++;
  74. builder.Append(s, index, s.Length - index);
  75. }
  76. }
  77. return builder.ToString();
  78. }
  79. protected override void InitializeAndValidate(Uri uri, out UriFormatException parsingError)
  80. {
  81. parsingError = null;
  82. }
  83. protected override UriParser OnNewUri()
  84. {
  85. return new PackUriParser();
  86. }
  87. }
  88. }