[corlib] Update public api
[mono.git] / mcs / class / corlib / ReferenceSources / CompareInfo.cs
1 //
2 // CompareInfo.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2015 Xamarin Inc (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Collections.Generic;
30 using System.Runtime.CompilerServices;
31 using Mono.Globalization.Unicode;
32 using System.Threading;
33
34 namespace System.Globalization
35 {
36         partial class CompareInfo
37         {
38                 [NonSerialized]
39                 SimpleCollator collator;
40
41                 // Maps culture IDs to SimpleCollator objects
42                 static Dictionary<string, SimpleCollator> collators;
43                 static bool managedCollation;
44                 static bool managedCollationChecked;
45
46                 static bool UseManagedCollation {
47                         get {
48                                 if (!managedCollationChecked) {
49                                         managedCollation = Environment.internalGetEnvironmentVariable ("MONO_DISABLE_MANAGED_COLLATION") != "yes" && MSCompatUnicodeTable.IsReady;
50                                         managedCollationChecked = true;
51                                 }
52
53                                 return managedCollation;
54                         }
55                 }
56
57                 SimpleCollator GetCollator ()
58                 {
59                         if (collator != null)
60                                 return collator;
61
62                         if (collators == null) {
63                                 Interlocked.CompareExchange (ref collators, new Dictionary<string, SimpleCollator> (StringComparer.Ordinal), null);
64                         }
65
66                         lock (collators) {
67                                 if (!collators.TryGetValue (m_sortName, out collator)) {
68                                         collator = new SimpleCollator (CultureInfo.GetCultureInfo (m_name));
69                                         collators [m_sortName] = collator;
70                                 }
71                         }
72
73                         return collator;
74                 }
75
76                 SortKey CreateSortKeyCore (string source, CompareOptions options)
77                 {
78                         if (UseManagedCollation)
79                                 return GetCollator ().GetSortKey (source, options);
80                         SortKey key=new SortKey (culture, source, options);
81
82                         /* Need to do the icall here instead of in the
83                          * SortKey constructor, as we need access to
84                          * this instance's collator.
85                          */
86                         assign_sortkey (key, source, options);
87                         
88                         return(key);            
89                 }
90
91                 int internal_index_switch (string s, int sindex, int count, char c, CompareOptions opt, bool first)
92                 {
93                         if (opt == CompareOptions.Ordinal && first)
94                                 return s.IndexOfUnchecked (c, sindex, count);
95
96                         return UseManagedCollation ?
97                                 internal_index_managed (s, sindex, count, c, opt, first) :
98                                 internal_index (s, sindex, count, c, opt, first);
99                 }       
100
101                 int internal_index_switch (string s1, int sindex, int count, string s2, CompareOptions opt, bool first)
102                 {
103                         if (opt == CompareOptions.Ordinal && first)
104                                 return s1.IndexOfUnchecked (s2, sindex, count);
105                         
106                         return UseManagedCollation ?
107                                 internal_index_managed (s1, sindex, count, s2, opt, first) :
108                                 internal_index (s1, sindex, count, s2, opt, first);
109                 }
110
111                 int internal_compare_switch (string str1, int offset1, int length1, string str2, int offset2, int length2, CompareOptions options)
112                 {
113                         return UseManagedCollation ?
114                                 internal_compare_managed (str1, offset1, length1,
115                                 str2, offset2, length2, options) :
116                                 internal_compare (str1, offset1, length1,
117                                 str2, offset2, length2, options);
118                 }
119
120                 int internal_compare_managed (string str1, int offset1, int length1, string str2, int offset2, int length2, CompareOptions options)
121                 {
122                         return GetCollator ().Compare (str1, offset1, length1,
123                                 str2, offset2, length2, options);
124                 }
125
126                 int internal_index_managed (string s, int sindex, int count, char c, CompareOptions opt, bool first)
127                 {
128                         return first ?
129                                 GetCollator ().IndexOf (s, c, sindex, count, opt) :
130                                 GetCollator ().LastIndexOf (s, c, sindex, count, opt);
131                 }
132
133                 int internal_index_managed (string s1, int sindex, int count, string s2, CompareOptions opt, bool first)
134                 {
135                         return first ?
136                                 GetCollator ().IndexOf (s1, s2, sindex, count, opt) :
137                                 GetCollator ().LastIndexOf (s1, s2, sindex, count, opt);
138                 }
139
140                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
141                 private extern void assign_sortkey (object key, string source,
142                                                         CompareOptions options);                
143
144                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
145                 private extern int internal_compare (string str1, int offset1,
146                                                          int length1, string str2,
147                                                          int offset2, int length2,
148                                                          CompareOptions options);
149
150                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
151                 private extern int internal_index (string source, int sindex,
152                                                    int count, char value,
153                                                    CompareOptions options,
154                                                    bool first);
155
156                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
157                 private extern int internal_index (string source, int sindex,
158                                                    int count, string value,
159                                                    CompareOptions options,
160                                                    bool first);
161         }
162 }