EncoderBestFitFallback.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. //
  5. // This is used internally to create best fit behavior as per the original windows best fit behavior.
  6. //
  7. using System.Diagnostics;
  8. using System.Globalization;
  9. using System.Threading;
  10. namespace System.Text
  11. {
  12. internal class InternalEncoderBestFitFallback : EncoderFallback
  13. {
  14. // Our variables
  15. internal Encoding _encoding = null;
  16. internal char[] _arrayBestFit = null;
  17. internal InternalEncoderBestFitFallback(Encoding encoding)
  18. {
  19. // Need to load our replacement characters table.
  20. _encoding = encoding;
  21. }
  22. public override EncoderFallbackBuffer CreateFallbackBuffer()
  23. {
  24. return new InternalEncoderBestFitFallbackBuffer(this);
  25. }
  26. // Maximum number of characters that this instance of this fallback could return
  27. public override int MaxCharCount
  28. {
  29. get
  30. {
  31. return 1;
  32. }
  33. }
  34. public override bool Equals(object value)
  35. {
  36. if (value is InternalEncoderBestFitFallback that)
  37. {
  38. return (_encoding.CodePage == that._encoding.CodePage);
  39. }
  40. return (false);
  41. }
  42. public override int GetHashCode()
  43. {
  44. return _encoding.CodePage;
  45. }
  46. }
  47. internal sealed class InternalEncoderBestFitFallbackBuffer : EncoderFallbackBuffer
  48. {
  49. // Our variables
  50. private char _cBestFit = '\0';
  51. private InternalEncoderBestFitFallback _oFallback;
  52. private int _iCount = -1;
  53. private int _iSize;
  54. // Private object for locking instead of locking on a public type for SQL reliability work.
  55. private static object s_InternalSyncObject;
  56. private static object InternalSyncObject
  57. {
  58. get
  59. {
  60. if (s_InternalSyncObject == null)
  61. {
  62. object o = new object();
  63. Interlocked.CompareExchange<object>(ref s_InternalSyncObject, o, null);
  64. }
  65. return s_InternalSyncObject;
  66. }
  67. }
  68. // Constructor
  69. public InternalEncoderBestFitFallbackBuffer(InternalEncoderBestFitFallback fallback)
  70. {
  71. _oFallback = fallback;
  72. if (_oFallback._arrayBestFit == null)
  73. {
  74. // Lock so we don't confuse ourselves.
  75. lock (InternalSyncObject)
  76. {
  77. // Double check before we do it again.
  78. if (_oFallback._arrayBestFit == null)
  79. _oFallback._arrayBestFit = fallback._encoding.GetBestFitUnicodeToBytesData();
  80. }
  81. }
  82. }
  83. // Fallback methods
  84. public override bool Fallback(char charUnknown, int index)
  85. {
  86. // If we had a buffer already we're being recursive, throw, it's probably at the suspect
  87. // character in our array.
  88. // Shouldn't be able to get here for all of our code pages, table would have to be messed up.
  89. Debug.Assert(_iCount < 1, "[InternalEncoderBestFitFallbackBuffer.Fallback(non surrogate)] Fallback char " + ((int)_cBestFit).ToString("X4", CultureInfo.InvariantCulture) + " caused recursive fallback");
  90. _iCount = _iSize = 1;
  91. _cBestFit = TryBestFit(charUnknown);
  92. if (_cBestFit == '\0')
  93. _cBestFit = '?';
  94. return true;
  95. }
  96. public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
  97. {
  98. // Double check input surrogate pair
  99. if (!char.IsHighSurrogate(charUnknownHigh))
  100. throw new ArgumentOutOfRangeException(nameof(charUnknownHigh),
  101. SR.Format(SR.ArgumentOutOfRange_Range,
  102. 0xD800, 0xDBFF));
  103. if (!char.IsLowSurrogate(charUnknownLow))
  104. throw new ArgumentOutOfRangeException(nameof(charUnknownLow),
  105. SR.Format(SR.ArgumentOutOfRange_Range,
  106. 0xDC00, 0xDFFF));
  107. // If we had a buffer already we're being recursive, throw, it's probably at the suspect
  108. // character in our array. 0 is processing last character, < 0 is not falling back
  109. // Shouldn't be able to get here, table would have to be messed up.
  110. Debug.Assert(_iCount < 1, "[InternalEncoderBestFitFallbackBuffer.Fallback(surrogate)] Fallback char " + ((int)_cBestFit).ToString("X4", CultureInfo.InvariantCulture) + " caused recursive fallback");
  111. // Go ahead and get our fallback, surrogates don't have best fit
  112. _cBestFit = '?';
  113. _iCount = _iSize = 2;
  114. return true;
  115. }
  116. // Default version is overridden in EncoderReplacementFallback.cs
  117. public override char GetNextChar()
  118. {
  119. // We want it to get < 0 because == 0 means that the current/last character is a fallback
  120. // and we need to detect recursion. We could have a flag but we already have this counter.
  121. _iCount--;
  122. // Do we have anything left? 0 is now last fallback char, negative is nothing left
  123. if (_iCount < 0)
  124. return '\0';
  125. // Need to get it out of the buffer.
  126. // Make sure it didn't wrap from the fast count-- path
  127. if (_iCount == int.MaxValue)
  128. {
  129. _iCount = -1;
  130. return '\0';
  131. }
  132. // Return the best fit character
  133. return _cBestFit;
  134. }
  135. public override bool MovePrevious()
  136. {
  137. // Exception fallback doesn't have anywhere to back up to.
  138. if (_iCount >= 0)
  139. _iCount++;
  140. // Return true if we could do it.
  141. return (_iCount >= 0 && _iCount <= _iSize);
  142. }
  143. // How many characters left to output?
  144. public override int Remaining
  145. {
  146. get
  147. {
  148. return (_iCount > 0) ? _iCount : 0;
  149. }
  150. }
  151. // Clear the buffer
  152. public override unsafe void Reset()
  153. {
  154. _iCount = -1;
  155. charStart = null;
  156. bFallingBack = false;
  157. }
  158. // private helper methods
  159. private char TryBestFit(char cUnknown)
  160. {
  161. // Need to figure out our best fit character, low is beginning of array, high is 1 AFTER end of array
  162. int lowBound = 0;
  163. int highBound = _oFallback._arrayBestFit.Length;
  164. int index;
  165. // Binary search the array
  166. int iDiff;
  167. while ((iDiff = (highBound - lowBound)) > 6)
  168. {
  169. // Look in the middle, which is complicated by the fact that we have 2 #s for each pair,
  170. // so we don't want index to be odd because we want to be on word boundaries.
  171. // Also note that index can never == highBound (because diff is rounded down)
  172. index = ((iDiff / 2) + lowBound) & 0xFFFE;
  173. char cTest = _oFallback._arrayBestFit[index];
  174. if (cTest == cUnknown)
  175. {
  176. // We found it
  177. Debug.Assert(index + 1 < _oFallback._arrayBestFit.Length,
  178. "[InternalEncoderBestFitFallbackBuffer.TryBestFit]Expected replacement character at end of array");
  179. return _oFallback._arrayBestFit[index + 1];
  180. }
  181. else if (cTest < cUnknown)
  182. {
  183. // We weren't high enough
  184. lowBound = index;
  185. }
  186. else
  187. {
  188. // We weren't low enough
  189. highBound = index;
  190. }
  191. }
  192. for (index = lowBound; index < highBound; index += 2)
  193. {
  194. if (_oFallback._arrayBestFit[index] == cUnknown)
  195. {
  196. // We found it
  197. Debug.Assert(index + 1 < _oFallback._arrayBestFit.Length,
  198. "[InternalEncoderBestFitFallbackBuffer.TryBestFit]Expected replacement character at end of array");
  199. return _oFallback._arrayBestFit[index + 1];
  200. }
  201. }
  202. // Char wasn't in our table
  203. return '\0';
  204. }
  205. }
  206. }