Skip to content

Commit

Permalink
Remove StringBuilder use
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed May 20, 2024
1 parent 70857b2 commit fc79990
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
16 changes: 0 additions & 16 deletions src/libraries/Fuzzing/DotnetFuzzing/Assert.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;

namespace DotnetFuzzing;

internal static class Assert
Expand Down Expand Up @@ -36,18 +34,4 @@ static void Throw(ReadOnlySpan<T> expected, ReadOnlySpan<T> actual)
throw new Exception($"Expected={expected[diffIndex]} Actual={actual[diffIndex]} at index {diffIndex}");
}
}

public static void SequenceEqual(ReadOnlySpan<char> expected, StringBuilder actual)
{
Equal(expected.Length, actual.Length);

foreach (ReadOnlyMemory<char> chunk in actual.GetChunks())
{
SequenceEqual(expected.Slice(0, chunk.Length), chunk.Span);

expected = expected.Slice(chunk.Length);
}

Equal(0, expected.Length);
}
}
10 changes: 5 additions & 5 deletions src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/UTF8Fuzzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class UTF8Fuzzer : IFuzzer
private static readonly Encoding s_encodingWithCustomReplacement =
Encoding.GetEncoding("utf-8", EncoderFallback.ExceptionFallback, new DecoderReplacementFallback("{BAD}"));

private static readonly StringBuilder s_replacementStringBuilder = new(4096);
private static readonly ArrayBufferWriter<char> s_replacementBufferWriter = new(4096);

public void FuzzTarget(ReadOnlySpan<byte> bytes)
{
Expand Down Expand Up @@ -91,12 +91,12 @@ private static void Test(ReadOnlySpan<byte> input)

using var decodedChars = PooledBoundedMemory<char>.Rent(decoded.Length, PoisonPagePlacement.After);

StringBuilder builder = s_replacementStringBuilder.Clear();
s_replacementBufferWriter.ResetWrittenCount();

while (!utf8.IsEmpty)
{
OperationStatus opStatus = Utf8.ToUtf16(utf8, decodedChars.Span, out int bytesReadJustNow, out int charsWrittenJustNow, replaceInvalidSequences: false, isFinalBlock: true);
builder.Append(decodedChars.Span.Slice(0, charsWrittenJustNow));
s_replacementBufferWriter.Write(decodedChars.Span.Slice(0, charsWrittenJustNow));

utf8 = utf8.Slice(bytesReadJustNow);

Expand All @@ -108,11 +108,11 @@ private static void Test(ReadOnlySpan<byte> input)
Rune.DecodeFromUtf8(utf8, out _, out int bytesToSkip);
utf8 = utf8.Slice(bytesToSkip);

builder.Append("{BAD}");
s_replacementBufferWriter.Write("{BAD}");
}
}

Assert.SequenceEqual(decoded, builder);
Assert.SequenceEqual(decoded, s_replacementBufferWriter.WrittenSpan);
}
}

Expand Down

0 comments on commit fc79990

Please sign in to comment.