String.Manipulation.cs 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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. private unsafe string InternalSubString(int startIndex, int length)
  1404. {
  1405. Debug.Assert(startIndex >= 0 && startIndex <= this.Length, "StartIndex is out of range!");
  1406. Debug.Assert(length >= 0 && startIndex <= this.Length - length, "length is out of range!");
  1407. string result = FastAllocateString(length);
  1408. fixed (char* dest = &result._firstChar)
  1409. fixed (char* src = &_firstChar)
  1410. {
  1411. wstrcpy(dest, src + startIndex, length);
  1412. }
  1413. return result;
  1414. }
  1415. // Creates a copy of this string in lower case. The culture is set by culture.
  1416. public string ToLower()
  1417. {
  1418. return CultureInfo.CurrentCulture.TextInfo.ToLower(this);
  1419. }
  1420. // Creates a copy of this string in lower case. The culture is set by culture.
  1421. public string ToLower(CultureInfo culture)
  1422. {
  1423. if (culture == null)
  1424. {
  1425. throw new ArgumentNullException(nameof(culture));
  1426. }
  1427. return culture.TextInfo.ToLower(this);
  1428. }
  1429. // Creates a copy of this string in lower case based on invariant culture.
  1430. public string ToLowerInvariant()
  1431. {
  1432. return CultureInfo.InvariantCulture.TextInfo.ToLower(this);
  1433. }
  1434. public string ToUpper()
  1435. {
  1436. return CultureInfo.CurrentCulture.TextInfo.ToUpper(this);
  1437. }
  1438. // Creates a copy of this string in upper case. The culture is set by culture.
  1439. public string ToUpper(CultureInfo culture)
  1440. {
  1441. if (culture == null)
  1442. {
  1443. throw new ArgumentNullException(nameof(culture));
  1444. }
  1445. return culture.TextInfo.ToUpper(this);
  1446. }
  1447. //Creates a copy of this string in upper case based on invariant culture.
  1448. public string ToUpperInvariant()
  1449. {
  1450. return CultureInfo.InvariantCulture.TextInfo.ToUpper(this);
  1451. }
  1452. // Trims the whitespace from both ends of the string. Whitespace is defined by
  1453. // char.IsWhiteSpace.
  1454. //
  1455. public string Trim() => TrimWhiteSpaceHelper(TrimType.Both);
  1456. // Removes a set of characters from the beginning and end of this string.
  1457. public unsafe string Trim(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Both);
  1458. // Removes a set of characters from the beginning and end of this string.
  1459. public unsafe string Trim(params char[] trimChars)
  1460. {
  1461. if (trimChars == null || trimChars.Length == 0)
  1462. {
  1463. return TrimWhiteSpaceHelper(TrimType.Both);
  1464. }
  1465. fixed (char* pTrimChars = &trimChars[0])
  1466. {
  1467. return TrimHelper(pTrimChars, trimChars.Length, TrimType.Both);
  1468. }
  1469. }
  1470. // Removes a set of characters from the beginning of this string.
  1471. public string TrimStart() => TrimWhiteSpaceHelper(TrimType.Head);
  1472. // Removes a set of characters from the beginning of this string.
  1473. public unsafe string TrimStart(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Head);
  1474. // Removes a set of characters from the beginning of this string.
  1475. public unsafe string TrimStart(params char[] trimChars)
  1476. {
  1477. if (trimChars == null || trimChars.Length == 0)
  1478. {
  1479. return TrimWhiteSpaceHelper(TrimType.Head);
  1480. }
  1481. fixed (char* pTrimChars = &trimChars[0])
  1482. {
  1483. return TrimHelper(pTrimChars, trimChars.Length, TrimType.Head);
  1484. }
  1485. }
  1486. // Removes a set of characters from the end of this string.
  1487. public string TrimEnd() => TrimWhiteSpaceHelper(TrimType.Tail);
  1488. // Removes a set of characters from the end of this string.
  1489. public unsafe string TrimEnd(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Tail);
  1490. // Removes a set of characters from the end of this string.
  1491. public unsafe string TrimEnd(params char[] trimChars)
  1492. {
  1493. if (trimChars == null || trimChars.Length == 0)
  1494. {
  1495. return TrimWhiteSpaceHelper(TrimType.Tail);
  1496. }
  1497. fixed (char* pTrimChars = &trimChars[0])
  1498. {
  1499. return TrimHelper(pTrimChars, trimChars.Length, TrimType.Tail);
  1500. }
  1501. }
  1502. private string TrimWhiteSpaceHelper(TrimType trimType)
  1503. {
  1504. // end will point to the first non-trimmed character on the right.
  1505. // start will point to the first non-trimmed character on the left.
  1506. int end = Length - 1;
  1507. int start = 0;
  1508. // Trim specified characters.
  1509. if (trimType != TrimType.Tail)
  1510. {
  1511. for (start = 0; start < Length; start++)
  1512. {
  1513. if (!char.IsWhiteSpace(this[start]))
  1514. {
  1515. break;
  1516. }
  1517. }
  1518. }
  1519. if (trimType != TrimType.Head)
  1520. {
  1521. for (end = Length - 1; end >= start; end--)
  1522. {
  1523. if (!char.IsWhiteSpace(this[end]))
  1524. {
  1525. break;
  1526. }
  1527. }
  1528. }
  1529. return CreateTrimmedString(start, end);
  1530. }
  1531. private unsafe string TrimHelper(char* trimChars, int trimCharsLength, TrimType trimType)
  1532. {
  1533. Debug.Assert(trimChars != null);
  1534. Debug.Assert(trimCharsLength > 0);
  1535. // end will point to the first non-trimmed character on the right.
  1536. // start will point to the first non-trimmed character on the left.
  1537. int end = Length - 1;
  1538. int start = 0;
  1539. // Trim specified characters.
  1540. if (trimType != TrimType.Tail)
  1541. {
  1542. for (start = 0; start < Length; start++)
  1543. {
  1544. int i = 0;
  1545. char ch = this[start];
  1546. for (i = 0; i < trimCharsLength; i++)
  1547. {
  1548. if (trimChars[i] == ch)
  1549. {
  1550. break;
  1551. }
  1552. }
  1553. if (i == trimCharsLength)
  1554. {
  1555. // The character is not in trimChars, so stop trimming.
  1556. break;
  1557. }
  1558. }
  1559. }
  1560. if (trimType != TrimType.Head)
  1561. {
  1562. for (end = Length - 1; end >= start; end--)
  1563. {
  1564. int i = 0;
  1565. char ch = this[end];
  1566. for (i = 0; i < trimCharsLength; i++)
  1567. {
  1568. if (trimChars[i] == ch)
  1569. {
  1570. break;
  1571. }
  1572. }
  1573. if (i == trimCharsLength)
  1574. {
  1575. // The character is not in trimChars, so stop trimming.
  1576. break;
  1577. }
  1578. }
  1579. }
  1580. return CreateTrimmedString(start, end);
  1581. }
  1582. private string CreateTrimmedString(int start, int end)
  1583. {
  1584. int len = end - start + 1;
  1585. return
  1586. len == Length ? this :
  1587. len == 0 ? string.Empty :
  1588. InternalSubString(start, len);
  1589. }
  1590. private enum TrimType
  1591. {
  1592. Head = 0,
  1593. Tail = 1,
  1594. Both = 2
  1595. }
  1596. }
  1597. }