From c059ec9cbbedd8a25a785a2c4d1a0515732134d6 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 18 Jul 2017 13:51:57 -0700 Subject: [PATCH] Encoding code clean up (#12864) * Encoding code clean up This change to clean all the serialization code from the encoding and move the files to the shared folder * delete DecoderNLS * re-add DecoderNls with the right casing * remove bIsMicrosoftBestFitFallback * Misc fixes per review comments --- src/mscorlib/System.Private.CoreLib.csproj | 12 --- .../shared/System.Private.CoreLib.Shared.projitems | 10 ++ src/mscorlib/shared/System/Text/ASCIIEncoding.cs | 31 +++---- src/mscorlib/shared/System/Text/Decoder.cs | 38 +++----- .../System/Text/DecoderBestFitFallback.cs | 80 ++++++++-------- .../System/Text/DecoderExceptionFallback.cs | 29 +++--- .../{src => shared}/System/Text/DecoderFallback.cs | 85 ++++------------- .../{src => shared}/System/Text/DecoderNLS.cs | 62 +++++-------- .../System/Text/DecoderReplacementFallback.cs | 57 ++++++------ src/mscorlib/shared/System/Text/Encoder.cs | 39 ++++---- .../System/Text/EncoderBestFitFallback.cs | 91 +++++++++--------- .../System/Text/EncoderExceptionFallback.cs | 28 +++--- .../{src => shared}/System/Text/EncoderFallback.cs | 59 ++++-------- .../{src => shared}/System/Text/EncoderNLS.cs | 70 ++++++-------- .../System/Text/EncoderReplacementFallback.cs | 68 +++++++------- src/mscorlib/shared/System/Text/Encoding.cs | 56 +++++------ src/mscorlib/shared/System/Text/Latin1Encoding.cs | 27 ++---- src/mscorlib/shared/System/Text/UTF32Encoding.cs | 28 +++--- src/mscorlib/shared/System/Text/UTF7Encoding.cs | 31 ++----- src/mscorlib/shared/System/Text/UTF8Encoding.cs | 93 ++++++++----------- src/mscorlib/shared/System/Text/UnicodeEncoding.cs | 102 ++++++++------------- 21 files changed, 444 insertions(+), 652 deletions(-) rename src/mscorlib/{src => shared}/System/Text/DecoderBestFitFallback.cs (76%) rename src/mscorlib/{src => shared}/System/Text/DecoderExceptionFallback.cs (83%) rename src/mscorlib/{src => shared}/System/Text/DecoderFallback.cs (73%) rename src/mscorlib/{src => shared}/System/Text/DecoderNLS.cs (85%) rename src/mscorlib/{src => shared}/System/Text/DecoderReplacementFallback.cs (81%) rename src/mscorlib/{src => shared}/System/Text/EncoderBestFitFallback.cs (73%) rename src/mscorlib/{src => shared}/System/Text/EncoderExceptionFallback.cs (90%) rename src/mscorlib/{src => shared}/System/Text/EncoderFallback.cs (76%) rename src/mscorlib/{src => shared}/System/Text/EncoderNLS.cs (83%) rename src/mscorlib/{src => shared}/System/Text/EncoderReplacementFallback.cs (80%) diff --git a/src/mscorlib/System.Private.CoreLib.csproj b/src/mscorlib/System.Private.CoreLib.csproj index 587f9d1..6f32dfc 100644 --- a/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/mscorlib/System.Private.CoreLib.csproj @@ -578,18 +578,6 @@ - - - - - - - - - - - - diff --git a/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems b/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems index 776fc04..07e15cf 100644 --- a/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems @@ -371,7 +371,17 @@ + + + + + + + + + + diff --git a/src/mscorlib/shared/System/Text/ASCIIEncoding.cs b/src/mscorlib/shared/System/Text/ASCIIEncoding.cs index 628ec9a..f614fb7 100644 --- a/src/mscorlib/shared/System/Text/ASCIIEncoding.cs +++ b/src/mscorlib/shared/System/Text/ASCIIEncoding.cs @@ -5,7 +5,6 @@ using System; using System.Diagnostics; using System.Diagnostics.Contracts; -using System.Runtime.Serialization; namespace System.Text { @@ -371,7 +370,7 @@ namespace System.Text if (encoder != null) { - charLeftOver = encoder.charLeftOver; + charLeftOver = encoder._charLeftOver; Debug.Assert(charLeftOver == 0 || Char.IsHighSurrogate(charLeftOver), "[ASCIIEncoding.GetByteCount]leftover character should be high surrogate"); @@ -382,7 +381,7 @@ namespace System.Text { // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary fallbackBuffer = encoder.FallbackBuffer; - if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) + if (fallbackBuffer.Remaining > 0 && encoder._throwOnOverflow) throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType())); // Set our internal fallback interesting things. @@ -390,7 +389,7 @@ namespace System.Text } // Verify that we have no fallbackbuffer, for ASCII its always empty, so just assert - Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || + Debug.Assert(!encoder._throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[ASCIICodePageEncoding.GetByteCount]Expected empty fallback buffer"); } @@ -511,7 +510,7 @@ namespace System.Text if (encoder != null) { - charLeftOver = encoder.charLeftOver; + charLeftOver = encoder._charLeftOver; fallback = encoder.Fallback as EncoderReplacementFallback; // We mustn't have left over fallback data when counting @@ -519,7 +518,7 @@ namespace System.Text { // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary fallbackBuffer = encoder.FallbackBuffer; - if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) + if (fallbackBuffer.Remaining > 0 && encoder._throwOnOverflow) throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType())); // Set our internal fallback interesting things. @@ -530,7 +529,7 @@ namespace System.Text "[ASCIIEncoding.GetBytes]leftover character should be high surrogate"); // Verify that we have no fallbackbuffer, for ASCII its always empty, so just assert - Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || + Debug.Assert(!encoder._throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[ASCIICodePageEncoding.GetBytes]Expected empty fallback buffer"); } @@ -588,8 +587,8 @@ namespace System.Text // Clear encoder if (encoder != null) { - encoder.charLeftOver = (char)0; - encoder.m_charsUsed = (int)(chars - charStart); + encoder._charLeftOver = (char)0; + encoder._charsUsed = (int)(chars - charStart); } return (int)(bytes - byteStart); @@ -686,14 +685,14 @@ namespace System.Text // Fallback stuck it in encoder if necessary, but we have to clear MustFlush cases if (fallbackBuffer != null && !fallbackBuffer.bUsedEncoder) // Clear it in case of MustFlush - encoder.charLeftOver = (char)0; + encoder._charLeftOver = (char)0; // Set our chars used count - encoder.m_charsUsed = (int)(chars - charStart); + encoder._charsUsed = (int)(chars - charStart); } Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0 || - (encoder != null && !encoder.m_throwOnOverflow), + (encoder != null && !encoder._throwOnOverflow), "[ASCIIEncoding.GetBytes]Expected Empty fallback buffer at end"); return (int)(bytes - byteStart); @@ -714,7 +713,7 @@ namespace System.Text else { fallback = decoder.Fallback as DecoderReplacementFallback; - Debug.Assert(!decoder.m_throwOnOverflow || !decoder.InternalHasFallbackBuffer || + Debug.Assert(!decoder._throwOnOverflow || !decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, "[ASCIICodePageEncoding.GetCharCount]Expected empty fallback buffer"); } @@ -797,7 +796,7 @@ namespace System.Text else { fallback = decoder.Fallback as DecoderReplacementFallback; - Debug.Assert(!decoder.m_throwOnOverflow || !decoder.InternalHasFallbackBuffer || + Debug.Assert(!decoder._throwOnOverflow || !decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, "[ASCIICodePageEncoding.GetChars]Expected empty fallback buffer"); } @@ -830,7 +829,7 @@ namespace System.Text // bytes & chars used are the same if (decoder != null) - decoder.m_bytesUsed = (int)(bytes - byteStart); + decoder._bytesUsed = (int)(bytes - byteStart); return (int)(chars - charStart); } @@ -896,7 +895,7 @@ namespace System.Text // Might have had decoder fallback stuff. if (decoder != null) - decoder.m_bytesUsed = (int)(bytes - byteStart); + decoder._bytesUsed = (int)(bytes - byteStart); // Expect Empty fallback buffer for GetChars Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, diff --git a/src/mscorlib/shared/System/Text/Decoder.cs b/src/mscorlib/shared/System/Text/Decoder.cs index aefe1f6..f109f3d 100644 --- a/src/mscorlib/shared/System/Text/Decoder.cs +++ b/src/mscorlib/shared/System/Text/Decoder.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Runtime.Serialization; using System.Text; using System; using System.Diagnostics; @@ -23,15 +22,9 @@ namespace System.Text // public abstract class Decoder { - internal DecoderFallback m_fallback = null; + internal DecoderFallback _fallback = null; - [NonSerialized] - internal DecoderFallbackBuffer m_fallbackBuffer = null; - - internal void SerializeDecoder(SerializationInfo info) - { - info.AddValue("m_fallback", this.m_fallback); - } + internal DecoderFallbackBuffer _fallbackBuffer = null; protected Decoder() { @@ -42,7 +35,7 @@ namespace System.Text { get { - return m_fallback; + return _fallback; } set @@ -52,12 +45,12 @@ namespace System.Text Contract.EndContractBlock(); // Can't change fallback if buffer is wrong - if (m_fallbackBuffer != null && m_fallbackBuffer.Remaining > 0) + if (_fallbackBuffer != null && _fallbackBuffer.Remaining > 0) throw new ArgumentException( SR.Argument_FallbackBufferNotEmpty, nameof(value)); - m_fallback = value; - m_fallbackBuffer = null; + _fallback = value; + _fallbackBuffer = null; } } @@ -67,15 +60,15 @@ namespace System.Text { get { - if (m_fallbackBuffer == null) + if (_fallbackBuffer == null) { - if (m_fallback != null) - m_fallbackBuffer = m_fallback.CreateFallbackBuffer(); + if (_fallback != null) + _fallbackBuffer = _fallback.CreateFallbackBuffer(); else - m_fallbackBuffer = DecoderFallback.ReplacementFallback.CreateFallbackBuffer(); + _fallbackBuffer = DecoderFallback.ReplacementFallback.CreateFallbackBuffer(); } - return m_fallbackBuffer; + return _fallbackBuffer; } } @@ -83,7 +76,7 @@ namespace System.Text { get { - return m_fallbackBuffer != null; + return _fallbackBuffer != null; } } @@ -101,8 +94,7 @@ namespace System.Text byte[] byteTemp = Array.Empty(); char[] charTemp = new char[GetCharCount(byteTemp, 0, 0, true)]; GetChars(byteTemp, 0, 0, charTemp, 0, true); - if (m_fallbackBuffer != null) - m_fallbackBuffer.Reset(); + _fallbackBuffer?.Reset(); } // Returns the number of characters the next call to GetChars will @@ -276,7 +268,7 @@ namespace System.Text { charsUsed = GetChars(bytes, byteIndex, bytesUsed, chars, charIndex, flush); completed = (bytesUsed == byteCount && - (m_fallbackBuffer == null || m_fallbackBuffer.Remaining == 0)); + (_fallbackBuffer == null || _fallbackBuffer.Remaining == 0)); return; } @@ -322,7 +314,7 @@ namespace System.Text { charsUsed = GetChars(bytes, bytesUsed, chars, charCount, flush); completed = (bytesUsed == byteCount && - (m_fallbackBuffer == null || m_fallbackBuffer.Remaining == 0)); + (_fallbackBuffer == null || _fallbackBuffer.Remaining == 0)); return; } diff --git a/src/mscorlib/src/System/Text/DecoderBestFitFallback.cs b/src/mscorlib/shared/System/Text/DecoderBestFitFallback.cs similarity index 76% rename from src/mscorlib/src/System/Text/DecoderBestFitFallback.cs rename to src/mscorlib/shared/System/Text/DecoderBestFitFallback.cs index f70213f..30c817c 100644 --- a/src/mscorlib/src/System/Text/DecoderBestFitFallback.cs +++ b/src/mscorlib/shared/System/Text/DecoderBestFitFallback.cs @@ -6,26 +6,22 @@ // This is used internally to create best fit behavior as per the original windows best fit behavior. // -using System; -using System.Text; -using System.Threading; using System.Diagnostics; -using System.Diagnostics.Contracts; +using System.Threading; namespace System.Text { internal sealed class InternalDecoderBestFitFallback : DecoderFallback { // Our variables - internal Encoding encoding = null; - internal char[] arrayBestFit = null; - internal char cReplacement = '?'; + internal Encoding _encoding = null; + internal char[] _arrayBestFit = null; + internal char _cReplacement = '?'; internal InternalDecoderBestFitFallback(Encoding encoding) { // Need to load our replacement characters table. - this.encoding = encoding; - this.bIsMicrosoftBestFitFallback = true; + _encoding = encoding; } public override DecoderFallbackBuffer CreateFallbackBuffer() @@ -47,24 +43,24 @@ namespace System.Text InternalDecoderBestFitFallback that = value as InternalDecoderBestFitFallback; if (that != null) { - return (this.encoding.CodePage == that.encoding.CodePage); + return (_encoding.CodePage == that._encoding.CodePage); } return (false); } public override int GetHashCode() { - return this.encoding.CodePage; + return _encoding.CodePage; } } internal sealed class InternalDecoderBestFitFallbackBuffer : DecoderFallbackBuffer { // Our variables - internal char cBestFit = '\0'; - internal int iCount = -1; - internal int iSize; - private InternalDecoderBestFitFallback oFallback; + private char _cBestFit = '\0'; + private int _iCount = -1; + private int _iSize; + private InternalDecoderBestFitFallback _oFallback; // Private object for locking instead of locking on a public type for SQL reliability work. private static Object s_InternalSyncObject; @@ -84,16 +80,16 @@ namespace System.Text // Constructor public InternalDecoderBestFitFallbackBuffer(InternalDecoderBestFitFallback fallback) { - oFallback = fallback; + _oFallback = fallback; - if (oFallback.arrayBestFit == null) + if (_oFallback._arrayBestFit == null) { // Lock so we don't confuse ourselves. lock (InternalSyncObject) { // Double check before we do it again. - if (oFallback.arrayBestFit == null) - oFallback.arrayBestFit = fallback.encoding.GetBestFitBytesToUnicodeData(); + if (_oFallback._arrayBestFit == null) + _oFallback._arrayBestFit = fallback._encoding.GetBestFitBytesToUnicodeData(); } } } @@ -102,13 +98,13 @@ namespace System.Text public override bool Fallback(byte[] bytesUnknown, int index) { // We expect no previous fallback in our buffer - Debug.Assert(iCount < 1, "[DecoderReplacementFallbackBuffer.Fallback] Calling fallback without a previously empty buffer"); + Debug.Assert(_iCount < 1, "[DecoderReplacementFallbackBuffer.Fallback] Calling fallback without a previously empty buffer"); - cBestFit = TryBestFit(bytesUnknown); - if (cBestFit == '\0') - cBestFit = oFallback.cReplacement; + _cBestFit = TryBestFit(bytesUnknown); + if (_cBestFit == '\0') + _cBestFit = _oFallback._cReplacement; - iCount = iSize = 1; + _iCount = _iSize = 1; return true; } @@ -118,32 +114,32 @@ namespace System.Text { // We want it to get < 0 because == 0 means that the current/last character is a fallback // and we need to detect recursion. We could have a flag but we already have this counter. - iCount--; + _iCount--; // Do we have anything left? 0 is now last fallback char, negative is nothing left - if (iCount < 0) + if (_iCount < 0) return '\0'; // Need to get it out of the buffer. // Make sure it didn't wrap from the fast count-- path - if (iCount == int.MaxValue) + if (_iCount == int.MaxValue) { - iCount = -1; + _iCount = -1; return '\0'; } // Return the best fit character - return cBestFit; + return _cBestFit; } public override bool MovePrevious() { // Exception fallback doesn't have anywhere to back up to. - if (iCount >= 0) - iCount++; + if (_iCount >= 0) + _iCount++; // Return true if we could do it. - return (iCount >= 0 && iCount <= iSize); + return (_iCount >= 0 && _iCount <= _iSize); } // How many characters left to output? @@ -151,14 +147,14 @@ namespace System.Text { get { - return (iCount > 0) ? iCount : 0; + return (_iCount > 0) ? _iCount : 0; } } // Clear the buffer public override unsafe void Reset() { - iCount = -1; + _iCount = -1; byteStart = null; } @@ -177,7 +173,7 @@ namespace System.Text { // Need to figure out our best fit character, low is beginning of array, high is 1 AFTER end of array int lowBound = 0; - int highBound = oFallback.arrayBestFit.Length; + int highBound = _oFallback._arrayBestFit.Length; int index; char cCheck; @@ -195,7 +191,7 @@ namespace System.Text cCheck = unchecked((char)((bytesCheck[0] << 8) + bytesCheck[1])); // Check trivial out of range case - if (cCheck < oFallback.arrayBestFit[0] || cCheck > oFallback.arrayBestFit[highBound - 2]) + if (cCheck < _oFallback._arrayBestFit[0] || cCheck > _oFallback._arrayBestFit[highBound - 2]) return '\0'; // Binary search the array @@ -207,13 +203,13 @@ namespace System.Text // Also note that index can never == highBound (because diff is rounded down) index = ((iDiff / 2) + lowBound) & 0xFFFE; - char cTest = oFallback.arrayBestFit[index]; + char cTest = _oFallback._arrayBestFit[index]; if (cTest == cCheck) { // We found it - Debug.Assert(index + 1 < oFallback.arrayBestFit.Length, + Debug.Assert(index + 1 < _oFallback._arrayBestFit.Length, "[InternalDecoderBestFitFallbackBuffer.TryBestFit]Expected replacement character at end of array"); - return oFallback.arrayBestFit[index + 1]; + return _oFallback._arrayBestFit[index + 1]; } else if (cTest < cCheck) { @@ -229,12 +225,12 @@ namespace System.Text for (index = lowBound; index < highBound; index += 2) { - if (oFallback.arrayBestFit[index] == cCheck) + if (_oFallback._arrayBestFit[index] == cCheck) { // We found it - Debug.Assert(index + 1 < oFallback.arrayBestFit.Length, + Debug.Assert(index + 1 < _oFallback._arrayBestFit.Length, "[InternalDecoderBestFitFallbackBuffer.TryBestFit]Expected replacement character at end of array"); - return oFallback.arrayBestFit[index + 1]; + return _oFallback._arrayBestFit[index + 1]; } } diff --git a/src/mscorlib/src/System/Text/DecoderExceptionFallback.cs b/src/mscorlib/shared/System/Text/DecoderExceptionFallback.cs similarity index 83% rename from src/mscorlib/src/System/Text/DecoderExceptionFallback.cs rename to src/mscorlib/shared/System/Text/DecoderExceptionFallback.cs index 7a8db57..b465aa6 100644 --- a/src/mscorlib/src/System/Text/DecoderExceptionFallback.cs +++ b/src/mscorlib/shared/System/Text/DecoderExceptionFallback.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.Runtime.Serialization; using System.Globalization; namespace System.Text @@ -82,9 +81,9 @@ namespace System.Text int i; for (i = 0; i < bytesUnknown.Length && i < 20; i++) { - strBytes.Append("["); + strBytes.Append('['); strBytes.Append(bytesUnknown[i].ToString("X2", CultureInfo.InvariantCulture)); - strBytes.Append("]"); + strBytes.Append(']'); } // In case the string's really long @@ -93,15 +92,16 @@ namespace System.Text // Known index throw new DecoderFallbackException( - SR.Format(SR.Argument_InvalidCodePageBytesIndex, strBytes, index), bytesUnknown, index); + SR.Format(SR.Argument_InvalidCodePageBytesIndex, + strBytes, index), bytesUnknown, index); } } // Exception for decoding unknown byte sequences. public sealed class DecoderFallbackException : ArgumentException { - private byte[] bytesUnknown = null; - private int index = 0; + private byte[] _bytesUnknown = null; + private int _index = 0; public DecoderFallbackException() : base(SR.Arg_ArgumentException) @@ -121,23 +121,18 @@ namespace System.Text HResult = __HResults.COR_E_ARGUMENT; } - internal DecoderFallbackException(SerializationInfo info, StreamingContext context) : base(info, context) - { - throw new PlatformNotSupportedException(); - } - - public DecoderFallbackException( - String message, byte[] bytesUnknown, int index) : base(message) + public DecoderFallbackException(String message, byte[] bytesUnknown, int index) + : base(message) { - this.bytesUnknown = bytesUnknown; - this.index = index; + _bytesUnknown = bytesUnknown; + _index = index; } public byte[] BytesUnknown { get { - return (bytesUnknown); + return (_bytesUnknown); } } @@ -145,7 +140,7 @@ namespace System.Text { get { - return index; + return _index; } } } diff --git a/src/mscorlib/src/System/Text/DecoderFallback.cs b/src/mscorlib/shared/System/Text/DecoderFallback.cs similarity index 73% rename from src/mscorlib/src/System/Text/DecoderFallback.cs rename to src/mscorlib/shared/System/Text/DecoderFallback.cs index 2a56706..11b9539 100644 --- a/src/mscorlib/src/System/Text/DecoderFallback.cs +++ b/src/mscorlib/shared/System/Text/DecoderFallback.cs @@ -2,72 +2,28 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -// - -using System; -using System.Security; -using System.Threading; -using System.Globalization; using System.Diagnostics; -using System.Diagnostics.Contracts; +using System.Globalization; +using System.Threading; namespace System.Text { public abstract class DecoderFallback { - internal bool bIsMicrosoftBestFitFallback = false; - - private static volatile DecoderFallback replacementFallback; // Default fallback, uses no best fit & "?" - private static volatile DecoderFallback exceptionFallback; - - // Private object for locking instead of locking on a internal type for SQL reliability work. - private static Object s_InternalSyncObject; - private static Object InternalSyncObject - { - get - { - if (s_InternalSyncObject == null) - { - Object o = new Object(); - Interlocked.CompareExchange(ref s_InternalSyncObject, o, null); - } - return s_InternalSyncObject; - } - } + private static DecoderFallback s_replacementFallback; // Default fallback, uses no best fit & "?" + private static DecoderFallback s_exceptionFallback; - // Get each of our generic fallbacks. + public static DecoderFallback ReplacementFallback => + s_replacementFallback ?? Interlocked.CompareExchange(ref s_replacementFallback, new DecoderReplacementFallback(), null) ?? s_replacementFallback; - public static DecoderFallback ReplacementFallback - { - get - { - if (replacementFallback == null) - lock (InternalSyncObject) - if (replacementFallback == null) - replacementFallback = new DecoderReplacementFallback(); - return replacementFallback; - } - } - - - public static DecoderFallback ExceptionFallback - { - get - { - if (exceptionFallback == null) - lock (InternalSyncObject) - if (exceptionFallback == null) - exceptionFallback = new DecoderExceptionFallback(); - - return exceptionFallback; - } - } + public static DecoderFallback ExceptionFallback => + s_exceptionFallback ?? Interlocked.CompareExchange(ref s_exceptionFallback, new DecoderExceptionFallback(), null) ?? s_exceptionFallback; // Fallback // // Return the appropriate unicode string alternative to the character that need to fall back. - // Most implimentations will be: + // Most implementations will be: // return new MyCustomDecoderFallbackBuffer(this); public abstract DecoderFallbackBuffer CreateFallbackBuffer(); @@ -80,9 +36,9 @@ namespace System.Text public abstract class DecoderFallbackBuffer { - // Most implimentations will probably need an implimenation-specific constructor + // Most implementations will probably need an implementation-specific constructor - // internal methods that cannot be overriden that let us do our fallback thing + // internal methods that cannot be overridden that let us do our fallback thing // These wrap the internal methods so that we can check for people doing stuff that's incorrect public abstract bool Fallback(byte[] bytesUnknown, int index); @@ -119,7 +75,7 @@ namespace System.Text } // Set the above values - // This can't be part of the constructor because DecoderFallbacks would have to know how to impliment these. + // This can't be part of the constructor because DecoderFallbacks would have to know how to implement these. internal unsafe void InternalInitialize(byte* byteStart, char* charEnd) { this.byteStart = byteStart; @@ -136,11 +92,6 @@ namespace System.Text // Don't touch ref chars unless we succeed internal unsafe virtual bool InternalFallback(byte[] bytes, byte* pBytes, ref char* chars) { - // Copy bytes to array (slow, but right now that's what we get to do. - // byte[] bytesUnknown = new byte[count]; - // for (int i = 0; i < count; i++) - // bytesUnknown[i] = *(bytes++); - Debug.Assert(byteStart != null, "[DecoderFallback.InternalFallback]Used InternalFallback without calling InternalInitialize"); // See if there's a fallback character and we have an output buffer then copy our string. @@ -196,11 +147,6 @@ namespace System.Text // Right now this has both bytes and bytes[], since we might have extra bytes, hence the // array, and we might need the index, hence the byte* { - // Copy bytes to array (slow, but right now that's what we get to do. - // byte[] bytesUnknown = new byte[count]; - // for (int i = 0; i < count; i++) - // bytesUnknown[i] = *(bytes++); - Debug.Assert(byteStart != null, "[DecoderFallback.InternalFallback]Used InternalFallback without calling InternalInitialize"); // See if there's a fallback character and we have an output buffer then copy our string. @@ -254,8 +200,8 @@ namespace System.Text for (i = 0; i < bytesUnknown.Length && i < 20; i++) { if (strBytes.Length > 0) - strBytes.Append(" "); - strBytes.Append(String.Format(CultureInfo.InvariantCulture, "\\x{0:X2}", bytesUnknown[i])); + strBytes.Append(' '); + strBytes.AppendFormat(CultureInfo.InvariantCulture, "\\x{0:X2}", bytesUnknown[i]); } // In case the string's really long if (i == 20) @@ -263,7 +209,8 @@ namespace System.Text // Throw it, using our complete bytes throw new ArgumentException( - SR.Format(SR.Argument_RecursiveFallbackBytes, strBytes.ToString()), nameof(bytesUnknown)); + SR.Format(SR.Argument_RecursiveFallbackBytes, + strBytes.ToString()), nameof(bytesUnknown)); } } } diff --git a/src/mscorlib/src/System/Text/DecoderNLS.cs b/src/mscorlib/shared/System/Text/DecoderNLS.cs similarity index 85% rename from src/mscorlib/src/System/Text/DecoderNLS.cs rename to src/mscorlib/shared/System/Text/DecoderNLS.cs index c2791e9..ef85200 100644 --- a/src/mscorlib/src/System/Text/DecoderNLS.cs +++ b/src/mscorlib/shared/System/Text/DecoderNLS.cs @@ -19,44 +19,32 @@ namespace System.Text // Instances of specific implementations of the Decoder abstract base // class are typically obtained through calls to the GetDecoder method // of Encoding objects. - // - internal class DecoderNLS : Decoder, ISerializable + internal class DecoderNLS : Decoder { // Remember our encoding - protected Encoding m_encoding; - [NonSerialized] protected bool m_mustFlush; - [NonSerialized] internal bool m_throwOnOverflow; - [NonSerialized] internal int m_bytesUsed; - - #region Serialization - - // ISerializable implementation. - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - - #endregion Serialization + private Encoding _encoding; + private bool _mustFlush; + internal bool _throwOnOverflow; + internal int _bytesUsed; internal DecoderNLS(Encoding encoding) { - this.m_encoding = encoding; - this.m_fallback = this.m_encoding.DecoderFallback; + _encoding = encoding; + _fallback = this._encoding.DecoderFallback; this.Reset(); } // This is used by our child deserializers internal DecoderNLS() { - this.m_encoding = null; + _encoding = null; this.Reset(); } public override void Reset() { - if (m_fallbackBuffer != null) - m_fallbackBuffer.Reset(); + _fallbackBuffer?.Reset(); } public override unsafe int GetCharCount(byte[] bytes, int index, int count) @@ -103,11 +91,11 @@ namespace System.Text Contract.EndContractBlock(); // Remember the flush - this.m_mustFlush = flush; - this.m_throwOnOverflow = true; + _mustFlush = flush; + _throwOnOverflow = true; // By default just call the encoding version, no flush by default - return m_encoding.GetCharCount(bytes, count, this); + return _encoding.GetCharCount(bytes, count, this); } public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, @@ -168,11 +156,11 @@ namespace System.Text Contract.EndContractBlock(); // Remember our flush - m_mustFlush = flush; - m_throwOnOverflow = true; + _mustFlush = flush; + _throwOnOverflow = true; - // By default just call the encoding's version - return m_encoding.GetChars(bytes, byteCount, chars, charCount, this); + // By default just call the encodings version + return _encoding.GetChars(bytes, byteCount, chars, charCount, this); } // This method is used when the output buffer might not be big enough. @@ -238,26 +226,26 @@ namespace System.Text Contract.EndContractBlock(); // We don't want to throw - this.m_mustFlush = flush; - this.m_throwOnOverflow = false; - this.m_bytesUsed = 0; + _mustFlush = flush; + _throwOnOverflow = false; + _bytesUsed = 0; // Do conversion - charsUsed = this.m_encoding.GetChars(bytes, byteCount, chars, charCount, this); - bytesUsed = this.m_bytesUsed; + charsUsed = _encoding.GetChars(bytes, byteCount, chars, charCount, this); + bytesUsed = _bytesUsed; // Its completed if they've used what they wanted AND if they didn't want flush or if we are flushed completed = (bytesUsed == byteCount) && (!flush || !this.HasState) && - (m_fallbackBuffer == null || m_fallbackBuffer.Remaining == 0); + (_fallbackBuffer == null || _fallbackBuffer.Remaining == 0); - // Our data thingys are now full, we can return + // Our data thingy are now full, we can return } public bool MustFlush { get { - return m_mustFlush; + return _mustFlush; } } @@ -273,7 +261,7 @@ namespace System.Text // Allow encoding to clear our must flush instead of throwing (in ThrowCharsOverflow) internal void ClearMustFlush() { - m_mustFlush = false; + _mustFlush = false; } } } diff --git a/src/mscorlib/src/System/Text/DecoderReplacementFallback.cs b/src/mscorlib/shared/System/Text/DecoderReplacementFallback.cs similarity index 81% rename from src/mscorlib/src/System/Text/DecoderReplacementFallback.cs rename to src/mscorlib/shared/System/Text/DecoderReplacementFallback.cs index 0eaca3d..8a8d550 100644 --- a/src/mscorlib/src/System/Text/DecoderReplacementFallback.cs +++ b/src/mscorlib/shared/System/Text/DecoderReplacementFallback.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Diagnostics; using System.Diagnostics.Contracts; @@ -11,7 +10,7 @@ namespace System.Text public sealed class DecoderReplacementFallback : DecoderFallback { // Our variables - private String strDefault; + private String _strDefault; // Construction. Default replacement fallback uses no best fit and ? replacement string public DecoderReplacementFallback() : this("?") @@ -60,14 +59,14 @@ namespace System.Text if (bFoundHigh) throw new ArgumentException(SR.Format(SR.Argument_InvalidCharSequenceNoIndex, nameof(replacement))); - strDefault = replacement; + _strDefault = replacement; } public String DefaultString { get { - return strDefault; + return _strDefault; } } @@ -81,7 +80,7 @@ namespace System.Text { get { - return strDefault.Length; + return _strDefault.Length; } } @@ -90,14 +89,14 @@ namespace System.Text DecoderReplacementFallback that = value as DecoderReplacementFallback; if (that != null) { - return (strDefault == that.strDefault); + return (_strDefault == that._strDefault); } return (false); } public override int GetHashCode() { - return strDefault.GetHashCode(); + return _strDefault.GetHashCode(); } } @@ -106,14 +105,14 @@ namespace System.Text public sealed class DecoderReplacementFallbackBuffer : DecoderFallbackBuffer { // Store our default string - private String strDefault; - private int fallbackCount = -1; - private int fallbackIndex = -1; + private String _strDefault; + private int _fallbackCount = -1; + private int _fallbackIndex = -1; // Construction public DecoderReplacementFallbackBuffer(DecoderReplacementFallback fallback) { - strDefault = fallback.DefaultString; + _strDefault = fallback.DefaultString; } // Fallback Methods @@ -121,17 +120,17 @@ namespace System.Text { // We expect no previous fallback in our buffer // We can't call recursively but others might (note, we don't test on last char!!!) - if (fallbackCount >= 1) + if (_fallbackCount >= 1) { ThrowLastBytesRecursive(bytesUnknown); } // Go ahead and get our fallback - if (strDefault.Length == 0) + if (_strDefault.Length == 0) return false; - fallbackCount = strDefault.Length; - fallbackIndex = -1; + _fallbackCount = _strDefault.Length; + _fallbackIndex = -1; return true; } @@ -140,35 +139,35 @@ namespace System.Text { // We want it to get < 0 because == 0 means that the current/last character is a fallback // and we need to detect recursion. We could have a flag but we already have this counter. - fallbackCount--; - fallbackIndex++; + _fallbackCount--; + _fallbackIndex++; // Do we have anything left? 0 is now last fallback char, negative is nothing left - if (fallbackCount < 0) + if (_fallbackCount < 0) return '\0'; // Need to get it out of the buffer. // Make sure it didn't wrap from the fast count-- path - if (fallbackCount == int.MaxValue) + if (_fallbackCount == int.MaxValue) { - fallbackCount = -1; + _fallbackCount = -1; return '\0'; } // Now make sure its in the expected range - Debug.Assert(fallbackIndex < strDefault.Length && fallbackIndex >= 0, + Debug.Assert(_fallbackIndex < _strDefault.Length && _fallbackIndex >= 0, "Index exceeds buffer range"); - return strDefault[fallbackIndex]; + return _strDefault[_fallbackIndex]; } public override bool MovePrevious() { // Back up one, only if we just processed the last character (or earlier) - if (fallbackCount >= -1 && fallbackIndex >= 0) + if (_fallbackCount >= -1 && _fallbackIndex >= 0) { - fallbackIndex--; - fallbackCount++; + _fallbackIndex--; + _fallbackCount++; return true; } @@ -182,15 +181,15 @@ namespace System.Text get { // Our count is 0 for 1 character left. - return (fallbackCount < 0) ? 0 : fallbackCount; + return (_fallbackCount < 0) ? 0 : _fallbackCount; } } // Clear the buffer public override unsafe void Reset() { - fallbackCount = -1; - fallbackIndex = -1; + _fallbackCount = -1; + _fallbackIndex = -1; byteStart = null; } @@ -200,7 +199,7 @@ namespace System.Text // array, and we might need the index, hence the byte* { // return our replacement string Length - return strDefault.Length; + return _strDefault.Length; } } } diff --git a/src/mscorlib/shared/System/Text/Encoder.cs b/src/mscorlib/shared/System/Text/Encoder.cs index c4b54ce..b0602f3 100644 --- a/src/mscorlib/shared/System/Text/Encoder.cs +++ b/src/mscorlib/shared/System/Text/Encoder.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Runtime.Serialization; using System.Text; using System; using System.Diagnostics; @@ -23,15 +22,9 @@ namespace System.Text // public abstract class Encoder { - internal EncoderFallback m_fallback = null; + internal EncoderFallback _fallback = null; - [NonSerialized] - internal EncoderFallbackBuffer m_fallbackBuffer = null; - - internal void SerializeEncoder(SerializationInfo info) - { - info.AddValue("m_fallback", this.m_fallback); - } + internal EncoderFallbackBuffer _fallbackBuffer = null; protected Encoder() { @@ -42,7 +35,7 @@ namespace System.Text { get { - return m_fallback; + return _fallback; } set @@ -52,12 +45,12 @@ namespace System.Text Contract.EndContractBlock(); // Can't change fallback if buffer is wrong - if (m_fallbackBuffer != null && m_fallbackBuffer.Remaining > 0) + if (_fallbackBuffer != null && _fallbackBuffer.Remaining > 0) throw new ArgumentException( SR.Argument_FallbackBufferNotEmpty, nameof(value)); - m_fallback = value; - m_fallbackBuffer = null; + _fallback = value; + _fallbackBuffer = null; } } @@ -67,15 +60,15 @@ namespace System.Text { get { - if (m_fallbackBuffer == null) + if (_fallbackBuffer == null) { - if (m_fallback != null) - m_fallbackBuffer = m_fallback.CreateFallbackBuffer(); + if (_fallback != null) + _fallbackBuffer = _fallback.CreateFallbackBuffer(); else - m_fallbackBuffer = EncoderFallback.ReplacementFallback.CreateFallbackBuffer(); + _fallbackBuffer = EncoderFallback.ReplacementFallback.CreateFallbackBuffer(); } - return m_fallbackBuffer; + return _fallbackBuffer; } } @@ -83,7 +76,7 @@ namespace System.Text { get { - return m_fallbackBuffer != null; + return _fallbackBuffer != null; } } @@ -101,8 +94,8 @@ namespace System.Text char[] charTemp = { }; byte[] byteTemp = new byte[GetByteCount(charTemp, 0, 0, true)]; GetBytes(charTemp, 0, 0, byteTemp, 0, true); - if (m_fallbackBuffer != null) - m_fallbackBuffer.Reset(); + if (_fallbackBuffer != null) + _fallbackBuffer.Reset(); } // Returns the number of bytes the next call to GetBytes will @@ -271,7 +264,7 @@ namespace System.Text { bytesUsed = GetBytes(chars, charIndex, charsUsed, bytes, byteIndex, flush); completed = (charsUsed == charCount && - (m_fallbackBuffer == null || m_fallbackBuffer.Remaining == 0)); + (_fallbackBuffer == null || _fallbackBuffer.Remaining == 0)); return; } @@ -315,7 +308,7 @@ namespace System.Text { bytesUsed = GetBytes(chars, charsUsed, bytes, byteCount, flush); completed = (charsUsed == charCount && - (m_fallbackBuffer == null || m_fallbackBuffer.Remaining == 0)); + (_fallbackBuffer == null || _fallbackBuffer.Remaining == 0)); return; } diff --git a/src/mscorlib/src/System/Text/EncoderBestFitFallback.cs b/src/mscorlib/shared/System/Text/EncoderBestFitFallback.cs similarity index 73% rename from src/mscorlib/src/System/Text/EncoderBestFitFallback.cs rename to src/mscorlib/shared/System/Text/EncoderBestFitFallback.cs index b007f57..a8c288b 100644 --- a/src/mscorlib/src/System/Text/EncoderBestFitFallback.cs +++ b/src/mscorlib/shared/System/Text/EncoderBestFitFallback.cs @@ -6,26 +6,23 @@ // This is used internally to create best fit behavior as per the original windows best fit behavior. // -using System; -using System.Globalization; -using System.Text; -using System.Threading; using System.Diagnostics; using System.Diagnostics.Contracts; +using System.Globalization; +using System.Threading; namespace System.Text { - internal sealed class InternalEncoderBestFitFallback : EncoderFallback + internal class InternalEncoderBestFitFallback : EncoderFallback { // Our variables - internal Encoding encoding = null; - internal char[] arrayBestFit = null; + internal Encoding _encoding = null; + internal char[] _arrayBestFit = null; internal InternalEncoderBestFitFallback(Encoding encoding) { // Need to load our replacement characters table. - this.encoding = encoding; - this.bIsMicrosoftBestFitFallback = true; + _encoding = encoding; } public override EncoderFallbackBuffer CreateFallbackBuffer() @@ -47,24 +44,24 @@ namespace System.Text InternalEncoderBestFitFallback that = value as InternalEncoderBestFitFallback; if (that != null) { - return (this.encoding.CodePage == that.encoding.CodePage); + return (_encoding.CodePage == that._encoding.CodePage); } return (false); } public override int GetHashCode() { - return this.encoding.CodePage; + return _encoding.CodePage; } } internal sealed class InternalEncoderBestFitFallbackBuffer : EncoderFallbackBuffer { // Our variables - private char cBestFit = '\0'; - private InternalEncoderBestFitFallback oFallback; - private int iCount = -1; - private int iSize; + private char _cBestFit = '\0'; + private InternalEncoderBestFitFallback _oFallback; + private int _iCount = -1; + private int _iSize; // Private object for locking instead of locking on a public type for SQL reliability work. private static Object s_InternalSyncObject; @@ -84,16 +81,16 @@ namespace System.Text // Constructor public InternalEncoderBestFitFallbackBuffer(InternalEncoderBestFitFallback fallback) { - oFallback = fallback; + _oFallback = fallback; - if (oFallback.arrayBestFit == null) + if (_oFallback._arrayBestFit == null) { // Lock so we don't confuse ourselves. lock (InternalSyncObject) { // Double check before we do it again. - if (oFallback.arrayBestFit == null) - oFallback.arrayBestFit = fallback.encoding.GetBestFitUnicodeToBytesData(); + if (_oFallback._arrayBestFit == null) + _oFallback._arrayBestFit = fallback._encoding.GetBestFitUnicodeToBytesData(); } } } @@ -104,12 +101,12 @@ namespace System.Text // If we had a buffer already we're being recursive, throw, it's probably at the suspect // character in our array. // Shouldn't be able to get here for all of our code pages, table would have to be messed up. - Debug.Assert(iCount < 1, "[InternalEncoderBestFitFallbackBuffer.Fallback(non surrogate)] Fallback char " + ((int)cBestFit).ToString("X4", CultureInfo.InvariantCulture) + " caused recursive fallback"); + Debug.Assert(_iCount < 1, "[InternalEncoderBestFitFallbackBuffer.Fallback(non surrogate)] Fallback char " + ((int)_cBestFit).ToString("X4", CultureInfo.InvariantCulture) + " caused recursive fallback"); - iCount = iSize = 1; - cBestFit = TryBestFit(charUnknown); - if (cBestFit == '\0') - cBestFit = '?'; + _iCount = _iSize = 1; + _cBestFit = TryBestFit(charUnknown); + if (_cBestFit == '\0') + _cBestFit = '?'; return true; } @@ -119,21 +116,23 @@ namespace System.Text // Double check input surrogate pair if (!Char.IsHighSurrogate(charUnknownHigh)) throw new ArgumentOutOfRangeException(nameof(charUnknownHigh), - SR.Format(SR.ArgumentOutOfRange_Range, 0xD800, 0xDBFF)); + SR.Format(SR.ArgumentOutOfRange_Range, + 0xD800, 0xDBFF)); if (!Char.IsLowSurrogate(charUnknownLow)) throw new ArgumentOutOfRangeException(nameof(charUnknownLow), - SR.Format(SR.ArgumentOutOfRange_Range, 0xDC00, 0xDFFF)); + SR.Format(SR.ArgumentOutOfRange_Range, + 0xDC00, 0xDFFF)); Contract.EndContractBlock(); // If we had a buffer already we're being recursive, throw, it's probably at the suspect // character in our array. 0 is processing last character, < 0 is not falling back // Shouldn't be able to get here, table would have to be messed up. - Debug.Assert(iCount < 1, "[InternalEncoderBestFitFallbackBuffer.Fallback(surrogate)] Fallback char " + ((int)cBestFit).ToString("X4", CultureInfo.InvariantCulture) + " caused recursive fallback"); + Debug.Assert(_iCount < 1, "[InternalEncoderBestFitFallbackBuffer.Fallback(surrogate)] Fallback char " + ((int)_cBestFit).ToString("X4", CultureInfo.InvariantCulture) + " caused recursive fallback"); // Go ahead and get our fallback, surrogates don't have best fit - cBestFit = '?'; - iCount = iSize = 2; + _cBestFit = '?'; + _iCount = _iSize = 2; return true; } @@ -143,32 +142,32 @@ namespace System.Text { // We want it to get < 0 because == 0 means that the current/last character is a fallback // and we need to detect recursion. We could have a flag but we already have this counter. - iCount--; + _iCount--; // Do we have anything left? 0 is now last fallback char, negative is nothing left - if (iCount < 0) + if (_iCount < 0) return '\0'; // Need to get it out of the buffer. // Make sure it didn't wrap from the fast count-- path - if (iCount == int.MaxValue) + if (_iCount == int.MaxValue) { - iCount = -1; + _iCount = -1; return '\0'; } // Return the best fit character - return cBestFit; + return _cBestFit; } public override bool MovePrevious() { // Exception fallback doesn't have anywhere to back up to. - if (iCount >= 0) - iCount++; + if (_iCount >= 0) + _iCount++; // Return true if we could do it. - return (iCount >= 0 && iCount <= iSize); + return (_iCount >= 0 && _iCount <= _iSize); } @@ -177,14 +176,14 @@ namespace System.Text { get { - return (iCount > 0) ? iCount : 0; + return (_iCount > 0) ? _iCount : 0; } } // Clear the buffer public override unsafe void Reset() { - iCount = -1; + _iCount = -1; charStart = null; bFallingBack = false; } @@ -194,7 +193,7 @@ namespace System.Text { // Need to figure out our best fit character, low is beginning of array, high is 1 AFTER end of array int lowBound = 0; - int highBound = oFallback.arrayBestFit.Length; + int highBound = _oFallback._arrayBestFit.Length; int index; // Binary search the array @@ -206,13 +205,13 @@ namespace System.Text // Also note that index can never == highBound (because diff is rounded down) index = ((iDiff / 2) + lowBound) & 0xFFFE; - char cTest = oFallback.arrayBestFit[index]; + char cTest = _oFallback._arrayBestFit[index]; if (cTest == cUnknown) { // We found it - Debug.Assert(index + 1 < oFallback.arrayBestFit.Length, + Debug.Assert(index + 1 < _oFallback._arrayBestFit.Length, "[InternalEncoderBestFitFallbackBuffer.TryBestFit]Expected replacement character at end of array"); - return oFallback.arrayBestFit[index + 1]; + return _oFallback._arrayBestFit[index + 1]; } else if (cTest < cUnknown) { @@ -228,12 +227,12 @@ namespace System.Text for (index = lowBound; index < highBound; index += 2) { - if (oFallback.arrayBestFit[index] == cUnknown) + if (_oFallback._arrayBestFit[index] == cUnknown) { // We found it - Debug.Assert(index + 1 < oFallback.arrayBestFit.Length, + Debug.Assert(index + 1 < _oFallback._arrayBestFit.Length, "[InternalEncoderBestFitFallbackBuffer.TryBestFit]Expected replacement character at end of array"); - return oFallback.arrayBestFit[index + 1]; + return _oFallback._arrayBestFit[index + 1]; } } diff --git a/src/mscorlib/src/System/Text/EncoderExceptionFallback.cs b/src/mscorlib/shared/System/Text/EncoderExceptionFallback.cs similarity index 90% rename from src/mscorlib/src/System/Text/EncoderExceptionFallback.cs rename to src/mscorlib/shared/System/Text/EncoderExceptionFallback.cs index 192ab78..98eff25 100644 --- a/src/mscorlib/src/System/Text/EncoderExceptionFallback.cs +++ b/src/mscorlib/shared/System/Text/EncoderExceptionFallback.cs @@ -100,10 +100,10 @@ namespace System.Text public sealed class EncoderFallbackException : ArgumentException { - private char charUnknown; - private char charUnknownHigh; - private char charUnknownLow; - private int index; + private char _charUnknown; + private char _charUnknownHigh; + private char _charUnknownLow; + private int _index; public EncoderFallbackException() : base(SR.Arg_ArgumentException) @@ -126,8 +126,8 @@ namespace System.Text internal EncoderFallbackException( String message, char charUnknown, int index) : base(message) { - this.charUnknown = charUnknown; - this.index = index; + _charUnknown = charUnknown; + _index = index; } internal EncoderFallbackException( @@ -145,16 +145,16 @@ namespace System.Text } Contract.EndContractBlock(); - this.charUnknownHigh = charUnknownHigh; - this.charUnknownLow = charUnknownLow; - this.index = index; + _charUnknownHigh = charUnknownHigh; + _charUnknownLow = charUnknownLow; + _index = index; } public char CharUnknown { get { - return (charUnknown); + return (_charUnknown); } } @@ -162,7 +162,7 @@ namespace System.Text { get { - return (charUnknownHigh); + return (_charUnknownHigh); } } @@ -170,7 +170,7 @@ namespace System.Text { get { - return (charUnknownLow); + return (_charUnknownLow); } } @@ -178,14 +178,14 @@ namespace System.Text { get { - return index; + return _index; } } // Return true if the unknown character is a surrogate pair. public bool IsUnknownSurrogate() { - return (charUnknownHigh != '\0'); + return (_charUnknownHigh != '\0'); } } } diff --git a/src/mscorlib/src/System/Text/EncoderFallback.cs b/src/mscorlib/shared/System/Text/EncoderFallback.cs similarity index 76% rename from src/mscorlib/src/System/Text/EncoderFallback.cs rename to src/mscorlib/shared/System/Text/EncoderFallback.cs index c3b9f47..d860a02 100644 --- a/src/mscorlib/src/System/Text/EncoderFallback.cs +++ b/src/mscorlib/shared/System/Text/EncoderFallback.cs @@ -2,38 +2,15 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; -using System.Security; -using System.Threading; using System.Diagnostics; -using System.Diagnostics.Contracts; +using System.Threading; namespace System.Text { public abstract class EncoderFallback { - // disable csharp compiler warning #0414: field assigned unused value -#pragma warning disable 0414 - internal bool bIsMicrosoftBestFitFallback = false; -#pragma warning restore 0414 - - private static volatile EncoderFallback replacementFallback; // Default fallback, uses no best fit & "?" - private static volatile EncoderFallback exceptionFallback; - - // Private object for locking instead of locking on a public type for SQL reliability work. - private static Object s_InternalSyncObject; - private static Object InternalSyncObject - { - get - { - if (s_InternalSyncObject == null) - { - Object o = new Object(); - Interlocked.CompareExchange(ref s_InternalSyncObject, o, null); - } - return s_InternalSyncObject; - } - } + private static EncoderFallback s_replacementFallback; // Default fallback, uses no best fit & "?" + private static EncoderFallback s_exceptionFallback; // Get each of our generic fallbacks. @@ -41,12 +18,10 @@ namespace System.Text { get { - if (replacementFallback == null) - lock (InternalSyncObject) - if (replacementFallback == null) - replacementFallback = new EncoderReplacementFallback(); + if (s_replacementFallback == null) + Interlocked.CompareExchange(ref s_replacementFallback, new EncoderReplacementFallback(), null); - return replacementFallback; + return s_replacementFallback; } } @@ -55,19 +30,17 @@ namespace System.Text { get { - if (exceptionFallback == null) - lock (InternalSyncObject) - if (exceptionFallback == null) - exceptionFallback = new EncoderExceptionFallback(); + if (s_exceptionFallback == null) + Interlocked.CompareExchange(ref s_exceptionFallback, new EncoderExceptionFallback(), null); - return exceptionFallback; + return s_exceptionFallback; } } // Fallback // // Return the appropriate unicode string alternative to the character that need to fall back. - // Most implimentations will be: + // Most implementations will be: // return new MyCustomEncoderFallbackBuffer(this); public abstract EncoderFallbackBuffer CreateFallbackBuffer(); @@ -80,9 +53,9 @@ namespace System.Text public abstract class EncoderFallbackBuffer { - // Most implementations will probably need an implemenation-specific constructor + // Most implementations will probably need an implementation-specific constructor - // Public methods that cannot be overriden that let us do our fallback thing + // Public methods that cannot be overridden that let us do our fallback thing // These wrap the internal methods so that we can check for people doing stuff that is incorrect public abstract bool Fallback(char charUnknown, int index); @@ -131,7 +104,7 @@ namespace System.Text } // Set the above values - // This can't be part of the constructor because EncoderFallbacks would have to know how to impliment these. + // This can't be part of the constructor because EncoderFallbacks would have to know how to implement these. internal unsafe void InternalInitialize(char* charStart, char* charEnd, EncoderNLS encoder, bool setEncoder) { this.charStart = charStart; @@ -182,7 +155,7 @@ namespace System.Text if (this.setEncoder) { bUsedEncoder = true; - this.encoder.charLeftOver = ch; + this.encoder._charLeftOver = ch; } bFallingBack = false; return false; @@ -203,7 +176,6 @@ namespace System.Text bFallingBack = Fallback(ch, cNext, index); return bFallingBack; } - // Next isn't a low surrogate, just fallback the high surrogate } } @@ -223,7 +195,8 @@ namespace System.Text { // Throw it, using our complete character throw new ArgumentException( - SR.Format(SR.Argument_RecursiveFallback, charRecursive), "chars"); + SR.Format(SR.Argument_RecursiveFallback, + charRecursive), "chars"); } } } diff --git a/src/mscorlib/src/System/Text/EncoderNLS.cs b/src/mscorlib/shared/System/Text/EncoderNLS.cs similarity index 83% rename from src/mscorlib/src/System/Text/EncoderNLS.cs rename to src/mscorlib/shared/System/Text/EncoderNLS.cs index 8a8c31e..28a1b8b 100644 --- a/src/mscorlib/src/System/Text/EncoderNLS.cs +++ b/src/mscorlib/shared/System/Text/EncoderNLS.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Runtime.Serialization; using System.Text; using System; using System.Diagnostics.Contracts; @@ -21,46 +20,33 @@ namespace System.Text // of Encoding objects. // - internal class EncoderNLS : Encoder, ISerializable + internal class EncoderNLS : Encoder { // Need a place for the last left over character, most of our encodings use this - internal char charLeftOver; - - protected Encoding m_encoding; - - [NonSerialized] protected bool m_mustFlush; - [NonSerialized] internal bool m_throwOnOverflow; - [NonSerialized] internal int m_charsUsed; - - #region Serialization - - // ISerializable implementation. - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - - #endregion Serialization + internal char _charLeftOver; + private Encoding _encoding; + private bool _mustFlush; + internal bool _throwOnOverflow; + internal int _charsUsed; internal EncoderNLS(Encoding encoding) { - this.m_encoding = encoding; - this.m_fallback = this.m_encoding.EncoderFallback; + _encoding = encoding; + _fallback = _encoding.EncoderFallback; this.Reset(); } - // This one is used when deserializing (like UTF7Encoding.Encoder) internal EncoderNLS() { - this.m_encoding = null; + _encoding = null; this.Reset(); } public override void Reset() { - this.charLeftOver = (char)0; - if (m_fallbackBuffer != null) - m_fallbackBuffer.Reset(); + _charLeftOver = (char)0; + if (_fallbackBuffer != null) + _fallbackBuffer.Reset(); } public override unsafe int GetByteCount(char[] chars, int index, int count, bool flush) @@ -104,9 +90,9 @@ namespace System.Text SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); - this.m_mustFlush = flush; - this.m_throwOnOverflow = true; - return m_encoding.GetByteCount(chars, count, this); + _mustFlush = flush; + _throwOnOverflow = true; + return _encoding.GetByteCount(chars, count, this); } public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, @@ -158,9 +144,9 @@ namespace System.Text SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); - this.m_mustFlush = flush; - this.m_throwOnOverflow = true; - return m_encoding.GetBytes(chars, charCount, bytes, byteCount, this); + _mustFlush = flush; + _throwOnOverflow = true; + return _encoding.GetBytes(chars, charCount, bytes, byteCount, this); } // This method is used when your output buffer might not be large enough for the entire result. @@ -225,17 +211,17 @@ namespace System.Text Contract.EndContractBlock(); // We don't want to throw - this.m_mustFlush = flush; - this.m_throwOnOverflow = false; - this.m_charsUsed = 0; + _mustFlush = flush; + _throwOnOverflow = false; + _charsUsed = 0; // Do conversion - bytesUsed = this.m_encoding.GetBytes(chars, charCount, bytes, byteCount, this); - charsUsed = this.m_charsUsed; + bytesUsed = _encoding.GetBytes(chars, charCount, bytes, byteCount, this); + charsUsed = _charsUsed; // Its completed if they've used what they wanted AND if they didn't want flush or if we are flushed completed = (charsUsed == charCount) && (!flush || !this.HasState) && - (m_fallbackBuffer == null || m_fallbackBuffer.Remaining == 0); + (_fallbackBuffer == null || _fallbackBuffer.Remaining == 0); // Our data thingys are now full, we can return } @@ -244,7 +230,7 @@ namespace System.Text { get { - return m_encoding; + return _encoding; } } @@ -252,7 +238,7 @@ namespace System.Text { get { - return m_mustFlush; + return _mustFlush; } } @@ -262,14 +248,14 @@ namespace System.Text { get { - return (this.charLeftOver != (char)0); + return (_charLeftOver != (char)0); } } // Allow encoding to clear our must flush instead of throwing (in ThrowBytesOverflow) internal void ClearMustFlush() { - m_mustFlush = false; + _mustFlush = false; } } } diff --git a/src/mscorlib/src/System/Text/EncoderReplacementFallback.cs b/src/mscorlib/shared/System/Text/EncoderReplacementFallback.cs similarity index 80% rename from src/mscorlib/src/System/Text/EncoderReplacementFallback.cs rename to src/mscorlib/shared/System/Text/EncoderReplacementFallback.cs index a9ce9c1..22d094c 100644 --- a/src/mscorlib/src/System/Text/EncoderReplacementFallback.cs +++ b/src/mscorlib/shared/System/Text/EncoderReplacementFallback.cs @@ -12,7 +12,7 @@ namespace System.Text public sealed class EncoderReplacementFallback : EncoderFallback { // Our variables - private String strDefault; + private String _strDefault; // Construction. Default replacement fallback uses no best fit and ? replacement string public EncoderReplacementFallback() : this("?") @@ -62,14 +62,14 @@ namespace System.Text if (bFoundHigh) throw new ArgumentException(SR.Format(SR.Argument_InvalidCharSequenceNoIndex, nameof(replacement))); - strDefault = replacement; + _strDefault = replacement; } public String DefaultString { get { - return strDefault; + return _strDefault; } } @@ -83,7 +83,7 @@ namespace System.Text { get { - return strDefault.Length; + return _strDefault.Length; } } @@ -92,14 +92,14 @@ namespace System.Text EncoderReplacementFallback that = value as EncoderReplacementFallback; if (that != null) { - return (strDefault == that.strDefault); + return (_strDefault == that._strDefault); } return (false); } public override int GetHashCode() { - return strDefault.GetHashCode(); + return _strDefault.GetHashCode(); } } @@ -108,15 +108,15 @@ namespace System.Text public sealed class EncoderReplacementFallbackBuffer : EncoderFallbackBuffer { // Store our default string - private String strDefault; - private int fallbackCount = -1; - private int fallbackIndex = -1; + private String _strDefault; + private int _fallbackCount = -1; + private int _fallbackIndex = -1; // Construction public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback) { // 2X in case we're a surrogate pair - strDefault = fallback.DefaultString + fallback.DefaultString; + _strDefault = fallback.DefaultString + fallback.DefaultString; } // Fallback Methods @@ -124,12 +124,12 @@ namespace System.Text { // If we had a buffer already we're being recursive, throw, it's probably at the suspect // character in our array. - if (fallbackCount >= 1) + if (_fallbackCount >= 1) { // If we're recursive we may still have something in our buffer that makes this a surrogate - if (char.IsHighSurrogate(charUnknown) && fallbackCount >= 0 && - char.IsLowSurrogate(strDefault[fallbackIndex + 1])) - ThrowLastCharRecursive(Char.ConvertToUtf32(charUnknown, strDefault[fallbackIndex + 1])); + if (char.IsHighSurrogate(charUnknown) && _fallbackCount >= 0 && + char.IsLowSurrogate(_strDefault[_fallbackIndex + 1])) + ThrowLastCharRecursive(Char.ConvertToUtf32(charUnknown, _strDefault[_fallbackIndex + 1])); // Nope, just one character ThrowLastCharRecursive(unchecked((int)charUnknown)); @@ -137,10 +137,10 @@ namespace System.Text // Go ahead and get our fallback // Divide by 2 because we aren't a surrogate pair - fallbackCount = strDefault.Length / 2; - fallbackIndex = -1; + _fallbackCount = _strDefault.Length / 2; + _fallbackIndex = -1; - return fallbackCount != 0; + return _fallbackCount != 0; } public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index) @@ -157,49 +157,49 @@ namespace System.Text // If we had a buffer already we're being recursive, throw, it's probably at the suspect // character in our array. - if (fallbackCount >= 1) + if (_fallbackCount >= 1) ThrowLastCharRecursive(Char.ConvertToUtf32(charUnknownHigh, charUnknownLow)); // Go ahead and get our fallback - fallbackCount = strDefault.Length; - fallbackIndex = -1; + _fallbackCount = _strDefault.Length; + _fallbackIndex = -1; - return fallbackCount != 0; + return _fallbackCount != 0; } public override char GetNextChar() { // We want it to get < 0 because == 0 means that the current/last character is a fallback // and we need to detect recursion. We could have a flag but we already have this counter. - fallbackCount--; - fallbackIndex++; + _fallbackCount--; + _fallbackIndex++; // Do we have anything left? 0 is now last fallback char, negative is nothing left - if (fallbackCount < 0) + if (_fallbackCount < 0) return '\0'; // Need to get it out of the buffer. // Make sure it didn't wrap from the fast count-- path - if (fallbackCount == int.MaxValue) + if (_fallbackCount == int.MaxValue) { - fallbackCount = -1; + _fallbackCount = -1; return '\0'; } // Now make sure its in the expected range - Debug.Assert(fallbackIndex < strDefault.Length && fallbackIndex >= 0, + Debug.Assert(_fallbackIndex < _strDefault.Length && _fallbackIndex >= 0, "Index exceeds buffer range"); - return strDefault[fallbackIndex]; + return _strDefault[_fallbackIndex]; } public override bool MovePrevious() { // Back up one, only if we just processed the last character (or earlier) - if (fallbackCount >= -1 && fallbackIndex >= 0) + if (_fallbackCount >= -1 && _fallbackIndex >= 0) { - fallbackIndex--; - fallbackCount++; + _fallbackIndex--; + _fallbackCount++; return true; } @@ -213,15 +213,15 @@ namespace System.Text get { // Our count is 0 for 1 character left. - return (fallbackCount < 0) ? 0 : fallbackCount; + return (_fallbackCount < 0) ? 0 : _fallbackCount; } } // Clear the buffer public override unsafe void Reset() { - fallbackCount = -1; - fallbackIndex = 0; + _fallbackCount = -1; + _fallbackIndex = 0; charStart = null; bFallingBack = false; } diff --git a/src/mscorlib/shared/System/Text/Encoding.cs b/src/mscorlib/shared/System/Text/Encoding.cs index d292b70..4f23d2a 100644 --- a/src/mscorlib/shared/System/Text/Encoding.cs +++ b/src/mscorlib/shared/System/Text/Encoding.cs @@ -1001,7 +1001,7 @@ namespace System.Text // GetCharCount method can be used to determine the exact number of // characters that will be produced for a given range of bytes. // Alternatively, the GetMaxCharCount method can be used to - // determine the maximum number of characterss that will be produced for a + // determine the maximum number of characters that will be produced for a // given number of bytes, regardless of the actual byte values. // @@ -1278,19 +1278,19 @@ namespace System.Text internal void ThrowBytesOverflow() { // Special message to include fallback type in case fallback's GetMaxCharCount is broken - // This happens if user has implimented an encoder fallback with a broken GetMaxCharCount + // This happens if user has implemented an encoder fallback with a broken GetMaxCharCount throw new ArgumentException( SR.Format(SR.Argument_EncodingConversionOverflowBytes, EncodingName, EncoderFallback.GetType()), "bytes"); } internal void ThrowBytesOverflow(EncoderNLS encoder, bool nothingEncoded) { - if (encoder == null || encoder.m_throwOnOverflow || nothingEncoded) + if (encoder == null || encoder._throwOnOverflow || nothingEncoded) { if (encoder != null && encoder.InternalHasFallbackBuffer) encoder.FallbackBuffer.InternalReset(); // Special message to include fallback type in case fallback's GetMaxCharCount is broken - // This happens if user has implimented an encoder fallback with a broken GetMaxCharCount + // This happens if user has implemented an encoder fallback with a broken GetMaxCharCount ThrowBytesOverflow(); } @@ -1301,20 +1301,20 @@ namespace System.Text internal void ThrowCharsOverflow() { // Special message to include fallback type in case fallback's GetMaxCharCount is broken - // This happens if user has implimented a decoder fallback with a broken GetMaxCharCount + // This happens if user has implemented a decoder fallback with a broken GetMaxCharCount throw new ArgumentException( SR.Format(SR.Argument_EncodingConversionOverflowChars, EncodingName, DecoderFallback.GetType()), "chars"); } internal void ThrowCharsOverflow(DecoderNLS decoder, bool nothingDecoded) { - if (decoder == null || decoder.m_throwOnOverflow || nothingDecoded) + if (decoder == null || decoder._throwOnOverflow || nothingDecoded) { if (decoder != null && decoder.InternalHasFallbackBuffer) decoder.FallbackBuffer.InternalReset(); // Special message to include fallback type in case fallback's GetMaxCharCount is broken - // This happens if user has implimented a decoder fallback with a broken GetMaxCharCount + // This happens if user has implemented a decoder fallback with a broken GetMaxCharCount ThrowCharsOverflow(); } @@ -1322,13 +1322,13 @@ namespace System.Text decoder.ClearMustFlush(); } - internal sealed class DefaultEncoder : Encoder, IObjectReference, ISerializable + internal sealed class DefaultEncoder : Encoder, IObjectReference { - private Encoding m_encoding; + private Encoding _encoding; public DefaultEncoder(Encoding encoding) { - m_encoding = encoding; + _encoding = encoding; } public Object GetRealObject(StreamingContext context) @@ -1336,12 +1336,6 @@ namespace System.Text throw new PlatformNotSupportedException(); } - // ISerializable implementation, get data for this object - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - // Returns the number of bytes the next call to GetBytes will // produce if presented with the given range of characters and the given // value of the flush parameter. The returned value takes into @@ -1352,13 +1346,13 @@ namespace System.Text public override int GetByteCount(char[] chars, int index, int count, bool flush) { - return m_encoding.GetByteCount(chars, index, count); + return _encoding.GetByteCount(chars, index, count); } [SuppressMessage("Microsoft.Contracts", "CC1055")] // Skip extra error checking to avoid *potential* AppCompat problems. public unsafe override int GetByteCount(char* chars, int count, bool flush) { - return m_encoding.GetByteCount(chars, count); + return _encoding.GetByteCount(chars, count); } // Encodes a range of characters in a character array into a range of bytes @@ -1384,24 +1378,24 @@ namespace System.Text public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush) { - return m_encoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex); + return _encoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex); } [SuppressMessage("Microsoft.Contracts", "CC1055")] // Skip extra error checking to avoid *potential* AppCompat problems. public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, bool flush) { - return m_encoding.GetBytes(chars, charCount, bytes, byteCount); + return _encoding.GetBytes(chars, charCount, bytes, byteCount); } } - internal sealed class DefaultDecoder : Decoder, IObjectReference, ISerializable + internal sealed class DefaultDecoder : Decoder, IObjectReference { - private Encoding m_encoding; + private Encoding _encoding; public DefaultDecoder(Encoding encoding) { - m_encoding = encoding; + _encoding = encoding; } public Object GetRealObject(StreamingContext context) @@ -1409,12 +1403,6 @@ namespace System.Text throw new PlatformNotSupportedException(); } - // ISerializable implementation - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - // Returns the number of characters the next call to GetChars will // produce if presented with the given range of bytes. The returned value // takes into account the state in which the decoder was left following the @@ -1429,14 +1417,14 @@ namespace System.Text public override int GetCharCount(byte[] bytes, int index, int count, bool flush) { - return m_encoding.GetCharCount(bytes, index, count); + return _encoding.GetCharCount(bytes, index, count); } [SuppressMessage("Microsoft.Contracts", "CC1055")] // Skip extra error checking to avoid *potential* AppCompat problems. public unsafe override int GetCharCount(byte* bytes, int count, bool flush) { // By default just call the encoding version, no flush by default - return m_encoding.GetCharCount(bytes, count); + return _encoding.GetCharCount(bytes, count); } // Decodes a range of bytes in a byte array into a range of characters @@ -1465,7 +1453,7 @@ namespace System.Text public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, bool flush) { - return m_encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex); + return _encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex); } [SuppressMessage("Microsoft.Contracts", "CC1055")] // Skip extra error checking to avoid *potential* AppCompat problems. @@ -1473,7 +1461,7 @@ namespace System.Text char* chars, int charCount, bool flush) { // By default just call the encoding's version - return m_encoding.GetChars(bytes, byteCount, chars, charCount); + return _encoding.GetChars(bytes, byteCount, chars, charCount); } } @@ -1683,7 +1671,7 @@ namespace System.Text { this.fallbackBuffer = _encoder.FallbackBuffer; // If we're not converting we must not have data in our fallback buffer - if (_encoder.m_throwOnOverflow && _encoder.InternalHasFallbackBuffer && + if (_encoder._throwOnOverflow && _encoder.InternalHasFallbackBuffer && this.fallbackBuffer.Remaining > 0) throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, _encoder.Encoding.EncodingName, _encoder.Fallback.GetType())); diff --git a/src/mscorlib/shared/System/Text/Latin1Encoding.cs b/src/mscorlib/shared/System/Text/Latin1Encoding.cs index 3f65f55..679698d 100644 --- a/src/mscorlib/shared/System/Text/Latin1Encoding.cs +++ b/src/mscorlib/shared/System/Text/Latin1Encoding.cs @@ -5,7 +5,6 @@ using System; using System.Diagnostics; using System.Diagnostics.Contracts; -using System.Runtime.Serialization; namespace System.Text { @@ -13,7 +12,7 @@ namespace System.Text // Latin1Encoding is a simple override to optimize the GetString version of Latin1Encoding. // because of the best fit cases we can't do this when encoding the string, only when decoding // - internal class Latin1Encoding : EncodingNLS, ISerializable + internal class Latin1Encoding : EncodingNLS { // Used by Encoding.Latin1 for lazy initialization // The initialization code will not be run until a static member of the class is referenced @@ -24,12 +23,6 @@ namespace System.Text { } - // ISerializable implementation - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - // GetByteCount // Note: We start by assuming that the output will be the same as count. Having // an encoder or fallback may change that assumption @@ -49,14 +42,14 @@ namespace System.Text EncoderReplacementFallback fallback; if (encoder != null) { - charLeftOver = encoder.charLeftOver; + charLeftOver = encoder._charLeftOver; Debug.Assert(charLeftOver == 0 || Char.IsHighSurrogate(charLeftOver), "[Latin1Encoding.GetByteCount]leftover character should be high surrogate"); fallback = encoder.Fallback as EncoderReplacementFallback; // Verify that we have no fallbackbuffer, for Latin1 its always empty, so just assert - Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || + Debug.Assert(!encoder._throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[Latin1CodePageEncoding.GetByteCount]Expected empty fallback buffer"); } @@ -170,13 +163,13 @@ namespace System.Text EncoderReplacementFallback fallback = null; if (encoder != null) { - charLeftOver = encoder.charLeftOver; + charLeftOver = encoder._charLeftOver; fallback = encoder.Fallback as EncoderReplacementFallback; Debug.Assert(charLeftOver == 0 || Char.IsHighSurrogate(charLeftOver), "[Latin1Encoding.GetBytes]leftover character should be high surrogate"); // Verify that we have no fallbackbuffer, for ASCII its always empty, so just assert - Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || + Debug.Assert(!encoder._throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[Latin1CodePageEncoding.GetBytes]Expected empty fallback buffer"); } @@ -239,8 +232,8 @@ namespace System.Text // Clear encoder if (encoder != null) { - encoder.charLeftOver = (char)0; - encoder.m_charsUsed = (int)(chars - charStart); + encoder._charLeftOver = (char)0; + encoder._charsUsed = (int)(chars - charStart); } return (int)(bytes - byteStart); } @@ -360,10 +353,10 @@ namespace System.Text // Fallback stuck it in encoder if necessary, but we have to clear MustFlush cases if (fallbackBuffer != null && !fallbackBuffer.bUsedEncoder) // Clear it in case of MustFlush - encoder.charLeftOver = (char)0; + encoder._charLeftOver = (char)0; // Set our chars used count - encoder.m_charsUsed = (int)(chars - charStart); + encoder._charsUsed = (int)(chars - charStart); } Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, @@ -416,7 +409,7 @@ namespace System.Text // Might need to know input bytes used if (decoder != null) - decoder.m_bytesUsed = byteCount; + decoder._bytesUsed = byteCount; // Converted sequence is same length as input, so output charsUsed is same as byteCount; return byteCount; diff --git a/src/mscorlib/shared/System/Text/UTF32Encoding.cs b/src/mscorlib/shared/System/Text/UTF32Encoding.cs index 450aee2..10161d1 100644 --- a/src/mscorlib/shared/System/Text/UTF32Encoding.cs +++ b/src/mscorlib/shared/System/Text/UTF32Encoding.cs @@ -84,7 +84,7 @@ namespace System.Text // The following methods are copied from EncodingNLS.cs. - // Unfortunately EncodingNLS.cs is internal and we're public, so we have to reimpliment them here. + // Unfortunately EncodingNLS.cs is internal and we're public, so we have to re-implement them here. // These should be kept in sync for the following classes: // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding @@ -403,7 +403,7 @@ namespace System.Text if (encoder != null) { - highSurrogate = encoder.charLeftOver; + highSurrogate = encoder._charLeftOver; fallbackBuffer = encoder.FallbackBuffer; // We mustn't have left over fallback data when counting @@ -509,7 +509,7 @@ namespace System.Text throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_GetByteCountOverflow); // Shouldn't have anything in fallback buffer for GetByteCount - // (don't have to check m_throwOnOverflow for count) + // (don't have to check _throwOnOverflow for count) Debug.Assert(fallbackBuffer.Remaining == 0, "[UTF32Encoding.GetByteCount]Expected empty fallback buffer at end"); @@ -538,11 +538,11 @@ namespace System.Text if (encoder != null) { - highSurrogate = encoder.charLeftOver; + highSurrogate = encoder._charLeftOver; fallbackBuffer = encoder.FallbackBuffer; // We mustn't have left over fallback data when not converting - if (encoder.m_throwOnOverflow && fallbackBuffer.Remaining > 0) + if (encoder._throwOnOverflow && fallbackBuffer.Remaining > 0) throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType())); } else @@ -709,10 +709,10 @@ namespace System.Text if (encoder != null) { // Remember our left over surrogate (or 0 if flushing) - encoder.charLeftOver = highSurrogate; + encoder._charLeftOver = highSurrogate; // Need # chars used - encoder.m_charsUsed = (int)(chars - charStart); + encoder._charsUsed = (int)(chars - charStart); } // return the new length @@ -746,7 +746,7 @@ namespace System.Text fallbackBuffer = decoder.FallbackBuffer; // Shouldn't have anything in fallback buffer for GetCharCount - // (don't have to check m_throwOnOverflow for chars or count) + // (don't have to check _throwOnOverflow for chars or count) Debug.Assert(fallbackBuffer.Remaining == 0, "[UTF32Encoding.GetCharCount]Expected empty fallback buffer at start"); } @@ -853,7 +853,7 @@ namespace System.Text throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_GetByteCountOverflow); // Shouldn't have anything in fallback buffer for GetCharCount - // (don't have to check m_throwOnOverflow for chars or count) + // (don't have to check _throwOnOverflow for chars or count) Debug.Assert(fallbackBuffer.Remaining == 0, "[UTF32Encoding.GetCharCount]Expected empty fallback buffer at end"); @@ -894,7 +894,7 @@ namespace System.Text fallbackBuffer = baseDecoder.FallbackBuffer; // Shouldn't have anything in fallback buffer for GetChars - // (don't have to check m_throwOnOverflow for chars) + // (don't have to check _throwOnOverflow for chars) Debug.Assert(fallbackBuffer.Remaining == 0, "[UTF32Encoding.GetChars]Expected empty fallback buffer at start"); } @@ -1065,11 +1065,11 @@ namespace System.Text { decoder.iChar = (int)iChar; decoder.readByteCount = readCount; - decoder.m_bytesUsed = (int)(bytes - byteStart); + decoder._bytesUsed = (int)(bytes - byteStart); } // Shouldn't have anything in fallback buffer for GetChars - // (don't have to check m_throwOnOverflow for chars) + // (don't have to check _throwOnOverflow for chars) Debug.Assert(fallbackBuffer.Remaining == 0, "[UTF32Encoding.GetChars]Expected empty fallback buffer at end"); @@ -1214,8 +1214,8 @@ namespace System.Text { this.iChar = 0; this.readByteCount = 0; - if (m_fallbackBuffer != null) - m_fallbackBuffer.Reset(); + if (_fallbackBuffer != null) + _fallbackBuffer.Reset(); } // Anything left in our decoder? diff --git a/src/mscorlib/shared/System/Text/UTF7Encoding.cs b/src/mscorlib/shared/System/Text/UTF7Encoding.cs index 141c03c..b906547 100644 --- a/src/mscorlib/shared/System/Text/UTF7Encoding.cs +++ b/src/mscorlib/shared/System/Text/UTF7Encoding.cs @@ -7,7 +7,6 @@ // using System; -using System.Runtime.Serialization; using System.Diagnostics; using System.Diagnostics.Contracts; @@ -118,7 +117,7 @@ namespace System.Text } // The following methods are copied from EncodingNLS.cs. - // Unfortunately EncodingNLS.cs is internal and we're public, so we have to reimpliment them here. + // Unfortunately EncodingNLS.cs is internal and we're public, so we have to re-implement them here. // These should be kept in sync for the following classes: // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding @@ -563,7 +562,7 @@ namespace System.Text // We already cleared bits & bitcount for mustflush case encoder.bits = bits; encoder.bitCount = bitCount; - encoder.m_charsUsed = buffer.CharsUsed; + encoder._charsUsed = buffer.CharsUsed; } return buffer.Count; @@ -737,7 +736,7 @@ namespace System.Text decoder.bitCount = bitCount; decoder.firstByte = firstByte; } - decoder.m_bytesUsed = buffer.BytesUsed; + decoder._bytesUsed = buffer.BytesUsed; } // else ignore any hanging bits. @@ -808,7 +807,7 @@ namespace System.Text // Of all the amazing things... This MUST be Decoder so that our com name // for System.Text.Decoder doesn't change - private sealed class Decoder : DecoderNLS, ISerializable + private sealed class Decoder : DecoderNLS { /*private*/ internal int bits; @@ -822,19 +821,13 @@ namespace System.Text // base calls reset } - // ISerializable implementation, get data for this object - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - public override void Reset() { this.bits = 0; this.bitCount = -1; this.firstByte = false; - if (m_fallbackBuffer != null) - m_fallbackBuffer.Reset(); + if (_fallbackBuffer != null) + _fallbackBuffer.Reset(); } // Anything left in our encoder? @@ -851,7 +844,7 @@ namespace System.Text // Of all the amazing things... This MUST be Encoder so that our com name // for System.Text.Encoder doesn't change - private sealed class Encoder : EncoderNLS, ISerializable + private sealed class Encoder : EncoderNLS { /*private*/ internal int bits; @@ -863,18 +856,12 @@ namespace System.Text // base calls reset } - // ISerializable implementation - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - public override void Reset() { this.bitCount = -1; this.bits = 0; - if (m_fallbackBuffer != null) - m_fallbackBuffer.Reset(); + if (_fallbackBuffer != null) + _fallbackBuffer.Reset(); } // Anything left in our encoder? diff --git a/src/mscorlib/shared/System/Text/UTF8Encoding.cs b/src/mscorlib/shared/System/Text/UTF8Encoding.cs index ee5c92c..02b1893 100644 --- a/src/mscorlib/shared/System/Text/UTF8Encoding.cs +++ b/src/mscorlib/shared/System/Text/UTF8Encoding.cs @@ -15,7 +15,6 @@ #define FASTLOOP using System; -using System.Runtime.Serialization; using System.Diagnostics; using System.Diagnostics.Contracts; using System.Globalization; @@ -51,7 +50,7 @@ namespace System.Text private const int UTF8_CODEPAGE = 65001; - // Allow for devirtualization (see https://github.com/dotnet/coreclr/pull/9230) + // Allow for de-virtualization (see https://github.com/dotnet/coreclr/pull/9230) internal sealed class UTF8EncodingSealed : UTF8Encoding { public UTF8EncodingSealed(bool encoderShouldEmitUTF8Identifier) : base(encoderShouldEmitUTF8Identifier) { } @@ -111,7 +110,7 @@ namespace System.Text // WARNING: otherwise it'll break VB's way of declaring these. // // The following methods are copied from EncodingNLS.cs. - // Unfortunately EncodingNLS.cs is internal and we're public, so we have to reimpliment them here. + // Unfortunately EncodingNLS.cs is internal and we're public, so we have to re-implement them here. // These should be kept in sync for the following classes: // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding @@ -586,7 +585,7 @@ namespace System.Text // Do our fallback. Actually we already know its a mixed up surrogate, // so the ref pSrc isn't gonna do anything. - pSrcForFallback = pSrc; // Avoid passing pSrc by reference to allow it to be enregistered + pSrcForFallback = pSrc; // Avoid passing pSrc by reference to allow it to be en-registered fallbackBuffer.InternalFallback(unchecked((char)ch), ref pSrcForFallback); pSrc = pSrcForFallback; @@ -631,7 +630,7 @@ namespace System.Text if (availableChars <= 13) { // try to get over the remainder of the ascii characters fast though - char* pLocalEnd = pEnd; // hint to get pLocalEnd enregistered + char* pLocalEnd = pEnd; // hint to get pLocalEnd en-registered while (pSrc < pLocalEnd) { ch = *pSrc; @@ -844,7 +843,7 @@ namespace System.Text int ch = 0; - // assume that JIT will enregister pSrc, pTarget and ch + // assume that JIT will en-register pSrc, pTarget and ch if (baseEncoder != null) { @@ -856,7 +855,7 @@ namespace System.Text { // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary fallbackBuffer = encoder.FallbackBuffer; - if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) + if (fallbackBuffer.Remaining > 0 && encoder._throwOnOverflow) throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType())); // Set our internal fallback interesting things. @@ -984,7 +983,7 @@ namespace System.Text // Do our fallback. Actually we already know its a mixed up surrogate, // so the ref pSrc isn't gonna do anything. - pSrcForFallback = pSrc; // Avoid passing pSrc by reference to allow it to be enregistered + pSrcForFallback = pSrc; // Avoid passing pSrc by reference to allow it to be en-registered fallbackBuffer.InternalFallback(unchecked((char)ch), ref pSrcForFallback); pSrc = pSrcForFallback; @@ -1026,7 +1025,7 @@ namespace System.Text Debug.Assert(pSrc >= chars || pTarget == bytes, "[UTF8Encoding.GetBytes]Expected pSrc to be within buffer or to throw with insufficient room."); ThrowBytesOverflow(encoder, pTarget == bytes); // Throw if we must - ch = 0; // Nothing left over (we backed up to start of pair if supplimentary) + ch = 0; // Nothing left over (we backed up to start of pair if supplementary) break; } @@ -1091,7 +1090,7 @@ namespace System.Text } // try to get over the remainder of the ascii characters fast though - char* pLocalEnd = pEnd; // hint to get pLocalEnd enregistered + char* pLocalEnd = pEnd; // hint to get pLocalEnd en-registered while (pSrc < pLocalEnd) { ch = *pSrc; @@ -1279,11 +1278,11 @@ namespace System.Text "[UTF8Encoding.GetBytes] Expected no mustflush or 0 leftover ch " + ch.ToString("X2", CultureInfo.InvariantCulture)); encoder.surrogateChar = ch; - encoder.m_charsUsed = (int)(pSrc - chars); + encoder._charsUsed = (int)(pSrc - chars); } Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0 || - baseEncoder == null || !baseEncoder.m_throwOnOverflow, + baseEncoder == null || !baseEncoder._throwOnOverflow, "[UTF8Encoding.GetBytes]Expected empty fallback buffer if not converting"); return (int)(pTarget - bytes); @@ -1326,7 +1325,7 @@ namespace System.Text charCount -= (ch >> 30); // Adjust char count for # of expected bytes and expected output chars. // Shouldn't have anything in fallback buffer for GetCharCount - // (don't have to check m_throwOnOverflow for count) + // (don't have to check _throwOnOverflow for count) Debug.Assert(!decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, "[UTF8Encoding.GetCharCount]Expected empty fallback buffer at start"); } @@ -1373,12 +1372,12 @@ namespace System.Text { if ((ch & (FinalByte >> 6)) != 0) { - // this is 3rd byte (of 4 byte supplimentary) - nothing to do + // this is 3rd byte (of 4 byte supplementary) - nothing to do continue; } - // 2nd byte, check for non-shortest form of supplimentary char and the valid - // supplimentary characters in range 0x010000 - 0x10FFFF at the same time + // 2nd byte, check for non-shortest form of supplementary char and the valid + // supplementary characters in range 0x010000 - 0x10FFFF at the same time if (!InRange(ch & 0x1F0, 0x10, 0x100)) { goto InvalidByteSequence; @@ -1407,7 +1406,7 @@ namespace System.Text goto EncodeChar; InvalidByteSequence: - // this code fragment should be close to the gotos referencing it + // this code fragment should be close to the goto referencing it // Have to do fallback for invalid bytes if (fallback == null) { @@ -1508,7 +1507,7 @@ namespace System.Text if (availableBytes <= 13) { // try to get over the remainder of the ascii characters fast though - byte* pLocalEnd = pEnd; // hint to get pLocalEnd enregistered + byte* pLocalEnd = pEnd; // hint to get pLocalEnd en-registered while (pSrc < pLocalEnd) { ch = *pSrc; @@ -1698,7 +1697,7 @@ namespace System.Text // May have a problem if we have to flush if (ch != 0) { - // We were already adjusting for these, so need to unadjust + // We were already adjusting for these, so need to un-adjust charCount += (ch >> 30); if (baseDecoder == null || baseDecoder.MustFlush) { @@ -1716,7 +1715,7 @@ namespace System.Text } // Shouldn't have anything in fallback buffer for GetCharCount - // (don't have to check m_throwOnOverflow for count) + // (don't have to check _throwOnOverflow for count) Debug.Assert(fallback == null || fallback.Remaining == 0, "[UTF8Encoding.GetCharCount]Expected empty fallback buffer at end"); @@ -1758,7 +1757,7 @@ namespace System.Text ch = decoder.bits; // Shouldn't have anything in fallback buffer for GetChars - // (don't have to check m_throwOnOverflow for chars, we always use all or none so always should be empty) + // (don't have to check _throwOnOverflow for chars, we always use all or none so always should be empty) Debug.Assert(!decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, "[UTF8Encoding.GetChars]Expected empty fallback buffer at start"); } @@ -1862,9 +1861,9 @@ namespace System.Text fallback = baseDecoder.FallbackBuffer; fallback.InternalInitialize(bytes, pAllocatedBufferEnd); } - // This'll back us up the appropriate # of bytes if we didn't get anywhere - pSrcForFallback = pSrc; // Avoid passing pSrc by reference to allow it to be enregistered - pTargetForFallback = pTarget; // Avoid passing pTarget by reference to allow it to be enregistered + // That'll back us up the appropriate # of bytes if we didn't get anywhere + pSrcForFallback = pSrc; // Avoid passing pSrc by reference to allow it to be en-registered + pTargetForFallback = pTarget; // Avoid passing pTarget by reference to allow it to be en-registered bool fallbackResult = FallbackInvalidByteSequence(ref pSrcForFallback, ch, fallback, ref pTargetForFallback); pSrc = pSrcForFallback; pTarget = pTargetForFallback; @@ -1973,7 +1972,7 @@ namespace System.Text pSrc--; // Throw that we don't have enough room (pSrc could be < chars if we had started to process - // a 4 byte sequence alredy) + // a 4 byte sequence already) Debug.Assert(pSrc >= bytes || pTarget == chars, "[UTF8Encoding.GetChars]Expected pSrc to be within input buffer or throw due to no output]"); ThrowCharsOverflow(baseDecoder, pTarget == chars); @@ -2270,9 +2269,9 @@ namespace System.Text fallback.InternalInitialize(bytes, pAllocatedBufferEnd); } - // This'll back us up the appropriate # of bytes if we didn't get anywhere - pSrcForFallback = pSrc; // Avoid passing pSrc by reference to allow it to be enregistered - pTargetForFallback = pTarget; // Avoid passing pTarget by reference to allow it to be enregistered + // That'll back us up the appropriate # of bytes if we didn't get anywhere + pSrcForFallback = pSrc; // Avoid passing pSrc by reference to allow it to be en-registered + pTargetForFallback = pTarget; // Avoid passing pTarget by reference to allow it to be en-registered bool fallbackResult = FallbackInvalidByteSequence(ref pSrcForFallback, ch, fallback, ref pTargetForFallback); pSrc = pSrcForFallback; pTarget = pTargetForFallback; @@ -2298,17 +2297,17 @@ namespace System.Text // If we're storing flush data we expect all bits to be used or else // we're stuck in the middle of a conversion - Debug.Assert(!baseDecoder.MustFlush || ch == 0 || !baseDecoder.m_throwOnOverflow, + Debug.Assert(!baseDecoder.MustFlush || ch == 0 || !baseDecoder._throwOnOverflow, "[UTF8Encoding.GetChars]Expected no must flush or no left over bits or no throw on overflow."); // Remember our leftover bits. decoder.bits = ch; - baseDecoder.m_bytesUsed = (int)(pSrc - bytes); + baseDecoder._bytesUsed = (int)(pSrc - bytes); } // Shouldn't have anything in fallback buffer for GetChars - // (don't have to check m_throwOnOverflow for chars) + // (don't have to check _throwOnOverflow for chars) Debug.Assert(fallback == null || fallback.Remaining == 0, "[UTF8Encoding.GetChars]Expected empty fallback buffer at end"); @@ -2317,7 +2316,7 @@ namespace System.Text // During GetChars we had an invalid byte sequence // pSrc is backed up to the start of the bad sequence if we didn't have room to - // fall it back. Otherwise pSrc remains wher it is. + // fall it back. Otherwise pSrc remains where it is. private unsafe bool FallbackInvalidByteSequence( ref byte* pSrc, int ch, DecoderFallbackBuffer fallback, ref char* pTarget) { @@ -2519,7 +2518,7 @@ namespace System.Text UTF8_CODEPAGE + (_emitUTF8Identifier ? 1 : 0); } - private sealed class UTF8Encoder : EncoderNLS, ISerializable + private sealed class UTF8Encoder : EncoderNLS { // We must save a high surrogate value until the next call, looking // for a low surrogate value. @@ -2530,18 +2529,12 @@ namespace System.Text // base calls reset } - // ISerializable implementation - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - public override void Reset() { this.surrogateChar = 0; - if (m_fallbackBuffer != null) - m_fallbackBuffer.Reset(); + if (_fallbackBuffer != null) + _fallbackBuffer.Reset(); } // Anything left in our encoder? @@ -2554,7 +2547,7 @@ namespace System.Text } } - private sealed class UTF8Decoder : DecoderNLS, ISerializable + private sealed class UTF8Decoder : DecoderNLS { // We'll need to remember the previous information. See the comments around definition // of FinalByte for details. @@ -2565,23 +2558,11 @@ namespace System.Text // base calls reset } - // Constructor called by serialization, have to handle deserializing from Everett - internal UTF8Decoder(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - - // ISerializable implementation, get data for this object - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - public override void Reset() { this.bits = 0; - if (m_fallbackBuffer != null) - m_fallbackBuffer.Reset(); + if (_fallbackBuffer != null) + _fallbackBuffer.Reset(); } // Anything left in our decoder? diff --git a/src/mscorlib/shared/System/Text/UnicodeEncoding.cs b/src/mscorlib/shared/System/Text/UnicodeEncoding.cs index 846946c..8e44317 100644 --- a/src/mscorlib/shared/System/Text/UnicodeEncoding.cs +++ b/src/mscorlib/shared/System/Text/UnicodeEncoding.cs @@ -8,7 +8,6 @@ using System; using System.Globalization; -using System.Runtime.Serialization; using System.Diagnostics; using System.Diagnostics.Contracts; @@ -21,7 +20,6 @@ namespace System.Text internal static readonly UnicodeEncoding s_bigEndianDefault = new UnicodeEncoding(bigEndian: true, byteOrderMark: true); internal static readonly UnicodeEncoding s_littleEndianDefault = new UnicodeEncoding(bigEndian: false, byteOrderMark: true); - [OptionalField(VersionAdded = 2)] internal bool isThrowException = false; internal bool bigEndian = false; @@ -55,15 +53,6 @@ namespace System.Text SetDefaultFallbacks(); } - #region Serialization - [OnDeserializing] - private void OnDeserializing(StreamingContext ctx) - { - // In Everett it is false. Whidbey will overwrite this value. - isThrowException = false; - } - #endregion Serialization - internal override void SetDefaultFallbacks() { // For UTF-X encodings, we use a replacement fallback with an empty string @@ -80,7 +69,7 @@ namespace System.Text } // The following methods are copied from EncodingNLS.cs. - // Unfortunately EncodingNLS.cs is internal and we're public, so we have to reimpliment them here. + // Unfortunately EncodingNLS.cs is internal and we're public, so we have to re-implement them here. // These should be kept in sync for the following classes: // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding // @@ -414,7 +403,7 @@ namespace System.Text if (encoder != null) { - charLeftOver = encoder.charLeftOver; + charLeftOver = encoder._charLeftOver; // Assume extra bytes to encode charLeftOver if it existed if (charLeftOver > 0) @@ -461,7 +450,7 @@ namespace System.Text { // See if we potentially have surrogates (0x8000 bit set) // (We're either big endian on a big endian machine or little endian on - // a little endian machine so this'll work) + // a little endian machine so that'll work) if ((0x8000800080008000 & *longChars) != 0) { // See if any of these are high or low surrogates (0xd800 - 0xdfff). If the high @@ -589,7 +578,7 @@ namespace System.Text // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, false); } - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered fallbackBuffer.InternalFallback(ch, ref charsForFallback); chars = charsForFallback; continue; @@ -622,7 +611,7 @@ namespace System.Text // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, false); } - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); chars = charsForFallback; @@ -663,7 +652,7 @@ namespace System.Text // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, false); } - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); chars = charsForFallback; charLeftOver = (char)0; @@ -674,7 +663,7 @@ namespace System.Text } // Shouldn't have anything in fallback buffer for GetByteCount - // (don't have to check m_throwOnOverflow for count) + // (don't have to check _throwOnOverflow for count) Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[UnicodeEncoding.GetByteCount]Expected empty fallback buffer at end"); @@ -707,14 +696,14 @@ namespace System.Text // Get our encoder, but don't clear it yet. if (encoder != null) { - charLeftOver = encoder.charLeftOver; + charLeftOver = encoder._charLeftOver; // We mustn't have left over fallback data when counting if (encoder.InternalHasFallbackBuffer) { // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary fallbackBuffer = encoder.FallbackBuffer; - if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) + if (fallbackBuffer.Remaining > 0 && encoder._throwOnOverflow) throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType())); // Set our internal fallback interesting things. @@ -760,7 +749,7 @@ namespace System.Text { // See if we potentially have surrogates (0x8000 bit set) // (We're either big endian on a big endian machine or little endian on - // a little endian machine so this'll work) + // a little endian machine so that'll work) if ((0x8000800080008000 & *longChars) != 0) { // See if any of these are high or low surrogates (0xd800 - 0xdfff). If the high @@ -824,9 +813,9 @@ namespace System.Text #endif // BIGENDIAN #if BIT64 - (unchecked((long)chars) & 7) != (unchecked((long)bytes) & 7) && // Only do this if chars & bytes are out of line, otherwise faster loop'll be faster next time + (unchecked((long)chars) & 7) != (unchecked((long)bytes) & 7) && // Only do this if chars & bytes are out of line, otherwise faster loop will be faster next time #else - (unchecked((int)chars) & 3) != (unchecked((int)bytes) & 3) && // Only do this if chars & bytes are out of line, otherwise faster loop'll be faster next time + (unchecked((int)chars) & 3) != (unchecked((int)bytes) & 3) && // Only do this if chars & bytes are out of line, otherwise faster loop will be faster next time #endif // BIT64 (unchecked((int)(bytes)) & 1) == 0) { @@ -908,7 +897,7 @@ namespace System.Text fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); } - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); chars = charsForFallback; @@ -937,7 +926,7 @@ namespace System.Text fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); } - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered fallbackBuffer.InternalFallback(ch, ref charsForFallback); chars = charsForFallback; continue; @@ -1007,7 +996,7 @@ namespace System.Text fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); } - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); chars = charsForFallback; @@ -1073,8 +1062,8 @@ namespace System.Text fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); } - // If we're not flushing, this'll remember the left over character. - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + // If we're not flushing, that'll remember the left over character. + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); chars = charsForFallback; @@ -1088,8 +1077,8 @@ namespace System.Text // Not flushing, remember it in the encoder if (encoder != null) { - encoder.charLeftOver = charLeftOver; - encoder.m_charsUsed = (int)(chars - charStart); + encoder._charLeftOver = charLeftOver; + encoder._charsUsed = (int)(chars - charStart); } // Remember charLeftOver if we must, or clear it if we're flushing @@ -1098,7 +1087,7 @@ namespace System.Text "[UnicodeEncoding.GetBytes] Expected no left over characters if flushing"); Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0 || - encoder == null || !encoder.m_throwOnOverflow, + encoder == null || !encoder._throwOnOverflow, "[UnicodeEncoding.GetBytes]Expected empty fallback buffer if not converting"); // We used to copy it fast, but this doesn't check for surrogates @@ -1148,7 +1137,7 @@ namespace System.Text } // Shouldn't have anything in fallback buffer for GetCharCount - // (don't have to check m_throwOnOverflow for count) + // (don't have to check _throwOnOverflow for count) Debug.Assert(!decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, "[UnicodeEncoding.GetCharCount]Expected empty fallback buffer at start"); } @@ -1156,7 +1145,7 @@ namespace System.Text while (bytes < byteEnd) { // If we're aligned then maybe we can do it fast - // This'll hurt if we're unaligned because we'll always test but never be aligned + // That'll hurt if we're unaligned because we'll always test but never be aligned #if !NO_FAST_UNICODE_LOOP #if BIGENDIAN if (bigEndian && @@ -1177,7 +1166,7 @@ namespace System.Text { // See if we potentially have surrogates (0x8000 bit set) // (We're either big endian on a big endian machine or little endian on - // a little endian machine so this'll work) + // a little endian machine so that'll work) if ((0x8000800080008000 & *longBytes) != 0) { // See if any of these are high or low surrogates (0xd800 - 0xdfff). If the high @@ -1338,7 +1327,7 @@ namespace System.Text else if (lastChar > 0) { // Had a high surrogate, expected a low surrogate - // Uncount the last high surrogate + // Un-count the last high surrogate charCount--; // fall back the high surrogate. @@ -1434,7 +1423,7 @@ namespace System.Text charCount--; // Shouldn't have anything in fallback buffer for GetCharCount - // (don't have to check m_throwOnOverflow for count) + // (don't have to check _throwOnOverflow for count) Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[UnicodeEncoding.GetCharCount]Expected empty fallback buffer at end"); @@ -1462,7 +1451,7 @@ namespace System.Text lastChar = decoder.lastChar; // Shouldn't have anything in fallback buffer for GetChars - // (don't have to check m_throwOnOverflow for chars) + // (don't have to check _throwOnOverflow for chars) Debug.Assert(!decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, "[UnicodeEncoding.GetChars]Expected empty fallback buffer at start"); } @@ -1479,7 +1468,7 @@ namespace System.Text while (bytes < byteEnd) { // If we're aligned then maybe we can do it fast - // This'll hurt if we're unaligned because we'll always test but never be aligned + // That'll hurt if we're unaligned because we'll always test but never be aligned #if !NO_FAST_UNICODE_LOOP #if BIGENDIAN if (bigEndian && @@ -1509,7 +1498,7 @@ namespace System.Text { // See if we potentially have surrogates (0x8000 bit set) // (We're either big endian on a big endian machine or little endian on - // a little endian machine so this'll work) + // a little endian machine so that'll work) if ((0x8000800080008000 & *longBytes) != 0) { // See if any of these are high or low surrogates (0xd800 - 0xdfff). If the high @@ -1617,7 +1606,7 @@ namespace System.Text fallbackBuffer.InternalInitialize(byteStart, charEnd); } - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered bool fallbackResult = fallbackBuffer.InternalFallback(byteBuffer, bytes, ref charsForFallback); chars = charsForFallback; @@ -1669,7 +1658,7 @@ namespace System.Text fallbackBuffer.InternalInitialize(byteStart, charEnd); } - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered bool fallbackResult = fallbackBuffer.InternalFallback(byteBuffer, bytes, ref charsForFallback); chars = charsForFallback; @@ -1731,7 +1720,7 @@ namespace System.Text fallbackBuffer.InternalInitialize(byteStart, charEnd); } - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered bool fallbackResult = fallbackBuffer.InternalFallback(byteBuffer, bytes, ref charsForFallback); chars = charsForFallback; @@ -1796,7 +1785,7 @@ namespace System.Text fallbackBuffer.InternalInitialize(byteStart, charEnd); } - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered bool fallbackResult = fallbackBuffer.InternalFallback(byteBuffer, bytes, ref charsForFallback); chars = charsForFallback; @@ -1836,7 +1825,7 @@ namespace System.Text } // No hanging odd bytes allowed if must flush - charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered + charsForFallback = chars; // Avoid passing chars by reference to allow it to be en-registered bool fallbackResult = fallbackBuffer.InternalFallback(new byte[] { unchecked((byte)lastByte) }, bytes, ref charsForFallback); chars = charsForFallback; @@ -1866,7 +1855,7 @@ namespace System.Text // + " " + ((int)lastChar).ToString("X4") + " " + lastByte.ToString("X2") ); - decoder.m_bytesUsed = (int)(bytes - byteStart); + decoder._bytesUsed = (int)(bytes - byteStart); decoder.lastChar = lastChar; decoder.lastByte = lastByte; } @@ -1875,7 +1864,7 @@ namespace System.Text // System.IO.__UnmanagedMemoryStream.memcpyimpl((byte*)chars, bytes, byteCount); // Shouldn't have anything in fallback buffer for GetChars - // (don't have to check m_throwOnOverflow for count or chars) + // (don't have to check _throwOnOverflow for count or chars) Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[UnicodeEncoding.GetChars]Expected empty fallback buffer at end"); @@ -1946,7 +1935,7 @@ namespace System.Text // Might also need an extra 1 if there's a left over high surrogate in the decoder. long charCount = (long)(byteCount >> 1) + (byteCount & 1) + 1; - // Don't forget fallback (in case they have a bunch of lonely surrogates or something bizzare like that) + // Don't forget fallback (in case they have a bunch of lonely surrogates or something bizarre like that) if (DecoderFallback.MaxCharCount > 1) charCount *= DecoderFallback.MaxCharCount; @@ -1964,7 +1953,7 @@ namespace System.Text { // // Big Endian Unicode has different code page (1201) than small Endian one (1200), - // so we still have to check m_codePage here. + // so we still have to check _codePage here. // return (CodePage == that.CodePage) && byteOrderMark == that.byteOrderMark && @@ -1982,7 +1971,7 @@ namespace System.Text (byteOrderMark ? 4 : 0) + (bigEndian ? 8 : 0); } - private sealed class Decoder : System.Text.DecoderNLS, ISerializable + private sealed class Decoder : System.Text.DecoderNLS { internal int lastByte = -1; internal char lastChar = '\0'; @@ -1992,23 +1981,12 @@ namespace System.Text // base calls reset } - internal Decoder(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - - // ISerializable implementation - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - throw new PlatformNotSupportedException(); - } - public override void Reset() { lastByte = -1; lastChar = '\0'; - if (m_fallbackBuffer != null) - m_fallbackBuffer.Reset(); + if (_fallbackBuffer != null) + _fallbackBuffer.Reset(); } // Anything left in our decoder? -- 2.7.4