Skip to content

Commit

Permalink
Added Utf8.IsValid(bytes) (#88004)
Browse files Browse the repository at this point in the history
* Added Utf8.IsValid(bytes)

* Code review

* Delete System.Runtime.sln

* solution file

* Solution file

* Code review: Removed duplicate tests
  • Loading branch information
AlexRadch committed Jun 28, 2023
1 parent 52bc96d commit 336e9d6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -702,5 +702,13 @@ private bool Fail()
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;
}
}
1 change: 1 addition & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14787,6 +14787,7 @@ public static partial class Utf8
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetPointerToFirstInvalidByteDel> CreateGetPointerToFirstInvalidByteFn()
Expand Down

0 comments on commit 336e9d6

Please sign in to comment.