Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e617b8f

Browse files
committedJun 11, 2024·
Rename utf8 -> source/destintion
1 parent dea9006 commit e617b8f

File tree

4 files changed

+60
-53
lines changed

4 files changed

+60
-53
lines changed
 

‎src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ internal interface IBase64Decoder<T> where T : unmanaged
101101
static abstract ReadOnlySpan<uint> Vector128LutShift { get; }
102102
static abstract ReadOnlySpan<uint> AdvSimdLutOne3 { get; }
103103
static abstract uint AdvSimdLutTwo3Uint1 { get; }
104-
static abstract int SrcLength(bool isFinalBlock, int utf8Length);
105-
static abstract int GetMaxDecodedLength(int utf8Length);
104+
static abstract int SrcLength(bool isFinalBlock, int sourceLength);
105+
static abstract int GetMaxDecodedLength(int sourceLength);
106106
static abstract bool IsInvalidLength(int bufferLength);
107107
static abstract bool IsValidPadding(uint padChar);
108108
static abstract bool TryDecode128Core(
@@ -127,7 +127,7 @@ static abstract bool TryDecode256Core(
127127
static abstract unsafe int Decode(T* encodedBytes, ref sbyte decodingMap);
128128
static abstract unsafe int DecodeRemaining(T* srcEnd, ref sbyte decodingMap, long remaining, out uint t2, out uint t3);
129129
static abstract int IndexOfAnyExceptWhiteSpace(ReadOnlySpan<T> span);
130-
static abstract OperationStatus DecodeWithWhiteSpaceBlockwiseWrapper<TTBase64Decoder>(ReadOnlySpan<T> utf8,
130+
static abstract OperationStatus DecodeWithWhiteSpaceBlockwiseWrapper<TTBase64Decoder>(ReadOnlySpan<T> source,
131131
Span<byte> bytes, ref int bytesConsumed, ref int bytesWritten, bool isFinalBlock = true)
132132
where TTBase64Decoder : IBase64Decoder<T>;
133133
static abstract unsafe bool TryLoadVector512(T* src, T* srcStart, int sourceLength, out Vector512<sbyte> str);

‎src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs

+35-28
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@ public static partial class Base64
3737
public static OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Span<byte> bytes, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true) =>
3838
DecodeFrom<Base64DecoderByte, byte>(utf8, bytes, out bytesConsumed, out bytesWritten, isFinalBlock, ignoreWhiteSpace: true);
3939

40-
internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(ReadOnlySpan<T> utf8, Span<byte> bytes,
40+
internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(ReadOnlySpan<T> source, Span<byte> bytes,
4141
out int bytesConsumed, out int bytesWritten, bool isFinalBlock, bool ignoreWhiteSpace)
4242
where TBase64Decoder : IBase64Decoder<T>
4343
where T : unmanaged
4444
{
45-
if (utf8.IsEmpty)
45+
if (source.IsEmpty)
4646
{
4747
bytesConsumed = 0;
4848
bytesWritten = 0;
4949
return OperationStatus.Done;
5050
}
5151

52-
fixed (T* srcBytes = &MemoryMarshal.GetReference(utf8))
52+
fixed (T* srcBytes = &MemoryMarshal.GetReference(source))
5353
fixed (byte* destBytes = &MemoryMarshal.GetReference(bytes))
5454
{
55-
int srcLength = TBase64Decoder.SrcLength(isFinalBlock, utf8.Length);
55+
int srcLength = TBase64Decoder.SrcLength(isFinalBlock, source.Length);
5656
int destLength = bytes.Length;
5757
int maxSrcLength = srcLength;
5858
int decodedLength = TBase64Decoder.GetMaxDecodedLength(srcLength);
@@ -166,7 +166,7 @@ internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(ReadOnlySpa
166166
goto InvalidDataExit;
167167
}
168168

169-
if (src == srcBytes + utf8.Length)
169+
if (src == srcBytes + source.Length)
170170
{
171171
goto DoneExit;
172172
}
@@ -244,7 +244,7 @@ internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(ReadOnlySpa
244244
src += remaining;
245245
}
246246

247-
if (srcLength != utf8.Length)
247+
if (srcLength != source.Length)
248248
{
249249
goto InvalidDataExit;
250250
}
@@ -255,7 +255,7 @@ internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(ReadOnlySpa
255255
return OperationStatus.Done;
256256

257257
DestinationTooSmallExit:
258-
if (srcLength != utf8.Length && isFinalBlock)
258+
if (srcLength != source.Length && isFinalBlock)
259259
{
260260
goto InvalidDataExit; // if input is not a multiple of 4, and there is no more data, return invalid data instead
261261
}
@@ -273,7 +273,7 @@ internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(ReadOnlySpa
273273
bytesConsumed = (int)(src - srcBytes);
274274
bytesWritten = (int)(dest - destBytes);
275275
return ignoreWhiteSpace ?
276-
InvalidDataFallback(utf8, bytes, ref bytesConsumed, ref bytesWritten, isFinalBlock) :
276+
InvalidDataFallback(source, bytes, ref bytesConsumed, ref bytesWritten, isFinalBlock) :
277277
OperationStatus.InvalidData;
278278
}
279279

@@ -492,33 +492,33 @@ internal static unsafe OperationStatus DecodeFromUtf8InPlace<TBase64Decoder>(Spa
492492
}
493493
}
494494

495-
internal static OperationStatus DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(ReadOnlySpan<byte> utf8, Span<byte> bytes, ref int bytesConsumed, ref int bytesWritten, bool isFinalBlock = true)
495+
internal static OperationStatus DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(ReadOnlySpan<byte> source, Span<byte> bytes, ref int bytesConsumed, ref int bytesWritten, bool isFinalBlock = true)
496496
where TBase64Decoder : IBase64Decoder<byte>
497497
{
498498
const int BlockSize = 4;
499499
Span<byte> buffer = stackalloc byte[BlockSize];
500500
OperationStatus status = OperationStatus.Done;
501501

502-
while (!utf8.IsEmpty)
502+
while (!source.IsEmpty)
503503
{
504504
int encodedIdx = 0;
505505
int bufferIdx = 0;
506506
int skipped = 0;
507507

508-
for (; encodedIdx < utf8.Length && (uint)bufferIdx < (uint)buffer.Length; ++encodedIdx)
508+
for (; encodedIdx < source.Length && (uint)bufferIdx < (uint)buffer.Length; ++encodedIdx)
509509
{
510-
if (IsWhiteSpace(utf8[encodedIdx]))
510+
if (IsWhiteSpace(source[encodedIdx]))
511511
{
512512
skipped++;
513513
}
514514
else
515515
{
516-
buffer[bufferIdx] = utf8[encodedIdx];
516+
buffer[bufferIdx] = source[encodedIdx];
517517
bufferIdx++;
518518
}
519519
}
520520

521-
utf8 = utf8.Slice(encodedIdx);
521+
source = source.Slice(encodedIdx);
522522
bytesConsumed += skipped;
523523

524524
if (bufferIdx == 0)
@@ -530,11 +530,11 @@ internal static OperationStatus DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(Re
530530

531531
if (typeof(TBase64Decoder) == typeof(Base64DecoderByte) || bufferIdx == 1)
532532
{
533-
hasAnotherBlock = utf8.Length >= BlockSize && bufferIdx == BlockSize;
533+
hasAnotherBlock = source.Length >= BlockSize && bufferIdx == BlockSize;
534534
}
535535
else
536536
{
537-
hasAnotherBlock = utf8.Length > 1;
537+
hasAnotherBlock = source.Length > 1;
538538
}
539539

540540
bool localIsFinalBlock = !hasAnotherBlock;
@@ -567,9 +567,9 @@ internal static OperationStatus DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(Re
567567
// The remaining data must all be whitespace in order to be valid.
568568
if (!hasAnotherBlock)
569569
{
570-
for (int i = 0; i < utf8.Length; ++i)
570+
for (int i = 0; i < source.Length; ++i)
571571
{
572-
if (!IsWhiteSpace(utf8[i]))
572+
if (!IsWhiteSpace(source[i]))
573573
{
574574
// Revert previous dest increment, since an invalid state followed.
575575
bytesConsumed -= localConsumed;
@@ -596,32 +596,39 @@ private static int GetPaddingCount<TBase64Decoder>(ref byte ptrToLastElement)
596596
{
597597
int padding = 0;
598598

599-
if (TBase64Decoder.IsValidPadding(ptrToLastElement)) padding++;
600-
if (TBase64Decoder.IsValidPadding(Unsafe.Subtract(ref ptrToLastElement, 1))) padding++;
599+
if (TBase64Decoder.IsValidPadding(ptrToLastElement))
600+
{
601+
padding++;
602+
}
603+
604+
if (TBase64Decoder.IsValidPadding(Unsafe.Subtract(ref ptrToLastElement, 1)))
605+
{
606+
padding++;
607+
}
601608

602609
return padding;
603610
}
604611

605-
private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace<TBase64Decoder>(Span<byte> utf8, ref int destIndex, uint sourceIndex)
612+
private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace<TBase64Decoder>(Span<byte> source, ref int destIndex, uint sourceIndex)
606613
where TBase64Decoder : IBase64Decoder<byte>
607614
{
608-
int BlockSize = Math.Min(utf8.Length - (int)sourceIndex, 4);
615+
int BlockSize = Math.Min(source.Length - (int)sourceIndex, 4);
609616
Span<byte> buffer = stackalloc byte[BlockSize];
610617

611618
OperationStatus status = OperationStatus.Done;
612619
int localDestIndex = destIndex;
613620
bool hasPaddingBeenProcessed = false;
614621
int localBytesWritten = 0;
615622

616-
while (sourceIndex < (uint)utf8.Length)
623+
while (sourceIndex < (uint)source.Length)
617624
{
618625
int bufferIdx = 0;
619626

620-
while (bufferIdx < BlockSize && sourceIndex < (uint)utf8.Length)
627+
while (bufferIdx < BlockSize && sourceIndex < (uint)source.Length)
621628
{
622-
if (!IsWhiteSpace(utf8[(int)sourceIndex]))
629+
if (!IsWhiteSpace(source[(int)sourceIndex]))
623630
{
624-
buffer[bufferIdx] = utf8[(int)sourceIndex];
631+
buffer[bufferIdx] = source[(int)sourceIndex];
625632
bufferIdx++;
626633
}
627634

@@ -645,7 +652,7 @@ private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace<TBase64Decode
645652
{
646653
while (bufferIdx < BlockSize) // Can happen only for last block
647654
{
648-
Debug.Assert(utf8.Length == sourceIndex);
655+
Debug.Assert(source.Length == sourceIndex);
649656
buffer[bufferIdx++] = (byte)EncodingPad;
650657
}
651658
}
@@ -672,7 +679,7 @@ private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace<TBase64Decode
672679
// Write result to source span in place.
673680
for (int i = 0; i < localBytesWritten; i++)
674681
{
675-
utf8[localDestIndex - localBytesWritten + i] = buffer[i];
682+
source[localDestIndex - localBytesWritten + i] = buffer[i];
676683
}
677684
}
678685

‎src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,23 @@ public static partial class Base64
3737
public static unsafe OperationStatus EncodeToUtf8(ReadOnlySpan<byte> bytes, Span<byte> utf8, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true) =>
3838
EncodeTo<Base64EncoderByte, byte>(bytes, utf8, out bytesConsumed, out bytesWritten, isFinalBlock);
3939

40-
internal static unsafe OperationStatus EncodeTo<TBase64Encoder, T>(ReadOnlySpan<byte> bytes,
41-
Span<T> utf8, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true)
40+
internal static unsafe OperationStatus EncodeTo<TBase64Encoder, T>(ReadOnlySpan<byte> source,
41+
Span<T> destination, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true)
4242
where TBase64Encoder : IBase64Encoder<T>
4343
where T : unmanaged
4444
{
45-
if (bytes.IsEmpty)
45+
if (source.IsEmpty)
4646
{
4747
bytesConsumed = 0;
4848
bytesWritten = 0;
4949
return OperationStatus.Done;
5050
}
5151

52-
fixed (byte* srcBytes = &MemoryMarshal.GetReference(bytes))
53-
fixed (T* destBytes = &MemoryMarshal.GetReference(utf8))
52+
fixed (byte* srcBytes = &MemoryMarshal.GetReference(source))
53+
fixed (T* destBytes = &MemoryMarshal.GetReference(destination))
5454
{
55-
int srcLength = bytes.Length;
56-
int destLength = utf8.Length;
55+
int srcLength = source.Length;
56+
int destLength = destination.Length;
5757
int maxSrcLength = TBase64Encoder.GetMaxSrcLength(srcLength, destLength);
5858

5959
byte* src = srcBytes;

‎src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Url/Base64UrlDecoder.cs

+15-15
Original file line numberDiff line numberDiff line change
@@ -186,41 +186,41 @@ public static OperationStatus DecodeFromChars(ReadOnlySpan<char> source, Span<by
186186
out int charsConsumed, out int bytesWritten, bool isFinalBlock = true) =>
187187
DecodeFrom<Base64UrlDecoderChar, ushort>(MemoryMarshal.Cast<char, ushort>(source), destination, out charsConsumed, out bytesWritten, isFinalBlock, ignoreWhiteSpace: true);
188188

189-
private static OperationStatus DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(ReadOnlySpan<ushort> utf8, Span<byte> bytes, ref int bytesConsumed, ref int bytesWritten, bool isFinalBlock = true)
189+
private static OperationStatus DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(ReadOnlySpan<ushort> source, Span<byte> bytes, ref int bytesConsumed, ref int bytesWritten, bool isFinalBlock = true)
190190
where TBase64Decoder : IBase64Decoder<ushort>
191191
{
192192
const int BlockSize = 4;
193193
Span<ushort> buffer = stackalloc ushort[BlockSize];
194194
OperationStatus status = OperationStatus.Done;
195195

196-
while (!utf8.IsEmpty)
196+
while (!source.IsEmpty)
197197
{
198198
int encodedIdx = 0;
199199
int bufferIdx = 0;
200200
int skipped = 0;
201201

202-
for (; encodedIdx < utf8.Length && (uint)bufferIdx < (uint)buffer.Length; ++encodedIdx)
202+
for (; encodedIdx < source.Length && (uint)bufferIdx < (uint)buffer.Length; ++encodedIdx)
203203
{
204-
if (IsWhiteSpace(utf8[encodedIdx]))
204+
if (IsWhiteSpace(source[encodedIdx]))
205205
{
206206
skipped++;
207207
}
208208
else
209209
{
210-
buffer[bufferIdx] = utf8[encodedIdx];
210+
buffer[bufferIdx] = source[encodedIdx];
211211
bufferIdx++;
212212
}
213213
}
214214

215-
utf8 = utf8.Slice(encodedIdx);
215+
source = source.Slice(encodedIdx);
216216
bytesConsumed += skipped;
217217

218218
if (bufferIdx == 0)
219219
{
220220
continue;
221221
}
222222

223-
bool hasAnotherBlock = utf8.Length >= BlockSize && bufferIdx == BlockSize;
223+
bool hasAnotherBlock = source.Length >= BlockSize && bufferIdx == BlockSize;
224224
bool localIsFinalBlock = !hasAnotherBlock;
225225

226226
// If this block contains padding and there's another block, then only whitespace may follow for being valid.
@@ -251,9 +251,9 @@ private static OperationStatus DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(Rea
251251
// The remaining data must all be whitespace in order to be valid.
252252
if (!hasAnotherBlock)
253253
{
254-
for (int i = 0; i < utf8.Length; ++i)
254+
for (int i = 0; i < source.Length; ++i)
255255
{
256-
if (!IsWhiteSpace(utf8[i]))
256+
if (!IsWhiteSpace(source[i]))
257257
{
258258
// Revert previous dest increment, since an invalid state followed.
259259
bytesConsumed -= localConsumed;
@@ -454,13 +454,13 @@ public static byte[] DecodeFromChars(ReadOnlySpan<char> source)
454454

455455
public static uint AdvSimdLutTwo3Uint1 => 0x1B1AFF3F;
456456

457-
public static int GetMaxDecodedLength(int utf8Length) => Base64Url.GetMaxDecodedLength(utf8Length);
457+
public static int GetMaxDecodedLength(int sourceLength) => Base64Url.GetMaxDecodedLength(sourceLength);
458458

459459
public static bool IsInvalidLength(int bufferLength) => (bufferLength & 3) == 1; // One byte cannot be decoded completely
460460

461461
public static bool IsValidPadding(uint padChar) => padChar == EncodingPad || padChar == UrlEncodingPad;
462462

463-
public static int SrcLength(bool isFinalBlock, int utf8Length) => isFinalBlock ? utf8Length : utf8Length & ~0x3;
463+
public static int SrcLength(bool isFinalBlock, int sourceLength) => isFinalBlock ? sourceLength : sourceLength & ~0x3;
464464

465465
[MethodImpl(MethodImplOptions.AggressiveInlining)]
466466
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
@@ -596,13 +596,13 @@ public static unsafe bool TryLoadArmVector128x4(byte* src, byte* srcStart, int s
596596

597597
public static uint AdvSimdLutTwo3Uint1 => Base64UrlDecoderByte.AdvSimdLutTwo3Uint1;
598598

599-
public static int GetMaxDecodedLength(int utf8Length) => Base64UrlDecoderByte.GetMaxDecodedLength(utf8Length);
599+
public static int GetMaxDecodedLength(int sourceLength) => Base64UrlDecoderByte.GetMaxDecodedLength(sourceLength);
600600

601601
public static bool IsInvalidLength(int bufferLength) => Base64DecoderByte.IsInvalidLength(bufferLength);
602602

603603
public static bool IsValidPadding(uint padChar) => Base64UrlDecoderByte.IsValidPadding(padChar);
604604

605-
public static int SrcLength(bool isFinalBlock, int utf8Length) => Base64UrlDecoderByte.SrcLength(isFinalBlock, utf8Length);
605+
public static int SrcLength(bool isFinalBlock, int sourceLength) => Base64UrlDecoderByte.SrcLength(isFinalBlock, sourceLength);
606606

607607
[MethodImpl(MethodImplOptions.AggressiveInlining)]
608608
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
@@ -704,9 +704,9 @@ public static int IndexOfAnyExceptWhiteSpace(ReadOnlySpan<ushort> span)
704704
}
705705

706706
[MethodImpl(MethodImplOptions.AggressiveInlining)]
707-
public static OperationStatus DecodeWithWhiteSpaceBlockwiseWrapper<TBase64Decoder>(ReadOnlySpan<ushort> utf8, Span<byte> bytes,
707+
public static OperationStatus DecodeWithWhiteSpaceBlockwiseWrapper<TBase64Decoder>(ReadOnlySpan<ushort> source, Span<byte> bytes,
708708
ref int bytesConsumed, ref int bytesWritten, bool isFinalBlock = true) where TBase64Decoder : IBase64Decoder<ushort> =>
709-
DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(utf8, bytes, ref bytesConsumed, ref bytesWritten, isFinalBlock);
709+
DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(source, bytes, ref bytesConsumed, ref bytesWritten, isFinalBlock);
710710

711711
[MethodImpl(MethodImplOptions.AggressiveInlining)]
712712
public static unsafe bool TryLoadVector512(ushort* src, ushort* srcStart, int sourceLength, out Vector512<sbyte> str)

0 commit comments

Comments
 (0)
Please sign in to comment.