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 2.6k
StringComparer Create(culture, CompareOptions) overload #16334
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4591869
String Create options overlaod
Anipik 7606c3c
Minor Change
Anipik aa21f43
Feedback
Anipik 2904680
validation on options
Anipik 23acf6c
obsolete removed
Anipik cbb560a
Implementing Iserializable and removing ignorecase
Anipik d279555
HashCode and serialization changes
Anipik f143205
made inline
Anipik d592c7a
Space Corrected
Anipik 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,8 @@ namespace System | |
| [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] | ||
| public abstract class StringComparer : IComparer, IEqualityComparer, IComparer<string>, IEqualityComparer<string> | ||
| { | ||
| private static readonly CultureAwareComparer s_invariantCulture = new CultureAwareComparer(CultureInfo.InvariantCulture, false); | ||
| private static readonly CultureAwareComparer s_invariantCultureIgnoreCase = new CultureAwareComparer(CultureInfo.InvariantCulture, true); | ||
| private static readonly CultureAwareComparer s_invariantCulture = new CultureAwareComparer(CultureInfo.InvariantCulture, CompareOptions.None); | ||
| private static readonly CultureAwareComparer s_invariantCultureIgnoreCase = new CultureAwareComparer(CultureInfo.InvariantCulture, CompareOptions.IgnoreCase); | ||
| private static readonly OrdinalCaseSensitiveComparer s_ordinal = new OrdinalCaseSensitiveComparer(); | ||
| private static readonly OrdinalIgnoreCaseComparer s_ordinalIgnoreCase = new OrdinalIgnoreCaseComparer(); | ||
|
|
||
|
|
@@ -38,15 +38,15 @@ public static StringComparer CurrentCulture | |
| { | ||
| get | ||
| { | ||
| return new CultureAwareComparer(CultureInfo.CurrentCulture, false); | ||
| return new CultureAwareComparer(CultureInfo.CurrentCulture, CompareOptions.None); | ||
| } | ||
| } | ||
|
|
||
| public static StringComparer CurrentCultureIgnoreCase | ||
| { | ||
| get | ||
| { | ||
| return new CultureAwareComparer(CultureInfo.CurrentCulture, true); | ||
| return new CultureAwareComparer(CultureInfo.CurrentCulture, CompareOptions.IgnoreCase); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -94,8 +94,18 @@ public static StringComparer Create(CultureInfo culture, bool ignoreCase) | |
| { | ||
| throw new ArgumentNullException(nameof(culture)); | ||
| } | ||
|
|
||
| return new CultureAwareComparer(culture, ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None); | ||
| } | ||
|
|
||
| public static StringComparer Create(CultureInfo culture, CompareOptions options) | ||
| { | ||
| if (culture == null) | ||
| { | ||
| throw new ArgumentException(nameof(culture)); | ||
| } | ||
|
|
||
| return new CultureAwareComparer(culture, ignoreCase); | ||
| return new CultureAwareComparer(culture, options); | ||
| } | ||
|
|
||
| public int Compare(object x, object y) | ||
|
|
@@ -123,7 +133,6 @@ public int Compare(object x, object y) | |
| throw new ArgumentException(SR.Argument_ImplementIComparable); | ||
| } | ||
|
|
||
|
|
||
| public new bool Equals(Object x, Object y) | ||
| { | ||
| if (x == y) return true; | ||
|
|
@@ -163,32 +172,50 @@ public int GetHashCode(object obj) | |
|
|
||
| [Serializable] | ||
| [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] | ||
| public sealed class CultureAwareComparer : StringComparer | ||
| public sealed class CultureAwareComparer : StringComparer, ISerializable | ||
| { | ||
| private const CompareOptions ValidCompareMaskOffFlags = ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreWidth | CompareOptions.IgnoreKanaType | CompareOptions.StringSort); | ||
|
|
||
| private readonly CompareInfo _compareInfo; // Do not rename (binary serialization) | ||
| private readonly bool _ignoreCase; // Do not rename (binary serialization) | ||
| private CompareOptions _options; | ||
|
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. that one should be readonly 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. I just checked with net471, they named the field the same. lucky us. |
||
|
|
||
| internal CultureAwareComparer(CultureInfo culture, bool ignoreCase) | ||
| internal CultureAwareComparer(CultureInfo culture, CompareOptions compareOptions) | ||
| { | ||
| _compareInfo = culture.CompareInfo; | ||
| _ignoreCase = ignoreCase; | ||
|
|
||
| if ((compareOptions & CultureAwareComparer.ValidCompareMaskOffFlags) != 0) | ||
| { | ||
| throw new ArgumentException(SR.Argument_InvalidFlag, nameof(compareOptions)); | ||
| } | ||
| _options = compareOptions; | ||
| } | ||
|
|
||
| private CompareOptions Options => _ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None; | ||
| private CultureAwareComparer(SerializationInfo info, StreamingContext context) | ||
| { | ||
| _compareInfo = (CompareInfo)info.GetValue("_compareInfo", typeof(CompareInfo)); | ||
| bool ignoreCase = info.GetBoolean("_ignoreCase"); | ||
|
|
||
| var obj = info.GetValueNoThrow("_options", typeof(CompareOptions)); | ||
| if (obj != null) | ||
| _options = (CompareOptions)obj; | ||
|
|
||
| // fix up the _options value in case we are getting old serialized object not having _options | ||
| _options |= ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None; | ||
| } | ||
|
|
||
| public override int Compare(string x, string y) | ||
| { | ||
| if (object.ReferenceEquals(x, y)) return 0; | ||
| if (x == null) return -1; | ||
| if (y == null) return 1; | ||
| return _compareInfo.Compare(x, y, Options); | ||
| return _compareInfo.Compare(x, y, _options); | ||
| } | ||
|
|
||
| public override bool Equals(string x, string y) | ||
| { | ||
| if (object.ReferenceEquals(x, y)) return true; | ||
| if (x == null || y == null) return false; | ||
| return _compareInfo.Compare(x, y, Options) == 0; | ||
| return _compareInfo.Compare(x, y, _options) == 0; | ||
| } | ||
|
|
||
| public override int GetHashCode(string obj) | ||
|
|
@@ -197,7 +224,7 @@ public override int GetHashCode(string obj) | |
| { | ||
| throw new ArgumentNullException(nameof(obj)); | ||
| } | ||
| return _compareInfo.GetHashCodeOfString(obj, Options); | ||
| return _compareInfo.GetHashCodeOfString(obj, _options); | ||
| } | ||
|
|
||
| // Equals method for the comparer itself. | ||
|
|
@@ -206,14 +233,20 @@ public override bool Equals(object obj) | |
| CultureAwareComparer comparer = obj as CultureAwareComparer; | ||
| return | ||
| comparer != null && | ||
| _ignoreCase == comparer._ignoreCase && | ||
| _options == comparer._options && | ||
| _compareInfo.Equals(comparer._compareInfo); | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| int hashCode = _compareInfo.GetHashCode(); | ||
| return _ignoreCase ? ~hashCode : hashCode; | ||
| return _compareInfo.GetHashCode() ^ ((int)_options & 0x7FFFFFFF); | ||
| } | ||
|
|
||
| public void GetObjectData(SerializationInfo info, StreamingContext context) | ||
| { | ||
| info.AddValue("_compareInfo", _compareInfo); | ||
| info.AddValue("_options", _options); | ||
| info.AddValue("_ignoreCase", (_options & CompareOptions.IgnoreCase) != 0); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
I know this PR is extremely old but why doesn't this throw an
ArgumentNullException?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.
@TylerBrinkley I think it is a mistake. Thanks for pointing it out. Generally it's best to open a new issue (or just PR) since posting on closed issues/PR's sometimes gets overlooked.
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.
Did somebody follow-up on this? Was an issue created?
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.
I created #26570 to address this.