CompareInfo.Invariant.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. namespace System.Globalization
  7. {
  8. public partial class CompareInfo
  9. {
  10. internal static unsafe int InvariantIndexOf(string source, string value, int startIndex, int count, bool ignoreCase)
  11. {
  12. Debug.Assert(source != null);
  13. Debug.Assert(value != null);
  14. Debug.Assert(startIndex >= 0 && startIndex < source.Length);
  15. fixed (char* pSource = source) fixed (char* pValue = value)
  16. {
  17. char* pSrc = &pSource[startIndex];
  18. int index = InvariantFindString(pSrc, count, pValue, value.Length, ignoreCase, fromBeginning : true);
  19. if (index >= 0)
  20. {
  21. return index + startIndex;
  22. }
  23. return -1;
  24. }
  25. }
  26. internal static unsafe int InvariantIndexOf(ReadOnlySpan<char> source, ReadOnlySpan<char> value, bool ignoreCase, bool fromBeginning = true)
  27. {
  28. Debug.Assert(source.Length != 0);
  29. Debug.Assert(value.Length != 0);
  30. fixed (char* pSource = &MemoryMarshal.GetReference(source))
  31. fixed (char* pValue = &MemoryMarshal.GetReference(value))
  32. {
  33. return InvariantFindString(pSource, source.Length, pValue, value.Length, ignoreCase, fromBeginning);
  34. }
  35. }
  36. internal static unsafe int InvariantLastIndexOf(string source, string value, int startIndex, int count, bool ignoreCase)
  37. {
  38. Debug.Assert(source != null);
  39. Debug.Assert(value != null);
  40. Debug.Assert(startIndex >= 0 && startIndex < source.Length);
  41. fixed (char* pSource = source) fixed (char* pValue = value)
  42. {
  43. char* pSrc = &pSource[startIndex - count + 1];
  44. int index = InvariantFindString(pSrc, count, pValue, value.Length, ignoreCase, fromBeginning : false);
  45. if (index >= 0)
  46. {
  47. return index + startIndex - count + 1;
  48. }
  49. return -1;
  50. }
  51. }
  52. private static unsafe int InvariantFindString(char* source, int sourceCount, char* value, int valueCount, bool ignoreCase, bool fromBeginning)
  53. {
  54. int ctrSource = 0; // index value into source
  55. int ctrValue = 0; // index value into value
  56. char sourceChar; // Character for case lookup in source
  57. char valueChar; // Character for case lookup in value
  58. int lastSourceStart;
  59. Debug.Assert(source != null);
  60. Debug.Assert(value != null);
  61. Debug.Assert(sourceCount >= 0);
  62. Debug.Assert(valueCount >= 0);
  63. if (valueCount == 0)
  64. {
  65. return fromBeginning ? 0 : sourceCount - 1;
  66. }
  67. if (sourceCount < valueCount)
  68. {
  69. return -1;
  70. }
  71. if (fromBeginning)
  72. {
  73. lastSourceStart = sourceCount - valueCount;
  74. if (ignoreCase)
  75. {
  76. char firstValueChar = InvariantToUpper(value[0]);
  77. for (ctrSource = 0; ctrSource <= lastSourceStart; ctrSource++)
  78. {
  79. sourceChar = InvariantToUpper(source[ctrSource]);
  80. if (sourceChar != firstValueChar)
  81. {
  82. continue;
  83. }
  84. for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
  85. {
  86. sourceChar = InvariantToUpper(source[ctrSource + ctrValue]);
  87. valueChar = InvariantToUpper(value[ctrValue]);
  88. if (sourceChar != valueChar)
  89. {
  90. break;
  91. }
  92. }
  93. if (ctrValue == valueCount)
  94. {
  95. return ctrSource;
  96. }
  97. }
  98. }
  99. else
  100. {
  101. char firstValueChar = value[0];
  102. for (ctrSource = 0; ctrSource <= lastSourceStart; ctrSource++)
  103. {
  104. sourceChar = source[ctrSource];
  105. if (sourceChar != firstValueChar)
  106. {
  107. continue;
  108. }
  109. for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
  110. {
  111. sourceChar = source[ctrSource + ctrValue];
  112. valueChar = value[ctrValue];
  113. if (sourceChar != valueChar)
  114. {
  115. break;
  116. }
  117. }
  118. if (ctrValue == valueCount)
  119. {
  120. return ctrSource;
  121. }
  122. }
  123. }
  124. }
  125. else
  126. {
  127. lastSourceStart = sourceCount - valueCount;
  128. if (ignoreCase)
  129. {
  130. char firstValueChar = InvariantToUpper(value[0]);
  131. for (ctrSource = lastSourceStart; ctrSource >= 0; ctrSource--)
  132. {
  133. sourceChar = InvariantToUpper(source[ctrSource]);
  134. if (sourceChar != firstValueChar)
  135. {
  136. continue;
  137. }
  138. for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
  139. {
  140. sourceChar = InvariantToUpper(source[ctrSource + ctrValue]);
  141. valueChar = InvariantToUpper(value[ctrValue]);
  142. if (sourceChar != valueChar)
  143. {
  144. break;
  145. }
  146. }
  147. if (ctrValue == valueCount)
  148. {
  149. return ctrSource;
  150. }
  151. }
  152. }
  153. else
  154. {
  155. char firstValueChar = value[0];
  156. for (ctrSource = lastSourceStart; ctrSource >= 0; ctrSource--)
  157. {
  158. sourceChar = source[ctrSource];
  159. if (sourceChar != firstValueChar)
  160. {
  161. continue;
  162. }
  163. for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
  164. {
  165. sourceChar = source[ctrSource + ctrValue];
  166. valueChar = value[ctrValue];
  167. if (sourceChar != valueChar)
  168. {
  169. break;
  170. }
  171. }
  172. if (ctrValue == valueCount)
  173. {
  174. return ctrSource;
  175. }
  176. }
  177. }
  178. }
  179. return -1;
  180. }
  181. private static char InvariantToUpper(char c)
  182. {
  183. return (uint)(c - 'a') <= (uint)('z' - 'a') ? (char)(c - 0x20) : c;
  184. }
  185. private unsafe SortKey InvariantCreateSortKey(string source, CompareOptions options)
  186. {
  187. if (source == null) { throw new ArgumentNullException(nameof(source)); }
  188. if ((options & ValidSortkeyCtorMaskOffFlags) != 0)
  189. {
  190. throw new ArgumentException(SR.Argument_InvalidFlag, nameof(options));
  191. }
  192. byte [] keyData;
  193. if (source.Length == 0)
  194. {
  195. keyData = Array.Empty<byte>();
  196. }
  197. else
  198. {
  199. // In the invariant mode, all string comparisons are done as ordinal so when generating the sort keys we generate it according to this fact
  200. keyData = new byte[source.Length * sizeof(char)];
  201. fixed (char* pChar = source) fixed (byte* pByte = keyData)
  202. {
  203. if ((options & (CompareOptions.IgnoreCase | CompareOptions.OrdinalIgnoreCase)) != 0)
  204. {
  205. short *pShort = (short *) pByte;
  206. for (int i=0; i<source.Length; i++)
  207. {
  208. pShort[i] = (short) InvariantToUpper(source[i]);
  209. }
  210. }
  211. else
  212. {
  213. Buffer.MemoryCopy(pChar, pByte, keyData.Length, keyData.Length);
  214. }
  215. }
  216. }
  217. return new SortKey(Name, source, options, keyData);
  218. }
  219. }
  220. }