Skip to content

Commit f389289

Browse files
committed
Address PR feedback: Optimize for whitespace (\r\n) every 76 bytes
1 parent 9983889 commit f389289

File tree

4 files changed

+122
-48
lines changed

4 files changed

+122
-48
lines changed

src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ public void DecodeInPlaceInvalidBytesPadding()
661661

662662
[Theory]
663663
[MemberData(nameof(ValidBase64Strings_WithCharsThatMustBeIgnored))]
664-
public void BasicDecodingIgnoresCharsToBeIgnoredAsConvertToBase64Does(string utf8WithCharsToBeIgnored, byte[] expectedBytes)
664+
public void BasicDecodingIgnoresCharsToBeIgnoredAsConvertToBase64Does(string utf8WithCharsToBeIgnored, byte[] expectedBytes, int expectedBytesConsumed)
665665
{
666666
byte[] utf8BytesWithByteToBeIgnored = UTF8Encoding.UTF8.GetBytes(utf8WithCharsToBeIgnored);
667667
byte[] resultBytes = new byte[5];
@@ -671,16 +671,17 @@ public void BasicDecodingIgnoresCharsToBeIgnoredAsConvertToBase64Does(string utf
671671
byte[] stringBytes = Convert.FromBase64String(utf8WithCharsToBeIgnored);
672672

673673
Assert.Equal(OperationStatus.Done, result);
674-
Assert.Equal(8, bytesConsumed);
674+
Assert.Equal(expectedBytesConsumed, bytesConsumed);
675675
Assert.Equal(expectedBytes.Length, bytesWritten);
676676
Assert.True(expectedBytes.SequenceEqual(resultBytes));
677677
Assert.True(stringBytes.SequenceEqual(resultBytes));
678678
}
679679

680680
[Theory]
681681
[MemberData(nameof(ValidBase64Strings_WithCharsThatMustBeIgnored))]
682-
public void DecodeInPlaceIgnoresCharsToBeIgnoredAsConvertToBase64Does(string utf8WithCharsToBeIgnored, byte[] expectedBytes)
682+
public void DecodeInPlaceIgnoresCharsToBeIgnoredAsConvertToBase64Does(string utf8WithCharsToBeIgnored, byte[] expectedBytes, int expectedBytesConsumed)
683683
{
684+
_ = expectedBytesConsumed;
684685
Span<byte> utf8BytesWithByteToBeIgnored = UTF8Encoding.UTF8.GetBytes(utf8WithCharsToBeIgnored);
685686
OperationStatus result = Base64.DecodeFromUtf8InPlace(utf8BytesWithByteToBeIgnored, out int bytesWritten);
686687
Span<byte> bytesOverwritten = utf8BytesWithByteToBeIgnored.Slice(0, bytesWritten);

src/libraries/System.Memory/tests/Base64/Base64TestBase.cs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Licensed to the .NET Foundation under one or more agreements.
2-
// The .NET Foundation licenses this file to you under the MIT license.
2+
// The .NET Foundation licenses this file to you under the MIT license.utf8Bytes, utf8Bytes.Length
33

44
using System.Collections.Generic;
55
using System.Text;
@@ -24,62 +24,63 @@ public static IEnumerable<object[]> ValidBase64Strings_WithCharsThatMustBeIgnore
2424
// One will have 1 char, another will have 3
2525

2626
// Line feed
27-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(9), 1), utf8Bytes };
28-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(9), 3), utf8Bytes };
27+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(9), 1), utf8Bytes, utf8Bytes.Length + 4 };
28+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(9), 3), utf8Bytes, utf8Bytes.Length + 6 };
2929

3030
// Horizontal tab
31-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(10), 1), utf8Bytes };
32-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(10), 3), utf8Bytes };
31+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(10), 1), utf8Bytes, utf8Bytes.Length + 4 };
32+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(10), 3), utf8Bytes, utf8Bytes.Length + 6 };
3333

3434
// Carriage return
35-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(13), 1), utf8Bytes };
36-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(13), 3), utf8Bytes };
35+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(13), 1), utf8Bytes, utf8Bytes.Length + 4 };
36+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(13), 3), utf8Bytes, utf8Bytes.Length + 6 };
3737

3838
// Space
39-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(32), 1), utf8Bytes };
40-
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(32), 3), utf8Bytes };
39+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(32), 1), utf8Bytes, utf8Bytes.Length + 4 };
40+
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(32), 3), utf8Bytes, utf8Bytes.Length + 6 };
4141

4242
string GetBase64StringWithPassedCharInsertedInTheMiddle(char charToInsert, int numberOfTimesToInsert) => $"{firstSegment}{new string(charToInsert, numberOfTimesToInsert)}{secondSegment}";
4343

4444
// Insert ignored chars at the start of the base 64 string
4545
// One will have 1 char, another will have 3
4646

