DecoderReplacementFallback.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 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
  56. {
  57. get
  58. {
  59. return _strDefault;
  60. }
  61. }
  62. public override DecoderFallbackBuffer CreateFallbackBuffer()
  63. {
  64. return new DecoderReplacementFallbackBuffer(this);
  65. }
  66. // Maximum number of characters that this instance of this fallback could return
  67. public override int MaxCharCount
  68. {
  69. get
  70. {
  71. return _strDefault.Length;
  72. }
  73. }
  74. public override bool Equals(object value)
  75. {
  76. if (value is DecoderReplacementFallback that)
  77. {
  78. return (_strDefault == that._strDefault);
  79. }
  80. return (false);
  81. }
  82. public override int GetHashCode()
  83. {
  84. return _strDefault.GetHashCode();
  85. }
  86. }
  87. public sealed class DecoderReplacementFallbackBuffer : DecoderFallbackBuffer
  88. {
  89. // Store our default string
  90. private string _strDefault;
  91. private int _fallbackCount = -1;
  92. private int _fallbackIndex = -1;
  93. // Construction
  94. public DecoderReplacementFallbackBuffer(DecoderReplacementFallback fallback)
  95. {
  96. _strDefault = fallback.DefaultString;
  97. }
  98. // Fallback Methods
  99. public override bool Fallback(byte[] bytesUnknown, int index)
  100. {
  101. // We expect no previous fallback in our buffer
  102. // We can't call recursively but others might (note, we don't test on last char!!!)
  103. if (_fallbackCount >= 1)
  104. {
  105. ThrowLastBytesRecursive(bytesUnknown);
  106. }
  107. // Go ahead and get our fallback
  108. if (_strDefault.Length == 0)
  109. return false;
  110. _fallbackCount = _strDefault.Length;
  111. _fallbackIndex = -1;
  112. return true;
  113. }
  114. public override char GetNextChar()
  115. {
  116. // We want it to get < 0 because == 0 means that the current/last character is a fallback
  117. // and we need to detect recursion. We could have a flag but we already have this counter.
  118. _fallbackCount--;
  119. _fallbackIndex++;
  120. // Do we have anything left? 0 is now last fallback char, negative is nothing left
  121. if (_fallbackCount < 0)
  122. return '\0';
  123. // Need to get it out of the buffer.
  124. // Make sure it didn't wrap from the fast count-- path
  125. if (_fallbackCount == int.MaxValue)
  126. {
  127. _fallbackCount = -1;
  128. return '\0';
  129. }
  130. // Now make sure its in the expected range
  131. Debug.Assert(_fallbackIndex < _strDefault.Length && _fallbackIndex >= 0,
  132. "Index exceeds buffer range");
  133. return _strDefault[_fallbackIndex];
  134. }
  135. public override bool MovePrevious()
  136. {
  137. // Back up one, only if we just processed the last character (or earlier)
  138. if (_fallbackCount >= -1 && _fallbackIndex >= 0)
  139. {
  140. _fallbackIndex--;
  141. _fallbackCount++;
  142. return true;
  143. }
  144. // Return false 'cause we couldn't do it.
  145. return false;
  146. }
  147. // How many characters left to output?
  148. public override int Remaining
  149. {
  150. get
  151. {
  152. // Our count is 0 for 1 character left.
  153. return (_fallbackCount < 0) ? 0 : _fallbackCount;
  154. }
  155. }
  156. // Clear the buffer
  157. public override unsafe void Reset()
  158. {
  159. _fallbackCount = -1;
  160. _fallbackIndex = -1;
  161. byteStart = null;
  162. }
  163. // This version just counts the fallback and doesn't actually copy anything.
  164. internal unsafe override int InternalFallback(byte[] bytes, byte* pBytes)
  165. // Right now this has both bytes and bytes[], since we might have extra bytes, hence the
  166. // array, and we might need the index, hence the byte*
  167. {
  168. // return our replacement string Length
  169. return _strDefault.Length;
  170. }
  171. }
  172. }