DecoderReplacementFallback.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. namespace System.Text
  6. {
  7. public sealed class DecoderReplacementFallback : DecoderFallback
  8. {
  9. // Our variables
  10. private readonly string _strDefault;
  11. // Construction. Default replacement fallback uses no best fit and ? replacement string
  12. public DecoderReplacementFallback() : this("?")
  13. {
  14. }
  15. public DecoderReplacementFallback(string replacement)
  16. {
  17. if (replacement == null)
  18. throw new ArgumentNullException(nameof(replacement));
  19. // Make sure it doesn't have bad surrogate pairs
  20. bool bFoundHigh = false;
  21. for (int i = 0; i < replacement.Length; i++)
  22. {
  23. // Found a surrogate?
  24. if (char.IsSurrogate(replacement, i))
  25. {
  26. // High or Low?
  27. if (char.IsHighSurrogate(replacement, i))
  28. {
  29. // if already had a high one, stop
  30. if (bFoundHigh)
  31. break; // break & throw at the bFoundHIgh below
  32. bFoundHigh = true;
  33. }
  34. else
  35. {
  36. // Low, did we have a high?
  37. if (!bFoundHigh)
  38. {
  39. // Didn't have one, make if fail when we stop
  40. bFoundHigh = true;
  41. break;
  42. }
  43. // Clear flag
  44. bFoundHigh = false;
  45. }
  46. }
  47. // If last was high we're in trouble (not surrogate so not low surrogate, so break)
  48. else if (bFoundHigh)
  49. break;
  50. }
  51. if (bFoundHigh)
  52. throw new ArgumentException(SR.Format(SR.Argument_InvalidCharSequenceNoIndex, nameof(replacement)));
  53. _strDefault = replacement;
  54. }
  55. public string DefaultString => _strDefault;
  56. public override DecoderFallbackBuffer CreateFallbackBuffer() =>
  57. new DecoderReplacementFallbackBuffer(this);
  58. // Maximum number of characters that this instance of this fallback could return
  59. public override int MaxCharCount => _strDefault.Length;
  60. public override bool Equals(object? value) =>
  61. value is DecoderReplacementFallback that &&
  62. _strDefault == that._strDefault;
  63. public override int GetHashCode() => _strDefault.GetHashCode();
  64. }
  65. public sealed class DecoderReplacementFallbackBuffer : DecoderFallbackBuffer
  66. {
  67. // Store our default string
  68. private readonly string _strDefault;
  69. private int _fallbackCount = -1;
  70. private int _fallbackIndex = -1;
  71. // Construction
  72. public DecoderReplacementFallbackBuffer(DecoderReplacementFallback fallback)
  73. {
  74. _strDefault = fallback.DefaultString;
  75. }
  76. // Fallback Methods
  77. public override bool Fallback(byte[] bytesUnknown, int index)
  78. {
  79. // We expect no previous fallback in our buffer
  80. // We can't call recursively but others might (note, we don't test on last char!!!)
  81. if (_fallbackCount >= 1)
  82. {
  83. ThrowLastBytesRecursive(bytesUnknown);
  84. }
  85. // Go ahead and get our fallback
  86. if (_strDefault.Length == 0)
  87. return false;
  88. _fallbackCount = _strDefault.Length;
  89. _fallbackIndex = -1;
  90. return true;
  91. }
  92. public override char GetNextChar()
  93. {
  94. // We want it to get < 0 because == 0 means that the current/last character is a fallback
  95. // and we need to detect recursion. We could have a flag but we already have this counter.
  96. _fallbackCount--;
  97. _fallbackIndex++;
  98. // Do we have anything left? 0 is now last fallback char, negative is nothing left
  99. if (_fallbackCount < 0)
  100. return '\0';
  101. // Need to get it out of the buffer.
  102. // Make sure it didn't wrap from the fast count-- path
  103. if (_fallbackCount == int.MaxValue)
  104. {
  105. _fallbackCount = -1;
  106. return '\0';
  107. }
  108. // Now make sure its in the expected range
  109. Debug.Assert(_fallbackIndex < _strDefault.Length && _fallbackIndex >= 0,
  110. "Index exceeds buffer range");
  111. return _strDefault[_fallbackIndex];
  112. }
  113. public override bool MovePrevious()
  114. {
  115. // Back up one, only if we just processed the last character (or earlier)
  116. if (_fallbackCount >= -1 && _fallbackIndex >= 0)
  117. {
  118. _fallbackIndex--;
  119. _fallbackCount++;
  120. return true;
  121. }
  122. // Return false 'cause we couldn't do it.
  123. return false;
  124. }
  125. // How many characters left to output?
  126. public override int Remaining =>
  127. // Our count is 0 for 1 character left.
  128. (_fallbackCount < 0) ? 0 : _fallbackCount;
  129. // Clear the buffer
  130. public override unsafe void Reset()
  131. {
  132. _fallbackCount = -1;
  133. _fallbackIndex = -1;
  134. byteStart = null;
  135. }
  136. // This version just counts the fallback and doesn't actually copy anything.
  137. internal override unsafe int InternalFallback(byte[] bytes, byte* pBytes) =>
  138. // Right now this has both bytes and bytes[], since we might have extra bytes,
  139. // hence the array, and we might need the index, hence the byte*.
  140. // Return our replacement string Length
  141. _strDefault.Length;
  142. }
  143. }