Skip to content

StringSegment.EndsWith inconsistent (buggy?) behavior #39140

Closed

Description

@adams85

Description

Console.WriteLine("/".StartsWith("/", StringComparison.Ordinal));
// prints True

Console.WriteLine(new StringSegment("/").StartsWith("/", StringComparison.Ordinal));
// prints True

Console.WriteLine("/".EndsWith("/", StringComparison.Ordinal));
// prints True

Console.WriteLine(new StringSegment("/").EndsWith("/", StringComparison.Ordinal));
// prints False !!!

Is this by design or simply a bug?

Configuration

.NET Core 3.1.5, Windows 10 x64

Activity

  1. Dotnet-GitSync-Bot commented on Jul 11, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

  2. added
    untriaged
    on Jul 11, 2020
  3. GrabYourPitchforks commented on Jul 12, 2020

    @GrabYourPitchforks
    Member

    FYI to whoever addresses this bug: using string.Compare may give incorrect results, especially when dealing with linguistic comparisons (e.g., StringComparison.InvariantCulture). Use CompareInfo.IsPrefix and CompareInfo.IsSuffix instead within the implementation, as it will always give the correct results.

  4. added
    help wanted
    and removed
    untriaged
    on Jul 16, 2020
  5. added this to the Future milestone on Jul 16, 2020
  6. maryamariyan commented on Jul 16, 2020

    @maryamariyan
    Contributor

    Here's the EndsWith logic on StringSegment:

    /// </exception>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public bool EndsWith(string text, StringComparison comparisonType)
    {
    if (text == null)
    {
    ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text);
    }
    var result = false;
    var textLength = text.Length;
    var comparisonLength = Offset + Length - textLength;
    if (HasValue && comparisonLength > 0)
    {
    result = string.Compare(Buffer, comparisonLength, text, 0, textLength, comparisonType) == 0;
    }
    return result;
    }

    The problem is on the comparisonLength.

    @adams85 would you like to push up a PR to fix this?

  7. added
    good first issue
    on Jul 16, 2020
  8. adams85 commented on Jul 16, 2020

    @adams85
    Author

    @maryamariyan No, I'd pass on this. I guess it's not too much work to fix the method itself but then - if one wants to be thorough - the full codebase needs to be scrutinized to make sure the fix doesn't break anything. Unfortunately, I won't have the time for this in the near future.

  9. GrabYourPitchforks commented on Jul 17, 2020

    @GrabYourPitchforks
    Member

    Reiterating my previous comment:

    The index needs to be fixed, sure. But this code should not use string.Compare. It should instead use CompareInfo.IsPrefix or CompareInfo.IsSuffix, as mentioned earlier.

    (Or, even better, just use the StartsWith or EndsWith extensions already on ROS<char>.)

  10. violetshine commented on Jul 23, 2020

    @violetshine

    I can take this, assign me please.

  11. ghost added
    in-pr
    on Dec 26, 2020
  12. ghost removed
    in-pr
    on Jan 23, 2021
  13. ghost locked as resolved and limited conversation to collaborators on Feb 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Primitivesbuggood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions