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.
32 using System.Collections;
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35 using System.Collections.Generic;
39 [Serializable, ComVisible(true)]
40 public abstract class StringComparer : IComparer, IEqualityComparer, IComparer<string>, IEqualityComparer<string>
43 class StringCultureComparer: StringComparer
48 public StringCultureComparer (CultureInfo ci, bool ignore_case)
50 this.ci = ci.CompareInfo;
51 co = ignore_case ? CompareOptions.IgnoreCase : CompareOptions.None;
54 public override int Compare (string x, string y)
56 return ci.Compare (x, y, co);
59 public override bool Equals (string x, string y)
61 return Compare (x, y) == 0;
64 public override int GetHashCode (string s)
67 throw new ArgumentNullException("s");
69 return ci.GetSortKey (s, co).GetHashCode ();
74 class OrdinalComparer : StringComparer
76 public OrdinalComparer ()
80 public override int Compare (string x, string y)
82 return String.CompareOrdinal (x, y);
85 public override bool Equals (string x, string y)
90 public override int GetHashCode (string s)
92 return s.GetHashCode ();
97 class OrdinalIgnoreCaseComparer : StringComparer
99 public OrdinalIgnoreCaseComparer ()
103 public override int Compare (string x, string y)
105 // copied from String.CompareOrdinal()
112 else if (y == null) {
116 int max = x.Length > y.Length ? y.Length : x.Length;
117 for (int i = 0; i < max; i++) {
120 char xc = Char.ToUpperInvariant (x [i]);
121 char yc = Char.ToUpperInvariant (y [i]);
125 return max < x.Length ? -1 :
126 max == y.Length ? 0 : 1;
129 public override bool Equals (string x, string y)
131 return Compare (x, y) == 0;
134 public override int GetHashCode (string s)
136 return s.GetCaseInsensitiveHashCode ();
140 static StringComparer invariantCultureIgnoreCase = new StringCultureComparer (CultureInfo.InvariantCulture, true);
141 static StringComparer invariantCulture = new StringCultureComparer (CultureInfo.InvariantCulture, false);
142 static StringComparer ordinalIgnoreCase =
143 new OrdinalIgnoreCaseComparer ();
144 static StringComparer ordinal = new OrdinalComparer ();
147 protected StringComparer ()
152 public static StringComparer CurrentCulture {
154 return new StringCultureComparer (CultureInfo.CurrentCulture, false);
158 public static StringComparer CurrentCultureIgnoreCase {
160 return new StringCultureComparer (CultureInfo.CurrentCulture, true);
164 public static StringComparer InvariantCulture {
166 return invariantCulture;
170 public static StringComparer InvariantCultureIgnoreCase {
172 return invariantCultureIgnoreCase;
176 public static StringComparer Ordinal {
177 get { return ordinal; }
180 public static StringComparer OrdinalIgnoreCase {
181 get { return ordinalIgnoreCase; }
185 public static StringComparer Create (CultureInfo culture, bool ignoreCase)
187 return new StringCultureComparer (culture, ignoreCase);
190 public int Compare (object x, object y)
199 string s_x = x as string;
201 string s_y = y as string;
203 return Compare (s_x, s_y);
206 IComparable ic = x as IComparable;
208 throw new ArgumentException ();
210 return ic.CompareTo (y);
213 public new bool Equals (object x, object y)
217 if (x == null || y == null)
220 string s_x = x as string;
222 string s_y = y as string;
224 return Equals (s_x, s_y);
229 public int GetHashCode (object o)
232 throw new ArgumentNullException("o");
234 string s = o as string;
235 return s == null ? o.GetHashCode (): GetHashCode(s);
238 public abstract int Compare (string x, string y);
239 public abstract bool Equals (string x, string y);
240 public abstract int GetHashCode (string s);