Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Short-circuit on the count, not the array length, in common Encoding …
Browse files Browse the repository at this point in the history
…methods (#6108)

* Short-circuit using count instead of array.Length
* Use Array.Empty in default Reset implementation
  • Loading branch information
jamesqo authored and jkotas committed Jul 4, 2016
1 parent c3e09ed commit a1e785d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/mscorlib/src/System/Text/ASCIIEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
Contract.EndContractBlock();

// If no input, return 0, avoid fixed empty array problem
if (chars.Length == 0)
if (count == 0)
return 0;

// Just call the pointer version
Expand Down Expand Up @@ -196,7 +196,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (chars.Length == 0)
if (charCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -261,7 +261,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// If no input just return 0, fixed doesn't like 0 length arrays
if (bytes.Length == 0)
if (count == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -319,7 +319,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount,
Contract.EndContractBlock();

// If no input, return 0 & avoid fixed problem
if (bytes.Length == 0)
if (byteCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -385,7 +385,7 @@ public override unsafe String GetString(byte[] bytes, int byteIndex, int byteCou
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (bytes.Length == 0) return String.Empty;
if (byteCount == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
Expand Down
2 changes: 1 addition & 1 deletion src/mscorlib/src/System/Text/Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ internal bool InternalHasFallbackBuffer
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void Reset()
{
byte[] byteTemp = {};
byte[] byteTemp = Array.Empty<byte>();
char[] charTemp = new char[GetCharCount(byteTemp, 0, 0, true)];
GetChars(byteTemp, 0, 0, charTemp, 0, true);
if (m_fallbackBuffer != null)
Expand Down
10 changes: 5 additions & 5 deletions src/mscorlib/src/System/Text/EncodingNLS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
Contract.EndContractBlock();

// If no input, return 0, avoid fixed empty array problem
if (chars.Length == 0)
if (count == 0)
return 0;

// Just call the pointer version
Expand Down Expand Up @@ -176,7 +176,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (chars.Length == 0)
if (charCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -237,7 +237,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// If no input just return 0, fixed doesn't like 0 length arrays
if (bytes.Length == 0)
if (count == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -291,7 +291,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount,
Contract.EndContractBlock();

// If no input, return 0 & avoid fixed problem
if (bytes.Length == 0)
if (byteCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -352,7 +352,7 @@ public override unsafe String GetString(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (bytes.Length == 0) return String.Empty;
if (count == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
Expand Down
10 changes: 5 additions & 5 deletions src/mscorlib/src/System/Text/UTF32Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
Contract.EndContractBlock();

// If no input, return 0, avoid fixed empty array problem
if (chars.Length == 0)
if (count == 0)
return 0;

// Just call the pointer version
Expand Down Expand Up @@ -234,7 +234,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (chars.Length == 0)
if (charCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -298,7 +298,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// If no input just return 0, fixed doesn't like 0 length arrays.
if (bytes.Length == 0)
if (count == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -355,7 +355,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount,
Contract.EndContractBlock();

// If no input, return 0 & avoid fixed problem
if (bytes.Length == 0)
if (byteCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -419,7 +419,7 @@ public override unsafe String GetString(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (bytes.Length == 0) return String.Empty;
if (count == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
Expand Down
10 changes: 5 additions & 5 deletions src/mscorlib/src/System/Text/UTF7Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
Contract.EndContractBlock();

// If no input, return 0, avoid fixed empty array problem
if (chars.Length == 0)
if (count == 0)
return 0;

// Just call the pointer version
Expand Down Expand Up @@ -303,7 +303,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (chars.Length == 0)
if (charCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -368,7 +368,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// If no input just return 0, fixed doesn't like 0 length arrays.
if (bytes.Length == 0)
if (count == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -426,7 +426,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount,
Contract.EndContractBlock();

// If no input, return 0 & avoid fixed problem
if (bytes.Length == 0)
if (byteCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -492,7 +492,7 @@ public override unsafe String GetString(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (bytes.Length == 0) return String.Empty;
if (count == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
Expand Down
10 changes: 5 additions & 5 deletions src/mscorlib/src/System/Text/UTF8Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
Contract.EndContractBlock();

// If no input, return 0, avoid fixed empty array problem
if (chars.Length == 0)
if (count == 0)
return 0;

// Just call the pointer version
Expand Down Expand Up @@ -259,7 +259,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (chars.Length == 0)
if (charCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -324,7 +324,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// If no input just return 0, fixed doesn't like 0 length arrays.
if (bytes.Length == 0)
if (count == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -382,7 +382,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount,
Contract.EndContractBlock();

// If no input, return 0 & avoid fixed problem
if (bytes.Length == 0)
if (byteCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -448,7 +448,7 @@ public override unsafe String GetString(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (bytes.Length == 0) return String.Empty;
if (count == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
Expand Down
10 changes: 5 additions & 5 deletions src/mscorlib/src/System/Text/UnicodeEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
Contract.EndContractBlock();

// If no input, return 0, avoid fixed empty array problem
if (chars.Length == 0)
if (count == 0)
return 0;

// Just call the pointer version
Expand Down Expand Up @@ -233,7 +233,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (chars.Length == 0)
if (charCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -298,7 +298,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// If no input just return 0, fixed doesn't like 0 length arrays
if (bytes.Length == 0)
if (count == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -356,7 +356,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount,
Contract.EndContractBlock();

// If no input, return 0 & avoid fixed problem
if (bytes.Length == 0)
if (byteCount == 0)
return 0;

// Just call pointer version
Expand Down Expand Up @@ -422,7 +422,7 @@ public override unsafe String GetString(byte[] bytes, int index, int count)
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (bytes.Length == 0) return String.Empty;
if (count == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
Expand Down

0 comments on commit a1e785d

Please sign in to comment.