2 // System.StringComparer
5 // Marek Safar (marek.safar@seznam.cz)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33 using System.Collections.Generic;
37 [Serializable, ComVisible(true)]
38 public abstract class StringComparer : IComparer, IEqualityComparer, IComparer<string>, IEqualityComparer<string>
40 static class Predefined
42 public static readonly StringComparer invariantCultureIgnoreCase = new CultureAwareComparer (CultureInfo.InvariantCulture, true);
43 public static readonly StringComparer invariantCulture = new CultureAwareComparer (CultureInfo.InvariantCulture, false);
44 public static readonly StringComparer ordinalIgnoreCase = new OrdinalComparer (true);
45 public static readonly StringComparer ordinal = new OrdinalComparer (false);
49 protected StringComparer ()
54 public static StringComparer CurrentCulture {
56 return new CultureAwareComparer (CultureInfo.CurrentCulture, false);
60 public static StringComparer CurrentCultureIgnoreCase {
62 return new CultureAwareComparer (CultureInfo.CurrentCulture, true);
66 public static StringComparer InvariantCulture {
68 return Predefined.invariantCulture;
72 public static StringComparer InvariantCultureIgnoreCase {
74 return Predefined.invariantCultureIgnoreCase;
78 public static StringComparer Ordinal {
79 get { return Predefined.ordinal; }
82 public static StringComparer OrdinalIgnoreCase {
83 get { return Predefined.ordinalIgnoreCase; }
87 public static StringComparer Create (CultureInfo culture, bool ignoreCase)
90 throw new ArgumentNullException ("culture");
92 return new CultureAwareComparer (culture, ignoreCase);
95 public int Compare (object x, object y)
104 string s_x = x as string;
106 string s_y = y as string;
108 return Compare (s_x, s_y);
111 IComparable ic = x as IComparable;
113 throw new ArgumentException ();
115 return ic.CompareTo (y);
118 public new bool Equals (object x, object y)
122 if (x == null || y == null)
125 string s_x = x as string;
127 string s_y = y as string;
129 return Equals (s_x, s_y);
134 public int GetHashCode (object obj)
137 throw new ArgumentNullException("obj");
139 string s = obj as string;
140 return s == null ? obj.GetHashCode (): GetHashCode(s);
143 public abstract int Compare (string x, string y);
144 public abstract bool Equals (string x, string y);
145 public abstract int GetHashCode (string obj);
149 sealed class CultureAwareComparer : StringComparer
151 readonly bool _ignoreCase;
152 readonly CompareInfo _compareInfo;
154 public CultureAwareComparer (CultureInfo ci, bool ignore_case)
156 _compareInfo = ci.CompareInfo;
157 _ignoreCase = ignore_case;
160 public override int Compare (string x, string y)
162 CompareOptions co = _ignoreCase ? CompareOptions.IgnoreCase :
164 return _compareInfo.Compare (x, y, co);
167 public override bool Equals (string x, string y)
169 return Compare (x, y) == 0;
172 public override int GetHashCode (string s)
175 throw new ArgumentNullException("s");
177 CompareOptions co = _ignoreCase ? CompareOptions.IgnoreCase :
179 return _compareInfo.GetSortKey (s, co).GetHashCode ();
184 internal sealed class OrdinalComparer : StringComparer
186 readonly bool _ignoreCase;
188 public OrdinalComparer (bool ignoreCase)
190 _ignoreCase = ignoreCase;
193 public override int Compare (string x, string y)
196 return String.CompareOrdinalCaseInsensitiveUnchecked (x, 0, Int32.MaxValue, y, 0, Int32.MaxValue);
198 return String.CompareOrdinalUnchecked (x, 0, Int32.MaxValue, y, 0, Int32.MaxValue);
201 public override bool Equals (string x, string y)
204 return Compare (x, y) == 0;
209 public override int GetHashCode (string s)
212 throw new ArgumentNullException ("s");
215 return s.GetCaseInsensitiveHashCode ();
217 return s.GetHashCode ();