2005-08-10 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System / StringComparer.cs
1 //
2 // System.StringComparer
3 //
4 // Authors:
5 //      Marek Safar (marek.safar@seznam.cz)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 #if NET_2_0
31
32 using System.Collections;
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35 using System.Collections.Generic;
36
37 namespace System
38 {
39         [Serializable, ComVisible(true)]
40         public abstract class StringComparer : IComparer, IEqualityComparer, IComparer<string>, IEqualityComparer<string>
41         {
42                 class StringCultureComparer: StringComparer
43                 {
44                         CompareOptions co;
45                         CompareInfo ci;
46
47                         public StringCultureComparer (CultureInfo ci, bool ignore_case)
48                         {
49                                 this.ci = ci.CompareInfo;
50                                 co = ignore_case ? CompareOptions.IgnoreCase : CompareOptions.None;
51                         }
52
53                         public override int Compare (string x, string y)
54                         {
55                                 return ci.Compare (x, y, co);
56                         }
57
58                         public override bool Equals (string x, string y)
59                         {
60                                 return Compare (x, y) == 0;
61                         }
62
63                         public override int GetHashCode (string s)
64                         {
65                                 if (s == null)
66                                         throw new ArgumentNullException("s");
67
68                                 return ci.GetSortKey (s, co).GetHashCode ();
69                         }
70                 }
71
72                 class OrdinalComparer : StringComparer
73                 {
74                         public OrdinalComparer ()
75                         {
76                         }
77
78                         public override int Compare (string x, string y)
79                         {
80                                 return String.CompareOrdinal (x, y);
81                         }
82
83                         public override bool Equals (string x, string y)
84                         {
85                                 return x == y;
86                         }
87
88                         public override int GetHashCode (string s)
89                         {
90                                 return s.GetHashCode ();
91                         }
92                 }
93
94                 class OrdinalIgnoreCaseComparer : StringComparer
95                 {
96                         public OrdinalIgnoreCaseComparer ()
97                         {
98                         }
99
100                         public override int Compare (string x, string y)
101                         {
102                                 // copied from String.CompareOrdinal()
103                                 if (x == null) {
104                                         if (y == null)
105                                                 return 0;
106                                         else
107                                                 return -1;
108                                 }
109                                 else if (y == null) {
110                                         return 1;
111                                 }
112
113                                 int max = x.Length > y.Length ? y.Length : x.Length;
114                                 for (int i = 0; i < max; i++) {
115                                         if (x [i] == y [i])
116                                                 continue;
117                                         char xc = Char.ToUpperInvariant (x [i]);
118                                         char yc = Char.ToUpperInvariant (y [i]);
119                                         if (xc != yc)
120                                                 return xc - yc;
121                                 }
122                                 return max < x.Length ? -1 :
123                                         max == y.Length ? 0 : 1;
124                         }
125
126                         public override bool Equals (string x, string y)
127                         {
128                                 return Compare (x, y) != 0;
129                         }
130
131                         public override int GetHashCode (string s)
132                         {
133                                 return s.ToUpperInvariant ().GetHashCode ();
134                         }
135                 }
136
137                 static StringComparer invariantCultureIgnoreCase = new StringCultureComparer (CultureInfo.InvariantCulture, true);
138                 static StringComparer invariantCulture = new StringCultureComparer (CultureInfo.InvariantCulture, false);
139                 static StringComparer ordinalIgnoreCase =
140                         new OrdinalIgnoreCaseComparer ();
141                 static StringComparer ordinal = new OrdinalComparer ();
142
143                 // Constructors
144                 protected StringComparer ()
145                 {
146                 }
147
148                 // Properties
149                 public static StringComparer CurrentCulture {
150                         get {
151                                 return new StringCultureComparer (CultureInfo.CurrentCulture, false);
152                         }
153                 }
154
155                 public static StringComparer CurrentCultureIgnoreCase {
156                         get {
157                                 return new StringCultureComparer (CultureInfo.CurrentCulture, true);
158                         }
159                 }
160
161                 public static StringComparer InvariantCulture {
162                         get {
163                                 return invariantCulture;
164                         }
165                 }
166
167                 public static StringComparer InvariantCultureIgnoreCase {
168                         get {
169                                 return invariantCultureIgnoreCase;
170                         }
171                 }
172
173                 public static StringComparer Ordinal {
174                         get { return ordinal; }
175                 }
176
177                 public static StringComparer OrdinalIgnoreCase {
178                         get { return ordinalIgnoreCase; }
179                 }
180
181                 // Methods
182                 public static StringComparer Create (CultureInfo culture, bool ignoreCase)
183                 {
184                         return new StringCultureComparer (culture, ignoreCase);
185                 }
186
187                 public int Compare (object x, object y)
188                 {
189                         if (x == y)
190                                 return 0;
191                         if (x == null)
192                                 return -1;
193                         if (y == null)
194                                 return 1;
195
196                         string s_x = x as string;
197                         if (s_x != null) {
198                                 string s_y = y as string;
199                                 if (s_y != null)
200                                         return Compare (s_x, s_y);
201                         }
202
203                         IComparable ic = x as IComparable;
204                         if (ic == null)
205                                 throw new ArgumentException ();
206
207                         return ic.CompareTo (y);
208                 }
209
210                 public new bool Equals (object x, object y)
211                 {
212                         if (x == y)
213                                 return true;
214                         if (x == null || y == null)
215                                 return false;
216
217                         string s_x = x as string;
218                         if (s_x != null) {
219                                 string s_y = y as string;
220                                 if (s_y != null)
221                                         return Equals (s_x, s_y);
222                         }
223                         return x.Equals (y);
224                 }
225
226                 public int GetHashCode (object o)
227                 {
228                         if (o == null)
229                                 throw new ArgumentNullException("o");
230
231                         string s = o as string;
232                         return s == null ? o.GetHashCode (): GetHashCode(s);
233                 }
234
235                 public abstract int Compare (string x, string y);
236                 public abstract bool Equals (string x, string y);
237                 public abstract int GetHashCode (string s);
238         }
239 }
240
241 #endif