MemoryExtensions.Globalization.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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.Globalization;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. namespace System
  10. {
  11. public static partial class MemoryExtensions
  12. {
  13. /// <summary>
  14. /// Indicates whether the specified span contains only white-space characters.
  15. /// </summary>
  16. public static bool IsWhiteSpace(this ReadOnlySpan<char> span)
  17. {
  18. for (int i = 0; i < span.Length; i++)
  19. {
  20. if (!char.IsWhiteSpace(span[i]))
  21. return false;
  22. }
  23. return true;
  24. }
  25. /// <summary>
  26. /// Returns a value indicating whether the specified <paramref name="value"/> occurs within the <paramref name="span"/>.
  27. /// <param name="span">The source span.</param>
  28. /// <param name="value">The value to seek within the source span.</param>
  29. /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
  30. /// </summary>
  31. public static bool Contains(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
  32. {
  33. return IndexOf(span, value, comparisonType) >= 0;
  34. }
  35. /// <summary>
  36. /// Determines whether this <paramref name="span"/> and the specified <paramref name="other"/> span have the same characters
  37. /// when compared using the specified <paramref name="comparisonType"/> option.
  38. /// <param name="span">The source span.</param>
  39. /// <param name="other">The value to compare with the source span.</param>
  40. /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="other"/> are compared.</param>
  41. /// </summary>
  42. public static bool Equals(this ReadOnlySpan<char> span, ReadOnlySpan<char> other, StringComparison comparisonType)
  43. {
  44. string.CheckStringComparison(comparisonType);
  45. switch (comparisonType)
  46. {
  47. case StringComparison.CurrentCulture:
  48. return CultureInfo.CurrentCulture.CompareInfo.CompareOptionNone(span, other) == 0;
  49. case StringComparison.CurrentCultureIgnoreCase:
  50. return CultureInfo.CurrentCulture.CompareInfo.CompareOptionIgnoreCase(span, other) == 0;
  51. case StringComparison.InvariantCulture:
  52. return CompareInfo.Invariant.CompareOptionNone(span, other) == 0;
  53. case StringComparison.InvariantCultureIgnoreCase:
  54. return CompareInfo.Invariant.CompareOptionIgnoreCase(span, other) == 0;
  55. case StringComparison.Ordinal:
  56. return EqualsOrdinal(span, other);
  57. case StringComparison.OrdinalIgnoreCase:
  58. return EqualsOrdinalIgnoreCase(span, other);
  59. }
  60. Debug.Fail("StringComparison outside range");
  61. return false;
  62. }
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. internal static bool EqualsOrdinal(this ReadOnlySpan<char> span, ReadOnlySpan<char> value)
  65. {
  66. if (span.Length != value.Length)
  67. return false;
  68. if (value.Length == 0) // span.Length == value.Length == 0
  69. return true;
  70. return span.SequenceEqual(value);
  71. }
  72. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  73. internal static bool EqualsOrdinalIgnoreCase(this ReadOnlySpan<char> span, ReadOnlySpan<char> value)
  74. {
  75. if (span.Length != value.Length)
  76. return false;
  77. if (value.Length == 0) // span.Length == value.Length == 0
  78. return true;
  79. return CompareInfo.EqualsOrdinalIgnoreCase(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(value), span.Length);
  80. }
  81. /// <summary>
  82. /// Compares the specified <paramref name="span"/> and <paramref name="other"/> using the specified <paramref name="comparisonType"/>,
  83. /// and returns an integer that indicates their relative position in the sort order.
  84. /// <param name="span">The source span.</param>
  85. /// <param name="other">The value to compare with the source span.</param>
  86. /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="other"/> are compared.</param>
  87. /// </summary>
  88. public static int CompareTo(this ReadOnlySpan<char> span, ReadOnlySpan<char> other, StringComparison comparisonType)
  89. {
  90. string.CheckStringComparison(comparisonType);
  91. switch (comparisonType)
  92. {
  93. case StringComparison.CurrentCulture:
  94. return CultureInfo.CurrentCulture.CompareInfo.CompareOptionNone(span, other);
  95. case StringComparison.CurrentCultureIgnoreCase:
  96. return CultureInfo.CurrentCulture.CompareInfo.CompareOptionIgnoreCase(span, other);
  97. case StringComparison.InvariantCulture:
  98. return CompareInfo.Invariant.CompareOptionNone(span, other);
  99. case StringComparison.InvariantCultureIgnoreCase:
  100. return CompareInfo.Invariant.CompareOptionIgnoreCase(span, other);
  101. case StringComparison.Ordinal:
  102. if (span.Length == 0 || other.Length == 0)
  103. return span.Length - other.Length;
  104. return string.CompareOrdinal(span, other);
  105. case StringComparison.OrdinalIgnoreCase:
  106. return CompareInfo.CompareOrdinalIgnoreCase(span, other);
  107. }
  108. Debug.Fail("StringComparison outside range");
  109. return 0;
  110. }
  111. /// <summary>
  112. /// Reports the zero-based index of the first occurrence of the specified <paramref name="value"/> in the current <paramref name="span"/>.
  113. /// <param name="span">The source span.</param>
  114. /// <param name="value">The value to seek within the source span.</param>
  115. /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
  116. /// </summary>
  117. public static int IndexOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
  118. {
  119. string.CheckStringComparison(comparisonType);
  120. if (value.Length == 0)
  121. {
  122. return 0;
  123. }
  124. if (span.Length == 0)
  125. {
  126. return -1;
  127. }
  128. if (comparisonType == StringComparison.Ordinal)
  129. {
  130. return SpanHelpers.IndexOf(
  131. ref MemoryMarshal.GetReference(span),
  132. span.Length,
  133. ref MemoryMarshal.GetReference(value),
  134. value.Length);
  135. }
  136. if (GlobalizationMode.Invariant)
  137. {
  138. return CompareInfo.InvariantIndexOf(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType) != CompareOptions.None);
  139. }
  140. switch (comparisonType)
  141. {
  142. case StringComparison.CurrentCulture:
  143. case StringComparison.CurrentCultureIgnoreCase:
  144. return CultureInfo.CurrentCulture.CompareInfo.IndexOf(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType));
  145. case StringComparison.InvariantCulture:
  146. case StringComparison.InvariantCultureIgnoreCase:
  147. return CompareInfo.Invariant.IndexOf(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType));
  148. default:
  149. Debug.Assert(comparisonType == StringComparison.OrdinalIgnoreCase);
  150. return CompareInfo.Invariant.IndexOfOrdinalIgnoreCase(span, value);
  151. }
  152. }
  153. /// <summary>
  154. /// Reports the zero-based index of the last occurrence of the specified <paramref name="value"/> in the current <paramref name="span"/>.
  155. /// <param name="span">The source span.</param>
  156. /// <param name="value">The value to seek within the source span.</param>
  157. /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
  158. /// </summary>
  159. public static int LastIndexOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
  160. {
  161. string.CheckStringComparison(comparisonType);
  162. if (value.Length == 0)
  163. {
  164. return span.Length > 0 ? span.Length - 1 : 0;
  165. }
  166. if (span.Length == 0)
  167. {
  168. return -1;
  169. }
  170. if (GlobalizationMode.Invariant)
  171. {
  172. return CompareInfo.InvariantIndexOf(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType) != CompareOptions.None, fromBeginning: false);
  173. }
  174. switch (comparisonType)
  175. {
  176. case StringComparison.CurrentCulture:
  177. case StringComparison.CurrentCultureIgnoreCase:
  178. return CultureInfo.CurrentCulture.CompareInfo.LastIndexOf(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType));
  179. case StringComparison.InvariantCulture:
  180. case StringComparison.InvariantCultureIgnoreCase:
  181. return CompareInfo.Invariant.LastIndexOf(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType));
  182. default:
  183. Debug.Assert(comparisonType == StringComparison.Ordinal || comparisonType == StringComparison.OrdinalIgnoreCase);
  184. return CompareInfo.Invariant.LastIndexOfOrdinal(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType) != CompareOptions.None);
  185. }
  186. }
  187. /// <summary>
  188. /// Copies the characters from the source span into the destination, converting each character to lowercase,
  189. /// using the casing rules of the specified culture.
  190. /// </summary>
  191. /// <param name="source">The source span.</param>
  192. /// <param name="destination">The destination span which contains the transformed characters.</param>
  193. /// <param name="culture">An object that supplies culture-specific casing rules.</param>
  194. /// <remarks>If <paramref name="culture"/> is null, <see cref="System.Globalization.CultureInfo.CurrentCulture"/> will be used.</remarks>
  195. /// <returns>The number of characters written into the destination span. If the destination is too small, returns -1.</returns>
  196. /// <exception cref="InvalidOperationException">The source and destination buffers overlap.</exception>
  197. public static int ToLower(this ReadOnlySpan<char> source, Span<char> destination, CultureInfo? culture)
  198. {
  199. if (source.Overlaps(destination))
  200. throw new InvalidOperationException(SR.InvalidOperation_SpanOverlappedOperation);
  201. culture ??= CultureInfo.CurrentCulture;
  202. // Assuming that changing case does not affect length
  203. if (destination.Length < source.Length)
  204. return -1;
  205. if (GlobalizationMode.Invariant)
  206. TextInfo.ToLowerAsciiInvariant(source, destination);
  207. else
  208. culture.TextInfo.ChangeCaseToLower(source, destination);
  209. return source.Length;
  210. }
  211. /// <summary>
  212. /// Copies the characters from the source span into the destination, converting each character to lowercase,
  213. /// using the casing rules of the invariant culture.
  214. /// </summary>
  215. /// <param name="source">The source span.</param>
  216. /// <param name="destination">The destination span which contains the transformed characters.</param>
  217. /// <returns>The number of characters written into the destination span. If the destination is too small, returns -1.</returns>
  218. /// <exception cref="InvalidOperationException">The source and destination buffers overlap.</exception>
  219. public static int ToLowerInvariant(this ReadOnlySpan<char> source, Span<char> destination)
  220. {
  221. if (source.Overlaps(destination))
  222. throw new InvalidOperationException(SR.InvalidOperation_SpanOverlappedOperation);
  223. // Assuming that changing case does not affect length
  224. if (destination.Length < source.Length)
  225. return -1;
  226. if (GlobalizationMode.Invariant)
  227. TextInfo.ToLowerAsciiInvariant(source, destination);
  228. else
  229. CultureInfo.InvariantCulture.TextInfo.ChangeCaseToLower(source, destination);
  230. return source.Length;
  231. }
  232. /// <summary>
  233. /// Copies the characters from the source span into the destination, converting each character to uppercase,
  234. /// using the casing rules of the specified culture.
  235. /// </summary>
  236. /// <param name="source">The source span.</param>
  237. /// <param name="destination">The destination span which contains the transformed characters.</param>
  238. /// <param name="culture">An object that supplies culture-specific casing rules.</param>
  239. /// <remarks>If <paramref name="culture"/> is null, <see cref="System.Globalization.CultureInfo.CurrentCulture"/> will be used.</remarks>
  240. /// <returns>The number of characters written into the destination span. If the destination is too small, returns -1.</returns>
  241. /// <exception cref="InvalidOperationException">The source and destination buffers overlap.</exception>
  242. public static int ToUpper(this ReadOnlySpan<char> source, Span<char> destination, CultureInfo? culture)
  243. {
  244. if (source.Overlaps(destination))
  245. throw new InvalidOperationException(SR.InvalidOperation_SpanOverlappedOperation);
  246. culture ??= CultureInfo.CurrentCulture;
  247. // Assuming that changing case does not affect length
  248. if (destination.Length < source.Length)
  249. return -1;
  250. if (GlobalizationMode.Invariant)
  251. TextInfo.ToUpperAsciiInvariant(source, destination);
  252. else
  253. culture.TextInfo.ChangeCaseToUpper(source, destination);
  254. return source.Length;
  255. }
  256. /// <summary>
  257. /// Copies the characters from the source span into the destination, converting each character to uppercase
  258. /// using the casing rules of the invariant culture.
  259. /// </summary>
  260. /// <param name="source">The source span.</param>
  261. /// <param name="destination">The destination span which contains the transformed characters.</param>
  262. /// <returns>The number of characters written into the destination span. If the destination is too small, returns -1.</returns>
  263. /// <exception cref="InvalidOperationException">The source and destination buffers overlap.</exception>
  264. public static int ToUpperInvariant(this ReadOnlySpan<char> source, Span<char> destination)
  265. {
  266. if (source.Overlaps(destination))
  267. throw new InvalidOperationException(SR.InvalidOperation_SpanOverlappedOperation);
  268. // Assuming that changing case does not affect length
  269. if (destination.Length < source.Length)
  270. return -1;
  271. if (GlobalizationMode.Invariant)
  272. TextInfo.ToUpperAsciiInvariant(source, destination);
  273. else
  274. CultureInfo.InvariantCulture.TextInfo.ChangeCaseToUpper(source, destination);
  275. return source.Length;
  276. }
  277. /// <summary>
  278. /// Determines whether the end of the <paramref name="span"/> matches the specified <paramref name="value"/> when compared using the specified <paramref name="comparisonType"/> option.
  279. /// </summary>
  280. /// <param name="span">The source span.</param>
  281. /// <param name="value">The sequence to compare to the end of the source span.</param>
  282. /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
  283. public static bool EndsWith(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
  284. {
  285. string.CheckStringComparison(comparisonType);
  286. if (value.Length == 0)
  287. {
  288. return true;
  289. }
  290. if (comparisonType >= StringComparison.Ordinal || GlobalizationMode.Invariant)
  291. {
  292. if (string.GetCaseCompareOfComparisonCulture(comparisonType) == CompareOptions.None)
  293. return span.EndsWith(value);
  294. return (span.Length >= value.Length) ? (CompareInfo.CompareOrdinalIgnoreCase(span.Slice(span.Length - value.Length), value) == 0) : false;
  295. }
  296. if (span.Length == 0)
  297. {
  298. return false;
  299. }
  300. return (comparisonType >= StringComparison.InvariantCulture) ?
  301. CompareInfo.Invariant.IsSuffix(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType)) :
  302. CultureInfo.CurrentCulture.CompareInfo.IsSuffix(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType));
  303. }
  304. /// <summary>
  305. /// Determines whether the beginning of the <paramref name="span"/> matches the specified <paramref name="value"/> when compared using the specified <paramref name="comparisonType"/> option.
  306. /// </summary>
  307. /// <param name="span">The source span.</param>
  308. /// <param name="value">The sequence to compare to the beginning of the source span.</param>
  309. /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
  310. public static bool StartsWith(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType)
  311. {
  312. string.CheckStringComparison(comparisonType);
  313. if (value.Length == 0)
  314. {
  315. return true;
  316. }
  317. if (comparisonType >= StringComparison.Ordinal || GlobalizationMode.Invariant)
  318. {
  319. if (string.GetCaseCompareOfComparisonCulture(comparisonType) == CompareOptions.None)
  320. return span.StartsWith(value);
  321. return (span.Length >= value.Length) ? (CompareInfo.CompareOrdinalIgnoreCase(span.Slice(0, value.Length), value) == 0) : false;
  322. }
  323. if (span.Length == 0)
  324. {
  325. return false;
  326. }
  327. return (comparisonType >= StringComparison.InvariantCulture) ?
  328. CompareInfo.Invariant.IsPrefix(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType)) :
  329. CultureInfo.CurrentCulture.CompareInfo.IsPrefix(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType));
  330. }
  331. /// <summary>
  332. /// Returns an enumeration of <see cref="Rune"/> from the provided span.
  333. /// </summary>
  334. /// <remarks>
  335. /// Invalid sequences will be represented in the enumeration by <see cref="Rune.ReplacementChar"/>.
  336. /// </remarks>
  337. public static SpanRuneEnumerator EnumerateRunes(this ReadOnlySpan<char> span)
  338. {
  339. return new SpanRuneEnumerator(span);
  340. }
  341. /// <summary>
  342. /// Returns an enumeration of <see cref="Rune"/> from the provided span.
  343. /// </summary>
  344. /// <remarks>
  345. /// Invalid sequences will be represented in the enumeration by <see cref="Rune.ReplacementChar"/>.
  346. /// </remarks>
  347. public static SpanRuneEnumerator EnumerateRunes(this Span<char> span)
  348. {
  349. return new SpanRuneEnumerator(span);
  350. }
  351. }
  352. }