4747
// Line feed
48-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(9), 1), utf8Bytes };
49-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(9), 3), utf8Bytes };
48+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(9), 1), utf8Bytes, utf8Bytes.Length + 4 };
49+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(9), 3), utf8Bytes, utf8Bytes.Length + 6 };
5050

5151
// Horizontal tab
52-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(10), 1), utf8Bytes };
53-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(10), 3), utf8Bytes };
52+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(10), 1), utf8Bytes, utf8Bytes.Length + 4 };
53+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(10), 3), utf8Bytes, utf8Bytes.Length + 6 };
5454

5555
// Carriage return
56-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(13), 1), utf8Bytes };
57-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(13), 3), utf8Bytes };
56+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(13), 1), utf8Bytes, utf8Bytes.Length + 4 };
57+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(13), 3), utf8Bytes, utf8Bytes.Length + 6 };
5858

5959
// Space
60-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(32), 1), utf8Bytes };
61-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(32), 3), utf8Bytes };
60+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(32), 1), utf8Bytes, utf8Bytes.Length + 4 };
61+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(32), 3), utf8Bytes, utf8Bytes.Length + 6 };
6262

6363
string GetBase64StringWithPassedCharInsertedAtTheStart(char charToInsert, int numberOfTimesToInsert) => $"{new string(charToInsert, numberOfTimesToInsert)}{firstSegment}{secondSegment}";
6464

6565
// Insert ignored chars at the end of the base 64 string
6666
// One will have 1 char, another will have 3
67+
// Whitespace after end/padding is not included in consumed bytes
6768

6869
// Line feed
69-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(9), 1), utf8Bytes };
70-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(9), 3), utf8Bytes };
70+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(9), 1), utf8Bytes, utf8Bytes.Length + 3 };
71+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(9), 3), utf8Bytes, utf8Bytes.Length + 3 };
7172

7273
// Horizontal tab
73-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(10), 1), utf8Bytes };
74-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(10), 3), utf8Bytes };
74+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(10), 1), utf8Bytes, utf8Bytes.Length + 3 };
75+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(10), 3), utf8Bytes, utf8Bytes.Length + 3 };
7576

7677
// Carriage return
77-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(13), 1), utf8Bytes };
78-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(13), 3), utf8Bytes };
78+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(13), 1), utf8Bytes, utf8Bytes.Length + 3 };
79+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(13), 3), utf8Bytes, utf8Bytes.Length + 3 };
7980

8081
// Space
81-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(32), 1), utf8Bytes };
82-
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(32), 3), utf8Bytes };
82+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(32), 1), utf8Bytes, utf8Bytes.Length + 3 };
83+
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(32), 3), utf8Bytes, utf8Bytes.Length + 3 };
8384

8485
string GetBase64StringWithPassedCharInsertedAtTheEnd(char charToInsert, int numberOfTimesToInsert) => $"{firstSegment}{secondSegment}{new string(charToInsert, numberOfTimesToInsert)}";
8586
}

src/libraries/System.Memory/tests/Base64/Base64ValidationUnitTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ public void ValidateGuidChars()
147147

148148
[Theory]
149149
[MemberData(nameof(ValidBase64Strings_WithCharsThatMustBeIgnored))]
150-
public void ValidateBytesIgnoresCharsToBeIgnoredBytes(string utf8WithByteToBeIgnored, byte[] expectedBytes)
150+
public void ValidateBytesIgnoresCharsToBeIgnoredBytes(string utf8WithByteToBeIgnored, byte[] expectedBytes, int expectedBytesConsumed)
151151
{
152+
_ = expectedBytesConsumed;
152153
byte[] utf8BytesWithByteToBeIgnored = UTF8Encoding.UTF8.GetBytes(utf8WithByteToBeIgnored);
153154

154155
Assert.True(Base64.IsValid(utf8BytesWithByteToBeIgnored));
@@ -158,8 +159,9 @@ public void ValidateBytesIgnoresCharsToBeIgnoredBytes(string utf8WithByteToBeIgn
158159

159160
[Theory]
160161
[MemberData(nameof(ValidBase64Strings_WithCharsThatMustBeIgnored))]
161-
public void ValidateBytesIgnoresCharsToBeIgnoredChars(string utf8WithByteToBeIgnored, byte[] expectedBytes)
162+
public void ValidateBytesIgnoresCharsToBeIgnoredChars(string utf8WithByteToBeIgnored, byte[] expectedBytes, int expectedBytesConsumed)
162163
{
164+
_ = expectedBytesConsumed;
163165
ReadOnlySpan<char> utf8BytesWithByteToBeIgnored = utf8WithByteToBeIgnored.ToArray();
164166

165167
Assert.True(Base64.IsValid(utf8BytesWithByteToBeIgnored));

0 commit comments

Comments
 (0)