You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Console.WriteLine("/".StartsWith("/",StringComparison.Ordinal));// prints TrueConsole.WriteLine(newStringSegment("/").StartsWith("/",StringComparison.Ordinal));// prints TrueConsole.WriteLine("/".EndsWith("/",StringComparison.Ordinal));// prints TrueConsole.WriteLine(newStringSegment("/").EndsWith("/",StringComparison.Ordinal));// prints False !!!
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.
@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.
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>.)
Activity
Dotnet-GitSync-Bot commentedon Jul 11, 2020
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.
GrabYourPitchforks commentedon Jul 12, 2020
FYI to whoever addresses this bug: using
string.Comparemay give incorrect results, especially when dealing with linguistic comparisons (e.g.,StringComparison.InvariantCulture). UseCompareInfo.IsPrefixandCompareInfo.IsSuffixinstead within the implementation, as it will always give the correct results.maryamariyan commentedon Jul 16, 2020
Here's the EndsWith logic on StringSegment:
runtime/src/libraries/Microsoft.Extensions.Primitives/src/StringSegment.cs
Lines 333 to 352 in bd6cbe3
The problem is on the comparisonLength.
@adams85 would you like to push up a PR to fix this?
adams85 commentedon Jul 16, 2020
@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.
GrabYourPitchforks commentedon Jul 17, 2020
Reiterating my previous comment:
The index needs to be fixed, sure. But this code should not use
string.Compare. It should instead useCompareInfo.IsPrefixorCompareInfo.IsSuffix, as mentioned earlier.(Or, even better, just use the
StartsWithorEndsWithextensions already onROS<char>.)violetshine commentedon Jul 23, 2020
I can take this, assign me please.