Added Utf8.IsValid(bytes) (#88004)
authorAlexander Radchenko <radchenkosasha@gmail.com>
Wed, 28 Jun 2023 20:58:51 +0000 (02:58 +0600)
committerGitHub <noreply@github.com>
Wed, 28 Jun 2023 20:58:51 +0000 (16:58 -0400)
* Added Utf8.IsValid(bytes)

* Code review

* Delete System.Runtime.sln

* solution file

* Solution file

* Code review: Removed duplicate tests

src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8.cs
src/libraries/System.Runtime/ref/System.Runtime.cs
src/libraries/System.Runtime/tests/System/Text/Unicode/Utf8UtilityTests.ValidateBytes.cs

index e263bbc..a9c2476 100644 (file)
@@ -702,5 +702,13 @@ namespace System.Text.Unicode
                 return false;
             }
         }
+
+        /// <summary>
+        /// Validates that the value is well-formed UTF-8.
+        /// </summary>
+        /// <param name="value">The <see cref="ReadOnlySpan{T}"/> string.</param>
+        /// <returns><c>true</c> if value is well-formed UTF-8, <c>false</c> otherwise.</returns>
+        public static unsafe bool IsValid(ReadOnlySpan<byte> value) =>
+            Utf8Utility.GetIndexOfFirstInvalidUtf8Sequence(value, out _) < 0;
     }
 }
index e5421cf..897ba76 100644 (file)
@@ -14787,6 +14787,7 @@ namespace System.Text.Unicode
         public static System.Buffers.OperationStatus ToUtf16(System.ReadOnlySpan<byte> source, System.Span<char> destination, out int bytesRead, out int charsWritten, bool replaceInvalidSequences = true, bool isFinalBlock = true) { throw null; }
         public static bool TryWrite(System.Span<byte> destination, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute("destination")] ref System.Text.Unicode.Utf8.TryWriteInterpolatedStringHandler handler, out int bytesWritten) { throw null; }
         public static bool TryWrite(System.Span<byte> 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<byte> value) { throw null; }
         [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
         [System.Runtime.CompilerServices.InterpolatedStringHandlerAttribute]
         public ref struct TryWriteInterpolatedStringHandler
index db70182..4730337 100644 (file)
@@ -390,6 +390,8 @@ namespace System.Text.Unicode.Tests
             Assert.Equal(expectedRetVal, actualRetVal);
             Assert.Equal(expectedRuneCount, actualRuneCount);
             Assert.Equal(expectedSurrogatePairCount, actualSurrogatePairCount);
+
+            Assert.True(Utf8.IsValid(boundedMemory.Span) == (expectedRetVal < 0));
         }
 
         private static Lazy<GetPointerToFirstInvalidByteDel> CreateGetPointerToFirstInvalidByteFn()