Repository: dotnet/runtime
File: src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64DecoderHelper.cs (C#)

Code before:
                        return decoder.DecodeWithWhiteSpaceBlockwiseWrapper(decoder, source, bytes, ref bytesConsumed, ref bytesWritten, isFinalBlock);
                    }

                    // Skip over the starting whitespace and continue.
                    bytesConsumed += localConsumed;
                    source = source.Slice(localConsumed);

                    // Try again after consumed whitespace
                    status = DecodeFrom(decoder, source, bytes, out localConsumed, out int localWritten, isFinalBlock, ignoreWhiteSpace: false);
                    bytesConsumed += localConsumed;
                    bytesWritten += localWritten;

                    if (status is OperationStatus.Done or OperationStatus.NeedMoreData)
                    {
                        break;
                    }

                    // The DecodeFrom helper will return DestinationTooSmall if the destination is too small,
                    // regardless of whether it's actually too small once you skip whitespace characters.
                    // As long as we're making progress, try to trim whitespace characters again and continue.
                    if (status == OperationStatus.DestinationTooSmall && localConsumed == 0)
                    {
                        // If the input contains whitespace in the middle of the next Base64 block AND the destination is small,
                        // the DecodeFrom helper may be unable to make forward progress. Fall back to block-wise decoding.
                        return decoder.DecodeWithWhiteSpaceBlockwiseWrapper(decoder, source, bytes, ref bytesConsumed, ref bytesWritten, isFinalBlock);
                    }

                    source = source.Slice(localConsumed);
                    bytes = bytes.Slice(localWritten);
                }
                while (!source.IsEmpty);

                return status;
            }
        }

        internal static unsafe OperationStatus DecodeFromUtf8InPlace<TBase64Decoder>(TBase64Decoder decoder, Span<byte> buffer, out int bytesWritten, bool ignoreWhiteSpace)
            where TBase64Decoder : IBase64Decoder<byte>
        {
            if (buffer.IsEmpty)
            {
                bytesWritten = 0;
                return OperationStatus.Done;
            }

            fixed (byte* bufferBytes = &MemoryMarshal.GetReference(buffer))
            {
                uint bufferLength = (uint)buffer.Length;
                uint sourceIndex = 0;
                uint destIndex = 0;


Review comment (bug):
I'm missing why this block is necessary. If you remove this block, then we'll loop back around, IndexOfAnyExceptWhiteSpace will return 0, and we'll do the earlier return decoder.DecodeWithXx, just as is being done here. Am I reading it incorrectly, or is this an attempt at an optimization?

Code after:
                        return decoder.DecodeWithWhiteSpaceBlockwiseWrapper(decoder, source, bytes, ref bytesConsumed, ref bytesWritten, isFinalBlock);
                    }

                    // Skip over the starting whitespace and continue.
                    bytesConsumed += localConsumed;
                    source = source.Slice(localConsumed);

                    // Try again after consumed whitespace
                    status = DecodeFrom(decoder, source, bytes, out localConsumed, out int localWritten, isFinalBlock, ignoreWhiteSpace: false);
                    bytesConsumed += localConsumed;
                    bytesWritten += localWritten;

                    if (status is OperationStatus.Done or OperationStatus.NeedMoreData)
                    {
                        break;
                    }

                    // The DecodeFrom helper will return DestinationTooSmall if the destination is too small,
                    // regardless of whether it's actually too small once you skip whitespace characters.
                    // In that case we loop again and fall back to block-wise decoding if we can't make progress.

                    source = source.Slice(localConsumed);
                    bytes = bytes.Slice(localWritten);
                }
                while (!source.IsEmpty);

                return status;
            }
        }

        internal static unsafe OperationStatus DecodeFromUtf8InPlace<TBase64Decoder>(TBase64Decoder decoder, Span<byte> buffer, out int bytesWritten, bool ignoreWhiteSpace)
            where TBase64Decoder : IBase64Decoder<byte>
        {
            if (buffer.IsEmpty)
            {
                bytesWritten = 0;
                return OperationStatus.Done;
            }

            fixed (byte* bufferBytes = &MemoryMarshal.GetReference(buffer))
            {
                uint bufferLength = (uint)buffer.Length;
                uint sourceIndex = 0;
                uint destIndex = 0;

                if (decoder.IsInvalidLength(buffer.Length))
                {
                    goto InvalidExit;
                }

                ref sbyte decodingMap = ref MemoryMarshal.GetReference(decoder.DecodingMap);