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