From fc7999073f8fff21c779d8f356c92c22481338f8 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Mon, 20 May 2024 23:23:25 +0200 Subject: [PATCH] Remove StringBuilder use --- src/libraries/Fuzzing/DotnetFuzzing/Assert.cs | 16 ---------------- .../Fuzzing/DotnetFuzzing/Fuzzers/UTF8Fuzzer.cs | 10 +++++----- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/src/libraries/Fuzzing/DotnetFuzzing/Assert.cs b/src/libraries/Fuzzing/DotnetFuzzing/Assert.cs index cdb0324eafcb3..810174ccbc131 100644 --- a/src/libraries/Fuzzing/DotnetFuzzing/Assert.cs +++ b/src/libraries/Fuzzing/DotnetFuzzing/Assert.cs @@ -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 @@ -36,18 +34,4 @@ static void Throw(ReadOnlySpan expected, ReadOnlySpan actual) throw new Exception($"Expected={expected[diffIndex]} Actual={actual[diffIndex]} at index {diffIndex}"); } } - - public static void SequenceEqual(ReadOnlySpan expected, StringBuilder actual) - { - Equal(expected.Length, actual.Length); - - foreach (ReadOnlyMemory chunk in actual.GetChunks()) - { - SequenceEqual(expected.Slice(0, chunk.Length), chunk.Span); - - expected = expected.Slice(chunk.Length); - } - - Equal(0, expected.Length); - } } diff --git a/src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/UTF8Fuzzer.cs b/src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/UTF8Fuzzer.cs index 4b35f23ae69bf..b42adc6a70e42 100644 --- a/src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/UTF8Fuzzer.cs +++ b/src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/UTF8Fuzzer.cs @@ -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 s_replacementBufferWriter = new(4096); public void FuzzTarget(ReadOnlySpan bytes) { @@ -91,12 +91,12 @@ private static void Test(ReadOnlySpan input) using var decodedChars = PooledBoundedMemory.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); @@ -108,11 +108,11 @@ private static void Test(ReadOnlySpan 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); } }