Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CompareInfo.cs « corefx « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b8205f21ac5ab530e08b3241c5aa894ff1a7fbac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// CompareInfo.cs
//
// Authors:
//	Marek Safar  <marek.safar@gmail.com>
//
// Copyright (C) 2018  Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

// Disable unreachable code warnings in this entire file.
#pragma warning disable 162

namespace System.Globalization
{
	partial class CompareInfo
	{
		void InitSort (CultureInfo culture)
		{
			_sortName = culture.SortName;
		}

		unsafe static int CompareStringOrdinalIgnoreCase (char* pString1, int length1, char* pString2, int length2)
		{
			var ti = CultureInfo.InvariantCulture.TextInfo;

			int index = 0;
			while (index < length1 && index < length2 && ti.ToUpper (*pString1) == ti.ToUpper (*pString2)) {
				++index;
				++pString1;
				++pString2;
			}

			if (index >= length1) {
				if (index >= length2)
					return 0;

				return -1;
			}

			if (index >= length2)
				return 1;

			return ti.ToUpper (*pString1) - ti.ToUpper (*pString2);
		}

		internal static int IndexOfOrdinalCore (string source, string value, int startIndex, int count, bool ignoreCase)
		{
			if (!ignoreCase)
				return source.IndexOfUnchecked (value, startIndex, count);

			return source.IndexOfUncheckedIgnoreCase (value, startIndex, count);
		}

		internal static unsafe int LastIndexOfOrdinalCore (string source, string value, int startIndex, int count, bool ignoreCase)
		{
			if (!ignoreCase)
				return source.LastIndexOfUnchecked (value, startIndex, count);

			return source.LastIndexOfUncheckedIgnoreCase (value, startIndex, count);
		}

		int LastIndexOfCore (string source, string target, int startIndex, int count, CompareOptions options)
		{
			return internal_index_switch (source, startIndex, count, target, options, false);
		}

		unsafe int IndexOfCore (string source, string target, int startIndex, int count, CompareOptions options, int* matchLengthPtr)
		{
			if (matchLengthPtr != null)
				throw new NotImplementedException ();

			return internal_index_switch (source, startIndex, count, target, options, true);
		}

		unsafe int IndexOfCore (ReadOnlySpan<char> source, ReadOnlySpan<char> target, CompareOptions options, int* matchLengthPtr)
		{
			// TODO: optimize

			var s = new string (source);
			var t = new string (target);

			return IndexOfCore (s, t, 0, s.Length, options, matchLengthPtr);
		}

		int IndexOfOrdinalCore (ReadOnlySpan<char> source, ReadOnlySpan<char> value, bool ignoreCase)
		{
			// TODO: optimize

			var s = new string (source);
			var v = new string (value);

			if (!ignoreCase)
				return s.IndexOfUnchecked (v, 0, s.Length);

			return s.IndexOfUncheckedIgnoreCase (v, 0, s.Length);
		}

		int CompareString (ReadOnlySpan<char> string1, string string2, CompareOptions options)
		{
			// TODO: optimize
			
			var s1 = new string (string1);

			return internal_compare_switch (s1, 0, s1.Length, string2, 0, string2.Length, options);
		}

		int CompareString (ReadOnlySpan<char> string1, ReadOnlySpan<char> string2, CompareOptions options)
		{
			// TODO: optimize

			var s1 = new string (string1);
			var s2 = new string (string2);

			return internal_compare_switch (s1, 0, s1.Length, new string (s2), 0, s2.Length, options);
		}

		unsafe static bool IsSortable (char *text, int length)
		{
			return Mono.Globalization.Unicode.MSCompatUnicodeTable.IsSortable (new string (text, 0, length));
		}

		SortKey CreateSortKey (String source, CompareOptions options)
		{
			if (source == null)
				throw new ArgumentNullException (nameof (source));

			if ((options & ValidSortkeyCtorMaskOffFlags) != 0)
				throw new ArgumentException (SR.Argument_InvalidFlag, nameof (options));

			return CreateSortKeyCore (source, options);
		}

		bool StartsWith (string source, string prefix, CompareOptions options)
		{
			if (UseManagedCollation)
				return GetCollator ().IsPrefix (source, prefix, options);

			if (source.Length < prefix.Length)
				return false;

			return Compare (source, 0, prefix.Length, prefix, 0, prefix.Length, options) == 0;
		}

		bool StartsWith (ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options)
		{
			// TODO: optimize
			return StartsWith (new string (source), new string (prefix), options);
		}

		bool EndsWith (string source, string suffix, CompareOptions options)
		{
			if (UseManagedCollation)
				return GetCollator ().IsSuffix (source, suffix, options);

			if (source.Length < suffix.Length)
				return false;

			return Compare (source, source.Length - suffix.Length, suffix.Length, suffix, 0, suffix.Length, options) == 0;
		}

		bool EndsWith (ReadOnlySpan<char> source, ReadOnlySpan<char> suffix, CompareOptions options)
		{
			// TODO: optimize
			return EndsWith (new string (source), new string (suffix), options);
		}

		internal int GetHashCodeOfStringCore (string source, CompareOptions options)
		{
			return GetSortKey (source, options).GetHashCode ();
		}

		SortVersion GetSortVersion ()
		{
			throw new NotImplementedException ();
		}
	}
}