mscorlib(4.0.0.0) API with additions
CompareInfo.cs
1 using Microsoft.Win32;
2 using System.Reflection;
5 using System.Security;
6 
7 namespace System.Globalization
8 {
10  [Serializable]
11  [ComVisible(true)]
12  [__DynamicallyInvokable]
14  {
15  private const CompareOptions ValidIndexMaskOffFlags = ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth);
16 
17  private const CompareOptions ValidCompareMaskOffFlags = ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth | CompareOptions.StringSort);
18 
19  private const CompareOptions ValidHashCodeOfStringMaskOffFlags = ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth);
20 
21  [OptionalField(VersionAdded = 2)]
22  private string m_name;
23 
24  [NonSerialized]
25  private string m_sortName;
26 
27  [NonSerialized]
28  private IntPtr m_dataHandle;
29 
30  [NonSerialized]
31  private IntPtr m_handleOrigin;
32 
33  [OptionalField(VersionAdded = 1)]
34  private int win32LCID;
35 
36  private int culture;
37 
38  private const int LINGUISTIC_IGNORECASE = 16;
39 
40  private const int NORM_IGNORECASE = 1;
41 
42  private const int NORM_IGNOREKANATYPE = 65536;
43 
44  private const int LINGUISTIC_IGNOREDIACRITIC = 32;
45 
46  private const int NORM_IGNORENONSPACE = 2;
47 
48  private const int NORM_IGNORESYMBOLS = 4;
49 
50  private const int NORM_IGNOREWIDTH = 131072;
51 
52  private const int SORT_STRINGSORT = 4096;
53 
54  private const int COMPARE_OPTIONS_ORDINAL = 1073741824;
55 
56  internal const int NORM_LINGUISTIC_CASING = 134217728;
57 
58  private const int RESERVED_FIND_ASCII_STRING = 536870912;
59 
60  private const int SORT_VERSION_WHIDBEY = 4096;
61 
62  private const int SORT_VERSION_V4 = 393473;
63 
64  [OptionalField(VersionAdded = 3)]
65  private SortVersion m_SortVersion;
66 
69  [ComVisible(false)]
70  [__DynamicallyInvokable]
71  public virtual string Name
72  {
73  [__DynamicallyInvokable]
74  get
75  {
76  if (m_name == "zh-CHT" || m_name == "zh-CHS")
77  {
78  return m_name;
79  }
80  return m_sortName;
81  }
82  }
83 
87 
88  internal static bool IsLegacy20SortingBehaviorRequested => InternalSortVersion == 4096;
89 
90  private static uint InternalSortVersion
91  {
92  [SecuritySafeCritical]
93  get
94  {
95  return InternalGetSortVersion();
96  }
97  }
98 
101  public SortVersion Version
102  {
103  [SecuritySafeCritical]
104  get
105  {
106  if (m_SortVersion == null)
107  {
108  Win32Native.NlsVersionInfoEx lpNlsVersionInformation = default(Win32Native.NlsVersionInfoEx);
109  lpNlsVersionInformation.dwNLSVersionInfoSize = Marshal.SizeOf(typeof(Win32Native.NlsVersionInfoEx));
110  InternalGetNlsVersionEx(m_dataHandle, m_handleOrigin, m_sortName, ref lpNlsVersionInformation);
111  m_SortVersion = new SortVersion(lpNlsVersionInformation.dwNLSVersion, (lpNlsVersionInformation.dwEffectiveId != 0) ? lpNlsVersionInformation.dwEffectiveId : LCID, lpNlsVersionInformation.guidCustomVersion);
112  }
113  return m_SortVersion;
114  }
115  }
116 
117  internal CompareInfo(CultureInfo culture)
118  {
119  m_name = culture.m_name;
120  m_sortName = culture.SortName;
121  m_dataHandle = InternalInitSortHandle(m_sortName, out IntPtr handleOrigin);
122  m_handleOrigin = handleOrigin;
123  }
124 
133  public static CompareInfo GetCompareInfo(int culture, Assembly assembly)
134  {
135  if (assembly == null)
136  {
137  throw new ArgumentNullException("assembly");
138  }
139  if (assembly != typeof(object).Module.Assembly)
140  {
141  throw new ArgumentException(Environment.GetResourceString("Argument_OnlyMscorlib"));
142  }
143  return GetCompareInfo(culture);
144  }
145 
156  public static CompareInfo GetCompareInfo(string name, Assembly assembly)
157  {
158  if (name == null || assembly == null)
159  {
160  throw new ArgumentNullException((name == null) ? "name" : "assembly");
161  }
162  if (assembly != typeof(object).Module.Assembly)
163  {
164  throw new ArgumentException(Environment.GetResourceString("Argument_OnlyMscorlib"));
165  }
166  return GetCompareInfo(name);
167  }
168 
172  public static CompareInfo GetCompareInfo(int culture)
173  {
174  if (CultureData.IsCustomCultureId(culture))
175  {
176  throw new ArgumentException(Environment.GetResourceString("Argument_CustomCultureCannotBePassedByNumber", "culture"));
177  }
178  return CultureInfo.GetCultureInfo(culture).CompareInfo;
179  }
180 
188  [__DynamicallyInvokable]
189  public static CompareInfo GetCompareInfo(string name)
190  {
191  if (name == null)
192  {
193  throw new ArgumentNullException("name");
194  }
196  }
197 
202  [ComVisible(false)]
203  public static bool IsSortable(char ch)
204  {
205  return IsSortable(ch.ToString());
206  }
207 
214  [SecuritySafeCritical]
215  [ComVisible(false)]
216  public static bool IsSortable(string text)
217  {
218  if (text == null)
219  {
220  throw new ArgumentNullException("text");
221  }
222  if (text.Length == 0)
223  {
224  return false;
225  }
227  return InternalIsSortable(compareInfo.m_dataHandle, compareInfo.m_handleOrigin, compareInfo.m_sortName, text, text.Length);
228  }
229 
230  [OnDeserializing]
231  private void OnDeserializing(StreamingContext ctx)
232  {
233  m_name = null;
234  }
235 
236  private void OnDeserialized()
237  {
238  CultureInfo cultureInfo;
239  if (m_name == null)
240  {
241  cultureInfo = CultureInfo.GetCultureInfo(culture);
242  m_name = cultureInfo.m_name;
243  }
244  else
245  {
246  cultureInfo = CultureInfo.GetCultureInfo(m_name);
247  }
248  m_sortName = cultureInfo.SortName;
249  m_dataHandle = InternalInitSortHandle(m_sortName, out IntPtr handleOrigin);
250  m_handleOrigin = handleOrigin;
251  }
252 
253  [OnDeserialized]
254  private void OnDeserialized(StreamingContext ctx)
255  {
256  OnDeserialized();
257  }
258 
259  [OnSerializing]
260  private void OnSerializing(StreamingContext ctx)
261  {
262  culture = CultureInfo.GetCultureInfo(Name).LCID;
263  }
264 
267  void IDeserializationCallback.OnDeserialization(object sender)
268  {
269  OnDeserialized();
270  }
271 
272  internal static int GetNativeCompareFlags(CompareOptions options)
273  {
274  int num = 134217728;
275  if ((options & CompareOptions.IgnoreCase) != 0)
276  {
277  num |= 1;
278  }
279  if ((options & CompareOptions.IgnoreKanaType) != 0)
280  {
281  num |= 0x10000;
282  }
283  if ((options & CompareOptions.IgnoreNonSpace) != 0)
284  {
285  num |= 2;
286  }
287  if ((options & CompareOptions.IgnoreSymbols) != 0)
288  {
289  num |= 4;
290  }
291  if ((options & CompareOptions.IgnoreWidth) != 0)
292  {
293  num |= 0x20000;
294  }
295  if ((options & CompareOptions.StringSort) != 0)
296  {
297  num |= 0x1000;
298  }
299  if (options == CompareOptions.Ordinal)
300  {
301  num = 1073741824;
302  }
303  return num;
304  }
305 
312  [__DynamicallyInvokable]
313  public virtual int Compare(string string1, string string2)
314  {
315  return Compare(string1, string2, CompareOptions.None);
316  }
317 
327  [SecuritySafeCritical]
328  [__DynamicallyInvokable]
329  public virtual int Compare(string string1, string string2, CompareOptions options)
330  {
331  if (options == CompareOptions.OrdinalIgnoreCase)
332  {
333  return string.Compare(string1, string2, StringComparison.OrdinalIgnoreCase);
334  }
335  if ((options & CompareOptions.Ordinal) != 0)
336  {
337  if (options != CompareOptions.Ordinal)
338  {
339  throw new ArgumentException(Environment.GetResourceString("Argument_CompareOptionOrdinal"), "options");
340  }
341  return string.CompareOrdinal(string1, string2);
342  }
343  if ((options & ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth | CompareOptions.StringSort)) != 0)
344  {
345  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
346  }
347  if (string1 == null)
348  {
349  if (string2 == null)
350  {
351  return 0;
352  }
353  return -1;
354  }
355  if (string2 == null)
356  {
357  return 1;
358  }
359  return InternalCompareString(m_dataHandle, m_handleOrigin, m_sortName, string1, 0, string1.Length, string2, 0, string2.Length, GetNativeCompareFlags(options));
360  }
361 
376  [__DynamicallyInvokable]
377  public virtual int Compare(string string1, int offset1, int length1, string string2, int offset2, int length2)
378  {
379  return Compare(string1, offset1, length1, string2, offset2, length2, CompareOptions.None);
380  }
381 
395  [__DynamicallyInvokable]
396  public virtual int Compare(string string1, int offset1, string string2, int offset2, CompareOptions options)
397  {
398  return Compare(string1, offset1, (string1 != null) ? (string1.Length - offset1) : 0, string2, offset2, (string2 != null) ? (string2.Length - offset2) : 0, options);
399  }
400 
411  [__DynamicallyInvokable]
412  public virtual int Compare(string string1, int offset1, string string2, int offset2)
413  {
414  return Compare(string1, offset1, string2, offset2, CompareOptions.None);
415  }
416 
434  [SecuritySafeCritical]
435  [__DynamicallyInvokable]
436  public virtual int Compare(string string1, int offset1, int length1, string string2, int offset2, int length2, CompareOptions options)
437  {
438  if (options == CompareOptions.OrdinalIgnoreCase)
439  {
440  int num = string.Compare(string1, offset1, string2, offset2, (length1 < length2) ? length1 : length2, StringComparison.OrdinalIgnoreCase);
441  if (length1 != length2 && num == 0)
442  {
443  if (length1 <= length2)
444  {
445  return -1;
446  }
447  return 1;
448  }
449  return num;
450  }
451  if (length1 < 0 || length2 < 0)
452  {
453  throw new ArgumentOutOfRangeException((length1 < 0) ? "length1" : "length2", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
454  }
455  if (offset1 < 0 || offset2 < 0)
456  {
457  throw new ArgumentOutOfRangeException((offset1 < 0) ? "offset1" : "offset2", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
458  }
459  if (offset1 > (string1?.Length ?? 0) - length1)
460  {
461  throw new ArgumentOutOfRangeException("string1", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength"));
462  }
463  if (offset2 > (string2?.Length ?? 0) - length2)
464  {
465  throw new ArgumentOutOfRangeException("string2", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength"));
466  }
467  if ((options & CompareOptions.Ordinal) != 0)
468  {
469  if (options != CompareOptions.Ordinal)
470  {
471  throw new ArgumentException(Environment.GetResourceString("Argument_CompareOptionOrdinal"), "options");
472  }
473  }
474  else if ((options & ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth | CompareOptions.StringSort)) != 0)
475  {
476  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
477  }
478  if (string1 == null)
479  {
480  if (string2 == null)
481  {
482  return 0;
483  }
484  return -1;
485  }
486  if (string2 == null)
487  {
488  return 1;
489  }
490  if (options == CompareOptions.Ordinal)
491  {
492  return CompareOrdinal(string1, offset1, length1, string2, offset2, length2);
493  }
494  return InternalCompareString(m_dataHandle, m_handleOrigin, m_sortName, string1, offset1, length1, string2, offset2, length2, GetNativeCompareFlags(options));
495  }
496 
497  [SecurityCritical]
498  private static int CompareOrdinal(string string1, int offset1, int length1, string string2, int offset2, int length2)
499  {
500  int num = string.nativeCompareOrdinalEx(string1, offset1, string2, offset2, (length1 < length2) ? length1 : length2);
501  if (length1 != length2 && num == 0)
502  {
503  if (length1 <= length2)
504  {
505  return -1;
506  }
507  return 1;
508  }
509  return num;
510  }
511 
523  [SecuritySafeCritical]
524  [__DynamicallyInvokable]
525  public virtual bool IsPrefix(string source, string prefix, CompareOptions options)
526  {
527  if (source == null || prefix == null)
528  {
529  throw new ArgumentNullException((source == null) ? "source" : "prefix", Environment.GetResourceString("ArgumentNull_String"));
530  }
531  if (prefix.Length == 0)
532  {
533  return true;
534  }
535  switch (options)
536  {
537  case CompareOptions.OrdinalIgnoreCase:
538  return source.StartsWith(prefix, StringComparison.OrdinalIgnoreCase);
539  case CompareOptions.Ordinal:
540  return source.StartsWith(prefix, StringComparison.Ordinal);
541  default:
542  if ((options & ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth)) != 0)
543  {
544  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
545  }
546  return InternalFindNLSStringEx(m_dataHandle, m_handleOrigin, m_sortName, GetNativeCompareFlags(options) | 0x100000 | ((source.IsAscii() && prefix.IsAscii()) ? 536870912 : 0), source, source.Length, 0, prefix, prefix.Length) > -1;
547  }
548  }
549 
558  [__DynamicallyInvokable]
559  public virtual bool IsPrefix(string source, string prefix)
560  {
561  return IsPrefix(source, prefix, CompareOptions.None);
562  }
563 
575  [SecuritySafeCritical]
576  [__DynamicallyInvokable]
577  public virtual bool IsSuffix(string source, string suffix, CompareOptions options)
578  {
579  if (source == null || suffix == null)
580  {
581  throw new ArgumentNullException((source == null) ? "source" : "suffix", Environment.GetResourceString("ArgumentNull_String"));
582  }
583  if (suffix.Length == 0)
584  {
585  return true;
586  }
587  switch (options)
588  {
589  case CompareOptions.OrdinalIgnoreCase:
590  return source.EndsWith(suffix, StringComparison.OrdinalIgnoreCase);
591  case CompareOptions.Ordinal:
592  return source.EndsWith(suffix, StringComparison.Ordinal);
593  default:
594  if ((options & ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth)) != 0)
595  {
596  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
597  }
598  return InternalFindNLSStringEx(m_dataHandle, m_handleOrigin, m_sortName, GetNativeCompareFlags(options) | 0x200000 | ((source.IsAscii() && suffix.IsAscii()) ? 536870912 : 0), source, source.Length, source.Length - 1, suffix, suffix.Length) >= 0;
599  }
600  }
601 
610  [__DynamicallyInvokable]
611  public virtual bool IsSuffix(string source, string suffix)
612  {
613  return IsSuffix(source, suffix, CompareOptions.None);
614  }
615 
622  [__DynamicallyInvokable]
623  public virtual int IndexOf(string source, char value)
624  {
625  if (source == null)
626  {
627  throw new ArgumentNullException("source");
628  }
629  return IndexOf(source, value, 0, source.Length, CompareOptions.None);
630  }
631 
639  [__DynamicallyInvokable]
640  public virtual int IndexOf(string source, string value)
641  {
642  if (source == null)
643  {
644  throw new ArgumentNullException("source");
645  }
646  return IndexOf(source, value, 0, source.Length, CompareOptions.None);
647  }
648 
658  [__DynamicallyInvokable]
659  public virtual int IndexOf(string source, char value, CompareOptions options)
660  {
661  if (source == null)
662  {
663  throw new ArgumentNullException("source");
664  }
665  return IndexOf(source, value, 0, source.Length, options);
666  }
667 
678  [__DynamicallyInvokable]
679  public virtual int IndexOf(string source, string value, CompareOptions options)
680  {
681  if (source == null)
682  {
683  throw new ArgumentNullException("source");
684  }
685  return IndexOf(source, value, 0, source.Length, options);
686  }
687 
697  public virtual int IndexOf(string source, char value, int startIndex)
698  {
699  if (source == null)
700  {
701  throw new ArgumentNullException("source");
702  }
703  return IndexOf(source, value, startIndex, source.Length - startIndex, CompareOptions.None);
704  }
705 
716  public virtual int IndexOf(string source, string value, int startIndex)
717  {
718  if (source == null)
719  {
720  throw new ArgumentNullException("source");
721  }
722  return IndexOf(source, value, startIndex, source.Length - startIndex, CompareOptions.None);
723  }
724 
737  [__DynamicallyInvokable]
738  public virtual int IndexOf(string source, char value, int startIndex, CompareOptions options)
739  {
740  if (source == null)
741  {
742  throw new ArgumentNullException("source");
743  }
744  return IndexOf(source, value, startIndex, source.Length - startIndex, options);
745  }
746 
760  [__DynamicallyInvokable]
761  public virtual int IndexOf(string source, string value, int startIndex, CompareOptions options)
762  {
763  if (source == null)
764  {
765  throw new ArgumentNullException("source");
766  }
767  return IndexOf(source, value, startIndex, source.Length - startIndex, options);
768  }
769 
782  [__DynamicallyInvokable]
783  public virtual int IndexOf(string source, char value, int startIndex, int count)
784  {
785  return IndexOf(source, value, startIndex, count, CompareOptions.None);
786  }
787 
801  [__DynamicallyInvokable]
802  public virtual int IndexOf(string source, string value, int startIndex, int count)
803  {
804  return IndexOf(source, value, startIndex, count, CompareOptions.None);
805  }
806 
822  [SecuritySafeCritical]
823  [__DynamicallyInvokable]
824  public virtual int IndexOf(string source, char value, int startIndex, int count, CompareOptions options)
825  {
826  if (source == null)
827  {
828  throw new ArgumentNullException("source");
829  }
830  if (startIndex < 0 || startIndex > source.Length)
831  {
832  throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
833  }
834  if (count < 0 || startIndex > source.Length - count)
835  {
836  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
837  }
838  if (options == CompareOptions.OrdinalIgnoreCase)
839  {
840  return source.IndexOf(value.ToString(), startIndex, count, StringComparison.OrdinalIgnoreCase);
841  }
842  if ((options & ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth)) != 0 && options != CompareOptions.Ordinal)
843  {
844  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
845  }
846  return InternalFindNLSStringEx(m_dataHandle, m_handleOrigin, m_sortName, GetNativeCompareFlags(options) | 0x400000 | ((source.IsAscii() && value <= '\u007f') ? 536870912 : 0), source, count, startIndex, new string(value, 1), 1);
847  }
848 
865  [SecuritySafeCritical]
866  [__DynamicallyInvokable]
867  public virtual int IndexOf(string source, string value, int startIndex, int count, CompareOptions options)
868  {
869  if (source == null)
870  {
871  throw new ArgumentNullException("source");
872  }
873  if (value == null)
874  {
875  throw new ArgumentNullException("value");
876  }
877  if (startIndex > source.Length)
878  {
879  throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
880  }
881  if (source.Length == 0)
882  {
883  if (value.Length == 0)
884  {
885  return 0;
886  }
887  return -1;
888  }
889  if (startIndex < 0)
890  {
891  throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
892  }
893  if (count < 0 || startIndex > source.Length - count)
894  {
895  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
896  }
897  if (options == CompareOptions.OrdinalIgnoreCase)
898  {
899  return source.IndexOf(value, startIndex, count, StringComparison.OrdinalIgnoreCase);
900  }
901  if ((options & ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth)) != 0 && options != CompareOptions.Ordinal)
902  {
903  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
904  }
905  return InternalFindNLSStringEx(m_dataHandle, m_handleOrigin, m_sortName, GetNativeCompareFlags(options) | 0x400000 | ((source.IsAscii() && value.IsAscii()) ? 536870912 : 0), source, count, startIndex, value, value.Length);
906  }
907 
914  [__DynamicallyInvokable]
915  public virtual int LastIndexOf(string source, char value)
916  {
917  if (source == null)
918  {
919  throw new ArgumentNullException("source");
920  }
921  return LastIndexOf(source, value, source.Length - 1, source.Length, CompareOptions.None);
922  }
923 
931  [__DynamicallyInvokable]
932  public virtual int LastIndexOf(string source, string value)
933  {
934  if (source == null)
935  {
936  throw new ArgumentNullException("source");
937  }
938  return LastIndexOf(source, value, source.Length - 1, source.Length, CompareOptions.None);
939  }
940 
950  [__DynamicallyInvokable]
951  public virtual int LastIndexOf(string source, char value, CompareOptions options)
952  {
953  if (source == null)
954  {
955  throw new ArgumentNullException("source");
956  }
957  return LastIndexOf(source, value, source.Length - 1, source.Length, options);
958  }
959 
970  [__DynamicallyInvokable]
971  public virtual int LastIndexOf(string source, string value, CompareOptions options)
972  {
973  if (source == null)
974  {
975  throw new ArgumentNullException("source");
976  }
977  return LastIndexOf(source, value, source.Length - 1, source.Length, options);
978  }
979 
989  public virtual int LastIndexOf(string source, char value, int startIndex)
990  {
991  return LastIndexOf(source, value, startIndex, startIndex + 1, CompareOptions.None);
992  }
993 
1004  public virtual int LastIndexOf(string source, string value, int startIndex)
1005  {
1006  return LastIndexOf(source, value, startIndex, startIndex + 1, CompareOptions.None);
1007  }
1008 
1021  [__DynamicallyInvokable]
1022  public virtual int LastIndexOf(string source, char value, int startIndex, CompareOptions options)
1023  {
1024  return LastIndexOf(source, value, startIndex, startIndex + 1, options);
1025  }
1026 
1040  [__DynamicallyInvokable]
1041  public virtual int LastIndexOf(string source, string value, int startIndex, CompareOptions options)
1042  {
1043  return LastIndexOf(source, value, startIndex, startIndex + 1, options);
1044  }
1045 
1058  [__DynamicallyInvokable]
1059  public virtual int LastIndexOf(string source, char value, int startIndex, int count)
1060  {
1061  return LastIndexOf(source, value, startIndex, count, CompareOptions.None);
1062  }
1063 
1077  [__DynamicallyInvokable]
1078  public virtual int LastIndexOf(string source, string value, int startIndex, int count)
1079  {
1080  return LastIndexOf(source, value, startIndex, count, CompareOptions.None);
1081  }
1082 
1098  [SecuritySafeCritical]
1099  [__DynamicallyInvokable]
1100  public virtual int LastIndexOf(string source, char value, int startIndex, int count, CompareOptions options)
1101  {
1102  if (source == null)
1103  {
1104  throw new ArgumentNullException("source");
1105  }
1106  if ((options & ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth)) != 0 && options != CompareOptions.Ordinal && options != CompareOptions.OrdinalIgnoreCase)
1107  {
1108  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
1109  }
1110  if (source.Length == 0 && (startIndex == -1 || startIndex == 0))
1111  {
1112  return -1;
1113  }
1114  if (startIndex < 0 || startIndex > source.Length)
1115  {
1116  throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
1117  }
1118  if (startIndex == source.Length)
1119  {
1120  startIndex--;
1121  if (count > 0)
1122  {
1123  count--;
1124  }
1125  }
1126  if (count < 0 || startIndex - count + 1 < 0)
1127  {
1128  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
1129  }
1130  if (options == CompareOptions.OrdinalIgnoreCase)
1131  {
1132  return source.LastIndexOf(value.ToString(), startIndex, count, StringComparison.OrdinalIgnoreCase);
1133  }
1134  return InternalFindNLSStringEx(m_dataHandle, m_handleOrigin, m_sortName, GetNativeCompareFlags(options) | 0x800000 | ((source.IsAscii() && value <= '\u007f') ? 536870912 : 0), source, count, startIndex, new string(value, 1), 1);
1135  }
1136 
1153  [SecuritySafeCritical]
1154  [__DynamicallyInvokable]
1155  public virtual int LastIndexOf(string source, string value, int startIndex, int count, CompareOptions options)
1156  {
1157  if (source == null)
1158  {
1159  throw new ArgumentNullException("source");
1160  }
1161  if (value == null)
1162  {
1163  throw new ArgumentNullException("value");
1164  }
1165  if ((options & ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth)) != 0 && options != CompareOptions.Ordinal && options != CompareOptions.OrdinalIgnoreCase)
1166  {
1167  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
1168  }
1169  if (source.Length == 0 && (startIndex == -1 || startIndex == 0))
1170  {
1171  if (value.Length != 0)
1172  {
1173  return -1;
1174  }
1175  return 0;
1176  }
1177  if (startIndex < 0 || startIndex > source.Length)
1178  {
1179  throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
1180  }
1181  if (startIndex == source.Length)
1182  {
1183  startIndex--;
1184  if (count > 0)
1185  {
1186  count--;
1187  }
1188  if (value.Length == 0 && count >= 0 && startIndex - count + 1 >= 0)
1189  {
1190  return startIndex;
1191  }
1192  }
1193  if (count < 0 || startIndex - count + 1 < 0)
1194  {
1195  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
1196  }
1197  if (options == CompareOptions.OrdinalIgnoreCase)
1198  {
1199  return source.LastIndexOf(value, startIndex, count, StringComparison.OrdinalIgnoreCase);
1200  }
1201  return InternalFindNLSStringEx(m_dataHandle, m_handleOrigin, m_sortName, GetNativeCompareFlags(options) | 0x800000 | ((source.IsAscii() && value.IsAscii()) ? 536870912 : 0), source, count, startIndex, value, value.Length);
1202  }
1203 
1210  public virtual SortKey GetSortKey(string source, CompareOptions options)
1211  {
1212  return CreateSortKey(source, options);
1213  }
1214 
1218  public virtual SortKey GetSortKey(string source)
1219  {
1220  return CreateSortKey(source, CompareOptions.None);
1221  }
1222 
1223  [SecuritySafeCritical]
1224  private SortKey CreateSortKey(string source, CompareOptions options)
1225  {
1226  if (source == null)
1227  {
1228  throw new ArgumentNullException("source");
1229  }
1230  if ((options & ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth | CompareOptions.StringSort)) != 0)
1231  {
1232  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
1233  }
1234  byte[] array = null;
1235  if (string.IsNullOrEmpty(source))
1236  {
1237  array = EmptyArray<byte>.Value;
1238  source = "\0";
1239  }
1240  int nativeCompareFlags = GetNativeCompareFlags(options);
1241  int num = InternalGetSortKey(m_dataHandle, m_handleOrigin, m_sortName, nativeCompareFlags, source, source.Length, null, 0);
1242  if (num == 0)
1243  {
1244  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "source");
1245  }
1246  if (array == null)
1247  {
1248  array = new byte[num];
1249  num = InternalGetSortKey(m_dataHandle, m_handleOrigin, m_sortName, nativeCompareFlags, source, source.Length, array, array.Length);
1250  }
1251  else
1252  {
1253  source = string.Empty;
1254  }
1255  return new SortKey(Name, source, options, array);
1256  }
1257 
1262  [__DynamicallyInvokable]
1263  public override bool Equals(object value)
1264  {
1265  CompareInfo compareInfo = value as CompareInfo;
1266  if (compareInfo != null)
1267  {
1268  return Name == compareInfo.Name;
1269  }
1270  return false;
1271  }
1272 
1275  [__DynamicallyInvokable]
1276  public override int GetHashCode()
1277  {
1278  return Name.GetHashCode();
1279  }
1280 
1287  [__DynamicallyInvokable]
1288  public virtual int GetHashCode(string source, CompareOptions options)
1289  {
1290  if (source == null)
1291  {
1292  throw new ArgumentNullException("source");
1293  }
1294  switch (options)
1295  {
1296  case CompareOptions.Ordinal:
1297  return source.GetHashCode();
1298  case CompareOptions.OrdinalIgnoreCase:
1299  return TextInfo.GetHashCodeOrdinalIgnoreCase(source);
1300  default:
1301  return GetHashCodeOfString(source, options, forceRandomizedHashing: false, 0L);
1302  }
1303  }
1304 
1305  internal int GetHashCodeOfString(string source, CompareOptions options)
1306  {
1307  return GetHashCodeOfString(source, options, forceRandomizedHashing: false, 0L);
1308  }
1309 
1310  [SecuritySafeCritical]
1311  internal int GetHashCodeOfString(string source, CompareOptions options, bool forceRandomizedHashing, long additionalEntropy)
1312  {
1313  if (source == null)
1314  {
1315  throw new ArgumentNullException("source");
1316  }
1317  if ((options & ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth)) != 0)
1318  {
1319  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
1320  }
1321  if (source.Length == 0)
1322  {
1323  return 0;
1324  }
1325  return InternalGetGlobalizedHashCode(m_dataHandle, m_handleOrigin, m_sortName, source, source.Length, GetNativeCompareFlags(options), forceRandomizedHashing, additionalEntropy);
1326  }
1327 
1330  [__DynamicallyInvokable]
1331  public override string ToString()
1332  {
1333  return "CompareInfo - " + Name;
1334  }
1335 
1336  [SecuritySafeCritical]
1337  internal static IntPtr InternalInitSortHandle(string localeName, out IntPtr handleOrigin)
1338  {
1339  return NativeInternalInitSortHandle(localeName, out handleOrigin);
1340  }
1341 
1342  [DllImport("QCall", CharSet = CharSet.Unicode)]
1343  [SecurityCritical]
1344  [SuppressUnmanagedCodeSecurity]
1345  [return: MarshalAs(UnmanagedType.Bool)]
1346  private static extern bool InternalGetNlsVersionEx(IntPtr handle, IntPtr handleOrigin, string localeName, ref Win32Native.NlsVersionInfoEx lpNlsVersionInformation);
1347 
1348  [DllImport("QCall", CharSet = CharSet.Unicode)]
1349  [SecurityCritical]
1350  [SuppressUnmanagedCodeSecurity]
1351  private static extern uint InternalGetSortVersion();
1352 
1353  [DllImport("QCall", CharSet = CharSet.Unicode)]
1354  [SecurityCritical]
1355  [SuppressUnmanagedCodeSecurity]
1356  private static extern IntPtr NativeInternalInitSortHandle(string localeName, out IntPtr handleOrigin);
1357 
1358  [DllImport("QCall", CharSet = CharSet.Unicode)]
1359  [SecurityCritical]
1360  [SuppressUnmanagedCodeSecurity]
1361  private static extern int InternalGetGlobalizedHashCode(IntPtr handle, IntPtr handleOrigin, string localeName, string source, int length, int dwFlags, bool forceRandomizedHashing, long additionalEntropy);
1362 
1363  [DllImport("QCall", CharSet = CharSet.Unicode)]
1364  [SecurityCritical]
1365  [SuppressUnmanagedCodeSecurity]
1366  [return: MarshalAs(UnmanagedType.Bool)]
1367  private static extern bool InternalIsSortable(IntPtr handle, IntPtr handleOrigin, string localeName, string source, int length);
1368 
1369  [DllImport("QCall", CharSet = CharSet.Unicode)]
1370  [SecurityCritical]
1371  [SuppressUnmanagedCodeSecurity]
1372  private static extern int InternalCompareString(IntPtr handle, IntPtr handleOrigin, string localeName, string string1, int offset1, int length1, string string2, int offset2, int length2, int flags);
1373 
1374  [DllImport("QCall", CharSet = CharSet.Unicode)]
1375  [SecurityCritical]
1376  [SuppressUnmanagedCodeSecurity]
1377  private static extern int InternalFindNLSStringEx(IntPtr handle, IntPtr handleOrigin, string localeName, int flags, string source, int sourceCount, int startIndex, string target, int targetCount);
1378 
1379  [DllImport("QCall", CharSet = CharSet.Unicode)]
1380  [SecurityCritical]
1381  [SuppressUnmanagedCodeSecurity]
1382  private static extern int InternalGetSortKey(IntPtr handle, IntPtr handleOrigin, string localeName, int flags, string source, int sourceCount, byte[] target, int targetCount);
1383  }
1384 }
Performs reflection on a module.
Definition: Module.cs:17
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
virtual Assembly Assembly
Gets the appropriate T:System.Reflection.Assembly for this instance of T:System.Reflection....
Definition: Module.cs:133
virtual SortKey GetSortKey(string source)
Gets the sort key for the specified string.
virtual int LastIndexOf(string source, string value, CompareOptions options)
Searches for the specified substring and returns the zero-based index of the last occurrence within t...
Definition: CompareInfo.cs:971
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual int LastIndexOf(string source, string value, int startIndex, int count)
Searches for the specified substring and returns the zero-based index of the last occurrence within t...
virtual bool IsSuffix(string source, string suffix)
Determines whether the specified source string ends with the specified suffix.
Definition: CompareInfo.cs:611
virtual CompareInfo? CompareInfo
Gets the T:System.Globalization.CompareInfo that defines how to compare strings for the culture.
Definition: CultureInfo.cs:447
virtual int GetHashCode(string source, CompareOptions options)
Gets the hash code for a string based on specified comparison options.
virtual int Compare(string string1, int offset1, string string2, int offset2)
Compares the end section of a string with the end section of another string.
Definition: CompareInfo.cs:412
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
override bool Equals(object value)
Determines whether the specified object is equal to the current T:System.Globalization....
virtual int LastIndexOf(string source, char value, int startIndex, int count, CompareOptions options)
Searches for the specified character and returns the zero-based index of the last occurrence within t...
Indicates that a class is to be notified when deserialization of the entire object graph has been com...
virtual bool IsPrefix(string source, string prefix)
Determines whether the specified source string starts with the specified prefix.
Definition: CompareInfo.cs:559
virtual int LastIndexOf(string source, string value, int startIndex, CompareOptions options)
Searches for the specified substring and returns the zero-based index of the last occurrence within t...
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static int SizeOf(object structure)
Returns the unmanaged size of an object in bytes.
Definition: Marshal.cs:159
virtual int IndexOf(string source, string value, int startIndex)
Searches for the specified substring and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:716
virtual int LastIndexOf(string source, char value, CompareOptions options)
Searches for the specified character and returns the zero-based index of the last occurrence within t...
Definition: CompareInfo.cs:951
override int GetHashCode()
Serves as a hash function for the current T:System.Globalization.CompareInfo for hashing algorithms a...
virtual int IndexOf(string source, string value)
Searches for the specified substring and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:640
static CompareInfo GetCompareInfo(int culture, Assembly assembly)
Initializes a new T:System.Globalization.CompareInfo object that is associated with the specified cul...
Definition: CompareInfo.cs:133
override string ToString()
Returns a string that represents the current T:System.Globalization.CompareInfo object.
static CompareInfo GetCompareInfo(string name, Assembly assembly)
Initializes a new T:System.Globalization.CompareInfo object that is associated with the specified cul...
Definition: CompareInfo.cs:156
virtual int IndexOf(string source, string value, CompareOptions options)
Searches for the specified substring and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:679
virtual int Compare(string string1, int offset1, string string2, int offset2, CompareOptions options)
Compares the end section of a string with the end section of another string using the specified T:Sys...
Definition: CompareInfo.cs:396
static CompareInfo GetCompareInfo(int culture)
Initializes a new T:System.Globalization.CompareInfo object that is associated with the culture with ...
Definition: CompareInfo.cs:172
virtual int IndexOf(string source, char value, int startIndex, CompareOptions options)
Searches for the specified character and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:738
Describes the source and destination of a given serialized stream, and provides an additional caller-...
virtual int LastIndexOf(string source, char value)
Searches for the specified character and returns the zero-based index of the last occurrence within t...
Definition: CompareInfo.cs:915
virtual int IndexOf(string source, string value, int startIndex, CompareOptions options)
Searches for the specified substring and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:761
virtual bool IsSuffix(string source, string suffix, CompareOptions options)
Determines whether the specified source string ends with the specified suffix using the specified T:S...
Definition: CompareInfo.cs:577
virtual int Compare(string string1, string string2, CompareOptions options)
Compares two strings using the specified T:System.Globalization.CompareOptions value.
Definition: CompareInfo.cs:329
static CultureInfo GetCultureInfo(int culture)
Retrieves a cached, read-only instance of a culture by using the specified culture identifier.
virtual int IndexOf(string source, string value, int startIndex, int count)
Searches for the specified substring and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:802
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
virtual int IndexOf(string source, char value, int startIndex, int count)
Searches for the specified character and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:783
virtual int LastIndexOf(string source, string value, int startIndex, int count, CompareOptions options)
Searches for the specified substring and returns the zero-based index of the last occurrence within t...
Provides information about the version of Unicode used to compare and order strings.
Definition: SortVersion.cs:5
UnmanagedType
Identifies how to marshal parameters or fields to unmanaged code.
Definition: UnmanagedType.cs:7
void OnDeserialization(object sender)
Runs when the entire object graph has been deserialized.
virtual int IndexOf(string source, char value, int startIndex)
Searches for the specified character and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:697
int LCID
Gets the properly formed culture identifier for the current T:System.Globalization....
Definition: CompareInfo.cs:86
virtual int Compare(string string1, string string2)
Compares two strings.
Definition: CompareInfo.cs:313
virtual int Compare(string string1, int offset1, int length1, string string2, int offset2, int length2, CompareOptions options)
Compares a section of one string with a section of another string using the specified T:System....
Definition: CompareInfo.cs:436
Implements a set of methods for culture-sensitive string comparisons.
Definition: CompareInfo.cs:13
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
virtual int Compare(string string1, int offset1, int length1, string string2, int offset2, int length2)
Compares a section of one string with a section of another string.
Definition: CompareInfo.cs:377
virtual int LastIndexOf(string source, char value, int startIndex, int count)
Searches for the specified character and returns the zero-based index of the last occurrence within t...
static CompareInfo GetCompareInfo(string name)
Initializes a new T:System.Globalization.CompareInfo object that is associated with the culture with ...
Definition: CompareInfo.cs:189
virtual int IndexOf(string source, string value, int startIndex, int count, CompareOptions options)
Searches for the specified substring and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:867
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
Represents the version number of an assembly, operating system, or the common language runtime....
Definition: Version.cs:11
virtual string Name
Gets the name of the culture used for sorting operations by this T:System.Globalization....
Definition: CompareInfo.cs:72
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
virtual int LastIndexOf(string source, char value, int startIndex)
Searches for the specified character and returns the zero-based index of the last occurrence within t...
Definition: CompareInfo.cs:989
virtual int IndexOf(string source, char value, int startIndex, int count, CompareOptions options)
Searches for the specified character and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:824
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual int IndexOf(string source, char value)
Searches for the specified character and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:623
Represents the result of mapping a string to its sort key.
Definition: SortKey.cs:9
virtual bool IsPrefix(string source, string prefix, CompareOptions options)
Determines whether the specified source string starts with the specified prefix using the specified T...
Definition: CompareInfo.cs:525
virtual int LastIndexOf(string source, string value, int startIndex)
Searches for the specified substring and returns the zero-based index of the last occurrence within t...
static bool IsSortable(char ch)
Indicates whether a specified Unicode character is sortable.
Definition: CompareInfo.cs:203
Specifies that the class can be serialized.
static bool IsSortable(string text)
Indicates whether a specified Unicode string is sortable.
Definition: CompareInfo.cs:216
virtual int LCID
Gets the culture identifier for the current T:System.Globalization.CultureInfo.
Definition: CultureInfo.cs:304
virtual int LastIndexOf(string source, char value, int startIndex, CompareOptions options)
Searches for the specified character and returns the zero-based index of the last occurrence within t...
Defines text properties and behaviors, such as casing, that are specific to a writing system.
Definition: TextInfo.cs:13
virtual int IndexOf(string source, char value, CompareOptions options)
Searches for the specified character and returns the zero-based index of the first occurrence within ...
Definition: CompareInfo.cs:659
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
CompareOptions
Defines the string comparison options to use with T:System.Globalization.CompareInfo.
virtual SortKey GetSortKey(string source, CompareOptions options)
Gets a T:System.Globalization.SortKey object for the specified string using the specified T:System....
virtual int LastIndexOf(string source, string value)
Searches for the specified substring and returns the zero-based index of the last occurrence within t...
Definition: CompareInfo.cs:932