Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 79 additions & 3 deletions src/mscorlib/shared/System/Span.NonGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,51 @@ namespace System
/// </summary>
public static class Span
{
/// <summary>Creates a new <see cref="ReadOnlyMemory{T}"/> over the portion of the target string.</summary>
/// <summary>Creates a new <see cref="ReadOnlyMemory{char}"/> over the portion of the target string.</summary>
/// <param name="text">The target string.</param>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null reference (Nothing in Visual Basic).</exception>
public static ReadOnlyMemory<char> AsReadOnlyMemory(this string text)
{
if (text == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text);
}

return new ReadOnlyMemory<char>(text, 0, text.Length);
}

/// <summary>Creates a new <see cref="ReadOnlyMemory{char}"/> over the portion of the target string.</summary>
/// <param name="text">The target string.</param>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown when the specified <paramref name="start"/> index is not in range (&lt;0 or &gt;text.Length).
/// </exception>
public static ReadOnlyMemory<char> AsReadOnlyMemory(this string text, int start)
{
if (text == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text);

if ((uint)start > (uint)text.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();

return new ReadOnlyMemory<char>(text, start, text.Length - start);
}

/// <summary>Creates a new <see cref="ReadOnlyMemory{char}"/> over the portion of the target string.</summary>
/// <param name="text">The target string.</param>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown when the specified <paramref name="start"/> index or <paramref name="length"/> is not in range.
/// </exception>
public static ReadOnlyMemory<char> AsReadOnlyMemory(this string text, int start, int length)
{
if (text == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text);

if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();

return new ReadOnlyMemory<char>(text, start, length);
}

/// <summary>Attempts to get the underlying <see cref="string"/> from a <see cref="ReadOnlyMemory{T}"/>.</summary>
/// <param name="readOnlyMemory">The memory that may be wrapping a <see cref="string"/> object.</param>
/// <param name="text">The string.</param>
Expand Down Expand Up @@ -166,6 +198,50 @@ public static ReadOnlySpan<char> AsReadOnlySpan(this string text)
return new ReadOnlySpan<char>(ref text.GetRawStringData(), text.Length);
}

/// <summary>
/// Creates a new readonly span over the portion of the target string.
/// </summary>
/// <param name="text">The target string.</param>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null
/// reference (Nothing in Visual Basic).
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown when the specified <paramref name="start"/> index is not in range (&lt;0 or &gt;text.Length).
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<char> AsReadOnlySpan(this string text, int start)
{
if (text == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text);

if ((uint)start > (uint)text.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();

return new ReadOnlySpan<char>(ref Unsafe.Add(ref text.GetRawStringData(), start), text.Length - start);
}

/// <summary>
/// Creates a new readonly span over the portion of the target string.
/// </summary>
/// <param name="text">The target string.</param>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null
/// reference (Nothing in Visual Basic).
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown when the specified <paramref name="start"/> index or <paramref name="length"/> is not in range.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<char> AsReadOnlySpan(this string text, int start, int length)
{
if (text == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text);

if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();

return new ReadOnlySpan<char>(ref Unsafe.Add(ref text.GetRawStringData(), start), length);
}

internal static unsafe void CopyTo<T>(ref T destination, ref T source, int elementsCount)
{
if (Unsafe.AreSame(ref destination, ref source))
Expand Down