Add serializable attributes, patch from David Mitchell (dmitchell@logos.com), fixes...
[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                 [Serializable]
43                 class StringCultureComparer: StringComparer
44                 {
45                         CompareOptions co;
46                         CompareInfo ci;
47
48                         public StringCultureComparer (CultureInfo ci, bool ignore_case)
49                         {
50                                 this.ci = ci.CompareInfo;
51                                 co = ignore_case ? CompareOptions.IgnoreCase : CompareOptions.None;
52                         }
53
54                         public override int Compare (string x, string y)
55                         {
56                                 return ci.Compare (x, y, co);
57                         }
58
59                         public override bool Equals (string x, string y)
60                         {
61                                 return Compare (x, y) == 0;
62                         }
63
64                         public override int GetHashCode (string s)
65                         {
66                                 if (s == null)
67                                         throw new ArgumentNullException("s");
68
69                                 return ci.GetSortKey (s, co).GetHashCode ();
70                         }
71                 }
72
73                 [Serializable]
74                 class OrdinalComparer : StringComparer
75                 {
76                         public OrdinalComparer ()
77                         {
78                         }
79
80                         public override int Compare (string x, string y)
81                         {
82                                 return String.CompareOrdinal (x, y);
83                         }
84
85                         public override bool Equals (string x, string y)
86                         {
87                                 return x == y;
88                         }
89
90                         public override int GetHashCode (string s)
91                         {
92                                 return s.GetHashCode ();
93                         }
94                 }
95
96                 [Serializable]
97                 class OrdinalIgnoreCaseComparer : StringComparer
98                 {
99                         public OrdinalIgnoreCaseComparer ()
100                         {
101                         }
102
103                         public override int Compare (string x, string y)
104                         {
105                                 // copied from String.CompareOrdinal()
106                                 if (x == null) {
107                                         if (y == null)
108                                                 return 0;
109                                         else
110                                                 return -1;
111                                 }
112                                 else if (y == null) {
113                                         return 1;
114                                 }
115
116                                 int max = x.Length > y.Length ? y.Length : x.Length;
117                                 for (int i = 0; i < max; i++) {
118                                         if (x [i] == y [i])
119                                                 continue;
120                                         char xc = Char.ToUpperInvariant (x [i]);
121                                         char yc = Char.ToUpperInvariant (y [i]);
122                                         if (xc != yc)
123                                                 return xc - yc;
124                                 }
125                                 return max < x.Length ? -1 :
126                                         max == y.Length ? 0 : 1;
127                         }
128
129                         public override bool Equals (string x, string y)
130                         {
131                                 return Compare (x, y) == 0;
132                         }
133
134                         public override int GetHashCode (string s)
135                         {
136                                 return s.GetCaseInsensitiveHashCode ();
137                         }
138                 }
139
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 ();
145
146                 // Constructors
147                 protected StringComparer ()
148                 {
149                 }
150
151                 // Properties
152                 public static StringComparer CurrentCulture {
153                         get {
154                                 return new StringCultureComparer (CultureInfo.CurrentCulture, false);
155                         }
156                 }
157
158                 public static StringComparer CurrentCultureIgnoreCase {
159                         get {
160                                 return new StringCultureComparer (CultureInfo.CurrentCulture, true);
161                         }
162                 }
163
164                 public static StringComparer InvariantCulture {
165                         get {
166                                 return invariantCulture;
167                         }
168                 }
169
170                 public static StringComparer InvariantCultureIgnoreCase {
171                         get {
172                                 return invariantCultureIgnoreCase;
173                         }
174                 }
175
176                 public static StringComparer Ordinal {
177                         get { return ordinal; }
178                 }
179
180                 public static StringComparer OrdinalIgnoreCase {
181                         get { return ordinalIgnoreCase; }
182                 }
183
184                 // Methods
185                 public static StringComparer Create (CultureInfo culture, bool ignoreCase)
186                 {
187                         return new StringCultureComparer (culture, ignoreCase);
188                 }
189
190                 public int Compare (object x, object y)
191                 {
192                         if (x == y)
193                                 return 0;
194                         if (x == null)
195                                 return -1;
196                         if (y == null)
197                                 return 1;
198
199                         string s_x = x as string;
200                         if (s_x != null) {
201                                 string s_y = y as string;
202                                 if (s_y != null)
203                                         return Compare (s_x, s_y);
204                         }
205
206                         IComparable ic = x as IComparable;
207                         if (ic == null)
208                                 throw new ArgumentException ();
209
210                         return ic.CompareTo (y);
211                 }
212
213                 public new bool Equals (object x, object y)
214                 {
215                         if (x == y)
216                                 return true;
217                         if (x == null || y == null)
218                                 return false;
219
220                         string s_x = x as string;
221                         if (s_x != null) {
222                                 string s_y = y as string;
223                                 if (s_y != null)
224                                         return Equals (s_x, s_y);
225                         }
226                         return x.Equals (y);
227                 }
228
229                 public int GetHashCode (object o)
230                 {
231                         if (o == null)
232                                 throw new ArgumentNullException("o");
233
234                         string s = o as string;
235                         return s == null ? o.GetHashCode (): GetHashCode(s);
236                 }
237
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);
241         }
242 }
243
244 #endif