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

Commit a1e785d

Browse files
jamesqojkotas
authored andcommitted
Short-circuit on the count, not the array length, in common Encoding methods (#6108)
* Short-circuit using count instead of array.Length * Use Array.Empty in default Reset implementation
1 parent c3e09ed commit a1e785d

File tree

7 files changed

+31
-31
lines changed

7 files changed

+31
-31
lines changed

src/mscorlib/src/System/Text/ASCIIEncoding.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
7373
Contract.EndContractBlock();
7474

7575
// If no input, return 0, avoid fixed empty array problem
76-
if (chars.Length == 0)
76+
if (count == 0)
7777
return 0;
7878

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

198198
// If nothing to encode return 0, avoid fixed problem
199-
if (chars.Length == 0)
199+
if (charCount == 0)
200200
return 0;
201201

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

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

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

321321
// If no input, return 0 & avoid fixed problem
322-
if (bytes.Length == 0)
322+
if (byteCount == 0)
323323
return 0;
324324

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

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

390390
fixed (byte* pBytes = bytes)
391391
return String.CreateStringFromEncoding(

src/mscorlib/src/System/Text/Decoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ internal bool InternalHasFallbackBuffer
101101
[System.Runtime.InteropServices.ComVisible(false)]
102102
public virtual void Reset()
103103
{
104-
byte[] byteTemp = {};
104+
byte[] byteTemp = Array.Empty<byte>();
105105
char[] charTemp = new char[GetCharCount(byteTemp, 0, 0, true)];
106106
GetChars(byteTemp, 0, 0, charTemp, 0, true);
107107
if (m_fallbackBuffer != null)

src/mscorlib/src/System/Text/EncodingNLS.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
5959
Contract.EndContractBlock();
6060

6161
// If no input, return 0, avoid fixed empty array problem
62-
if (chars.Length == 0)
62+
if (count == 0)
6363
return 0;
6464

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

178178
// If nothing to encode return 0, avoid fixed problem
179-
if (chars.Length == 0)
179+
if (charCount == 0)
180180
return 0;
181181

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

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

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

293293
// If no input, return 0 & avoid fixed problem
294-
if (bytes.Length == 0)
294+
if (byteCount == 0)
295295
return 0;
296296

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

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

357357
fixed (byte* pBytes = bytes)
358358
return String.CreateStringFromEncoding(

src/mscorlib/src/System/Text/UTF32Encoding.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
112112
Contract.EndContractBlock();
113113

114114
// If no input, return 0, avoid fixed empty array problem
115-
if (chars.Length == 0)
115+
if (count == 0)
116116
return 0;
117117

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

236236
// If nothing to encode return 0, avoid fixed problem
237-
if (chars.Length == 0)
237+
if (charCount == 0)
238238
return 0;
239239

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

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

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

357357
// If no input, return 0 & avoid fixed problem
358-
if (bytes.Length == 0)
358+
if (byteCount == 0)
359359
return 0;
360360

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

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

424424
fixed (byte* pBytes = bytes)
425425
return String.CreateStringFromEncoding(

src/mscorlib/src/System/Text/UTF7Encoding.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
178178
Contract.EndContractBlock();
179179

180180
// If no input, return 0, avoid fixed empty array problem
181-
if (chars.Length == 0)
181+
if (count == 0)
182182
return 0;
183183

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

305305
// If nothing to encode return 0, avoid fixed problem
306-
if (chars.Length == 0)
306+
if (charCount == 0)
307307
return 0;
308308

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

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

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

428428
// If no input, return 0 & avoid fixed problem
429-
if (bytes.Length == 0)
429+
if (byteCount == 0)
430430
return 0;
431431

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

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

497497
fixed (byte* pBytes = bytes)
498498
return String.CreateStringFromEncoding(

src/mscorlib/src/System/Text/UTF8Encoding.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
136136
Contract.EndContractBlock();
137137

138138
// If no input, return 0, avoid fixed empty array problem
139-
if (chars.Length == 0)
139+
if (count == 0)
140140
return 0;
141141

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

261261
// If nothing to encode return 0, avoid fixed problem
262-
if (chars.Length == 0)
262+
if (charCount == 0)
263263
return 0;
264264

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

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

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

384384
// If no input, return 0 & avoid fixed problem
385-
if (bytes.Length == 0)
385+
if (byteCount == 0)
386386
return 0;
387387

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

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

453453
fixed (byte* pBytes = bytes)
454454
return String.CreateStringFromEncoding(

src/mscorlib/src/System/Text/UnicodeEncoding.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count)
110110
Contract.EndContractBlock();
111111

112112
// If no input, return 0, avoid fixed empty array problem
113-
if (chars.Length == 0)
113+
if (count == 0)
114114
return 0;
115115

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

235235
// If nothing to encode return 0, avoid fixed problem
236-
if (chars.Length == 0)
236+
if (charCount == 0)
237237
return 0;
238238

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

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

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

358358
// If no input, return 0 & avoid fixed problem
359-
if (bytes.Length == 0)
359+
if (byteCount == 0)
360360
return 0;
361361

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

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

427427
fixed (byte* pBytes = bytes)
428428
return String.CreateStringFromEncoding(

0 commit comments

Comments
 (0)