Encoding code clean up (#12864)
[platform/upstream/coreclr.git] / src / mscorlib / shared / System / Text / Encoding.cs
index ea75ace..4f23d2a 100644 (file)
@@ -154,23 +154,16 @@ namespace System.Text
         private const int CodePageUTF32 = 12000;
         private const int CodePageUTF32BE = 12001;
 
-        internal int m_codePage = 0;
+        internal int _codePage = 0;
 
-        // dataItem should be internal (not private). otherwise it will break during the deserialization
-        // of the data came from Everett
-        internal CodePageDataItem dataItem = null;
-
-        [NonSerialized]
-        internal bool m_deserializedFromEverett = false;
+        internal CodePageDataItem _dataItem = null;
 
         // Because of encoders we may be read only
         [OptionalField(VersionAdded = 2)]
-        private bool m_isReadOnly = true;
+        private bool _isReadOnly = true;
 
         // Encoding (encoder) fallback
-        [OptionalField(VersionAdded = 2)]
         internal EncoderFallback encoderFallback = null;
-        [OptionalField(VersionAdded = 2)]
         internal DecoderFallback decoderFallback = null;
 
         protected Encoding() : this(0)
@@ -188,7 +181,7 @@ namespace System.Text
             Contract.EndContractBlock();
 
             // Remember code page
-            m_codePage = codePage;
+            _codePage = codePage;
 
             // Use default encoder/decoder fallbacks
             this.SetDefaultFallbacks();
@@ -207,7 +200,7 @@ namespace System.Text
             Contract.EndContractBlock();
 
             // Remember code page
-            m_codePage = codePage;
+            _codePage = codePage;
 
             this.encoderFallback = encoderFallback ?? new InternalEncoderBestFitFallback(this);
             this.decoderFallback = decoderFallback ?? new InternalDecoderBestFitFallback(this);
@@ -218,117 +211,10 @@ namespace System.Text
         {
             // For UTF-X encodings, we use a replacement fallback with an "\xFFFD" string,
             // For ASCII we use "?" replacement fallback, etc.
-            this.encoderFallback = new InternalEncoderBestFitFallback(this);
-            this.decoderFallback = new InternalDecoderBestFitFallback(this);
-        }
-
-
-        #region Serialization
-        internal void OnDeserializing()
-        {
-            // intialize the optional Whidbey fields
-            encoderFallback = null;
-            decoderFallback = null;
-            m_isReadOnly = true;
-        }
-
-        internal void OnDeserialized()
-        {
-            if (encoderFallback == null || decoderFallback == null)
-            {
-                m_deserializedFromEverett = true;
-                SetDefaultFallbacks();
-            }
-
-            // dataItem is always recalculated from the code page #
-            dataItem = null;
-        }
-
-        [OnDeserializing]
-        private void OnDeserializing(StreamingContext ctx)
-        {
-            OnDeserializing();
-        }
-
-
-        [OnDeserialized]
-        private void OnDeserialized(StreamingContext ctx)
-        {
-            OnDeserialized();
-        }
-
-        [OnSerializing]
-        private void OnSerializing(StreamingContext ctx)
-        {
-            // to be consistent with SerializeEncoding
-            dataItem = null;
-        }
-
-        // the following two methods are used for the inherited classes which implemented ISerializable
-        // Deserialization Helper
-        internal void DeserializeEncoding(SerializationInfo info, StreamingContext context)
-        {
-            // Any info?
-            if (info == null) throw new ArgumentNullException(nameof(info));
-            Contract.EndContractBlock();
-
-            // All versions have a code page
-            this.m_codePage = (int)info.GetValue("m_codePage", typeof(int));
-
-            // We can get dataItem on the fly if needed, and the index is different between versions
-            // so ignore whatever dataItem data we get from Everett.
-            this.dataItem = null;
-
-            // See if we have a code page
-            try
-            {
-                //
-                // Try Whidbey V2.0 Fields
-                //
-
-                m_isReadOnly = (bool)info.GetValue("m_isReadOnly", typeof(bool));
-
-                this.encoderFallback = (EncoderFallback)info.GetValue("encoderFallback", typeof(EncoderFallback));
-                this.decoderFallback = (DecoderFallback)info.GetValue("decoderFallback", typeof(DecoderFallback));
-            }
-            catch (SerializationException)
-            {
-                //
-                // Didn't have Whidbey things, must be Everett
-                //
-                this.m_deserializedFromEverett = true;
-
-                // May as well be read only
-                m_isReadOnly = true;
-                SetDefaultFallbacks();
-            }
+            encoderFallback = new InternalEncoderBestFitFallback(this);
+            decoderFallback = new InternalDecoderBestFitFallback(this);
         }
 
-        // Serialization Helper
-        internal void SerializeEncoding(SerializationInfo info, StreamingContext context)
-        {
-            // Any Info?
-            if (info == null) throw new ArgumentNullException(nameof(info));
-            Contract.EndContractBlock();
-
-            // These are new V2.0 Whidbey stuff
-            info.AddValue("m_isReadOnly", m_isReadOnly);
-            info.AddValue("encoderFallback", this.EncoderFallback);
-            info.AddValue("decoderFallback", this.DecoderFallback);
-
-            // These were in Everett V1.1 as well
-            info.AddValue("m_codePage", this.m_codePage);
-
-            // This was unique to Everett V1.1
-            info.AddValue("dataItem", null);
-
-            // Everett duplicated these fields, so these are needed for portability
-            info.AddValue("Encoding+m_codePage", this.m_codePage);
-            info.AddValue("Encoding+dataItem", null);
-        }
-
-        #endregion Serialization
-
         // Converts a byte array from one encoding to another. The bytes in the
         // bytes array are converted from srcEncoding to
         // dstEncoding, and the returned value is a new byte array
@@ -498,13 +384,12 @@ namespace System.Text
 
         private void GetDataItem()
         {
-            if (dataItem == null)
+            if (_dataItem == null)
             {
-                dataItem = EncodingTable.GetCodePageDataItem(m_codePage);
-                if (dataItem == null)
+                _dataItem = EncodingTable.GetCodePageDataItem(_codePage);
+                if (_dataItem == null)
                 {
-                    throw new NotSupportedException(
-                        SR.Format(SR.NotSupported_NoCodepageData, m_codePage));
+                    throw new NotSupportedException(SR.Format(SR.NotSupported_NoCodepageData, _codePage));
                 }
             }
         }
@@ -516,11 +401,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return (dataItem.BodyName);
+                return (_dataItem.BodyName);
             }
         }
 
