2008-06-30 Andreas Nahr <ClassDevelopment@A-SoftTech.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                 static StringComparer invariantCultureIgnoreCase = new CultureAwareComparer (CultureInfo.InvariantCulture, true);
43                 static StringComparer invariantCulture = new CultureAwareComparer (CultureInfo.InvariantCulture, false);
44                 static StringComparer ordinalIgnoreCase = new OrdinalComparer (true);
45                 static StringComparer ordinal = new OrdinalComparer (false);
46
47                 // Constructors
48                 protected StringComparer ()
49                 {
50                 }
51
52                 // Properties
53                 public static StringComparer CurrentCulture {
54                         get {
55                                 return new CultureAwareComparer (CultureInfo.CurrentCulture, false);
56                         }
57                 }
58
59                 public static StringComparer CurrentCultureIgnoreCase {
60                         get {
61                                 return new CultureAwareComparer (CultureInfo.CurrentCulture, true);
62                         }
63                 }
64
65                 public static StringComparer InvariantCulture {
66                         get {
67                                 return invariantCulture;
68                         }
69                 }
70
71                 public static StringComparer InvariantCultureIgnoreCase {
72                         get {
73                                 return invariantCultureIgnoreCase;
74                         }
75                 }
76
77                 public static StringComparer Ordinal {
78                         get { return ordinal; }
79                 }
80
81                 public static StringComparer OrdinalIgnoreCase {
82                         get { return ordinalIgnoreCase; }
83                 }
84
85                 // Methods
86                 public static StringComparer Create (CultureInfo culture, bool ignoreCase)
87                 {
88                         if (culture == null)
89                                 throw new ArgumentNullException ("culture");
90
91                         return new CultureAwareComparer (culture, ignoreCase);
92                 }
93
94                 public int Compare (object x, object y)
95                 {
96                         if (x == y)
97                                 return 0;
98                         if (x == null)
99                                 return -1;
100                         if (y == null)
101                                 return 1;
102
103                         string s_x = x as string;
104                         if (s_x != null) {
105                                 string s_y = y as string;
106                                 if (s_y != null)
107                                         return Compare (s_x, s_y);
108                         }
109
110                         IComparable ic = x as IComparable;
111                         if (ic == null)
112                                 throw new ArgumentException ();
113
114                         return ic.CompareTo (y);
115                 }
116
117                 public new bool Equals (object x, object y)
118                 {
119                         if (x == y)
120                                 return true;
121                         if (x == null || y == null)
122                                 return false;
123
124                         string s_x = x as string;
125                         if (s_x != null) {
126                                 string s_y = y as string;
127                                 if (s_y != null)
128                                         return Equals (s_x, s_y);
129                         }
130                         return x.Equals (y);
131                 }
132
133                 public int GetHashCode (object obj)
134                 {
135                         if (obj == null)
136                                 throw new ArgumentNullException("obj");
137
138                         string s = obj as string;
139                         return s == null ? obj.GetHashCode (): GetHashCode(s);
140                 }
141
142                 public abstract int Compare (string x, string y);
143                 public abstract bool Equals (string x, string y);
144                 public abstract int GetHashCode (string obj);
145         }
146
147         [Serializable]
148         class CultureAwareComparer : StringComparer
149         {
150                 readonly bool _ignoreCase;
151                 readonly CompareInfo _compareInfo;
152
153                 public CultureAwareComparer (CultureInfo ci, bool ignore_case)
154                 {
155                         _compareInfo = ci.CompareInfo;
156                         _ignoreCase = ignore_case;
157                 }
158
159                 public override int Compare (string x, string y)
160                 {
161                         CompareOptions co = _ignoreCase ? CompareOptions.IgnoreCase : 
162                                 CompareOptions.None;
163                         return _compareInfo.Compare (x, y, co);
164                 }
165
166                 public override bool Equals (string x, string y)
167                 {
168                         return Compare (x, y) == 0;
169                 }
170
171                 public override int GetHashCode (string s)
172                 {
173                         if (s == null)
174                                 throw new ArgumentNullException("s");
175
176                         CompareOptions co = _ignoreCase ? CompareOptions.IgnoreCase : 
177                                 CompareOptions.None;
178                         return _compareInfo.GetSortKey (s, co).GetHashCode ();
179                 }
180         }
181
182         [Serializable]
183         internal class OrdinalComparer : StringComparer
184         {
185                 readonly bool _ignoreCase;
186
187                 public OrdinalComparer (bool ignoreCase)
188                 {
189                         _ignoreCase = ignoreCase;
190                 }
191
192                 public override int Compare (string x, string y)
193                 {
194                         if (_ignoreCase)
195                                 return String.CompareOrdinalCaseInsensitiveUnchecked (x, 0, Int32.MaxValue, y, 0, Int32.MaxValue);
196                         else
197                                 return String.CompareOrdinalUnchecked (x, 0, Int32.MaxValue, y, 0, Int32.MaxValue);
198                 }
199
200                 public override bool Equals (string x, string y)
201                 {
202                         if (_ignoreCase)
203                                 return Compare (x, y) == 0;
204
205                         return x == y;
206                 }
207
208                 public override int GetHashCode (string s)
209                 {
210                         if (_ignoreCase)
211                                 return s.GetCaseInsensitiveHashCode ();
212                         else
213                                 return s.GetHashCode ();
214                 }
215         }
216 }
217
218 #endif