This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Add ROSpan Equals/CompareTo/IndexOf/Contains string-like APIs with StringComparison #27319
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b10228f
Add ROSpan Equals/CompareTo/IndexOf/Contains string-like APIs with St…
ahsonkhan 67bf3a9
Merge branch 'master' of https://github.com/dotnet/corefx into SpanSt…
ahsonkhan bec2f09
Respond to recent change AsReadOnlySpan -> AsSpan
ahsonkhan 8dd0adf
Remove the out parameter from IndexOf
ahsonkhan 23589ab
Add tests and avoid allocations for StringComparison.Ordinal
ahsonkhan 9b1ce10
Merge branch 'master' of https://github.com/dotnet/corefx into SpanSt…
ahsonkhan 06c34b4
Remove duplicate definition of SoftHyphen
ahsonkhan 3941a93
Merge branch 'master' of https://github.com/dotnet/corefx into SpanSt…
ahsonkhan 68feaf7
Merge branch 'master' into SpanStringAPIs
ahsonkhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,58 @@ namespace System | |
| /// </summary> | ||
| public static partial class MemoryExtensions | ||
| { | ||
| /// <summary> | ||
| /// Returns a value indicating whether the specified <paramref name="value"/> occurs within the <paramref name="span"/>. | ||
| /// <param name="span">The source span.</param> | ||
| /// <param name="value">The value to seek within the source span.</param> | ||
| /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param> | ||
| /// </summary> | ||
| public static bool Contains(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType) | ||
| => (IndexOf(span, value, comparisonType) >= 0); | ||
|
|
||
| /// <summary> | ||
| /// Determines whether this <paramref name="span"/> and the specified <paramref name="value"/> span have the same characters | ||
| /// when compared using the specified <paramref name="comparisonType"/> option. | ||
| /// <param name="span">The source span.</param> | ||
| /// <param name="value">The value to compare with the source span.</param> | ||
| /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param> | ||
| /// </summary> | ||
| public static bool Equals(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType) | ||
| { | ||
| if (comparisonType == StringComparison.Ordinal) | ||
| { | ||
| return span.SequenceEqual<char>(value); | ||
| } | ||
|
|
||
| return span.ToString().Equals(value.ToString(), comparisonType); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compares the specified <paramref name="span"/> and <paramref name="value"/> using the specified <paramref name="comparisonType"/>, | ||
| /// and returns an integer that indicates their relative position in the sort order. | ||
| /// <param name="span">The source span.</param> | ||
| /// <param name="value">The value to compare with the source span.</param> | ||
| /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param> | ||
| /// </summary> | ||
| public static int CompareTo(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not optimizing for the ordinal cases?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we don't already have an optimized ordinal CompareTo (unlike the other APIs), I will address this outside the PR.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for tracking this issue. |
||
| => string.Compare(span.ToString(), value.ToString(), comparisonType); | ||
|
|
||
| /// <summary> | ||
| /// Reports the zero-based index of the first occurrence of the specified <paramref name="value"/> in the current <paramref name="span"/>. | ||
| /// <param name="span">The source span.</param> | ||
| /// <param name="value">The value to seek within the source span.</param> | ||
| /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param> | ||
| /// </summary> | ||
| public static int IndexOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType) | ||
| { | ||
| if (comparisonType == StringComparison.Ordinal) | ||
| { | ||
| return span.IndexOf<char>(value); | ||
| } | ||
|
|
||
| return span.ToString().IndexOf(value.ToString(), comparisonType); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Copies the characters from the source span into the destination, converting each character to lowercase, | ||
| /// using the casing rules of the specified culture. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have any optimization for ordinal ignore case when having ascii cases as we did in coreclr? this apply for all introduced APIs here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will address outside the PR (see https://github.com/dotnet/corefx/issues/27379).