String.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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.Buffers;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Globalization;
  9. using System.Runtime.CompilerServices;
  10. using System.Runtime.InteropServices;
  11. using System.Runtime.Versioning;
  12. using System.Text;
  13. using Internal.Runtime.CompilerServices;
  14. namespace System
  15. {
  16. // The String class represents a static string of characters. Many of
  17. // the string methods perform some type of transformation on the current
  18. // instance and return the result as a new string. As with arrays, character
  19. // positions (indices) are zero-based.
  20. [Serializable]
  21. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  22. public sealed partial class String : IComparable, IEnumerable, IConvertible, IEnumerable<char>, IComparable<string>, IEquatable<string>, ICloneable
  23. {
  24. // String constructors
  25. // These are special. The implementation methods for these have a different signature from the
  26. // declared constructors.
  27. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  28. public extern String(char[] value);
  29. #if PROJECTN
  30. [DependencyReductionRoot]
  31. #endif
  32. #if !CORECLR
  33. static
  34. #endif
  35. private string Ctor(char[] value)
  36. {
  37. if (value == null || value.Length == 0)
  38. return Empty;
  39. string result = FastAllocateString(value.Length);
  40. unsafe
  41. {
  42. fixed (char* dest = &result._firstChar, source = value)
  43. wstrcpy(dest, source, value.Length);
  44. }
  45. return result;
  46. }
  47. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  48. public extern String(char[] value, int startIndex, int length);
  49. #if PROJECTN
  50. [DependencyReductionRoot]
  51. #endif
  52. #if !CORECLR
  53. static
  54. #endif
  55. private string Ctor(char[] value, int startIndex, int length)
  56. {
  57. if (value == null)
  58. throw new ArgumentNullException(nameof(value));
  59. if (startIndex < 0)
  60. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
  61. if (length < 0)
  62. throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength);
  63. if (startIndex > value.Length - length)
  64. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_Index);
  65. if (length == 0)
  66. return Empty;
  67. string result = FastAllocateString(length);
  68. unsafe
  69. {
  70. fixed (char* dest = &result._firstChar, source = value)
  71. wstrcpy(dest, source + startIndex, length);
  72. }
  73. return result;
  74. }
  75. [CLSCompliant(false)]
  76. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  77. public extern unsafe String(char* value);
  78. #if PROJECTN
  79. [DependencyReductionRoot]
  80. #endif
  81. #if !CORECLR
  82. static
  83. #endif
  84. private unsafe string Ctor(char* ptr)
  85. {
  86. if (ptr == null)
  87. return Empty;
  88. int count = wcslen(ptr);
  89. if (count == 0)
  90. return Empty;
  91. string result = FastAllocateString(count);
  92. fixed (char* dest = &result._firstChar)
  93. wstrcpy(dest, ptr, count);
  94. return result;
  95. }
  96. [CLSCompliant(false)]
  97. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  98. public extern unsafe String(char* value, int startIndex, int length);
  99. #if PROJECTN
  100. [DependencyReductionRoot]
  101. #endif
  102. #if !CORECLR
  103. static
  104. #endif
  105. private unsafe string Ctor(char* ptr, int startIndex, int length)
  106. {
  107. if (length < 0)
  108. throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength);
  109. if (startIndex < 0)
  110. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
  111. char* pStart = ptr + startIndex;
  112. // overflow check
  113. if (pStart < ptr)
  114. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_PartialWCHAR);
  115. if (length == 0)
  116. return Empty;
  117. if (ptr == null)
  118. throw new ArgumentOutOfRangeException(nameof(ptr), SR.ArgumentOutOfRange_PartialWCHAR);
  119. string result = FastAllocateString(length);
  120. fixed (char* dest = &result._firstChar)
  121. wstrcpy(dest, pStart, length);
  122. return result;
  123. }
  124. [CLSCompliant(false)]
  125. [MethodImpl(MethodImplOptions.InternalCall)]
  126. public extern unsafe String(sbyte* value);
  127. #if PROJECTN
  128. [DependencyReductionRoot]
  129. #endif
  130. #if !CORECLR
  131. static
  132. #endif
  133. private unsafe string Ctor(sbyte* value)
  134. {
  135. byte* pb = (byte*)value;
  136. if (pb == null)
  137. return Empty;
  138. int numBytes = new ReadOnlySpan<byte>((byte*)value, int.MaxValue).IndexOf<byte>(0);
  139. // Check for overflow
  140. if (numBytes < 0)
  141. throw new ArgumentException(SR.Arg_MustBeNullTerminatedString);
  142. return CreateStringForSByteConstructor(pb, numBytes);
  143. }
  144. [CLSCompliant(false)]
  145. [MethodImpl(MethodImplOptions.InternalCall)]
  146. public extern unsafe String(sbyte* value, int startIndex, int length);
  147. #if PROJECTN
  148. [DependencyReductionRoot]
  149. #endif
  150. #if !CORECLR
  151. static
  152. #endif
  153. private unsafe string Ctor(sbyte* value, int startIndex, int length)
  154. {
  155. if (startIndex < 0)
  156. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
  157. if (length < 0)
  158. throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength);
  159. if (value == null)
  160. {
  161. if (length == 0)
  162. return Empty;
  163. throw new ArgumentNullException(nameof(value));
  164. }
  165. byte* pStart = (byte*)(value + startIndex);
  166. // overflow check
  167. if (pStart < value)
  168. throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_PartialWCHAR);
  169. return CreateStringForSByteConstructor(pStart, length);
  170. }
  171. // Encoder for String..ctor(sbyte*) and String..ctor(sbyte*, int, int)
  172. private static unsafe string CreateStringForSByteConstructor(byte *pb, int numBytes)
  173. {
  174. Debug.Assert(numBytes >= 0);
  175. Debug.Assert(pb <= (pb + numBytes));
  176. if (numBytes == 0)
  177. return Empty;
  178. #if PLATFORM_WINDOWS
  179. int numCharsRequired = Interop.Kernel32.MultiByteToWideChar(Interop.Kernel32.CP_ACP, Interop.Kernel32.MB_PRECOMPOSED, pb, numBytes, (char*)null, 0);
  180. if (numCharsRequired == 0)
  181. throw new ArgumentException(SR.Arg_InvalidANSIString);
  182. string newString = FastAllocateString(numCharsRequired);
  183. fixed (char *pFirstChar = &newString._firstChar)
  184. {
  185. numCharsRequired = Interop.Kernel32.MultiByteToWideChar(Interop.Kernel32.CP_ACP, Interop.Kernel32.MB_PRECOMPOSED, pb, numBytes, pFirstChar, numCharsRequired);
  186. }
  187. if (numCharsRequired == 0)
  188. throw new ArgumentException(SR.Arg_InvalidANSIString);
  189. return newString;
  190. #else
  191. return Encoding.UTF8.GetString(pb, numBytes);
  192. #endif
  193. }
  194. [CLSCompliant(false)]
  195. [MethodImpl(MethodImplOptions.InternalCall)]
  196. public extern unsafe String(sbyte* value, int startIndex, int length, Encoding enc);
  197. #if PROJECTN
  198. [DependencyReductionRoot]
  199. #endif
  200. #if !CORECLR
  201. static
  202. #endif
  203. private unsafe string Ctor(sbyte* value, int startIndex, int length, Encoding enc)
  204. {
  205. if (enc == null)
  206. return new string(value, startIndex, length);
  207. if (length < 0)
  208. throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum);
  209. if (startIndex < 0)
  210. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
  211. if (value == null)
  212. {
  213. if (length == 0)
  214. return Empty;
  215. throw new ArgumentNullException(nameof(value));
  216. }
  217. byte* pStart = (byte*)(value + startIndex);
  218. // overflow check
  219. if (pStart < value)
  220. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_PartialWCHAR);
  221. return enc.GetString(new ReadOnlySpan<byte>(pStart, length));
  222. }
  223. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  224. public extern String(char c, int count);
  225. #if PROJECTN
  226. [DependencyReductionRoot]
  227. #endif
  228. #if !CORECLR
  229. static
  230. #endif
  231. private string Ctor(char c, int count)
  232. {
  233. if (count <= 0)
  234. {
  235. if (count == 0)
  236. return Empty;
  237. throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NegativeCount);
  238. }
  239. string result = FastAllocateString(count);
  240. if (c != '\0') // Fast path null char string
  241. {
  242. unsafe
  243. {
  244. fixed (char* dest = &result._firstChar)
  245. {
  246. uint cc = (uint)((c << 16) | c);
  247. uint* dmem = (uint*)dest;
  248. if (count >= 4)
  249. {
  250. count -= 4;
  251. do
  252. {
  253. dmem[0] = cc;
  254. dmem[1] = cc;
  255. dmem += 2;
  256. count -= 4;
  257. } while (count >= 0);
  258. }
  259. if ((count & 2) != 0)
  260. {
  261. *dmem = cc;
  262. dmem++;
  263. }
  264. if ((count & 1) != 0)
  265. ((char*)dmem)[0] = c;
  266. }
  267. }
  268. }
  269. return result;
  270. }
  271. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  272. public extern String(ReadOnlySpan<char> value);
  273. #if PROJECTN
  274. [DependencyReductionRoot]
  275. #endif
  276. #if !CORECLR
  277. static
  278. #endif
  279. private unsafe string Ctor(ReadOnlySpan<char> value)
  280. {
  281. if (value.Length == 0)
  282. return Empty;
  283. string result = FastAllocateString(value.Length);
  284. fixed (char* dest = &result._firstChar, src = &MemoryMarshal.GetReference(value))
  285. wstrcpy(dest, src, value.Length);
  286. return result;
  287. }
  288. public static string Create<TState>(int length, TState state, SpanAction<char, TState> action)
  289. {
  290. if (action == null)
  291. throw new ArgumentNullException(nameof(action));
  292. if (length <= 0)
  293. {
  294. if (length == 0)
  295. return Empty;
  296. throw new ArgumentOutOfRangeException(nameof(length));
  297. }
  298. string result = FastAllocateString(length);
  299. action(new Span<char>(ref result.GetRawStringData(), length), state);
  300. return result;
  301. }
  302. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  303. public static implicit operator ReadOnlySpan<char>(string value) =>
  304. value != null ? new ReadOnlySpan<char>(ref value.GetRawStringData(), value.Length) : default;
  305. public object Clone()
  306. {
  307. return this;
  308. }
  309. public static unsafe string Copy(string str)
  310. {
  311. if (str == null)
  312. throw new ArgumentNullException(nameof(str));
  313. string result = FastAllocateString(str.Length);
  314. fixed (char* dest = &result._firstChar, src = &str._firstChar)
  315. wstrcpy(dest, src, str.Length);
  316. return result;
  317. }
  318. // Converts a substring of this string to an array of characters. Copies the
  319. // characters of this string beginning at position sourceIndex and ending at
  320. // sourceIndex + count - 1 to the character array buffer, beginning
  321. // at destinationIndex.
  322. //
  323. public unsafe void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
  324. {
  325. if (destination == null)
  326. throw new ArgumentNullException(nameof(destination));
  327. if (count < 0)
  328. throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NegativeCount);
  329. if (sourceIndex < 0)
  330. throw new ArgumentOutOfRangeException(nameof(sourceIndex), SR.ArgumentOutOfRange_Index);
  331. if (count > Length - sourceIndex)
  332. throw new ArgumentOutOfRangeException(nameof(sourceIndex), SR.ArgumentOutOfRange_IndexCount);
  333. if (destinationIndex > destination.Length - count || destinationIndex < 0)
  334. throw new ArgumentOutOfRangeException(nameof(destinationIndex), SR.ArgumentOutOfRange_IndexCount);
  335. fixed (char* src = &_firstChar, dest = destination)
  336. wstrcpy(dest + destinationIndex, src + sourceIndex, count);
  337. }
  338. // Returns the entire string as an array of characters.
  339. public unsafe char[] ToCharArray()
  340. {
  341. if (Length == 0)
  342. return Array.Empty<char>();
  343. char[] chars = new char[Length];
  344. fixed (char* src = &_firstChar, dest = &chars[0])
  345. wstrcpy(dest, src, Length);
  346. return chars;
  347. }
  348. // Returns a substring of this string as an array of characters.
  349. //
  350. public unsafe char[] ToCharArray(int startIndex, int length)
  351. {
  352. // Range check everything.
  353. if (startIndex < 0 || startIndex > Length || startIndex > Length - length)
  354. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_Index);
  355. if (length <= 0)
  356. {
  357. if (length == 0)
  358. return Array.Empty<char>();
  359. throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_Index);
  360. }
  361. char[] chars = new char[length];
  362. fixed (char* src = &_firstChar, dest = &chars[0])
  363. wstrcpy(dest, src + startIndex, length);
  364. return chars;
  365. }
  366. [NonVersionable]
  367. public static bool IsNullOrEmpty(string value)
  368. {
  369. // Using 0u >= (uint)value.Length rather than
  370. // value.Length == 0 as it will elide the bounds check to
  371. // the first char: value[0] if that is performed following the test
  372. // for the same test cost.
  373. // Ternary operator returning true/false prevents redundant asm generation:
  374. // https://github.com/dotnet/coreclr/issues/914
  375. return (value == null || 0u >= (uint)value.Length) ? true : false;
  376. }
  377. public static bool IsNullOrWhiteSpace(string value)
  378. {
  379. if (value == null) return true;
  380. for (int i = 0; i < value.Length; i++)
  381. {
  382. if (!char.IsWhiteSpace(value[i])) return false;
  383. }
  384. return true;
  385. }
  386. internal ref char GetRawStringData() => ref _firstChar;
  387. // Helper for encodings so they can talk to our buffer directly
  388. // stringLength must be the exact size we'll expect
  389. internal static unsafe string CreateStringFromEncoding(
  390. byte* bytes, int byteLength, Encoding encoding)
  391. {
  392. Debug.Assert(bytes != null);
  393. Debug.Assert(byteLength >= 0);
  394. // Get our string length
  395. int stringLength = encoding.GetCharCount(bytes, byteLength, null);
  396. Debug.Assert(stringLength >= 0, "stringLength >= 0");
  397. // They gave us an empty string if they needed one
  398. // 0 bytelength might be possible if there's something in an encoder
  399. if (stringLength == 0)
  400. return Empty;
  401. string s = FastAllocateString(stringLength);
  402. fixed (char* pTempChars = &s._firstChar)
  403. {
  404. int doubleCheck = encoding.GetChars(bytes, byteLength, pTempChars, stringLength, null);
  405. Debug.Assert(stringLength == doubleCheck,
  406. "Expected encoding.GetChars to return same length as encoding.GetCharCount");
  407. }
  408. return s;
  409. }
  410. // This is only intended to be used by char.ToString.
  411. // It is necessary to put the code in this class instead of Char, since _firstChar is a private member.
  412. // Making _firstChar internal would be dangerous since it would make it much easier to break String's immutability.
  413. internal static string CreateFromChar(char c)
  414. {
  415. string result = FastAllocateString(1);
  416. result._firstChar = c;
  417. return result;
  418. }
  419. internal static string CreateFromChar(char c1, char c2)
  420. {
  421. string result = FastAllocateString(2);
  422. result._firstChar = c1;
  423. Unsafe.Add(ref result._firstChar, 1) = c2;
  424. return result;
  425. }
  426. internal static unsafe void wstrcpy(char* dmem, char* smem, int charCount)
  427. {
  428. Buffer.Memmove((byte*)dmem, (byte*)smem, ((uint)charCount) * 2);
  429. }
  430. // Returns this string.
  431. public override string ToString()
  432. {
  433. return this;
  434. }
  435. // Returns this string.
  436. public string ToString(IFormatProvider provider)
  437. {
  438. return this;
  439. }
  440. public CharEnumerator GetEnumerator()
  441. {
  442. return new CharEnumerator(this);
  443. }
  444. IEnumerator<char> IEnumerable<char>.GetEnumerator()
  445. {
  446. return new CharEnumerator(this);
  447. }
  448. IEnumerator IEnumerable.GetEnumerator()
  449. {
  450. return new CharEnumerator(this);
  451. }
  452. /// <summary>
  453. /// Returns an enumeration of <see cref="Rune"/> from this string.
  454. /// </summary>
  455. /// <remarks>
  456. /// Invalid sequences will be represented in the enumeration by <see cref="Rune.ReplacementChar"/>.
  457. /// </remarks>
  458. public StringRuneEnumerator EnumerateRunes()
  459. {
  460. return new StringRuneEnumerator(this);
  461. }
  462. internal static unsafe int wcslen(char* ptr)
  463. {
  464. char* end = ptr;
  465. // First make sure our pointer is aligned on a word boundary
  466. int alignment = IntPtr.Size - 1;
  467. // If ptr is at an odd address (e.g. 0x5), this loop will simply iterate all the way
  468. while (((uint)end & (uint)alignment) != 0)
  469. {
  470. if (*end == 0) goto FoundZero;
  471. end++;
  472. }
  473. #if !BIT64
  474. // The following code is (somewhat surprisingly!) significantly faster than a naive loop,
  475. // at least on x86 and the current jit.
  476. // The loop condition below works because if "end[0] & end[1]" is non-zero, that means
  477. // neither operand can have been zero. If is zero, we have to look at the operands individually,
  478. // but we hope this going to fairly rare.
  479. // In general, it would be incorrect to access end[1] if we haven't made sure
  480. // end[0] is non-zero. However, we know the ptr has been aligned by the loop above
  481. // so end[0] and end[1] must be in the same word (and therefore page), so they're either both accessible, or both not.
  482. while ((end[0] & end[1]) != 0 || (end[0] != 0 && end[1] != 0))
  483. {
  484. end += 2;
  485. }
  486. Debug.Assert(end[0] == 0 || end[1] == 0);
  487. if (end[0] != 0) end++;
  488. #else // !BIT64
  489. // Based on https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
  490. // 64-bit implementation: process 1 ulong (word) at a time
  491. // What we do here is add 0x7fff from each of the
  492. // 4 individual chars within the ulong, using MagicMask.
  493. // If the char > 0 and < 0x8001, it will have its high bit set.
  494. // We then OR with MagicMask, to set all the other bits.
  495. // This will result in all bits set (ulong.MaxValue) for any
  496. // char that fits the above criteria, and something else otherwise.
  497. // Note that for any char > 0x8000, this will be a false
  498. // positive and we will fallback to the slow path and
  499. // check each char individually. This is OK though, since
  500. // we optimize for the common case (ASCII chars, which are < 0x80).
  501. // NOTE: We can access a ulong a time since the ptr is aligned,
  502. // and therefore we're only accessing the same word/page. (See notes
  503. // for the 32-bit version above.)
  504. const ulong MagicMask = 0x7fff7fff7fff7fff;
  505. while (true)
  506. {
  507. ulong word = *(ulong*)end;
  508. word += MagicMask; // cause high bit to be set if not zero, and <= 0x8000
  509. word |= MagicMask; // set everything besides the high bits
  510. if (word == ulong.MaxValue) // 0xffff...
  511. {
  512. // all of the chars have their bits set (and therefore none can be 0)
  513. end += 4;
  514. continue;
  515. }
  516. // at least one of them didn't have their high bit set!
  517. // go through each char and check for 0.
  518. if (end[0] == 0) goto EndAt0;
  519. if (end[1] == 0) goto EndAt1;
  520. if (end[2] == 0) goto EndAt2;
  521. if (end[3] == 0) goto EndAt3;
  522. // if we reached here, it was a false positive-- just continue
  523. end += 4;
  524. }
  525. EndAt3: end++;
  526. EndAt2: end++;
  527. EndAt1: end++;
  528. EndAt0:
  529. #endif // !BIT64
  530. FoundZero:
  531. Debug.Assert(*end == 0);
  532. int count = (int)(end - ptr);
  533. #if BIT64
  534. // Check for overflow
  535. if (ptr + count != end)
  536. throw new ArgumentException(SR.Arg_MustBeNullTerminatedString);
  537. #else
  538. Debug.Assert(ptr + count == end);
  539. #endif
  540. return count;
  541. }
  542. //
  543. // IConvertible implementation
  544. //
  545. public TypeCode GetTypeCode()
  546. {
  547. return TypeCode.String;
  548. }
  549. bool IConvertible.ToBoolean(IFormatProvider provider)
  550. {
  551. return Convert.ToBoolean(this, provider);
  552. }
  553. char IConvertible.ToChar(IFormatProvider provider)
  554. {
  555. return Convert.ToChar(this, provider);
  556. }
  557. sbyte IConvertible.ToSByte(IFormatProvider provider)
  558. {
  559. return Convert.ToSByte(this, provider);
  560. }
  561. byte IConvertible.ToByte(IFormatProvider provider)
  562. {
  563. return Convert.ToByte(this, provider);
  564. }
  565. short IConvertible.ToInt16(IFormatProvider provider)
  566. {
  567. return Convert.ToInt16(this, provider);
  568. }
  569. ushort IConvertible.ToUInt16(IFormatProvider provider)
  570. {
  571. return Convert.ToUInt16(this, provider);
  572. }
  573. int IConvertible.ToInt32(IFormatProvider provider)
  574. {
  575. return Convert.ToInt32(this, provider);
  576. }
  577. uint IConvertible.ToUInt32(IFormatProvider provider)
  578. {
  579. return Convert.ToUInt32(this, provider);
  580. }
  581. long IConvertible.ToInt64(IFormatProvider provider)
  582. {
  583. return Convert.ToInt64(this, provider);
  584. }
  585. ulong IConvertible.ToUInt64(IFormatProvider provider)
  586. {
  587. return Convert.ToUInt64(this, provider);
  588. }
  589. float IConvertible.ToSingle(IFormatProvider provider)
  590. {
  591. return Convert.ToSingle(this, provider);
  592. }
  593. double IConvertible.ToDouble(IFormatProvider provider)
  594. {
  595. return Convert.ToDouble(this, provider);
  596. }
  597. decimal IConvertible.ToDecimal(IFormatProvider provider)
  598. {
  599. return Convert.ToDecimal(this, provider);
  600. }
  601. DateTime IConvertible.ToDateTime(IFormatProvider provider)
  602. {
  603. return Convert.ToDateTime(this, provider);
  604. }
  605. object IConvertible.ToType(Type type, IFormatProvider provider)
  606. {
  607. return Convert.DefaultToType((IConvertible)this, type, provider);
  608. }
  609. // Normalization Methods
  610. // These just wrap calls to Normalization class
  611. public bool IsNormalized()
  612. {
  613. return IsNormalized(NormalizationForm.FormC);
  614. }
  615. public bool IsNormalized(NormalizationForm normalizationForm)
  616. {
  617. #if CORECLR
  618. if (this.IsFastSort())
  619. {
  620. // If its FastSort && one of the 4 main forms, then its already normalized
  621. if (normalizationForm == NormalizationForm.FormC ||
  622. normalizationForm == NormalizationForm.FormKC ||
  623. normalizationForm == NormalizationForm.FormD ||
  624. normalizationForm == NormalizationForm.FormKD)
  625. return true;
  626. }
  627. #endif
  628. return Normalization.IsNormalized(this, normalizationForm);
  629. }
  630. public string Normalize()
  631. {
  632. return Normalize(NormalizationForm.FormC);
  633. }
  634. public string Normalize(NormalizationForm normalizationForm)
  635. {
  636. #if CORECLR
  637. if (this.IsAscii())
  638. {
  639. // If its FastSort && one of the 4 main forms, then its already normalized
  640. if (normalizationForm == NormalizationForm.FormC ||
  641. normalizationForm == NormalizationForm.FormKC ||
  642. normalizationForm == NormalizationForm.FormD ||
  643. normalizationForm == NormalizationForm.FormKD)
  644. return this;
  645. }
  646. #endif
  647. return Normalization.Normalize(this, normalizationForm);
  648. }
  649. }
  650. }