Skip to content
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

Nullable improvements for Microsoft.Extensions.Primitives.StringSegment №2 #58308

Merged
merged 5 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -54,22 +54,26 @@ public partial interface IChangeToken
public System.ReadOnlyMemory<char> AsMemory() { throw null; }
public System.ReadOnlySpan<char> AsSpan() { throw null; }
public System.ReadOnlySpan<char> AsSpan(int start) { throw null; }
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(Buffer))]
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(Value))]
public System.ReadOnlySpan<char> AsSpan(int start, int length) { throw null; }
public static int Compare(Microsoft.Extensions.Primitives.StringSegment a, Microsoft.Extensions.Primitives.StringSegment b, System.StringComparison comparisonType) { throw null; }
public bool EndsWith(string text, System.StringComparison comparisonType) { throw null; }
public bool Equals(Microsoft.Extensions.Primitives.StringSegment other) { throw null; }
public static bool Equals(Microsoft.Extensions.Primitives.StringSegment a, Microsoft.Extensions.Primitives.StringSegment b, System.StringComparison comparisonType) { throw null; }
public bool Equals(Microsoft.Extensions.Primitives.StringSegment other, System.StringComparison comparisonType) { throw null; }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? obj) { throw null; }
public bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? text) { throw null; }
public bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? text, System.StringComparison comparisonType) { throw null; }
public bool Equals(string? text) { throw null; }
public bool Equals(string? text, System.StringComparison comparisonType) { throw null; }
public override int GetHashCode() { throw null; }
public int IndexOf(char c) { throw null; }
public int IndexOf(char c, int start) { throw null; }
public int IndexOf(char c, int start, int count) { throw null; }
public int IndexOfAny(char[] anyOf) { throw null; }
public int IndexOfAny(char[] anyOf, int startIndex) { throw null; }
public int IndexOfAny(char[] anyOf, int startIndex, int count) { throw null; }
[System.Diagnostics.CodeAnalysis.MemberNotNullWhen(false, nameof(Buffer))]
[System.Diagnostics.CodeAnalysis.MemberNotNullWhen(false, nameof(Value))]
public static bool IsNullOrEmpty(Microsoft.Extensions.Primitives.StringSegment value) { throw null; }
public int LastIndexOf(char value) { throw null; }
public static bool operator ==(Microsoft.Extensions.Primitives.StringSegment left, Microsoft.Extensions.Primitives.StringSegment right) { throw null; }
Expand All @@ -80,8 +84,12 @@ public partial interface IChangeToken
public Microsoft.Extensions.Primitives.StringTokenizer Split(char[] chars) { throw null; }
public bool StartsWith(string text, System.StringComparison comparisonType) { throw null; }
public Microsoft.Extensions.Primitives.StringSegment Subsegment(int offset) { throw null; }
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(Buffer))]
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(Value))]
public Microsoft.Extensions.Primitives.StringSegment Subsegment(int offset, int length) { throw null; }
public string Substring(int offset) { throw null; }
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(Buffer))]
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(Value))]
public string Substring(int offset, int length) { throw null; }
public override string ToString() { throw null; }
public Microsoft.Extensions.Primitives.StringSegment Trim() { throw null; }
Expand Down
16 changes: 13 additions & 3 deletions src/libraries/Microsoft.Extensions.Primitives/src/StringSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public char this[int index]
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="start"/> is greater than or equal to <see cref="Length"/> or less than zero.
/// </exception>
[MemberNotNull(nameof(Buffer))]
[MemberNotNull(nameof(Value))]
public ReadOnlySpan<char> AsSpan(int start)
{
if (!HasValue || start < 0)
Expand All @@ -147,6 +149,8 @@ public ReadOnlySpan<char> AsSpan(int start)
/// <paramref name="start"/> or <paramref name="length"/> is less than zero, or <paramref name="start"/> + <paramref name="length"/> is
/// greater than <see cref="Length"/>.
/// </exception>
[MemberNotNull(nameof(Buffer))]
[MemberNotNull(nameof(Value))]
public ReadOnlySpan<char> AsSpan(int start, int length)
{
if (!HasValue || start < 0 || length < 0 || (uint)(start + length) > (uint)Length)
Expand Down Expand Up @@ -241,7 +245,7 @@ public bool Equals(StringSegment other, StringComparison comparisonType)
/// </summary>
/// <param name="text">The <see cref="string"/> to compare with the current <see cref="StringSegment"/>.</param>
/// <returns><see langword="true" /> if the specified <see cref="string"/> is equal to the current <see cref="StringSegment"/>; otherwise, <see langword="false" />.</returns>
public bool Equals([NotNullWhen(true)] string? text) => Equals(text, StringComparison.Ordinal);
public bool Equals(string? text) => Equals(text, StringComparison.Ordinal);

/// <summary>
/// Checks if the specified <see cref="string"/> is equal to the current <see cref="StringSegment"/>.
Expand All @@ -250,12 +254,12 @@ public bool Equals(StringSegment other, StringComparison comparisonType)
/// <param name="comparisonType">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns><see langword="true" /> if the specified <see cref="string"/> is equal to the current <see cref="StringSegment"/>; otherwise, <see langword="false" />.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals([NotNullWhen(true)] string? text, StringComparison comparisonType)
public bool Equals(string? text, StringComparison comparisonType)
{
if (!HasValue || text == null)
{
CheckStringComparison(comparisonType); // must arg check before returning
return text == Buffer; // return true if both are null
return text == Buffer; // only return true if both are null
}

return AsSpan().Equals(text.AsSpan(), comparisonType);
Expand Down Expand Up @@ -393,6 +397,8 @@ public bool EndsWith(string text, StringComparison comparisonType)
/// <paramref name="offset"/> or <paramref name="length"/> is less than zero, or <paramref name="offset"/> + <paramref name="length"/> is
/// greater than <see cref="Length"/>.
/// </exception>
[MemberNotNull(nameof(Buffer))]
[MemberNotNull(nameof(Value))]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Substring(int offset, int length)
{
Expand Down Expand Up @@ -427,6 +433,8 @@ public string Substring(int offset, int length)
/// <paramref name="offset"/> or <paramref name="length"/> is less than zero, or <paramref name="offset"/> + <paramref name="length"/> is
/// greater than <see cref="Length"/>.
/// </exception>
[MemberNotNull(nameof(Buffer))]
[MemberNotNull(nameof(Value))]
public StringSegment Subsegment(int offset, int length)
{
if (!HasValue || offset < 0 || length < 0 || (uint)(offset + length) > (uint)Length)
Expand Down Expand Up @@ -640,6 +648,8 @@ public StringTokenizer Split(char[] chars)
/// </summary>
/// <param name="value">The <see cref="StringSegment"/> to test.</param>
/// <returns></returns>
[MemberNotNullWhen(false, nameof(Buffer))]
[MemberNotNullWhen(false, nameof(Value))]
public static bool IsNullOrEmpty(StringSegment value)
{
bool res = false;
Expand Down