Skip to content

WIP: Add private {ReadOnly}Span ctor that takes a ref, index and length #70240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static ReadOnlySpan<char> AsSpan(this string? text, int start)
if ((uint)start > (uint)text.Length)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);

return new ReadOnlySpan<char>(ref Unsafe.Add(ref text.GetRawStringData(), (nint)(uint)start /* force zero-extension */), text.Length - start);
return new ReadOnlySpan<char>(ref text.GetRawStringData(), start, text.Length - start);
}

/// <summary>
Expand Down Expand Up @@ -152,7 +152,7 @@ public static ReadOnlySpan<char> AsSpan(this string? text, int start, int length
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
#endif

return new ReadOnlySpan<char>(ref Unsafe.Add(ref text.GetRawStringData(), (nint)(uint)start /* force zero-extension */), length);
return new ReadOnlySpan<char>(ref text.GetRawStringData(), start, length);
}

/// <summary>Creates a new <see cref="ReadOnlyMemory{T}"/> over the portion of the target string.</summary>
Expand Down
16 changes: 14 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ internal ReadOnlySpan(ref T reference, int length)
_length = length;
}

// Constructor for internal use only.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ReadOnlySpan(ref T reference, int start, int length)
{
Debug.Assert(start >= 0);
Debug.Assert(length >= 0);
Debug.Assert(length - start >= 0);

_reference = new ByReference<T>(ref Unsafe.Add(ref reference, (nint)(uint)start /* force zero-extension */));
_length = length;
}

/// <summary>
/// Returns the specified element of the read-only span.
/// </summary>
Expand Down Expand Up @@ -345,7 +357,7 @@ public ReadOnlySpan<T> Slice(int start)
if ((uint)start > (uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();

return new ReadOnlySpan<T>(ref Unsafe.Add(ref _reference.Value, (nint)(uint)start /* force zero-extension */), _length - start);
return new ReadOnlySpan<T>(ref _reference.Value, start, _length - start);
}

/// <summary>
Expand All @@ -368,7 +380,7 @@ public ReadOnlySpan<T> Slice(int start, int length)
ThrowHelper.ThrowArgumentOutOfRangeException();
#endif

return new ReadOnlySpan<T>(ref Unsafe.Add(ref _reference.Value, (nint)(uint)start /* force zero-extension */), length);
return new ReadOnlySpan<T>(ref _reference.Value, start, length);
}

/// <summary>
Expand Down
16 changes: 14 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ internal Span(ref T reference, int length)
_length = length;
}

// Constructor for internal use only.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Span(ref T reference, int start, int length)
{
Debug.Assert(start >= 0);
Debug.Assert(length >= 0);
Debug.Assert(length - start >= 0);

_reference = new ByReference<T>(ref Unsafe.Add(ref reference, (nint)(uint)start /* force zero-extension */));
_length = length;
}

/// <summary>
/// Returns a reference to specified element of the Span.
/// </summary>
Expand Down Expand Up @@ -392,7 +404,7 @@ public Span<T> Slice(int start)
if ((uint)start > (uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();

return new Span<T>(ref Unsafe.Add(ref _reference.Value, (nint)(uint)start /* force zero-extension */), _length - start);
return new Span<T>(ref _reference.Value, start, _length - start);
}

/// <summary>
Expand Down Expand Up @@ -420,7 +432,7 @@ public Span<T> Slice(int start, int length)
ThrowHelper.ThrowArgumentOutOfRangeException();
#endif

return new Span<T>(ref Unsafe.Add(ref _reference.Value, (nint)(uint)start /* force zero-extension */), length);
return new Span<T>(ref _reference.Value, start, length);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public int IndexOfAny(char[] anyOf, int startIndex, int count)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
}

int result = new ReadOnlySpan<char>(ref Unsafe.Add(ref _firstChar, startIndex), count).IndexOfAny(anyOf);
int result = new ReadOnlySpan<char>(ref _firstChar, startIndex, count).IndexOfAny(anyOf);

return result < 0 ? result : result + startIndex;
}
Expand Down Expand Up @@ -337,7 +337,7 @@ public unsafe int LastIndexOfAny(char[] anyOf, int startIndex, int count)
}

int startSearchAt = startIndex + 1 - count;
int result = new ReadOnlySpan<char>(ref Unsafe.Add(ref _firstChar, startSearchAt), count).LastIndexOfAny(anyOf);
int result = new ReadOnlySpan<char>(ref _firstChar, startSearchAt, count).LastIndexOfAny(anyOf);

return result < 0 ? result : result + startSearchAt;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ internal bool TryGetSpan(int startIndex, int count, out ReadOnlySpan<char> slice
}
#endif

slice = new ReadOnlySpan<char>(ref Unsafe.Add(ref _firstChar, (nint)(uint)startIndex /* force zero-extension */), count);
slice = new ReadOnlySpan<char>(ref _firstChar, startIndex, count);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ private void AppendWithExpansion(ref char value, int valueCount)
Debug.Assert(m_ChunkLength == 0, "A new block was not created.");

// Copy the second chunk
new ReadOnlySpan<char>(ref Unsafe.Add(ref value, firstLength), restLength).CopyTo(m_ChunkChars);
new ReadOnlySpan<char>(ref value, firstLength, restLength).CopyTo(m_ChunkChars);
m_ChunkLength = restLength;

AssertInvariants();
Expand Down