EncoderBestFitFallback.cs 8.5 KB

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