EncoderReplacementFallback.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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;
  5. using System.Runtime;
  6. using System.Diagnostics;
  7. namespace System.Text
  8. {
  9. public sealed class EncoderReplacementFallback : EncoderFallback
  10. {
  11. // Our variables
  12. private string _strDefault;
  13. // Construction. Default replacement fallback uses no best fit and ? replacement string
  14. public EncoderReplacementFallback() : this("?")
  15. {
  16. }
  17. public EncoderReplacementFallback(string replacement)
  18. {
  19. // Must not be null
  20. if (replacement == null)
  21. throw new ArgumentNullException(nameof(replacement));
  22. // Make sure it doesn't have bad surrogate pairs
  23. bool bFoundHigh = false;
  24. for (int i = 0; i < replacement.Length; i++)
  25. {
  26. // Found a surrogate?
  27. if (char.IsSurrogate(replacement, i))
  28. {
  29. // High or Low?
  30. if (char.IsHighSurrogate(replacement, i))
  31. {
  32. // if already had a high one, stop
  33. if (bFoundHigh)
  34. break; // break & throw at the bFoundHIgh below
  35. bFoundHigh = true;
  36. }
  37. else
  38. {
  39. // Low, did we have a high?
  40. if (!bFoundHigh)
  41. {
  42. // Didn't have one, make if fail when we stop
  43. bFoundHigh = true;
  44. break;
  45. }
  46. // Clear flag
  47. bFoundHigh = false;
  48. }
  49. }
  50. // If last was high we're in trouble (not surrogate so not low surrogate, so break)
  51. else if (bFoundHigh)
  52. break;
  53. }
  54. if (bFoundHigh)
  55. throw new ArgumentException(SR.Format(SR.Argument_InvalidCharSequenceNoIndex, nameof(replacement)));
  56. _strDefault = replacement;
  57. }
  58. public string DefaultString
  59. {
  60. get
  61. {
  62. return _strDefault;
  63. }
  64. }
  65. public override EncoderFallbackBuffer CreateFallbackBuffer()
  66. {
  67. return new EncoderReplacementFallbackBuffer(this);
  68. }
  69. // Maximum number of characters that this instance of this fallback could return
  70. public override int MaxCharCount
  71. {
  72. get
  73. {
  74. return _strDefault.Length;
  75. }
  76. }
  77. public override bool Equals(object value)
  78. {
  79. if (value is EncoderReplacementFallback that)
  80. {
  81. return (_strDefault == that._strDefault);
  82. }
  83. return (false);
  84. }
  85. public override int GetHashCode()
  86. {
  87. return _strDefault.GetHashCode();
  88. }
  89. }
  90. public sealed class EncoderReplacementFallbackBuffer : EncoderFallbackBuffer
  91. {
  92. // Store our default string
  93. private string _strDefault;
  94. private int _fallbackCount = -1;
  95. private int _fallbackIndex = -1;
  96. // Construction
  97. public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback)
  98. {
  99. // 2X in case we're a surrogate pair
  100. _strDefault = fallback.DefaultString + fallback.DefaultString;
  101. }
  102. // Fallback Methods
  103. public override bool Fallback(char charUnknown, int index)
  104. {
  105. // If we had a buffer already we're being recursive, throw, it's probably at the suspect
  106. // character in our array.
  107. if (_fallbackCount >= 1)
  108. {
  109. // If we're recursive we may still have something in our buffer that makes this a surrogate
  110. if (char.IsHighSurrogate(charUnknown) && _fallbackCount >= 0 &&
  111. char.IsLowSurrogate(_strDefault[_fallbackIndex + 1]))
  112. ThrowLastCharRecursive(char.ConvertToUtf32(charUnknown, _strDefault[_fallbackIndex + 1]));
  113. // Nope, just one character
  114. ThrowLastCharRecursive(unchecked((int)charUnknown));
  115. }
  116. // Go ahead and get our fallback
  117. // Divide by 2 because we aren't a surrogate pair
  118. _fallbackCount = _strDefault.Length / 2;
  119. _fallbackIndex = -1;
  120. return _fallbackCount != 0;
  121. }
  122. public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
  123. {
  124. // Double check input surrogate pair
  125. if (!char.IsHighSurrogate(charUnknownHigh))
  126. throw new ArgumentOutOfRangeException(nameof(charUnknownHigh),
  127. SR.Format(SR.ArgumentOutOfRange_Range, 0xD800, 0xDBFF));
  128. if (!char.IsLowSurrogate(charUnknownLow))
  129. throw new ArgumentOutOfRangeException(nameof(charUnknownLow),
  130. SR.Format(SR.ArgumentOutOfRange_Range, 0xDC00, 0xDFFF));
  131. // If we had a buffer already we're being recursive, throw, it's probably at the suspect
  132. // character in our array.
  133. if (_fallbackCount >= 1)
  134. ThrowLastCharRecursive(char.ConvertToUtf32(charUnknownHigh, charUnknownLow));
  135. // Go ahead and get our fallback
  136. _fallbackCount = _strDefault.Length;
  137. _fallbackIndex = -1;
  138. return _fallbackCount != 0;
  139. }
  140. public override char GetNextChar()
  141. {
  142. // We want it to get < 0 because == 0 means that the current/last character is a fallback
  143. // and we need to detect recursion. We could have a flag but we already have this counter.
  144. _fallbackCount--;
  145. _fallbackIndex++;
  146. // Do we have anything left? 0 is now last fallback char, negative is nothing left
  147. if (_fallbackCount < 0)
  148. return '\0';
  149. // Need to get it out of the buffer.
  150. // Make sure it didn't wrap from the fast count-- path
  151. if (_fallbackCount == int.MaxValue)
  152. {
  153. _fallbackCount = -1;
  154. return '\0';
  155. }
  156. // Now make sure its in the expected range
  157. Debug.Assert(_fallbackIndex < _strDefault.Length && _fallbackIndex >= 0,
  158. "Index exceeds buffer range");
  159. return _strDefault[_fallbackIndex];
  160. }
  161. public override bool MovePrevious()
  162. {
  163. // Back up one, only if we just processed the last character (or earlier)
  164. if (_fallbackCount >= -1 && _fallbackIndex >= 0)
  165. {
  166. _fallbackIndex--;
  167. _fallbackCount++;
  168. return true;
  169. }
  170. // Return false 'cause we couldn't do it.
  171. return false;
  172. }
  173. // How many characters left to output?
  174. public override int Remaining
  175. {
  176. get
  177. {
  178. // Our count is 0 for 1 character left.
  179. return (_fallbackCount < 0) ? 0 : _fallbackCount;
  180. }
  181. }
  182. // Clear the buffer
  183. public override unsafe void Reset()
  184. {
  185. _fallbackCount = -1;
  186. _fallbackIndex = 0;
  187. charStart = null;
  188. bFallingBack = false;
  189. }
  190. }
  191. }