5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2015 Xamarin Inc (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.Runtime.CompilerServices;
31 using Mono.Globalization.Unicode;
32 using System.Threading;
34 namespace System.Globalization
36 partial class CompareInfo
39 SimpleCollator collator;
41 // Maps culture IDs to SimpleCollator objects
42 static Dictionary<string, SimpleCollator> collators;
43 static bool managedCollation;
44 static bool managedCollationChecked;
46 static bool UseManagedCollation {
48 if (!managedCollationChecked) {
49 managedCollation = Environment.internalGetEnvironmentVariable ("MONO_DISABLE_MANAGED_COLLATION") != "yes" && MSCompatUnicodeTable.IsReady;
50 managedCollationChecked = true;
53 return managedCollation;
57 SimpleCollator GetCollator ()
62 if (collators == null) {
63 Interlocked.CompareExchange (ref collators, new Dictionary<string, SimpleCollator> (StringComparer.Ordinal), null);
67 if (!collators.TryGetValue (m_sortName, out collator)) {
68 collator = new SimpleCollator (CultureInfo.GetCultureInfo (m_name));
69 collators [m_sortName] = collator;
76 SortKey CreateSortKeyCore (string source, CompareOptions options)
78 if (UseManagedCollation)
79 return GetCollator ().GetSortKey (source, options);
80 SortKey key=new SortKey (culture, source, options);
82 /* Need to do the icall here instead of in the
83 * SortKey constructor, as we need access to
84 * this instance's collator.
86 assign_sortkey (key, source, options);
91 int internal_index_switch (string s, int sindex, int count, char c, CompareOptions opt, bool first)
93 if (opt == CompareOptions.Ordinal && first)
94 return s.IndexOfUnchecked (c, sindex, count);
96 return UseManagedCollation ?
97 internal_index_managed (s, sindex, count, c, opt, first) :
98 internal_index (s, sindex, count, c, opt, first);
101 int internal_index_switch (string s1, int sindex, int count, string s2, CompareOptions opt, bool first)
103 if (opt == CompareOptions.Ordinal && first)
104 return s1.IndexOfUnchecked (s2, sindex, count);
106 return UseManagedCollation ?
107 internal_index_managed (s1, sindex, count, s2, opt, first) :
108 internal_index (s1, sindex, count, s2, opt, first);
111 int internal_compare_switch (string str1, int offset1, int length1, string str2, int offset2, int length2, CompareOptions options)
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);
120 int internal_compare_managed (string str1, int offset1, int length1, string str2, int offset2, int length2, CompareOptions options)
122 return GetCollator ().Compare (str1, offset1, length1,
123 str2, offset2, length2, options);
126 int internal_index_managed (string s, int sindex, int count, char c, CompareOptions opt, bool first)
129 GetCollator ().IndexOf (s, c, sindex, count, opt) :
130 GetCollator ().LastIndexOf (s, c, sindex, count, opt);
133 int internal_index_managed (string s1, int sindex, int count, string s2, CompareOptions opt, bool first)
136 GetCollator ().IndexOf (s1, s2, sindex, count, opt) :
137 GetCollator ().LastIndexOf (s1, s2, sindex, count, opt);
140 [MethodImplAttribute (MethodImplOptions.InternalCall)]
141 private extern void assign_sortkey (object key, string source,
142 CompareOptions options);
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);
150 [MethodImplAttribute (MethodImplOptions.InternalCall)]
151 private extern int internal_index (string source, int sindex,
152 int count, char value,
153 CompareOptions options,
156 [MethodImplAttribute (MethodImplOptions.InternalCall)]
157 private extern int internal_index (string source, int sindex,
158 int count, string value,
159 CompareOptions options,