String.Manipulation.cs 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  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.Generic;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.Runtime.CompilerServices;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using Internal.Runtime.CompilerServices;
  12. namespace System
  13. {
  14. public partial class String
  15. {
  16. private const int StackallocIntBufferSizeLimit = 128;
  17. private static unsafe void FillStringChecked(string dest, int destPos, string src)
  18. {
  19. Debug.Assert(dest != null);
  20. Debug.Assert(src != null);
  21. if (src.Length > dest.Length - destPos)
  22. {
  23. throw new IndexOutOfRangeException();
  24. }
  25. fixed (char* pDest = &dest._firstChar)
  26. fixed (char* pSrc = &src._firstChar)
  27. {
  28. wstrcpy(pDest + destPos, pSrc, src.Length);
  29. }
  30. }
  31. public static string Concat(object arg0)
  32. {
  33. if (arg0 == null)
  34. {
  35. return string.Empty;
  36. }
  37. return arg0.ToString();
  38. }
  39. public static string Concat(object arg0, object arg1)
  40. {
  41. if (arg0 == null)
  42. {
  43. arg0 = string.Empty;
  44. }
  45. if (arg1 == null)
  46. {
  47. arg1 = string.Empty;
  48. }
  49. return Concat(arg0.ToString(), arg1.ToString());
  50. }
  51. public static string Concat(object arg0, object arg1, object arg2)
  52. {
  53. if (arg0 == null)
  54. {
  55. arg0 = string.Empty;
  56. }
  57. if (arg1 == null)
  58. {
  59. arg1 = string.Empty;
  60. }
  61. if (arg2 == null)
  62. {
  63. arg2 = string.Empty;
  64. }
  65. return Concat(arg0.ToString(), arg1.ToString(), arg2.ToString());
  66. }
  67. public static string Concat(params object[] args)
  68. {
  69. if (args == null)
  70. {
  71. throw new ArgumentNullException(nameof(args));
  72. }
  73. if (args.Length <= 1)
  74. {
  75. return args.Length == 0 ?
  76. string.Empty :
  77. args[0]?.ToString() ?? string.Empty;
  78. }
  79. // We need to get an intermediary string array
  80. // to fill with each of the args' ToString(),
  81. // and then just concat that in one operation.
  82. // This way we avoid any intermediary string representations,
  83. // or buffer resizing if we use StringBuilder (although the
  84. // latter case is partially alleviated due to StringBuilder's
  85. // linked-list style implementation)
  86. var strings = new string[args.Length];
  87. int totalLength = 0;
  88. for (int i = 0; i < args.Length; i++)
  89. {
  90. object value = args[i];
  91. string toString = value?.ToString() ?? string.Empty; // We need to handle both the cases when value or value.ToString() is null
  92. strings[i] = toString;
  93. totalLength += toString.Length;
  94. if (totalLength < 0) // Check for a positive overflow
  95. {
  96. throw new OutOfMemoryException();
  97. }
  98. }
  99. // If all of the ToStrings are null/empty, just return string.Empty
  100. if (totalLength == 0)
  101. {
  102. return string.Empty;
  103. }
  104. string result = FastAllocateString(totalLength);
  105. int position = 0; // How many characters we've copied so far
  106. for (int i = 0; i < strings.Length; i++)
  107. {
  108. string s = strings[i];
  109. Debug.Assert(s != null);
  110. Debug.Assert(position <= totalLength - s.Length, "We didn't allocate enough space for the result string!");
  111. FillStringChecked(result, position, s);
  112. position += s.Length;
  113. }
  114. return result;
  115. }
  116. public static string Concat<T>(IEnumerable<T> values)
  117. {
  118. if (values == null)
  119. throw new ArgumentNullException(nameof(values));
  120. if (typeof(T) == typeof(char))
  121. {
  122. // Special-case T==char, as we can handle that case much more efficiently,
  123. // and string.Concat(IEnumerable<char>) can be used as an efficient
  124. // enumerable-based equivalent of new string(char[]).
  125. using (IEnumerator<char> en = Unsafe.As<IEnumerable<char>>(values).GetEnumerator())
  126. {
  127. if (!en.MoveNext())
  128. {
  129. // There weren't any chars. Return the empty string.
  130. return Empty;
  131. }
  132. char c = en.Current; // save the first char
  133. if (!en.MoveNext())
  134. {
  135. // There was only one char. Return a string from it directly.
  136. return CreateFromChar(c);
  137. }
  138. // Create the StringBuilder, add the chars we've already enumerated,
  139. // add the rest, and then get the resulting string.
  140. StringBuilder result = StringBuilderCache.Acquire();
  141. result.Append(c); // first value
  142. do
  143. {
  144. c = en.Current;
  145. result.Append(c);
  146. }
  147. while (en.MoveNext());
  148. return StringBuilderCache.GetStringAndRelease(result);
  149. }
  150. }
  151. else
  152. {
  153. using (IEnumerator<T> en = values.GetEnumerator())
  154. {
  155. if (!en.MoveNext())
  156. return string.Empty;
  157. // We called MoveNext once, so this will be the first item
  158. T currentValue = en.Current;
  159. // Call ToString before calling MoveNext again, since
  160. // we want to stay consistent with the below loop
  161. // Everything should be called in the order
  162. // MoveNext-Current-ToString, unless further optimizations
  163. // can be made, to avoid breaking changes
  164. string firstString = currentValue?.ToString();
  165. // If there's only 1 item, simply call ToString on that
  166. if (!en.MoveNext())
  167. {
  168. // We have to handle the case of either currentValue
  169. // or its ToString being null
  170. return firstString ?? string.Empty;
  171. }
  172. StringBuilder result = StringBuilderCache.Acquire();
  173. result.Append(firstString);
  174. do
  175. {
  176. currentValue = en.Current;
  177. if (currentValue != null)
  178. {
  179. result.Append(currentValue.ToString());
  180. }
  181. }
  182. while (en.MoveNext());
  183. return StringBuilderCache.GetStringAndRelease(result);
  184. }
  185. }
  186. }
  187. public static string Concat(IEnumerable<string> values)
  188. {
  189. if (values == null)
  190. throw new ArgumentNullException(nameof(values));
  191. using (IEnumerator<string> en = values.GetEnumerator())
  192. {
  193. if (!en.MoveNext())
  194. return string.Empty;
  195. string firstValue = en.Current;
  196. if (!en.MoveNext())
  197. {
  198. return firstValue ?? string.Empty;
  199. }
  200. StringBuilder result = StringBuilderCache.Acquire();
  201. result.Append(firstValue);
  202. do
  203. {
  204. result.Append(en.Current);
  205. }
  206. while (en.MoveNext());
  207. return StringBuilderCache.GetStringAndRelease(result);
  208. }
  209. }
  210. public static string Concat(string str0, string str1)
  211. {
  212. if (IsNullOrEmpty(str0))
  213. {
  214. if (IsNullOrEmpty(str1))
  215. {
  216. return string.Empty;
  217. }
  218. return str1;
  219. }
  220. if (IsNullOrEmpty(str1))
  221. {
  222. return str0;
  223. }
  224. int str0Length = str0.Length;
  225. string result = FastAllocateString(str0Length + str1.Length);
  226. FillStringChecked(result, 0, str0);
  227. FillStringChecked(result, str0Length, str1);
  228. return result;
  229. }
  230. public static string Concat(string str0, string str1, string str2)
  231. {
  232. if (IsNullOrEmpty(str0))
  233. {
  234. return Concat(str1, str2);
  235. }
  236. if (IsNullOrEmpty(str1))
  237. {
  238. return Concat(str0, str2);
  239. }
  240. if (IsNullOrEmpty(str2))
  241. {
  242. return Concat(str0, str1);
  243. }
  244. int totalLength = str0.Length + str1.Length + str2.Length;
  245. string result = FastAllocateString(totalLength);
  246. FillStringChecked(result, 0, str0);
  247. FillStringChecked(result, str0.Length, str1);
  248. FillStringChecked(result, str0.Length + str1.Length, str2);
  249. return result;
  250. }
  251. public static string Concat(string str0, string str1, string str2, string str3)
  252. {
  253. if (IsNullOrEmpty(str0))
  254. {
  255. return Concat(str1, str2, str3);
  256. }
  257. if (IsNullOrEmpty(str1))
  258. {
  259. return Concat(str0, str2, str3);
  260. }
  261. if (IsNullOrEmpty(str2))
  262. {
  263. return Concat(str0, str1, str3);
  264. }
  265. if (IsNullOrEmpty(str3))
  266. {
  267. return Concat(str0, str1, str2);
  268. }
  269. int totalLength = str0.Length + str1.Length + str2.Length + str3.Length;
  270. string result = FastAllocateString(totalLength);
  271. FillStringChecked(result, 0, str0);
  272. FillStringChecked(result, str0.Length, str1);
  273. FillStringChecked(result, str0.Length + str1.Length, str2);
  274. FillStringChecked(result, str0.Length + str1.Length + str2.Length, str3);
  275. return result;
  276. }
  277. public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1)
  278. {
  279. int length = checked(str0.Length + str1.Length);
  280. if (length == 0)
  281. {
  282. return Empty;
  283. }
  284. string result = FastAllocateString(length);
  285. Span<char> resultSpan = new Span<char>(ref result.GetRawStringData(), result.Length);
  286. str0.CopyTo(resultSpan);
  287. str1.CopyTo(resultSpan.Slice(str0.Length));
  288. return result;
  289. }
  290. public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2)
  291. {
  292. int length = checked(str0.Length + str1.Length + str2.Length);
  293. if (length == 0)
  294. {
  295. return Empty;
  296. }
  297. string result = FastAllocateString(length);
  298. Span<char> resultSpan = new Span<char>(ref result.GetRawStringData(), result.Length);
  299. str0.CopyTo(resultSpan);
  300. resultSpan = resultSpan.Slice(str0.Length);
  301. str1.CopyTo(resultSpan);
  302. resultSpan = resultSpan.Slice(str1.Length);
  303. str2.CopyTo(resultSpan);
  304. return result;
  305. }
  306. public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2, ReadOnlySpan<char> str3)
  307. {
  308. int length = checked(str0.Length + str1.Length + str2.Length + str3.Length);
  309. if (length == 0)
  310. {
  311. return Empty;
  312. }
  313. string result = FastAllocateString(length);
  314. Span<char> resultSpan = new Span<char>(ref result.GetRawStringData(), result.Length);
  315. str0.CopyTo(resultSpan);
  316. resultSpan = resultSpan.Slice(str0.Length);
  317. str1.CopyTo(resultSpan);
  318. resultSpan = resultSpan.Slice(str1.Length);
  319. str2.CopyTo(resultSpan);
  320. resultSpan = resultSpan.Slice(str2.Length);
  321. str3.CopyTo(resultSpan);
  322. return result;
  323. }
  324. public static string Concat(params string[] values)
  325. {
  326. if (values == null)
  327. throw new ArgumentNullException(nameof(values));
  328. if (values.Length <= 1)
  329. {
  330. return values.Length == 0 ?
  331. string.Empty :
  332. values[0] ?? string.Empty;
  333. }
  334. // It's possible that the input values array could be changed concurrently on another
  335. // thread, such that we can't trust that each read of values[i] will be equivalent.
  336. // Worst case, we can make a defensive copy of the array and use that, but we first
  337. // optimistically try the allocation and copies assuming that the array isn't changing,
  338. // which represents the 99.999% case, in particular since string.Concat is used for
  339. // string concatenation by the languages, with the input array being a params array.
  340. // Sum the lengths of all input strings
  341. long totalLengthLong = 0;
  342. for (int i = 0; i < values.Length; i++)
  343. {
  344. string value = values[i];
  345. if (value != null)
  346. {
  347. totalLengthLong += value.Length;
  348. }
  349. }
  350. // If it's too long, fail, or if it's empty, return an empty string.
  351. if (totalLengthLong > int.MaxValue)
  352. {
  353. throw new OutOfMemoryException();
  354. }
  355. int totalLength = (int)totalLengthLong;
  356. if (totalLength == 0)
  357. {
  358. return string.Empty;
  359. }
  360. // Allocate a new string and copy each input string into it
  361. string result = FastAllocateString(totalLength);
  362. int copiedLength = 0;
  363. for (int i = 0; i < values.Length; i++)
  364. {
  365. string value = values[i];
  366. if (!string.IsNullOrEmpty(value))
  367. {
  368. int valueLen = value.Length;
  369. if (valueLen > totalLength - copiedLength)
  370. {
  371. copiedLength = -1;
  372. break;
  373. }
  374. FillStringChecked(result, copiedLength, value);
  375. copiedLength += valueLen;
  376. }
  377. }
  378. // If we copied exactly the right amount, return the new string. Otherwise,
  379. // something changed concurrently to mutate the input array: fall back to
  380. // doing the concatenation again, but this time with a defensive copy. This
  381. // fall back should be extremely rare.
  382. return copiedLength == totalLength ? result : Concat((string[])values.Clone());
  383. }
  384. public static string Format(string format, object arg0)
  385. {
  386. return FormatHelper(null, format, new ParamsArray(arg0));
  387. }
  388. public static string Format(string format, object arg0, object arg1)
  389. {
  390. return FormatHelper(null, format, new ParamsArray(arg0, arg1));
  391. }
  392. public static string Format(string format, object arg0, object arg1, object arg2)
  393. {
  394. return FormatHelper(null, format, new ParamsArray(arg0, arg1, arg2));
  395. }
  396. public static string Format(string format, params object[] args)
  397. {
  398. if (args == null)
  399. {
  400. // To preserve the original exception behavior, throw an exception about format if both
  401. // args and format are null. The actual null check for format is in FormatHelper.
  402. throw new ArgumentNullException((format == null) ? nameof(format) : nameof(args));
  403. }
  404. return FormatHelper(null, format, new ParamsArray(args));
  405. }
  406. public static string Format(IFormatProvider provider, string format, object arg0)
  407. {
  408. return FormatHelper(provider, format, new ParamsArray(arg0));
  409. }
  410. public static string Format(IFormatProvider provider, string format, object arg0, object arg1)
  411. {
  412. return FormatHelper(provider, format, new ParamsArray(arg0, arg1));
  413. }
  414. public static string Format(IFormatProvider provider, string format, object arg0, object arg1, object arg2)
  415. {
  416. return FormatHelper(provider, format, new ParamsArray(arg0, arg1, arg2));
  417. }
  418. public static string Format(IFormatProvider provider, string format, params object[] args)
  419. {
  420. if (args == null)
  421. {
  422. // To preserve the original exception behavior, throw an exception about format if both
  423. // args and format are null. The actual null check for format is in FormatHelper.
  424. throw new ArgumentNullException((format == null) ? nameof(format) : nameof(args));
  425. }
  426. return FormatHelper(provider, format, new ParamsArray(args));
  427. }
  428. private static string FormatHelper(IFormatProvider provider, string format, ParamsArray args)
  429. {
  430. if (format == null)
  431. throw new ArgumentNullException(nameof(format));
  432. return StringBuilderCache.GetStringAndRelease(
  433. StringBuilderCache
  434. .Acquire(format.Length + args.Length * 8)
  435. .AppendFormatHelper(provider, format, args));
  436. }
  437. public string Insert(int startIndex, string value)
  438. {
  439. if (value == null)
  440. throw new ArgumentNullException(nameof(value));
  441. if (startIndex < 0 || startIndex > this.Length)
  442. throw new ArgumentOutOfRangeException(nameof(startIndex));
  443. int oldLength = Length;
  444. int insertLength = value.Length;
  445. if (oldLength == 0)
  446. return value;
  447. if (insertLength == 0)
  448. return this;
  449. // In case this computation overflows, newLength will be negative and FastAllocateString throws OutOfMemoryException
  450. int newLength = oldLength + insertLength;
  451. string result = FastAllocateString(newLength);
  452. unsafe
  453. {
  454. fixed (char* srcThis = &_firstChar)
  455. {
  456. fixed (char* srcInsert = &value._firstChar)
  457. {
  458. fixed (char* dst = &result._firstChar)
  459. {
  460. wstrcpy(dst, srcThis, startIndex);
  461. wstrcpy(dst + startIndex, srcInsert, insertLength);
  462. wstrcpy(dst + startIndex + insertLength, srcThis + startIndex, oldLength - startIndex);
  463. }
  464. }
  465. }
  466. }
  467. return result;
  468. }
  469. public static string Join(char separator, params string[] value)
  470. {
  471. if (value == null)
  472. {
  473. throw new ArgumentNullException(nameof(value));
  474. }
  475. return Join(separator, value, 0, value.Length);
  476. }
  477. public static unsafe string Join(char separator, params object[] values)
  478. {
  479. // Defer argument validation to the internal function
  480. return JoinCore(&separator, 1, values);
  481. }
  482. public static unsafe string Join<T>(char separator, IEnumerable<T> values)
  483. {
  484. // Defer argument validation to the internal function
  485. return JoinCore(&separator, 1, values);
  486. }
  487. public static unsafe string Join(char separator, string[] value, int startIndex, int count)
  488. {
  489. // Defer argument validation to the internal function
  490. return JoinCore(&separator, 1, value, startIndex, count);
  491. }
  492. // Joins an array of strings together as one string with a separator between each original string.
  493. //
  494. public static string Join(string separator, params string[] value)
  495. {
  496. if (value == null)
  497. {
  498. throw new ArgumentNullException(nameof(value));
  499. }
  500. return Join(separator, value, 0, value.Length);
  501. }
  502. public static unsafe string Join(string separator, params object[] values)
  503. {
  504. separator = separator ?? string.Empty;
  505. fixed (char* pSeparator = &separator._firstChar)
  506. {
  507. // Defer argument validation to the internal function
  508. return JoinCore(pSeparator, separator.Length, values);
  509. }
  510. }
  511. public static unsafe string Join<T>(string separator, IEnumerable<T> values)
  512. {
  513. separator = separator ?? string.Empty;
  514. fixed (char* pSeparator = &separator._firstChar)
  515. {
  516. // Defer argument validation to the internal function
  517. return JoinCore(pSeparator, separator.Length, values);
  518. }
  519. }
  520. public static string Join(string separator, IEnumerable<string> values)
  521. {
  522. if (values == null)
  523. {
  524. throw new ArgumentNullException(nameof(values));
  525. }
  526. using (IEnumerator<string> en = values.GetEnumerator())
  527. {
  528. if (!en.MoveNext())
  529. {
  530. return string.Empty;
  531. }
  532. string firstValue = en.Current;
  533. if (!en.MoveNext())
  534. {
  535. // Only one value available
  536. return firstValue ?? string.Empty;
  537. }
  538. // Null separator and values are handled by the StringBuilder
  539. StringBuilder result = StringBuilderCache.Acquire();
  540. result.Append(firstValue);
  541. do
  542. {
  543. result.Append(separator);
  544. result.Append(en.Current);
  545. }
  546. while (en.MoveNext());
  547. return StringBuilderCache.GetStringAndRelease(result);
  548. }
  549. }
  550. // Joins an array of strings together as one string with a separator between each original string.
  551. //
  552. public static unsafe string Join(string separator, string[] value, int startIndex, int count)
  553. {
  554. separator = separator ?? string.Empty;
  555. fixed (char* pSeparator = &separator._firstChar)
  556. {
  557. // Defer argument validation to the internal function
  558. return JoinCore(pSeparator, separator.Length, value, startIndex, count);
  559. }
  560. }
  561. private static unsafe string JoinCore(char* separator, int separatorLength, object[] values)
  562. {
  563. if (values == null)
  564. {
  565. throw new ArgumentNullException(nameof(values));
  566. }
  567. if (values.Length == 0)
  568. {
  569. return string.Empty;
  570. }
  571. string firstString = values[0]?.ToString();
  572. if (values.Length == 1)
  573. {
  574. return firstString ?? string.Empty;
  575. }
  576. StringBuilder result = StringBuilderCache.Acquire();
  577. result.Append(firstString);
  578. for (int i = 1; i < values.Length; i++)
  579. {
  580. result.Append(separator, separatorLength);
  581. object value = values[i];
  582. if (value != null)
  583. {
  584. result.Append(value.ToString());
  585. }
  586. }
  587. return StringBuilderCache.GetStringAndRelease(result);
  588. }
  589. private static unsafe string JoinCore<T>(char* separator, int separatorLength, IEnumerable<T> values)
  590. {
  591. if (values == null)
  592. {
  593. throw new ArgumentNullException(nameof(values));
  594. }
  595. using (IEnumerator<T> en = values.GetEnumerator())
  596. {
  597. if (!en.MoveNext())
  598. {
  599. return string.Empty;
  600. }
  601. // We called MoveNext once, so this will be the first item
  602. T currentValue = en.Current;
  603. // Call ToString before calling MoveNext again, since
  604. // we want to stay consistent with the below loop
  605. // Everything should be called in the order
  606. // MoveNext-Current-ToString, unless further optimizations
  607. // can be made, to avoid breaking changes
  608. string firstString = currentValue?.ToString();
  609. // If there's only 1 item, simply call ToString on that
  610. if (!en.MoveNext())
  611. {
  612. // We have to handle the case of either currentValue
  613. // or its ToString being null
  614. return firstString ?? string.Empty;
  615. }
  616. StringBuilder result = StringBuilderCache.Acquire();
  617. result.Append(firstString);
  618. do
  619. {
  620. currentValue = en.Current;
  621. result.Append(separator, separatorLength);
  622. if (currentValue != null)
  623. {
  624. result.Append(currentValue.ToString());
  625. }
  626. }
  627. while (en.MoveNext());
  628. return StringBuilderCache.GetStringAndRelease(result);
  629. }
  630. }
  631. private static unsafe string JoinCore(char* separator, int separatorLength, string[] value, int startIndex, int count)
  632. {
  633. // If the separator is null, it is converted to an empty string before entering this function.
  634. // Even for empty strings, fixed should never return null (it should return a pointer to a null char).
  635. Debug.Assert(separator != null);
  636. Debug.Assert(separatorLength >= 0);
  637. if (value == null)
  638. {
  639. throw new ArgumentNullException(nameof(value));
  640. }
  641. if (startIndex < 0)
  642. {
  643. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
  644. }
  645. if (count < 0)
  646. {
  647. throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NegativeCount);
  648. }
  649. if (startIndex > value.Length - count)
  650. {
  651. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_IndexCountBuffer);
  652. }
  653. if (count <= 1)
  654. {
  655. return count == 0 ?
  656. string.Empty :
  657. value[startIndex] ?? string.Empty;
  658. }
  659. long totalSeparatorsLength = (long)(count - 1) * separatorLength;
  660. if (totalSeparatorsLength > int.MaxValue)
  661. {
  662. throw new OutOfMemoryException();
  663. }
  664. int totalLength = (int)totalSeparatorsLength;
  665. // Calculate the length of the resultant string so we know how much space to allocate.
  666. for (int i = startIndex, end = startIndex + count; i < end; i++)
  667. {
  668. string currentValue = value[i];
  669. if (currentValue != null)
  670. {
  671. totalLength += currentValue.Length;
  672. if (totalLength < 0) // Check for overflow
  673. {
  674. throw new OutOfMemoryException();
  675. }
  676. }
  677. }
  678. // Copy each of the strings into the resultant buffer, interleaving with the separator.
  679. string result = FastAllocateString(totalLength);
  680. int copiedLength = 0;
  681. for (int i = startIndex, end = startIndex + count; i < end; i++)
  682. {
  683. // It's possible that another thread may have mutated the input array
  684. // such that our second read of an index will not be the same string
  685. // we got during the first read.
  686. // We range check again to avoid buffer overflows if this happens.
  687. string currentValue = value[i];
  688. if (currentValue != null)
  689. {
  690. int valueLen = currentValue.Length;
  691. if (valueLen > totalLength - copiedLength)
  692. {
  693. copiedLength = -1;
  694. break;
  695. }
  696. // Fill in the value.
  697. FillStringChecked(result, copiedLength, currentValue);
  698. copiedLength += valueLen;
  699. }
  700. if (i < end - 1)
  701. {
  702. // Fill in the separator.
  703. fixed (char* pResult = &result._firstChar)
  704. {
  705. // If we are called from the char-based overload, we will not
  706. // want to call MemoryCopy each time we fill in the separator. So
  707. // specialize for 1-length separators.
  708. if (separatorLength == 1)
  709. {
  710. pResult[copiedLength] = *separator;
  711. }
  712. else
  713. {
  714. wstrcpy(pResult + copiedLength, separator, separatorLength);
  715. }
  716. }
  717. copiedLength += separatorLength;
  718. }
  719. }
  720. // If we copied exactly the right amount, return the new string. Otherwise,
  721. // something changed concurrently to mutate the input array: fall back to
  722. // doing the concatenation again, but this time with a defensive copy. This
  723. // fall back should be extremely rare.
  724. return copiedLength == totalLength ?
  725. result :
  726. JoinCore(separator, separatorLength, (string[])value.Clone(), startIndex, count);
  727. }
  728. public string PadLeft(int totalWidth) => PadLeft(totalWidth, ' ');
  729. public string PadLeft(int totalWidth, char paddingChar)
  730. {
  731. if (totalWidth < 0)
  732. throw new ArgumentOutOfRangeException(nameof(totalWidth), SR.ArgumentOutOfRange_NeedNonNegNum);
  733. int oldLength = Length;
  734. int count = totalWidth - oldLength;
  735. if (count <= 0)
  736. return this;
  737. string result = FastAllocateString(totalWidth);
  738. unsafe
  739. {
  740. fixed (char* dst = &result._firstChar)
  741. {
  742. for (int i = 0; i < count; i++)
  743. dst[i] = paddingChar;
  744. fixed (char* src = &_firstChar)
  745. {
  746. wstrcpy(dst + count, src, oldLength);
  747. }
  748. }
  749. }
  750. return result;
  751. }
  752. public string PadRight(int totalWidth) => PadRight(totalWidth, ' ');
  753. public string PadRight(int totalWidth, char paddingChar)
  754. {
  755. if (totalWidth < 0)
  756. throw new ArgumentOutOfRangeException(nameof(totalWidth), SR.ArgumentOutOfRange_NeedNonNegNum);
  757. int oldLength = Length;
  758. int count = totalWidth - oldLength;
  759. if (count <= 0)
  760. return this;
  761. string result = FastAllocateString(totalWidth);
  762. unsafe
  763. {
  764. fixed (char* dst = &result._firstChar)
  765. {
  766. fixed (char* src = &_firstChar)
  767. {
  768. wstrcpy(dst, src, oldLength);
  769. }
  770. for (int i = 0; i < count; i++)
  771. dst[oldLength + i] = paddingChar;
  772. }
  773. }
  774. return result;
  775. }
  776. public string Remove(int startIndex, int count)
  777. {
  778. if (startIndex < 0)
  779. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
  780. if (count < 0)
  781. throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NegativeCount);
  782. int oldLength = this.Length;
  783. if (count > oldLength - startIndex)
  784. throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_IndexCount);
  785. if (count == 0)
  786. return this;
  787. int newLength = oldLength - count;
  788. if (newLength == 0)
  789. return string.Empty;
  790. string result = FastAllocateString(newLength);
  791. unsafe
  792. {
  793. fixed (char* src = &_firstChar)
  794. {
  795. fixed (char* dst = &result._firstChar)
  796. {
  797. wstrcpy(dst, src, startIndex);
  798. wstrcpy(dst + startIndex, src + startIndex + count, newLength - startIndex);
  799. }
  800. }
  801. }
  802. return result;
  803. }
  804. // a remove that just takes a startindex.
  805. public string Remove(int startIndex)
  806. {
  807. if (startIndex < 0)
  808. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
  809. if (startIndex >= Length)
  810. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndexLessThanLength);
  811. return Substring(0, startIndex);
  812. }
  813. public string Replace(string oldValue, string newValue, bool ignoreCase, CultureInfo culture)
  814. {
  815. return ReplaceCore(oldValue, newValue, culture, ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None);
  816. }
  817. public string Replace(string oldValue, string newValue, StringComparison comparisonType)
  818. {
  819. switch (comparisonType)
  820. {
  821. case StringComparison.CurrentCulture:
  822. case StringComparison.CurrentCultureIgnoreCase:
  823. return ReplaceCore(oldValue, newValue, CultureInfo.CurrentCulture, GetCaseCompareOfComparisonCulture(comparisonType));
  824. case StringComparison.InvariantCulture:
  825. case StringComparison.InvariantCultureIgnoreCase:
  826. return ReplaceCore(oldValue, newValue, CultureInfo.InvariantCulture, GetCaseCompareOfComparisonCulture(comparisonType));
  827. case StringComparison.Ordinal:
  828. return Replace(oldValue, newValue);
  829. case StringComparison.OrdinalIgnoreCase:
  830. return ReplaceCore(oldValue, newValue, CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase);
  831. default:
  832. throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
  833. }
  834. }
  835. private unsafe string ReplaceCore(string oldValue, string newValue, CultureInfo culture, CompareOptions options)
  836. {
  837. if (oldValue == null)
  838. throw new ArgumentNullException(nameof(oldValue));
  839. if (oldValue.Length == 0)
  840. throw new ArgumentException(SR.Argument_StringZeroLength, nameof(oldValue));
  841. // If they asked to replace oldValue with a null, replace all occurrences
  842. // with the empty string.
  843. if (newValue == null)
  844. newValue = string.Empty;
  845. CultureInfo referenceCulture = culture ?? CultureInfo.CurrentCulture;
  846. StringBuilder result = StringBuilderCache.Acquire();
  847. int startIndex = 0;
  848. int index = 0;
  849. int matchLength = 0;
  850. bool hasDoneAnyReplacements = false;
  851. CompareInfo ci = referenceCulture.CompareInfo;
  852. do
  853. {
  854. index = ci.IndexOf(this, oldValue, startIndex, this.Length - startIndex, options, &matchLength);
  855. if (index >= 0)
  856. {
  857. // append the unmodified portion of string
  858. result.Append(this, startIndex, index - startIndex);
  859. // append the replacement
  860. result.Append(newValue);
  861. startIndex = index + matchLength;
  862. hasDoneAnyReplacements = true;
  863. }
  864. else if (!hasDoneAnyReplacements)
  865. {
  866. // small optimization,
  867. // if we have not done any replacements,
  868. // we will return the original string
  869. StringBuilderCache.Release(result);
  870. return this;
  871. }
  872. else
  873. {
  874. result.Append(this, startIndex, this.Length - startIndex);
  875. }
  876. } while (index >= 0);
  877. return StringBuilderCache.GetStringAndRelease(result);
  878. }
  879. // Replaces all instances of oldChar with newChar.
  880. //
  881. public string Replace(char oldChar, char newChar)
  882. {
  883. if (oldChar == newChar)
  884. return this;
  885. unsafe
  886. {
  887. int remainingLength = Length;
  888. fixed (char* pChars = &_firstChar)
  889. {
  890. char* pSrc = pChars;
  891. while (remainingLength > 0)
  892. {
  893. if (*pSrc == oldChar)
  894. {
  895. break;
  896. }
  897. remainingLength--;
  898. pSrc++;
  899. }
  900. }
  901. if (remainingLength == 0)
  902. return this;
  903. string result = FastAllocateString(Length);
  904. fixed (char* pChars = &_firstChar)
  905. {
  906. fixed (char* pResult = &result._firstChar)
  907. {
  908. int copyLength = Length - remainingLength;
  909. //Copy the characters already proven not to match.
  910. if (copyLength > 0)
  911. {
  912. wstrcpy(pResult, pChars, copyLength);
  913. }
  914. //Copy the remaining characters, doing the replacement as we go.
  915. char* pSrc = pChars + copyLength;
  916. char* pDst = pResult + copyLength;
  917. do
  918. {
  919. char currentChar = *pSrc;
  920. if (currentChar == oldChar)
  921. currentChar = newChar;
  922. *pDst = currentChar;
  923. remainingLength--;
  924. pSrc++;
  925. pDst++;
  926. } while (remainingLength > 0);
  927. }
  928. }
  929. return result;
  930. }
  931. }
  932. public string Replace(string oldValue, string newValue)
  933. {
  934. if (oldValue == null)
  935. throw new ArgumentNullException(nameof(oldValue));
  936. if (oldValue.Length == 0)
  937. throw new ArgumentException(SR.Argument_StringZeroLength, nameof(oldValue));
  938. // Api behavior: if newValue is null, instances of oldValue are to be removed.
  939. if (newValue == null)
  940. newValue = string.Empty;
  941. Span<int> initialSpan = stackalloc int[StackallocIntBufferSizeLimit];
  942. var replacementIndices = new ValueListBuilder<int>(initialSpan);
  943. unsafe
  944. {
  945. fixed (char* pThis = &_firstChar)
  946. {
  947. int matchIdx = 0;
  948. int lastPossibleMatchIdx = this.Length - oldValue.Length;
  949. while (matchIdx <= lastPossibleMatchIdx)
  950. {
  951. char* pMatch = pThis + matchIdx;
  952. for (int probeIdx = 0; probeIdx < oldValue.Length; probeIdx++)
  953. {
  954. if (pMatch[probeIdx] != oldValue[probeIdx])
  955. {
  956. goto Next;
  957. }
  958. }
  959. // Found a match for the string. Record the location of the match and skip over the "oldValue."
  960. replacementIndices.Append(matchIdx);
  961. matchIdx += oldValue.Length;
  962. continue;
  963. Next:
  964. matchIdx++;
  965. }
  966. }
  967. }
  968. if (replacementIndices.Length == 0)
  969. return this;
  970. // String allocation and copying is in separate method to make this method faster for the case where
  971. // nothing needs replacing.
  972. string dst = ReplaceHelper(oldValue.Length, newValue, replacementIndices.AsSpan());
  973. replacementIndices.Dispose();
  974. return dst;
  975. }
  976. private string ReplaceHelper(int oldValueLength, string newValue, ReadOnlySpan<int> indices)
  977. {
  978. Debug.Assert(indices.Length > 0);
  979. long dstLength = this.Length + ((long)(newValue.Length - oldValueLength)) * indices.Length;
  980. if (dstLength > int.MaxValue)
  981. throw new OutOfMemoryException();
  982. string dst = FastAllocateString((int)dstLength);
  983. Span<char> dstSpan = new Span<char>(ref dst.GetRawStringData(), dst.Length);
  984. int thisIdx = 0;
  985. int dstIdx = 0;
  986. for (int r = 0; r < indices.Length; r++)
  987. {
  988. int replacementIdx = indices[r];
  989. // Copy over the non-matching portion of the original that precedes this occurrence of oldValue.
  990. int count = replacementIdx - thisIdx;
  991. if (count != 0)
  992. {
  993. this.AsSpan(thisIdx, count).CopyTo(dstSpan.Slice(dstIdx));
  994. dstIdx += count;
  995. }
  996. thisIdx = replacementIdx + oldValueLength;
  997. // Copy over newValue to replace the oldValue.
  998. newValue.AsSpan().CopyTo(dstSpan.Slice(dstIdx));
  999. dstIdx += newValue.Length;
  1000. }
  1001. // Copy over the final non-matching portion at the end of the string.
  1002. Debug.Assert(this.Length - thisIdx == dstSpan.Length - dstIdx);
  1003. this.AsSpan(thisIdx).CopyTo(dstSpan.Slice(dstIdx));
  1004. return dst;
  1005. }
  1006. public string[] Split(char separator, StringSplitOptions options = StringSplitOptions.None)
  1007. {
  1008. return SplitInternal(new ReadOnlySpan<char>(ref separator, 1), int.MaxValue, options);
  1009. }
  1010. public string[] Split(char separator, int count, StringSplitOptions options = StringSplitOptions.None)
  1011. {
  1012. return SplitInternal(new ReadOnlySpan<char>(ref separator, 1), count, options);
  1013. }
  1014. // Creates an array of strings by splitting this string at each
  1015. // occurrence of a separator. The separator is searched for, and if found,
  1016. // the substring preceding the occurrence is stored as the first element in
  1017. // the array of strings. We then continue in this manner by searching
  1018. // the substring that follows the occurrence. On the other hand, if the separator
  1019. // is not found, the array of strings will contain this instance as its only element.
  1020. // If the separator is null
  1021. // whitespace (i.e., Character.IsWhitespace) is used as the separator.
  1022. //
  1023. public string[] Split(params char[] separator)
  1024. {
  1025. return SplitInternal(separator, int.MaxValue, StringSplitOptions.None);
  1026. }
  1027. // Creates an array of strings by splitting this string at each
  1028. // occurrence of a separator. The separator is searched for, and if found,
  1029. // the substring preceding the occurrence is stored as the first element in
  1030. // the array of strings. We then continue in this manner by searching
  1031. // the substring that follows the occurrence. On the other hand, if the separator
  1032. // is not found, the array of strings will contain this instance as its only element.
  1033. // If the separator is the empty string (i.e., string.Empty), then
  1034. // whitespace (i.e., Character.IsWhitespace) is used as the separator.
  1035. // If there are more than count different strings, the last n-(count-1)
  1036. // elements are concatenated and added as the last string.
  1037. //
  1038. public string[] Split(char[] separator, int count)
  1039. {
  1040. return SplitInternal(separator, count, StringSplitOptions.None);
  1041. }
  1042. public string[] Split(char[] separator, StringSplitOptions options)
  1043. {
  1044. return SplitInternal(separator, int.MaxValue, options);
  1045. }
  1046. public string[] Split(char[] separator, int count, StringSplitOptions options)
  1047. {
  1048. return SplitInternal(separator, count, options);
  1049. }
  1050. private string[] SplitInternal(ReadOnlySpan<char> separators, int count, StringSplitOptions options)
  1051. {
  1052. if (count < 0)
  1053. throw new ArgumentOutOfRangeException(nameof(count),
  1054. SR.ArgumentOutOfRange_NegativeCount);
  1055. if (options < StringSplitOptions.None || options > StringSplitOptions.RemoveEmptyEntries)
  1056. throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, options));
  1057. bool omitEmptyEntries = (options == StringSplitOptions.RemoveEmptyEntries);
  1058. if ((count == 0) || (omitEmptyEntries && Length == 0))
  1059. {
  1060. return Array.Empty<string>();
  1061. }
  1062. if (count == 1)
  1063. {
  1064. return new string[] { this };
  1065. }
  1066. Span<int> initialSpan = stackalloc int[StackallocIntBufferSizeLimit];
  1067. var sepListBuilder = new ValueListBuilder<int>(initialSpan);
  1068. MakeSeparatorList(separators, ref sepListBuilder);
  1069. ReadOnlySpan<int> sepList = sepListBuilder.AsSpan();
  1070. // Handle the special case of no replaces.
  1071. if (sepList.Length == 0)
  1072. {
  1073. return new string[] { this };
  1074. }
  1075. string[] result = omitEmptyEntries
  1076. ? SplitOmitEmptyEntries(sepList, default, 1, count)
  1077. : SplitKeepEmptyEntries(sepList, default, 1, count);
  1078. sepListBuilder.Dispose();
  1079. return result;
  1080. }
  1081. public string[] Split(string separator, StringSplitOptions options = StringSplitOptions.None)
  1082. {
  1083. return SplitInternal(separator ?? string.Empty, null, int.MaxValue, options);
  1084. }
  1085. public string[] Split(string separator, int count, StringSplitOptions options = StringSplitOptions.None)
  1086. {
  1087. return SplitInternal(separator ?? string.Empty, null, count, options);
  1088. }
  1089. public string[] Split(string[] separator, StringSplitOptions options)
  1090. {
  1091. return SplitInternal(null, separator, int.MaxValue, options);
  1092. }
  1093. public string[] Split(string[] separator, int count, StringSplitOptions options)
  1094. {
  1095. return SplitInternal(null, separator, count, options);
  1096. }
  1097. private string[] SplitInternal(string separator, string[] separators, int count, StringSplitOptions options)
  1098. {
  1099. if (count < 0)
  1100. {
  1101. throw new ArgumentOutOfRangeException(nameof(count),
  1102. SR.ArgumentOutOfRange_NegativeCount);
  1103. }
  1104. if (options < StringSplitOptions.None || options > StringSplitOptions.RemoveEmptyEntries)
  1105. {
  1106. throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)options));
  1107. }
  1108. bool omitEmptyEntries = (options == StringSplitOptions.RemoveEmptyEntries);
  1109. bool singleSeparator = separator != null;
  1110. if (!singleSeparator && (separators == null || separators.Length == 0))
  1111. {
  1112. return SplitInternal((char[])null, count, options);
  1113. }
  1114. if ((count == 0) || (omitEmptyEntries && Length == 0))
  1115. {
  1116. return Array.Empty<string>();
  1117. }
  1118. if (count == 1 || (singleSeparator && separator.Length == 0))
  1119. {
  1120. return new string[] { this };
  1121. }
  1122. if (singleSeparator)
  1123. {
  1124. return SplitInternal(separator, count, options);
  1125. }
  1126. Span<int> sepListInitialSpan = stackalloc int[StackallocIntBufferSizeLimit];
  1127. var sepListBuilder = new ValueListBuilder<int>(sepListInitialSpan);
  1128. Span<int> lengthListInitialSpan = stackalloc int[StackallocIntBufferSizeLimit];
  1129. var lengthListBuilder = new ValueListBuilder<int>(lengthListInitialSpan);
  1130. MakeSeparatorList(separators, ref sepListBuilder, ref lengthListBuilder);
  1131. ReadOnlySpan<int> sepList = sepListBuilder.AsSpan();
  1132. ReadOnlySpan<int> lengthList = lengthListBuilder.AsSpan();
  1133. // Handle the special case of no replaces.
  1134. if (sepList.Length == 0)
  1135. {
  1136. return new string[] { this };
  1137. }
  1138. string[] result = omitEmptyEntries
  1139. ? SplitOmitEmptyEntries(sepList, lengthList, 0, count)
  1140. : SplitKeepEmptyEntries(sepList, lengthList, 0, count);
  1141. sepListBuilder.Dispose();
  1142. lengthListBuilder.Dispose();
  1143. return result;
  1144. }
  1145. private string[] SplitInternal(string separator, int count, StringSplitOptions options)
  1146. {
  1147. Span<int> sepListInitialSpan = stackalloc int[StackallocIntBufferSizeLimit];
  1148. var sepListBuilder = new ValueListBuilder<int>(sepListInitialSpan);
  1149. MakeSeparatorList(separator, ref sepListBuilder);
  1150. ReadOnlySpan<int> sepList = sepListBuilder.AsSpan();
  1151. if (sepList.Length == 0)
  1152. {
  1153. // there are no separators so sepListBuilder did not rent an array from pool and there is no need to dispose it
  1154. return new string[] { this };
  1155. }
  1156. string[] result = options == StringSplitOptions.RemoveEmptyEntries
  1157. ? SplitOmitEmptyEntries(sepList, default, separator.Length, count)
  1158. : SplitKeepEmptyEntries(sepList, default, separator.Length, count);
  1159. sepListBuilder.Dispose();
  1160. return result;
  1161. }
  1162. private string[] SplitKeepEmptyEntries(ReadOnlySpan<int> sepList, ReadOnlySpan<int> lengthList, int defaultLength, int count)
  1163. {
  1164. Debug.Assert(count >= 2);
  1165. int currIndex = 0;
  1166. int arrIndex = 0;
  1167. count--;
  1168. int numActualReplaces = (sepList.Length < count) ? sepList.Length : count;
  1169. //Allocate space for the new array.
  1170. //+1 for the string from the end of the last replace to the end of the string.
  1171. string[] splitStrings = new string[numActualReplaces + 1];
  1172. for (int i = 0; i < numActualReplaces && currIndex < Length; i++)
  1173. {
  1174. splitStrings[arrIndex++] = Substring(currIndex, sepList[i] - currIndex);
  1175. currIndex = sepList[i] + (lengthList.IsEmpty ? defaultLength : lengthList[i]);
  1176. }
  1177. //Handle the last string at the end of the array if there is one.
  1178. if (currIndex < Length && numActualReplaces >= 0)
  1179. {
  1180. splitStrings[arrIndex] = Substring(currIndex);
  1181. }
  1182. else if (arrIndex == numActualReplaces)
  1183. {
  1184. //We had a separator character at the end of a string. Rather than just allowing
  1185. //a null character, we'll replace the last element in the array with an empty string.
  1186. splitStrings[arrIndex] = string.Empty;
  1187. }
  1188. return splitStrings;
  1189. }
  1190. // This function will not keep the Empty string
  1191. private string[] SplitOmitEmptyEntries(ReadOnlySpan<int> sepList, ReadOnlySpan<int> lengthList, int defaultLength, int count)
  1192. {
  1193. Debug.Assert(count >= 2);
  1194. int numReplaces = sepList.Length;
  1195. // Allocate array to hold items. This array may not be
  1196. // filled completely in this function, we will create a
  1197. // new array and copy string references to that new array.
  1198. int maxItems = (numReplaces < count) ? (numReplaces + 1) : count;
  1199. string[] splitStrings = new string[maxItems];
  1200. int currIndex = 0;
  1201. int arrIndex = 0;
  1202. for (int i = 0; i < numReplaces && currIndex < Length; i++)
  1203. {
  1204. if (sepList[i] - currIndex > 0)
  1205. {
  1206. splitStrings[arrIndex++] = Substring(currIndex, sepList[i] - currIndex);
  1207. }
  1208. currIndex = sepList[i] + (lengthList.IsEmpty ? defaultLength : lengthList[i]);
  1209. if (arrIndex == count - 1)
  1210. {
  1211. // If all the remaining entries at the end are empty, skip them
  1212. while (i < numReplaces - 1 && currIndex == sepList[++i])
  1213. {
  1214. currIndex += (lengthList.IsEmpty ? defaultLength : lengthList[i]);
  1215. }
  1216. break;
  1217. }
  1218. }
  1219. // we must have at least one slot left to fill in the last string.
  1220. Debug.Assert(arrIndex < maxItems);
  1221. //Handle the last string at the end of the array if there is one.
  1222. if (currIndex < Length)
  1223. {
  1224. splitStrings[arrIndex++] = Substring(currIndex);
  1225. }
  1226. string[] stringArray = splitStrings;
  1227. if (arrIndex != maxItems)
  1228. {
  1229. stringArray = new string[arrIndex];
  1230. for (int j = 0; j < arrIndex; j++)
  1231. {
  1232. stringArray[j] = splitStrings[j];
  1233. }
  1234. }
  1235. return stringArray;
  1236. }
  1237. /// <summary>
  1238. /// Uses ValueListBuilder to create list that holds indexes of separators in string.
  1239. /// </summary>
  1240. /// <param name="separators"><see cref="ReadOnlySpan{T}"/> of separator chars</param>
  1241. /// <param name="sepListBuilder"><see cref="ValueListBuilder{T}"/> to store indexes</param>
  1242. /// <returns></returns>
  1243. private void MakeSeparatorList(ReadOnlySpan<char> separators, ref ValueListBuilder<int> sepListBuilder)
  1244. {
  1245. char sep0, sep1, sep2;
  1246. switch (separators.Length)
  1247. {
  1248. // Special-case no separators to mean any whitespace is a separator.
  1249. case 0:
  1250. for (int i = 0; i < Length; i++)
  1251. {
  1252. if (char.IsWhiteSpace(this[i]))
  1253. {
  1254. sepListBuilder.Append(i);
  1255. }
  1256. }
  1257. break;
  1258. // Special-case the common cases of 1, 2, and 3 separators, with manual comparisons against each separator.
  1259. case 1:
  1260. sep0 = separators[0];
  1261. for (int i = 0; i < Length; i++)
  1262. {
  1263. if (this[i] == sep0)
  1264. {
  1265. sepListBuilder.Append(i);
  1266. }
  1267. }
  1268. break;
  1269. case 2:
  1270. sep0 = separators[0];
  1271. sep1 = separators[1];
  1272. for (int i = 0; i < Length; i++)
  1273. {
  1274. char c = this[i];
  1275. if (c == sep0 || c == sep1)
  1276. {
  1277. sepListBuilder.Append(i);
  1278. }
  1279. }
  1280. break;
  1281. case 3:
  1282. sep0 = separators[0];
  1283. sep1 = separators[1];
  1284. sep2 = separators[2];
  1285. for (int i = 0; i < Length; i++)
  1286. {
  1287. char c = this[i];
  1288. if (c == sep0 || c == sep1 || c == sep2)
  1289. {
  1290. sepListBuilder.Append(i);
  1291. }
  1292. }
  1293. break;
  1294. // Handle > 3 separators with a probabilistic map, ala IndexOfAny.
  1295. // This optimizes for chars being unlikely to match a separator.
  1296. default:
  1297. unsafe
  1298. {
  1299. ProbabilisticMap map = default;
  1300. uint* charMap = (uint*)&map;
  1301. InitializeProbabilisticMap(charMap, separators);
  1302. for (int i = 0; i < Length; i++)
  1303. {
  1304. char c = this[i];
  1305. if (IsCharBitSet(charMap, (byte)c) && IsCharBitSet(charMap, (byte)(c >> 8)) &&
  1306. separators.Contains(c))
  1307. {
  1308. sepListBuilder.Append(i);
  1309. }
  1310. }
  1311. }
  1312. break;
  1313. }
  1314. }
  1315. /// <summary>
  1316. /// Uses ValueListBuilder to create list that holds indexes of separators in string.
  1317. /// </summary>
  1318. /// <param name="separator">separator string</param>
  1319. /// <param name="sepListBuilder"><see cref="ValueListBuilder{T}"/> to store indexes</param>
  1320. /// <returns></returns>
  1321. private void MakeSeparatorList(string separator, ref ValueListBuilder<int> sepListBuilder)
  1322. {
  1323. Debug.Assert(!IsNullOrEmpty(separator), "!string.IsNullOrEmpty(separator)");
  1324. int currentSepLength = separator.Length;
  1325. for (int i = 0; i < Length; i++)
  1326. {
  1327. if (this[i] == separator[0] && currentSepLength <= Length - i)
  1328. {
  1329. if (currentSepLength == 1
  1330. || this.AsSpan(i, currentSepLength).SequenceEqual(separator))
  1331. {
  1332. sepListBuilder.Append(i);
  1333. i += currentSepLength - 1;
  1334. }
  1335. }
  1336. }
  1337. }
  1338. /// <summary>
  1339. /// Uses ValueListBuilder to create list that holds indexes of separators in string and list that holds length of separator strings.
  1340. /// </summary>
  1341. /// <param name="separators">separator strngs</param>
  1342. /// <param name="sepListBuilder"><see cref="ValueListBuilder{T}"/> for separator indexes</param>
  1343. /// <param name="lengthListBuilder"><see cref="ValueListBuilder{T}"/> for separator length values</param>
  1344. private void MakeSeparatorList(string[] separators, ref ValueListBuilder<int> sepListBuilder, ref ValueListBuilder<int> lengthListBuilder)
  1345. {
  1346. Debug.Assert(separators != null && separators.Length > 0, "separators != null && separators.Length > 0");
  1347. int sepCount = separators.Length;
  1348. for (int i = 0; i < Length; i++)
  1349. {
  1350. for (int j = 0; j < separators.Length; j++)
  1351. {
  1352. string separator = separators[j];
  1353. if (IsNullOrEmpty(separator))
  1354. {
  1355. continue;
  1356. }
  1357. int currentSepLength = separator.Length;
  1358. if (this[i] == separator[0] && currentSepLength <= Length - i)
  1359. {
  1360. if (currentSepLength == 1
  1361. || this.AsSpan(i, currentSepLength).SequenceEqual(separator))
  1362. {
  1363. sepListBuilder.Append(i);
  1364. lengthListBuilder.Append(currentSepLength);
  1365. i += currentSepLength - 1;
  1366. break;
  1367. }
  1368. }
  1369. }
  1370. }
  1371. }
  1372. // Returns a substring of this string.
  1373. //
  1374. public string Substring(int startIndex) => Substring(startIndex, Length - startIndex);
  1375. public string Substring(int startIndex, int length)
  1376. {
  1377. if (startIndex < 0)
  1378. {
  1379. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
  1380. }
  1381. if (startIndex > Length)
  1382. {
  1383. throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndexLargerThanLength);
  1384. }
  1385. if (length < 0)
  1386. {
  1387. throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength);
  1388. }
  1389. if (startIndex > Length - length)
  1390. {
  1391. throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_IndexLength);
  1392. }
  1393. if (length == 0)
  1394. {
  1395. return string.Empty;
  1396. }
  1397. if (startIndex == 0 && length == this.Length)
  1398. {
  1399. return this;
  1400. }
  1401. return InternalSubString(startIndex, length);
  1402. }
  1403. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1404. public string Substring(Index startIndex)
  1405. {
  1406. int actualIndex = startIndex.GetOffset(Length);
  1407. return Substring(actualIndex);
  1408. }
  1409. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1410. public string Substring(Range range)
  1411. {
  1412. (int start, int length) = range.GetOffsetAndLength(Length);
  1413. return Substring(start, length);
  1414. }
  1415. private unsafe string InternalSubString(int startIndex, int length)
  1416. {
  1417. Debug.Assert(startIndex >= 0 && startIndex <= this.Length, "StartIndex is out of range!");
  1418. Debug.Assert(length >= 0 && startIndex <= this.Length - length, "length is out of range!");
  1419. string result = FastAllocateString(length);
  1420. fixed (char* dest = &result._firstChar)
  1421. fixed (char* src = &_firstChar)
  1422. {
  1423. wstrcpy(dest, src + startIndex, length);
  1424. }
  1425. return result;
  1426. }
  1427. // Creates a copy of this string in lower case. The culture is set by culture.
  1428. public string ToLower()
  1429. {
  1430. return CultureInfo.CurrentCulture.TextInfo.ToLower(this);
  1431. }
  1432. // Creates a copy of this string in lower case. The culture is set by culture.
  1433. public string ToLower(CultureInfo culture)
  1434. {
  1435. if (culture == null)
  1436. {
  1437. throw new ArgumentNullException(nameof(culture));
  1438. }
  1439. return culture.TextInfo.ToLower(this);
  1440. }
  1441. // Creates a copy of this string in lower case based on invariant culture.
  1442. public string ToLowerInvariant()
  1443. {
  1444. return CultureInfo.InvariantCulture.TextInfo.ToLower(this);
  1445. }
  1446. public string ToUpper()
  1447. {
  1448. return CultureInfo.CurrentCulture.TextInfo.ToUpper(this);
  1449. }
  1450. // Creates a copy of this string in upper case. The culture is set by culture.
  1451. public string ToUpper(CultureInfo culture)
  1452. {
  1453. if (culture == null)
  1454. {
  1455. throw new ArgumentNullException(nameof(culture));
  1456. }
  1457. return culture.TextInfo.ToUpper(this);
  1458. }
  1459. //Creates a copy of this string in upper case based on invariant culture.
  1460. public string ToUpperInvariant()
  1461. {
  1462. return CultureInfo.InvariantCulture.TextInfo.ToUpper(this);
  1463. }
  1464. // Trims the whitespace from both ends of the string. Whitespace is defined by
  1465. // char.IsWhiteSpace.
  1466. //
  1467. public string Trim() => TrimWhiteSpaceHelper(TrimType.Both);
  1468. // Removes a set of characters from the beginning and end of this string.
  1469. public unsafe string Trim(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Both);
  1470. // Removes a set of characters from the beginning and end of this string.
  1471. public unsafe string Trim(params char[] trimChars)
  1472. {
  1473. if (trimChars == null || trimChars.Length == 0)
  1474. {
  1475. return TrimWhiteSpaceHelper(TrimType.Both);
  1476. }
  1477. fixed (char* pTrimChars = &trimChars[0])
  1478. {
  1479. return TrimHelper(pTrimChars, trimChars.Length, TrimType.Both);
  1480. }
  1481. }
  1482. // Removes a set of characters from the beginning of this string.
  1483. public string TrimStart() => TrimWhiteSpaceHelper(TrimType.Head);
  1484. // Removes a set of characters from the beginning of this string.
  1485. public unsafe string TrimStart(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Head);
  1486. // Removes a set of characters from the beginning of this string.
  1487. public unsafe string TrimStart(params char[] trimChars)
  1488. {
  1489. if (trimChars == null || trimChars.Length == 0)
  1490. {
  1491. return TrimWhiteSpaceHelper(TrimType.Head);
  1492. }
  1493. fixed (char* pTrimChars = &trimChars[0])
  1494. {
  1495. return TrimHelper(pTrimChars, trimChars.Length, TrimType.Head);
  1496. }
  1497. }
  1498. // Removes a set of characters from the end of this string.
  1499. public string TrimEnd() => TrimWhiteSpaceHelper(TrimType.Tail);
  1500. // Removes a set of characters from the end of this string.
  1501. public unsafe string TrimEnd(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Tail);
  1502. // Removes a set of characters from the end of this string.
  1503. public unsafe string TrimEnd(params char[] trimChars)
  1504. {
  1505. if (trimChars == null || trimChars.Length == 0)
  1506. {
  1507. return TrimWhiteSpaceHelper(TrimType.Tail);
  1508. }
  1509. fixed (char* pTrimChars = &trimChars[0])
  1510. {
  1511. return TrimHelper(pTrimChars, trimChars.Length, TrimType.Tail);
  1512. }
  1513. }
  1514. private string TrimWhiteSpaceHelper(TrimType trimType)
  1515. {
  1516. // end will point to the first non-trimmed character on the right.
  1517. // start will point to the first non-trimmed character on the left.
  1518. int end = Length - 1;
  1519. int start = 0;
  1520. // Trim specified characters.
  1521. if (trimType != TrimType.Tail)
  1522. {
  1523. for (start = 0; start < Length; start++)
  1524. {
  1525. if (!char.IsWhiteSpace(this[start]))
  1526. {
  1527. break;
  1528. }
  1529. }
  1530. }
  1531. if (trimType != TrimType.Head)
  1532. {
  1533. for (end = Length - 1; end >= start; end--)
  1534. {
  1535. if (!char.IsWhiteSpace(this[end]))
  1536. {
  1537. break;
  1538. }
  1539. }
  1540. }
  1541. return CreateTrimmedString(start, end);
  1542. }
  1543. private unsafe string TrimHelper(char* trimChars, int trimCharsLength, TrimType trimType)
  1544. {
  1545. Debug.Assert(trimChars != null);
  1546. Debug.Assert(trimCharsLength > 0);
  1547. // end will point to the first non-trimmed character on the right.
  1548. // start will point to the first non-trimmed character on the left.
  1549. int end = Length - 1;
  1550. int start = 0;
  1551. // Trim specified characters.
  1552. if (trimType != TrimType.Tail)
  1553. {
  1554. for (start = 0; start < Length; start++)
  1555. {
  1556. int i = 0;
  1557. char ch = this[start];
  1558. for (i = 0; i < trimCharsLength; i++)
  1559. {
  1560. if (trimChars[i] == ch)
  1561. {
  1562. break;
  1563. }
  1564. }
  1565. if (i == trimCharsLength)
  1566. {
  1567. // The character is not in trimChars, so stop trimming.
  1568. break;
  1569. }
  1570. }
  1571. }
  1572. if (trimType != TrimType.Head)
  1573. {
  1574. for (end = Length - 1; end >= start; end--)
  1575. {
  1576. int i = 0;
  1577. char ch = this[end];
  1578. for (i = 0; i < trimCharsLength; i++)
  1579. {
  1580. if (trimChars[i] == ch)
  1581. {
  1582. break;
  1583. }
  1584. }
  1585. if (i == trimCharsLength)
  1586. {
  1587. // The character is not in trimChars, so stop trimming.
  1588. break;
  1589. }
  1590. }
  1591. }
  1592. return CreateTrimmedString(start, end);
  1593. }
  1594. private string CreateTrimmedString(int start, int end)
  1595. {
  1596. int len = end - start + 1;
  1597. return
  1598. len == Length ? this :
  1599. len == 0 ? string.Empty :
  1600. InternalSubString(start, len);
  1601. }
  1602. private enum TrimType
  1603. {
  1604. Head = 0,
  1605. Tail = 1,
  1606. Both = 2
  1607. }
  1608. }
  1609. }