diff --git a/src/libraries/System.Private.CoreLib/src/System/Utf8Extensions.CoreLib.cs b/src/libraries/System.Private.CoreLib/src/System/Utf8Extensions.CoreLib.cs deleted file mode 100644 index ef0bc2105e1d3e..00000000000000 --- a/src/libraries/System.Private.CoreLib/src/System/Utf8Extensions.CoreLib.cs +++ /dev/null @@ -1,128 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.CompilerServices; - -namespace System -{ - public static partial class Utf8Extensions - { - /// Creates a new over the portion of the target . - /// The target . - /// Returns default when is null. - public static ReadOnlyMemory AsMemory(this Utf8String? text) - { - if (text is null) - return default; - - return new ReadOnlyMemory(text, 0, text.Length); - } - - /// Creates a new over the portion of the target . - /// The target . - /// The index at which to begin this slice. - /// Returns default when is null. - /// - /// Thrown when the specified index is not in range (<0 or >text.Length). - /// - public static ReadOnlyMemory AsMemory(this Utf8String? text, int start) - { - if (text is null) - { - if (start != 0) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - return default; - } - - if ((uint)start > (uint)text.Length) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - - return new ReadOnlyMemory(text, start, text.Length - start); - } - - /// Creates a new over the portion of the target . - /// The target . - /// The index at which to begin this slice. - public static ReadOnlyMemory AsMemory(this Utf8String? text, Index startIndex) - { - if (text is null) - { - if (!startIndex.Equals(Index.Start)) - ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); - - return default; - } - - int actualIndex = startIndex.GetOffset(text.Length); - if ((uint)actualIndex > (uint)text.Length) - ThrowHelper.ThrowArgumentOutOfRangeException(); - - return new ReadOnlyMemory(text, actualIndex, text.Length - actualIndex); - } - - /// Creates a new over the portion of the target . - /// The target . - /// The index at which to begin this slice. - /// The desired length for the slice (exclusive). - /// Returns default when is null. - /// - /// Thrown when the specified index or is not in range. - /// - public static ReadOnlyMemory AsMemory(this Utf8String? text, int start, int length) - { - if (text is null) - { - if (start != 0 || length != 0) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - return default; - } - -#if TARGET_64BIT - // See comment in Span.Slice for how this works. - if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)text.Length) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); -#else - if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); -#endif - - return new ReadOnlyMemory(text, start, length); - } - - /// Creates a new over the portion of the target . - /// The target . - /// The range used to indicate the start and length of the sliced string. - public static ReadOnlyMemory AsMemory(this Utf8String? text, Range range) - { - if (text is null) - { - Index startIndex = range.Start; - Index endIndex = range.End; - - if (!startIndex.Equals(Index.Start) || !endIndex.Equals(Index.Start)) - ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); - - return default; - } - - (int start, int length) = range.GetOffsetAndLength(text.Length); - return new ReadOnlyMemory(text, start, length); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static ReadOnlySpan CreateSpan(Utf8String text) => - new ReadOnlySpan(ref text.DangerousGetMutableReference(), text.Length); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static ReadOnlySpan CreateSpan(Utf8String text, int start) => - new ReadOnlySpan(ref text.DangerousGetMutableReference(start), text.Length - start); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static ReadOnlySpan CreateSpan(Utf8String text, int start, int length) => - new ReadOnlySpan(ref text.DangerousGetMutableReference(start), length); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static ReadOnlyMemory CreateMemoryBytes(Utf8String text, int start, int length) => - new ReadOnlyMemory(text, start, length); - } -} diff --git a/src/libraries/System.Private.CoreLib/src/System/Utf8Extensions.cs b/src/libraries/System.Private.CoreLib/src/System/Utf8Extensions.cs deleted file mode 100644 index 3249bb911ec985..00000000000000 --- a/src/libraries/System.Private.CoreLib/src/System/Utf8Extensions.cs +++ /dev/null @@ -1,295 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Text; -using System.Text.Unicode; - -namespace System -{ - public static partial class Utf8Extensions - { - /// - /// Projects as a . - /// - public static ReadOnlySpan AsBytes(this ReadOnlySpan text) - { - return MemoryMarshal.Cast(text); - } - - /// - /// Creates a new readonly span over the portion of the target . - /// - /// The target . - /// Returns default when is null. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ReadOnlySpan AsBytes(this Utf8String? text) - { - if (text is null) - return default; - - return CreateSpan(text); - } - - /// - /// Creates a new readonly span over the portion of the target . - /// - /// The target . - /// The index at which to begin this slice. - /// Thrown when is null. - /// - /// Thrown when the specified index is not in range (<0 or >text.Length). - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ReadOnlySpan AsBytes(this Utf8String? text, int start) - { - if (text is null) - { - if (start != 0) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - return default; - } - - if ((uint)start > (uint)text.Length) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - - return CreateSpan(text, start); - } - - /// - /// Creates a new readonly span over the portion of the target . - /// - /// The target . - /// The index at which to begin this slice. - /// The desired length for the slice (exclusive). - /// Returns default when is null. - /// - /// Thrown when the specified index or is not in range. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ReadOnlySpan AsBytes(this Utf8String? text, int start, int length) - { - if (text is null) - { - if (start != 0 || length != 0) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - return default; - } - -#if TARGET_64BIT - // See comment in Span.Slice for how this works. - if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)text.Length) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); -#else - if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); -#endif - - return CreateSpan(text, start, length); - } - - /// - /// Creates a new over the target . - /// - /// The target . - /// Returns default when is null. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Utf8Span AsSpan(this Utf8String? text) - { - if (text is null) - return default; - - return new Utf8Span(text); - } - - /// - /// Creates a new over the portion of the target . - /// - /// The target . - /// The index at which to begin this slice. - /// Thrown when is null. - /// - /// Thrown when the specified index is not in range (<0 or >text.Length). - /// - /// - /// Thrown if the resulting span would split a multi-byte UTF-8 subsequence. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Utf8Span AsSpan(this Utf8String? text, int start) - { - if (text is null) - { - if (start != 0) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - return default; - } - - if ((uint)start > (uint)text.Length) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - - // It's always safe for us to read just past the end of the string (since there's a null terminator), - // so we don't need to perform any additional bounds checking. We only need to check that we're not - // splitting in the middle of a multi-byte UTF-8 subsequence. - - if (Utf8Utility.IsUtf8ContinuationByte(text.DangerousGetMutableReference(start))) - { - Utf8String.ThrowImproperStringSplit(); - } - - return Utf8Span.UnsafeCreateWithoutValidation(CreateSpan(text, start)); - } - - /// - /// Creates a new over the portion of the target . - /// - /// The target . - /// The index at which to begin this slice. - /// The desired length for the slice (exclusive). - /// Returns default when is null. - /// - /// Thrown when the specified index or is not in range. - /// - /// - /// Thrown if the resulting span would split a multi-byte UTF-8 subsequence. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Utf8Span AsSpan(this Utf8String? text, int start, int length) - { - if (text is null) - { - if (start != 0 || length != 0) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - return default; - } - -#if TARGET_64BIT - // See comment in Span.Slice for how this works. - if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)text.Length) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); -#else - if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); -#endif - - // It's always safe for us to read just past the end of the string (since there's a null terminator), - // so we don't need to perform any additional bounds checking. We only need to check that we're not - // splitting in the middle of a multi-byte UTF-8 subsequence. - - if (Utf8Utility.IsUtf8ContinuationByte(text.DangerousGetMutableReference(start)) - || Utf8Utility.IsUtf8ContinuationByte(text.DangerousGetMutableReference(start + length))) - { - Utf8String.ThrowImproperStringSplit(); - } - - return Utf8Span.UnsafeCreateWithoutValidation(CreateSpan(text, start, length)); - } - - /// Creates a new over the portion of the target . - /// The target . - /// Returns default when is null. - public static ReadOnlyMemory AsMemoryBytes(this Utf8String? text) - { - if (text is null) - return default; - - return CreateMemoryBytes(text, 0, text.Length); - } - - /// Creates a new over the portion of the target . - /// The target . - /// The index at which to begin this slice. - /// Returns default when is null. - /// - /// Thrown when the specified index is not in range (<0 or >text.Length). - /// - public static ReadOnlyMemory AsMemoryBytes(this Utf8String? text, int start) - { - if (text is null) - { - if (start != 0) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - return default; - } - - if ((uint)start > (uint)text.Length) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - - return CreateMemoryBytes(text, start, text.Length - start); - } - - /// Creates a new over the portion of the target . - /// The target . - /// The index at which to begin this slice. - public static ReadOnlyMemory AsMemoryBytes(this Utf8String? text, Index startIndex) - { - if (text is null) - { - if (!startIndex.Equals(Index.Start)) - ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); - - return default; - } - - int actualIndex = startIndex.GetOffset(text.Length); - if ((uint)actualIndex > (uint)text.Length) - ThrowHelper.ThrowArgumentOutOfRangeException(); - - return CreateMemoryBytes(text, actualIndex, text.Length - actualIndex); - } - - /// Creates a new over the portion of the target . - /// The target . - /// The index at which to begin this slice. - /// The desired length for the slice (exclusive). - /// Returns default when is null. - /// - /// Thrown when the specified index or is not in range. - /// - public static ReadOnlyMemory AsMemoryBytes(this Utf8String? text, int start, int length) - { - if (text is null) - { - if (start != 0 || length != 0) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - return default; - } - -#if TARGET_64BIT - // See comment in Span.Slice for how this works. - if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)text.Length) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); -#else - if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); -#endif - - return CreateMemoryBytes(text, start, length); - } - - /// Creates a new over the portion of the target . - /// The target . - /// The range used to indicate the start and length of the sliced string. - public static ReadOnlyMemory AsMemoryBytes(this Utf8String? text, Range range) - { - if (text is null) - { - Index startIndex = range.Start; - Index endIndex = range.End; - - if (!startIndex.Equals(Index.Start) || !endIndex.Equals(Index.Start)) - ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); - - return default; - } - - (int start, int length) = range.GetOffsetAndLength(text.Length); - return CreateMemoryBytes(text, start, length); - } - - /// - /// Returns a representation of this instance. - /// - public static Utf8String ToUtf8String(this Rune rune) => Utf8String.CreateFromRune(rune); - } -}