From 336e9d6974afaa02e8802708e0fd57372ae4d822 Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Thu, 29 Jun 2023 02:58:51 +0600 Subject: [PATCH] Added Utf8.IsValid(bytes) (#88004) * Added Utf8.IsValid(bytes) * Code review * Delete System.Runtime.sln * solution file * Solution file * Code review: Removed duplicate tests --- .../src/System/Text/Unicode/Utf8.cs | 8 ++++++++ src/libraries/System.Runtime/ref/System.Runtime.cs | 1 + .../System/Text/Unicode/Utf8UtilityTests.ValidateBytes.cs | 2 ++ 3 files changed, 11 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8.cs index e263bbc49419e..a9c247630504c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8.cs @@ -702,5 +702,13 @@ private bool Fail() return false; } } + + /// + /// Validates that the value is well-formed UTF-8. + /// + /// The string. + /// true if value is well-formed UTF-8, false otherwise. + public static unsafe bool IsValid(ReadOnlySpan value) => + Utf8Utility.GetIndexOfFirstInvalidUtf8Sequence(value, out _) < 0; } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index e5421cf35e93a..897ba765d52ed 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -14787,6 +14787,7 @@ public static partial class Utf8 public static System.Buffers.OperationStatus ToUtf16(System.ReadOnlySpan source, System.Span destination, out int bytesRead, out int charsWritten, bool replaceInvalidSequences = true, bool isFinalBlock = true) { throw null; } public static bool TryWrite(System.Span destination, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute("destination")] ref System.Text.Unicode.Utf8.TryWriteInterpolatedStringHandler handler, out int bytesWritten) { throw null; } public static bool TryWrite(System.Span destination, IFormatProvider? provider, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute("destination", "provider")] ref System.Text.Unicode.Utf8.TryWriteInterpolatedStringHandler handler, out int bytesWritten) { throw null; } + public static bool IsValid(System.ReadOnlySpan value) { throw null; } [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] [System.Runtime.CompilerServices.InterpolatedStringHandlerAttribute] public ref struct TryWriteInterpolatedStringHandler diff --git a/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf8UtilityTests.ValidateBytes.cs b/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf8UtilityTests.ValidateBytes.cs index db701828ab657..4730337b0878e 100644 --- a/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf8UtilityTests.ValidateBytes.cs +++ b/src/libraries/System.Runtime/tests/System/Text/Unicode/Utf8UtilityTests.ValidateBytes.cs @@ -390,6 +390,8 @@ private static unsafe void GetIndexOfFirstInvalidUtf8Sequence_Test_Core(byte[] i Assert.Equal(expectedRetVal, actualRetVal); Assert.Equal(expectedRuneCount, actualRuneCount); Assert.Equal(expectedSurrogatePairCount, actualSurrogatePairCount); + + Assert.True(Utf8.IsValid(boundedMemory.Span) == (expectedRetVal < 0)); } private static Lazy CreateGetPointerToFirstInvalidByteFn()