diff options
Diffstat (limited to 'src/mscorlib/src/System/Text/Latin1Encoding.cs')
-rw-r--r-- | src/mscorlib/src/System/Text/Latin1Encoding.cs | 62 |
1 files changed, 36 insertions, 26 deletions
diff --git a/src/mscorlib/src/System/Text/Latin1Encoding.cs b/src/mscorlib/src/System/Text/Latin1Encoding.cs index 26009bf6c0..e456b8533d 100644 --- a/src/mscorlib/src/System/Text/Latin1Encoding.cs +++ b/src/mscorlib/src/System/Text/Latin1Encoding.cs @@ -2,19 +2,18 @@ // 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; +using System.Globalization; +using System.Runtime.InteropServices; +using System.Security; +using System.Collections; +using System.Runtime.CompilerServices; +using System.Runtime.Serialization; + namespace System.Text { - using System; - using System.Diagnostics; - using System.Diagnostics.Contracts; - using System.Globalization; - using System.Runtime.InteropServices; - using System.Security; - using System.Collections; - using System.Runtime.CompilerServices; - using System.Runtime.Serialization; - - // // 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 @@ -47,7 +46,7 @@ namespace System.Text { // Make sure to get the base stuff too This throws if info is null SerializeEncoding(info, context); - Debug.Assert(info!=null, "[Latin1Encoding.GetObjectData] Expected null info to throw"); + Debug.Assert(info != null, "[Latin1Encoding.GetObjectData] Expected null info to throw"); // In Everett this is a CodePageEncoding, so it needs maxCharSize info.AddValue("CodePageEncoding+maxCharSize", 1); @@ -115,6 +114,7 @@ namespace System.Text // For fallback we may need a fallback buffer, we know we aren't default fallback. EncoderFallbackBuffer fallbackBuffer = null; + char* charsForFallback; // We may have a left over character from last time, try and process it. if (charLeftOver > 0) @@ -128,7 +128,9 @@ namespace System.Text // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback // This will fallback a pair if *chars is a low surrogate - fallbackBuffer.InternalFallback(charLeftOver, ref chars); + charsForFallback = chars; + fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); + chars = charsForFallback; } // Now we may have fallback char[] already from the encoder @@ -161,7 +163,9 @@ namespace System.Text } // Get Fallback - fallbackBuffer.InternalFallback(ch, ref chars); + charsForFallback = chars; + fallbackBuffer.InternalFallback(ch, ref charsForFallback); + chars = charsForFallback; continue; } @@ -216,7 +220,7 @@ namespace System.Text if (fallback != null && fallback.MaxCharCount == 1) { // Fast version - char cReplacement=fallback.DefaultString[0]; + char cReplacement = fallback.DefaultString[0]; // Check for replacements in range, otherwise fall back to slow version. if (cReplacement <= (char)0xff) @@ -262,7 +266,7 @@ namespace System.Text if (encoder != null) { encoder.charLeftOver = (char)0; - encoder.m_charsUsed = (int)(chars-charStart); + encoder.m_charsUsed = (int)(chars - charStart); } return (int)(bytes - byteStart); } @@ -275,6 +279,7 @@ namespace System.Text // For fallback we may need a fallback buffer, we know we aren't default fallback, create & init it EncoderFallbackBuffer fallbackBuffer = null; + char* charsForFallback; // We may have a left over character from last time, try and process it. if (charLeftOver > 0) @@ -289,7 +294,10 @@ namespace System.Text // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback // This will fallback a pair if *chars is a low surrogate - fallbackBuffer.InternalFallback(charLeftOver, ref chars); + charsForFallback = chars; + fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); + chars = charsForFallback; + if (fallbackBuffer.Remaining > byteEnd - bytes) { // Throw it, if we don't have enough for this we never will @@ -327,7 +335,9 @@ namespace System.Text } // Get Fallback - fallbackBuffer.InternalFallback(ch, ref chars); + charsForFallback = chars; + fallbackBuffer.InternalFallback(ch, ref charsForFallback); + chars = charsForFallback; // Make sure we have enough room. Each fallback char will be 1 output char // (or else cause a recursion exception) @@ -335,7 +345,7 @@ namespace System.Text { // Didn't use this char, throw it. Chars should've advanced by now // If we had encoder fallback data it would've thrown before the loop - Debug.Assert(chars > charStart, + Debug.Assert(chars > charStart, "[Latin1Encoding.GetBytes]Expected chars to have advanced (fallback case)"); chars--; fallbackBuffer.InternalReset(); @@ -357,7 +367,7 @@ namespace System.Text "[Latin1Encoding.GetBytes]Expected fallback to have throw initially if insufficient space"); if (fallbackBuffer == null || fallbackBuffer.bFallingBack == false) { - Debug.Assert(chars > charStart, + Debug.Assert(chars > charStart, "[Latin1Encoding.GetBytes]Expected chars to have advanced (fallback case)"); chars--; // don't use last char } @@ -441,8 +451,8 @@ namespace System.Text public override int GetMaxByteCount(int charCount) { if (charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(charCount), + SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); // Characters would be # of characters + 1 in case high surrogate is ? * max fallback @@ -454,15 +464,15 @@ namespace System.Text // 1 to 1 for most characters. Only surrogates with fallbacks have less. if (byteCount > 0x7fffffff) - throw new ArgumentOutOfRangeException(nameof(charCount), Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow")); + throw new ArgumentOutOfRangeException(nameof(charCount), SR.ArgumentOutOfRange_GetByteCountOverflow); return (int)byteCount; } public override int GetMaxCharCount(int byteCount) { if (byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(byteCount), + SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); // Just return length, SBCS stay the same length because they don't map to surrogate @@ -473,7 +483,7 @@ namespace System.Text charCount *= DecoderFallback.MaxCharCount; if (charCount > 0x7fffffff) - throw new ArgumentOutOfRangeException(nameof(byteCount), Environment.GetResourceString("ArgumentOutOfRange_GetCharCountOverflow")); + throw new ArgumentOutOfRangeException(nameof(byteCount), SR.ArgumentOutOfRange_GetCharCountOverflow); return (int)charCount; } |