@@ -573,7 +458,7 @@ namespace System.Text
         {
             get
             {
-                return SR.GetResourceString("Globalization_cp_" + m_codePage.ToString());
+                return SR.GetResourceString("Globalization_cp_" + _codePage.ToString());
             }
         }
 #endif
@@ -584,11 +469,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return (dataItem.HeaderName);
+                return (_dataItem.HeaderName);
             }
         }
 
@@ -597,11 +482,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return (dataItem.WebName);
+                return (_dataItem.WebName);
             }
         }
 
@@ -611,11 +496,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return (dataItem.UIFamilyCodePage);
+                return (_dataItem.UIFamilyCodePage);
             }
         }
 
@@ -626,11 +511,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return ((dataItem.Flags & MIMECONTF_BROWSER) != 0);
+                return ((_dataItem.Flags & MIMECONTF_BROWSER) != 0);
             }
         }
 
@@ -640,11 +525,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return ((dataItem.Flags & MIMECONTF_SAVABLE_BROWSER) != 0);
+                return ((_dataItem.Flags & MIMECONTF_SAVABLE_BROWSER) != 0);
             }
         }
 
@@ -654,11 +539,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return ((dataItem.Flags & MIMECONTF_MAILNEWS) != 0);
+                return ((_dataItem.Flags & MIMECONTF_MAILNEWS) != 0);
             }
         }
 
@@ -670,11 +555,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return ((dataItem.Flags & MIMECONTF_SAVABLE_MAILNEWS) != 0);
+                return ((_dataItem.Flags & MIMECONTF_SAVABLE_MAILNEWS) != 0);
             }
         }
 
@@ -736,7 +621,7 @@ namespace System.Text
             Encoding newEncoding = (Encoding)this.MemberwiseClone();
 
             // New one should be readable
-            newEncoding.m_isReadOnly = false;
+            newEncoding._isReadOnly = false;
             return newEncoding;
         }
 
@@ -745,7 +630,7 @@ namespace System.Text
         {
             get
             {
-                return (m_isReadOnly);
+                return (_isReadOnly);
             }
         }
 
@@ -1116,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.
         //
 
@@ -1217,7 +1102,7 @@ namespace System.Text
         {
             get
             {
-                return m_codePage;
+                return _codePage;
             }
         }
 
@@ -1366,7 +1251,7 @@ namespace System.Text
         {
             Encoding that = value as Encoding;
             if (that != null)
-                return (m_codePage == that.m_codePage) &&
+                return (_codePage == that._codePage) &&
                        (EncoderFallback.Equals(that.EncoderFallback)) &&
                        (DecoderFallback.Equals(that.DecoderFallback));
             return (false);
@@ -1375,7 +1260,7 @@ namespace System.Text
 
         public override int GetHashCode()
         {
-            return m_codePage + this.EncoderFallback.GetHashCode() + this.DecoderFallback.GetHashCode();
+            return _codePage + this.EncoderFallback.GetHashCode() + this.DecoderFallback.GetHashCode();
         }
 
         internal virtual char[] GetBestFitUnicodeToBytesData()
@@ -1393,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();
             }
 
@@ -1416,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();
             }
 
@@ -1437,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)
@@ -1451,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
@@ -1467,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
@@ -1499,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)
@@ -1524,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
@@ -1544,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
@@ -1580,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.
@@ -1588,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);
             }
         }
 
@@ -1798,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()));