MemoryExtensions.Trim.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. namespace System
  6. {
  7. public static partial class MemoryExtensions
  8. {
  9. /// <summary>
  10. /// Removes all leading and trailing occurrences of a specified element from the memory.
  11. /// </summary>
  12. /// <param name="memory">The source memory from which the element is removed.</param>
  13. /// <param name="trimElement">The specified element to look for and remove.</param>
  14. public static Memory<T> Trim<T>(this Memory<T> memory, T trimElement)
  15. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  16. where T : IEquatable<T>
  17. #nullable restore
  18. {
  19. ReadOnlySpan<T> span = memory.Span;
  20. int start = ClampStart(span, trimElement);
  21. int length = ClampEnd(span, start, trimElement);
  22. return memory.Slice(start, length);
  23. }
  24. /// <summary>
  25. /// Removes all leading occurrences of a specified element from the memory.
  26. /// </summary>
  27. /// <param name="memory">The source memory from which the element is removed.</param>
  28. /// <param name="trimElement">The specified element to look for and remove.</param>
  29. public static Memory<T> TrimStart<T>(this Memory<T> memory, T trimElement)
  30. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  31. where T : IEquatable<T>
  32. #nullable restore
  33. => memory.Slice(ClampStart(memory.Span, trimElement));
  34. /// <summary>
  35. /// Removes all trailing occurrences of a specified element from the memory.
  36. /// </summary>
  37. /// <param name="memory">The source memory from which the element is removed.</param>
  38. /// <param name="trimElement">The specified element to look for and remove.</param>
  39. public static Memory<T> TrimEnd<T>(this Memory<T> memory, T trimElement)
  40. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  41. where T : IEquatable<T>
  42. #nullable restore
  43. => memory.Slice(0, ClampEnd(memory.Span, 0, trimElement));
  44. /// <summary>
  45. /// Removes all leading and trailing occurrences of a specified element from the memory.
  46. /// </summary>
  47. /// <param name="memory">The source memory from which the element is removed.</param>
  48. /// <param name="trimElement">The specified element to look for and remove.</param>
  49. public static ReadOnlyMemory<T> Trim<T>(this ReadOnlyMemory<T> memory, T trimElement)
  50. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  51. where T : IEquatable<T>
  52. #nullable restore
  53. {
  54. ReadOnlySpan<T> span = memory.Span;
  55. int start = ClampStart(span, trimElement);
  56. int length = ClampEnd(span, start, trimElement);
  57. return memory.Slice(start, length);
  58. }
  59. /// <summary>
  60. /// Removes all leading occurrences of a specified element from the memory.
  61. /// </summary>
  62. /// <param name="memory">The source memory from which the element is removed.</param>
  63. /// <param name="trimElement">The specified element to look for and remove.</param>
  64. public static ReadOnlyMemory<T> TrimStart<T>(this ReadOnlyMemory<T> memory, T trimElement)
  65. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  66. where T : IEquatable<T>
  67. #nullable restore
  68. => memory.Slice(ClampStart(memory.Span, trimElement));
  69. /// <summary>
  70. /// Removes all trailing occurrences of a specified element from the memory.
  71. /// </summary>
  72. /// <param name="memory">The source memory from which the element is removed.</param>
  73. /// <param name="trimElement">The specified element to look for and remove.</param>
  74. public static ReadOnlyMemory<T> TrimEnd<T>(this ReadOnlyMemory<T> memory, T trimElement)
  75. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  76. where T : IEquatable<T>
  77. #nullable restore
  78. => memory.Slice(0, ClampEnd(memory.Span, 0, trimElement));
  79. /// <summary>
  80. /// Removes all leading and trailing occurrences of a specified element from the span.
  81. /// </summary>
  82. /// <param name="span">The source span from which the element is removed.</param>
  83. /// <param name="trimElement">The specified element to look for and remove.</param>
  84. public static Span<T> Trim<T>(this Span<T> span, T trimElement)
  85. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  86. where T : IEquatable<T>
  87. #nullable restore
  88. {
  89. int start = ClampStart(span, trimElement);
  90. int length = ClampEnd(span, start, trimElement);
  91. return span.Slice(start, length);
  92. }
  93. /// <summary>
  94. /// Removes all leading occurrences of a specified element from the span.
  95. /// </summary>
  96. /// <param name="span">The source span from which the element is removed.</param>
  97. /// <param name="trimElement">The specified element to look for and remove.</param>
  98. public static Span<T> TrimStart<T>(this Span<T> span, T trimElement)
  99. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  100. where T : IEquatable<T>
  101. #nullable restore
  102. => span.Slice(ClampStart(span, trimElement));
  103. /// <summary>
  104. /// Removes all trailing occurrences of a specified element from the span.
  105. /// </summary>
  106. /// <param name="span">The source span from which the element is removed.</param>
  107. /// <param name="trimElement">The specified element to look for and remove.</param>
  108. public static Span<T> TrimEnd<T>(this Span<T> span, T trimElement)
  109. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  110. where T : IEquatable<T>
  111. #nullable restore
  112. => span.Slice(0, ClampEnd(span, 0, trimElement));
  113. /// <summary>
  114. /// Removes all leading and trailing occurrences of a specified element from the span.
  115. /// </summary>
  116. /// <param name="span">The source span from which the element is removed.</param>
  117. /// <param name="trimElement">The specified element to look for and remove.</param>
  118. public static ReadOnlySpan<T> Trim<T>(this ReadOnlySpan<T> span, T trimElement)
  119. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  120. where T : IEquatable<T>
  121. #nullable restore
  122. {
  123. int start = ClampStart(span, trimElement);
  124. int length = ClampEnd(span, start, trimElement);
  125. return span.Slice(start, length);
  126. }
  127. /// <summary>
  128. /// Removes all leading occurrences of a specified element from the span.
  129. /// </summary>
  130. /// <param name="span">The source span from which the element is removed.</param>
  131. /// <param name="trimElement">The specified element to look for and remove.</param>
  132. public static ReadOnlySpan<T> TrimStart<T>(this ReadOnlySpan<T> span, T trimElement)
  133. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  134. where T : IEquatable<T>
  135. #nullable restore
  136. => span.Slice(ClampStart(span, trimElement));
  137. /// <summary>
  138. /// Removes all trailing occurrences of a specified element from the span.
  139. /// </summary>
  140. /// <param name="span">The source span from which the element is removed.</param>
  141. /// <param name="trimElement">The specified element to look for and remove.</param>
  142. public static ReadOnlySpan<T> TrimEnd<T>(this ReadOnlySpan<T> span, T trimElement)
  143. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  144. where T : IEquatable<T>
  145. #nullable restore
  146. => span.Slice(0, ClampEnd(span, 0, trimElement));
  147. /// <summary>
  148. /// Delimits all leading occurrences of a specified element from the span.
  149. /// </summary>
  150. /// <param name="span">The source span from which the element is removed.</param>
  151. /// <param name="trimElement">The specified element to look for and remove.</param>
  152. private static int ClampStart<T>(ReadOnlySpan<T> span, T trimElement)
  153. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  154. where T : IEquatable<T>
  155. #nullable restore
  156. {
  157. int start = 0;
  158. if (trimElement != null)
  159. {
  160. for (; start < span.Length; start++)
  161. {
  162. if (!trimElement.Equals(span[start]))
  163. {
  164. break;
  165. }
  166. }
  167. }
  168. else
  169. {
  170. for (; start < span.Length; start++)
  171. {
  172. if (span[start] != null)
  173. {
  174. break;
  175. }
  176. }
  177. }
  178. return start;
  179. }
  180. /// <summary>
  181. /// Delimits all trailing occurrences of a specified element from the span.
  182. /// </summary>
  183. /// <param name="span">The source span from which the element is removed.</param>
  184. /// <param name="start">The start index from which to being searching.</param>
  185. /// <param name="trimElement">The specified element to look for and remove.</param>
  186. private static int ClampEnd<T>(ReadOnlySpan<T> span, int start, T trimElement)
  187. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  188. where T : IEquatable<T>
  189. #nullable restore
  190. {
  191. // Initially, start==len==0. If ClampStart trims all, start==len
  192. Debug.Assert((uint)start <= span.Length);
  193. int end = span.Length - 1;
  194. if (trimElement != null)
  195. {
  196. for (; end >= start; end--)
  197. {
  198. if (!trimElement.Equals(span[end]))
  199. {
  200. break;
  201. }
  202. }
  203. }
  204. else
  205. {
  206. for (; end >= start; end--)
  207. {
  208. if (span[end] != null)
  209. {
  210. break;
  211. }
  212. }
  213. }
  214. return end - start + 1;
  215. }
  216. /// <summary>
  217. /// Removes all leading and trailing occurrences of a set of elements specified
  218. /// in a readonly span from the memory.
  219. /// </summary>
  220. /// <param name="memory">The source memory from which the elements are removed.</param>
  221. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  222. /// <remarks>If <paramref name="trimElements"/> is empty, the memory is returned unaltered.</remarks>
  223. public static Memory<T> Trim<T>(this Memory<T> memory, ReadOnlySpan<T> trimElements)
  224. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  225. where T : IEquatable<T>
  226. #nullable restore
  227. {
  228. if (trimElements.Length > 1)
  229. {
  230. ReadOnlySpan<T> span = memory.Span;
  231. int start = ClampStart(span, trimElements);
  232. int length = ClampEnd(span, start, trimElements);
  233. return memory.Slice(start, length);
  234. }
  235. if (trimElements.Length == 1)
  236. {
  237. return Trim(memory, trimElements[0]);
  238. }
  239. return memory;
  240. }
  241. /// <summary>
  242. /// Removes all leading occurrences of a set of elements specified
  243. /// in a readonly span from the memory.
  244. /// </summary>
  245. /// <param name="memory">The source memory from which the elements are removed.</param>
  246. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  247. /// <remarks>If <paramref name="trimElements"/> is empty, the memory is returned unaltered.</remarks>
  248. public static Memory<T> TrimStart<T>(this Memory<T> memory, ReadOnlySpan<T> trimElements)
  249. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  250. where T : IEquatable<T>
  251. #nullable restore
  252. {
  253. if (trimElements.Length > 1)
  254. {
  255. return memory.Slice(ClampStart(memory.Span, trimElements));
  256. }
  257. if (trimElements.Length == 1)
  258. {
  259. return TrimStart(memory, trimElements[0]);
  260. }
  261. return memory;
  262. }
  263. /// <summary>
  264. /// Removes all trailing occurrences of a set of elements specified
  265. /// in a readonly span from the memory.
  266. /// </summary>
  267. /// <param name="memory">The source memory from which the elements are removed.</param>
  268. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  269. /// <remarks>If <paramref name="trimElements"/> is empty, the memory is returned unaltered.</remarks>
  270. public static Memory<T> TrimEnd<T>(this Memory<T> memory, ReadOnlySpan<T> trimElements)
  271. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  272. where T : IEquatable<T>
  273. #nullable restore
  274. {
  275. if (trimElements.Length > 1)
  276. {
  277. return memory.Slice(0, ClampEnd(memory.Span, 0, trimElements));
  278. }
  279. if (trimElements.Length == 1)
  280. {
  281. return TrimEnd(memory, trimElements[0]);
  282. }
  283. return memory;
  284. }
  285. /// <summary>
  286. /// Removes all leading and trailing occurrences of a set of elements specified
  287. /// in a readonly span from the memory.
  288. /// </summary>
  289. /// <param name="memory">The source memory from which the elements are removed.</param>
  290. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  291. /// <remarks>If <paramref name="trimElements"/> is empty, the memory is returned unaltered.</remarks>
  292. public static ReadOnlyMemory<T> Trim<T>(this ReadOnlyMemory<T> memory, ReadOnlySpan<T> trimElements)
  293. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  294. where T : IEquatable<T>
  295. #nullable restore
  296. {
  297. if (trimElements.Length > 1)
  298. {
  299. ReadOnlySpan<T> span = memory.Span;
  300. int start = ClampStart(span, trimElements);
  301. int length = ClampEnd(span, start, trimElements);
  302. return memory.Slice(start, length);
  303. }
  304. if (trimElements.Length == 1)
  305. {
  306. return Trim(memory, trimElements[0]);
  307. }
  308. return memory;
  309. }
  310. /// <summary>
  311. /// Removes all leading occurrences of a set of elements specified
  312. /// in a readonly span from the memory.
  313. /// </summary>
  314. /// <param name="memory">The source memory from which the elements are removed.</param>
  315. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  316. /// <remarks>If <paramref name="trimElements"/> is empty, the memory is returned unaltered.</remarks>
  317. public static ReadOnlyMemory<T> TrimStart<T>(this ReadOnlyMemory<T> memory, ReadOnlySpan<T> trimElements)
  318. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  319. where T : IEquatable<T>
  320. #nullable restore
  321. {
  322. if (trimElements.Length > 1)
  323. {
  324. return memory.Slice(ClampStart(memory.Span, trimElements));
  325. }
  326. if (trimElements.Length == 1)
  327. {
  328. return TrimStart(memory, trimElements[0]);
  329. }
  330. return memory;
  331. }
  332. /// <summary>
  333. /// Removes all trailing occurrences of a set of elements specified
  334. /// in a readonly span from the memory.
  335. /// </summary>
  336. /// <param name="memory">The source memory from which the elements are removed.</param>
  337. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  338. /// <remarks>If <paramref name="trimElements"/> is empty, the memory is returned unaltered.</remarks>
  339. public static ReadOnlyMemory<T> TrimEnd<T>(this ReadOnlyMemory<T> memory, ReadOnlySpan<T> trimElements)
  340. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  341. where T : IEquatable<T>
  342. #nullable restore
  343. {
  344. if (trimElements.Length > 1)
  345. {
  346. return memory.Slice(0, ClampEnd(memory.Span, 0, trimElements));
  347. }
  348. if (trimElements.Length == 1)
  349. {
  350. return TrimEnd(memory, trimElements[0]);
  351. }
  352. return memory;
  353. }
  354. /// <summary>
  355. /// Removes all leading and trailing occurrences of a set of elements specified
  356. /// in a readonly span from the span.
  357. /// </summary>
  358. /// <param name="span">The source span from which the elements are removed.</param>
  359. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  360. /// <remarks>If <paramref name="trimElements"/> is empty, the span is returned unaltered.</remarks>
  361. public static Span<T> Trim<T>(this Span<T> span, ReadOnlySpan<T> trimElements)
  362. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  363. where T : IEquatable<T>
  364. #nullable restore
  365. {
  366. if (trimElements.Length > 1)
  367. {
  368. int start = ClampStart(span, trimElements);
  369. int length = ClampEnd(span, start, trimElements);
  370. return span.Slice(start, length);
  371. }
  372. if (trimElements.Length == 1)
  373. {
  374. return Trim(span, trimElements[0]);
  375. }
  376. return span;
  377. }
  378. /// <summary>
  379. /// Removes all leading occurrences of a set of elements specified
  380. /// in a readonly span from the span.
  381. /// </summary>
  382. /// <param name="span">The source span from which the elements are removed.</param>
  383. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  384. /// <remarks>If <paramref name="trimElements"/> is empty, the span is returned unaltered.</remarks>
  385. public static Span<T> TrimStart<T>(this Span<T> span, ReadOnlySpan<T> trimElements)
  386. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  387. where T : IEquatable<T>
  388. #nullable restore
  389. {
  390. if (trimElements.Length > 1)
  391. {
  392. return span.Slice(ClampStart(span, trimElements));
  393. }
  394. if (trimElements.Length == 1)
  395. {
  396. return TrimStart(span, trimElements[0]);
  397. }
  398. return span;
  399. }
  400. /// <summary>
  401. /// Removes all trailing occurrences of a set of elements specified
  402. /// in a readonly span from the span.
  403. /// </summary>
  404. /// <param name="span">The source span from which the elements are removed.</param>
  405. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  406. /// <remarks>If <paramref name="trimElements"/> is empty, the span is returned unaltered.</remarks>
  407. public static Span<T> TrimEnd<T>(this Span<T> span, ReadOnlySpan<T> trimElements)
  408. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  409. where T : IEquatable<T>
  410. #nullable restore
  411. {
  412. if (trimElements.Length > 1)
  413. {
  414. return span.Slice(0, ClampEnd(span, 0, trimElements));
  415. }
  416. if (trimElements.Length == 1)
  417. {
  418. return TrimEnd(span, trimElements[0]);
  419. }
  420. return span;
  421. }
  422. /// <summary>
  423. /// Removes all leading and trailing occurrences of a set of elements specified
  424. /// in a readonly span from the span.
  425. /// </summary>
  426. /// <param name="span">The source span from which the elements are removed.</param>
  427. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  428. /// <remarks>If <paramref name="trimElements"/> is empty, the span is returned unaltered.</remarks>
  429. public static ReadOnlySpan<T> Trim<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> trimElements)
  430. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  431. where T : IEquatable<T>
  432. #nullable restore
  433. {
  434. if (trimElements.Length > 1)
  435. {
  436. int start = ClampStart(span, trimElements);
  437. int length = ClampEnd(span, start, trimElements);
  438. return span.Slice(start, length);
  439. }
  440. if (trimElements.Length == 1)
  441. {
  442. return Trim(span, trimElements[0]);
  443. }
  444. return span;
  445. }
  446. /// <summary>
  447. /// Removes all leading occurrences of a set of elements specified
  448. /// in a readonly span from the span.
  449. /// </summary>
  450. /// <param name="span">The source span from which the elements are removed.</param>
  451. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  452. /// <remarks>If <paramref name="trimElements"/> is empty, the span is returned unaltered.</remarks>
  453. public static ReadOnlySpan<T> TrimStart<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> trimElements)
  454. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  455. where T : IEquatable<T>
  456. #nullable restore
  457. {
  458. if (trimElements.Length > 1)
  459. {
  460. return span.Slice(ClampStart(span, trimElements));
  461. }
  462. if (trimElements.Length == 1)
  463. {
  464. return TrimStart(span, trimElements[0]);
  465. }
  466. return span;
  467. }
  468. /// <summary>
  469. /// Removes all trailing occurrences of a set of elements specified
  470. /// in a readonly span from the span.
  471. /// </summary>
  472. /// <param name="span">The source span from which the elements are removed.</param>
  473. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  474. /// <remarks>If <paramref name="trimElements"/> is empty, the span is returned unaltered.</remarks>
  475. public static ReadOnlySpan<T> TrimEnd<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> trimElements)
  476. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  477. where T : IEquatable<T>
  478. #nullable restore
  479. {
  480. if (trimElements.Length > 1)
  481. {
  482. return span.Slice(0, ClampEnd(span, 0, trimElements));
  483. }
  484. if (trimElements.Length == 1)
  485. {
  486. return TrimEnd(span, trimElements[0]);
  487. }
  488. return span;
  489. }
  490. /// <summary>
  491. /// Delimits all leading occurrences of a set of elements specified
  492. /// in a readonly span from the span.
  493. /// </summary>
  494. /// <param name="span">The source span from which the elements are removed.</param>
  495. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  496. private static int ClampStart<T>(ReadOnlySpan<T> span, ReadOnlySpan<T> trimElements)
  497. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  498. where T : IEquatable<T>
  499. #nullable restore
  500. {
  501. int start = 0;
  502. for (; start < span.Length; start++)
  503. {
  504. if (!trimElements.Contains(span[start]))
  505. {
  506. break;
  507. }
  508. }
  509. return start;
  510. }
  511. /// <summary>
  512. /// Delimits all trailing occurrences of a set of elements specified
  513. /// in a readonly span from the span.
  514. /// </summary>
  515. /// <param name="span">The source span from which the elements are removed.</param>
  516. /// <param name="start">The start index from which to being searching.</param>
  517. /// <param name="trimElements">The span which contains the set of elements to remove.</param>
  518. private static int ClampEnd<T>(ReadOnlySpan<T> span, int start, ReadOnlySpan<T> trimElements)
  519. #nullable disable // to enable use with both T and T? for reference types due to IEquatable<T> being invariant
  520. where T : IEquatable<T>
  521. #nullable restore
  522. {
  523. // Initially, start==len==0. If ClampStart trims all, start==len
  524. Debug.Assert((uint)start <= span.Length);
  525. int end = span.Length - 1;
  526. for (; end >= start; end--)
  527. {
  528. if (!trimElements.Contains(span[end]))
  529. {
  530. break;
  531. }
  532. }
  533. return end - start + 1;
  534. }
  535. /// <summary>
  536. /// Removes all leading and trailing white-space characters from the memory.
  537. /// </summary>
  538. /// <param name="memory">The source memory from which the characters are removed.</param>
  539. public static Memory<char> Trim(this Memory<char> memory)
  540. {
  541. ReadOnlySpan<char> span = memory.Span;
  542. int start = ClampStart(span);
  543. int length = ClampEnd(span, start);
  544. return memory.Slice(start, length);
  545. }
  546. /// <summary>
  547. /// Removes all leading white-space characters from the memory.
  548. /// </summary>
  549. /// <param name="memory">The source memory from which the characters are removed.</param>
  550. public static Memory<char> TrimStart(this Memory<char> memory)
  551. => memory.Slice(ClampStart(memory.Span));
  552. /// <summary>
  553. /// Removes all trailing white-space characters from the memory.
  554. /// </summary>
  555. /// <param name="memory">The source memory from which the characters are removed.</param>
  556. public static Memory<char> TrimEnd(this Memory<char> memory)
  557. => memory.Slice(0, ClampEnd(memory.Span, 0));
  558. /// <summary>
  559. /// Removes all leading and trailing white-space characters from the memory.
  560. /// </summary>
  561. /// <param name="memory">The source memory from which the characters are removed.</param>
  562. public static ReadOnlyMemory<char> Trim(this ReadOnlyMemory<char> memory)
  563. {
  564. ReadOnlySpan<char> span = memory.Span;
  565. int start = ClampStart(span);
  566. int length = ClampEnd(span, start);
  567. return memory.Slice(start, length);
  568. }
  569. /// <summary>
  570. /// Removes all leading white-space characters from the memory.
  571. /// </summary>
  572. /// <param name="memory">The source memory from which the characters are removed.</param>
  573. public static ReadOnlyMemory<char> TrimStart(this ReadOnlyMemory<char> memory)
  574. => memory.Slice(ClampStart(memory.Span));
  575. /// <summary>
  576. /// Removes all trailing white-space characters from the memory.
  577. /// </summary>
  578. /// <param name="memory">The source memory from which the characters are removed.</param>
  579. public static ReadOnlyMemory<char> TrimEnd(this ReadOnlyMemory<char> memory)
  580. => memory.Slice(0, ClampEnd(memory.Span, 0));
  581. /// <summary>
  582. /// Removes all leading and trailing white-space characters from the span.
  583. /// </summary>
  584. /// <param name="span">The source span from which the characters are removed.</param>
  585. public static ReadOnlySpan<char> Trim(this ReadOnlySpan<char> span)
  586. {
  587. int start = 0;
  588. for (; start < span.Length; start++)
  589. {
  590. if (!char.IsWhiteSpace(span[start]))
  591. {
  592. break;
  593. }
  594. }
  595. int end = span.Length - 1;
  596. for (; end > start; end--)
  597. {
  598. if (!char.IsWhiteSpace(span[end]))
  599. {
  600. break;
  601. }
  602. }
  603. return span.Slice(start, end - start + 1);
  604. }
  605. /// <summary>
  606. /// Removes all leading white-space characters from the span.
  607. /// </summary>
  608. /// <param name="span">The source span from which the characters are removed.</param>
  609. public static ReadOnlySpan<char> TrimStart(this ReadOnlySpan<char> span)
  610. {
  611. int start = 0;
  612. for (; start < span.Length; start++)
  613. {
  614. if (!char.IsWhiteSpace(span[start]))
  615. {
  616. break;
  617. }
  618. }
  619. return span.Slice(start);
  620. }
  621. /// <summary>
  622. /// Removes all trailing white-space characters from the span.
  623. /// </summary>
  624. /// <param name="span">The source span from which the characters are removed.</param>
  625. public static ReadOnlySpan<char> TrimEnd(this ReadOnlySpan<char> span)
  626. {
  627. int end = span.Length - 1;
  628. for (; end >= 0; end--)
  629. {
  630. if (!char.IsWhiteSpace(span[end]))
  631. {
  632. break;
  633. }
  634. }
  635. return span.Slice(0, end + 1);
  636. }
  637. /// <summary>
  638. /// Removes all leading and trailing occurrences of a specified character from the span.
  639. /// </summary>
  640. /// <param name="span">The source span from which the character is removed.</param>
  641. /// <param name="trimChar">The specified character to look for and remove.</param>
  642. public static ReadOnlySpan<char> Trim(this ReadOnlySpan<char> span, char trimChar)
  643. {
  644. int start = 0;
  645. for (; start < span.Length; start++)
  646. {
  647. if (span[start] != trimChar)
  648. {
  649. break;
  650. }
  651. }
  652. int end = span.Length - 1;
  653. for (; end > start; end--)
  654. {
  655. if (span[end] != trimChar)
  656. {
  657. break;
  658. }
  659. }
  660. return span.Slice(start, end - start + 1);
  661. }
  662. /// <summary>
  663. /// Removes all leading occurrences of a specified character from the span.
  664. /// </summary>
  665. /// <param name="span">The source span from which the character is removed.</param>
  666. /// <param name="trimChar">The specified character to look for and remove.</param>
  667. public static ReadOnlySpan<char> TrimStart(this ReadOnlySpan<char> span, char trimChar)
  668. {
  669. int start = 0;
  670. for (; start < span.Length; start++)
  671. {
  672. if (span[start] != trimChar)
  673. {
  674. break;
  675. }
  676. }
  677. return span.Slice(start);
  678. }
  679. /// <summary>
  680. /// Removes all trailing occurrences of a specified character from the span.
  681. /// </summary>
  682. /// <param name="span">The source span from which the character is removed.</param>
  683. /// <param name="trimChar">The specified character to look for and remove.</param>
  684. public static ReadOnlySpan<char> TrimEnd(this ReadOnlySpan<char> span, char trimChar)
  685. {
  686. int end = span.Length - 1;
  687. for (; end >= 0; end--)
  688. {
  689. if (span[end] != trimChar)
  690. {
  691. break;
  692. }
  693. }
  694. return span.Slice(0, end + 1);
  695. }
  696. /// <summary>
  697. /// Removes all leading and trailing occurrences of a set of characters specified
  698. /// in a readonly span from the span.
  699. /// </summary>
  700. /// <param name="span">The source span from which the characters are removed.</param>
  701. /// <param name="trimChars">The span which contains the set of characters to remove.</param>
  702. /// <remarks>If <paramref name="trimChars"/> is empty, white-space characters are removed instead.</remarks>
  703. public static ReadOnlySpan<char> Trim(this ReadOnlySpan<char> span, ReadOnlySpan<char> trimChars)
  704. => span.TrimStart(trimChars).TrimEnd(trimChars);
  705. /// <summary>
  706. /// Removes all leading occurrences of a set of characters specified
  707. /// in a readonly span from the span.
  708. /// </summary>
  709. /// <param name="span">The source span from which the characters are removed.</param>
  710. /// <param name="trimChars">The span which contains the set of characters to remove.</param>
  711. /// <remarks>If <paramref name="trimChars"/> is empty, white-space characters are removed instead.</remarks>
  712. public static ReadOnlySpan<char> TrimStart(this ReadOnlySpan<char> span, ReadOnlySpan<char> trimChars)
  713. {
  714. if (trimChars.IsEmpty)
  715. {
  716. return span.TrimStart();
  717. }
  718. int start = 0;
  719. for (; start < span.Length; start++)
  720. {
  721. for (int i = 0; i < trimChars.Length; i++)
  722. {
  723. if (span[start] == trimChars[i])
  724. {
  725. goto Next;
  726. }
  727. }
  728. break;
  729. Next:
  730. ;
  731. }
  732. return span.Slice(start);
  733. }
  734. /// <summary>
  735. /// Removes all trailing occurrences of a set of characters specified
  736. /// in a readonly span from the span.
  737. /// </summary>
  738. /// <param name="span">The source span from which the characters are removed.</param>
  739. /// <param name="trimChars">The span which contains the set of characters to remove.</param>
  740. /// <remarks>If <paramref name="trimChars"/> is empty, white-space characters are removed instead.</remarks>
  741. public static ReadOnlySpan<char> TrimEnd(this ReadOnlySpan<char> span, ReadOnlySpan<char> trimChars)
  742. {
  743. if (trimChars.IsEmpty)
  744. {
  745. return span.TrimEnd();
  746. }
  747. int end = span.Length - 1;
  748. for (; end >= 0; end--)
  749. {
  750. for (int i = 0; i < trimChars.Length; i++)
  751. {
  752. if (span[end] == trimChars[i])
  753. {
  754. goto Next;
  755. }
  756. }
  757. break;
  758. Next:
  759. ;
  760. }
  761. return span.Slice(0, end + 1);
  762. }
  763. /// <summary>
  764. /// Removes all leading and trailing white-space characters from the span.
  765. /// </summary>
  766. /// <param name="span">The source span from which the characters are removed.</param>
  767. public static Span<char> Trim(this Span<char> span)
  768. {
  769. int start = ClampStart(span);
  770. int length = ClampEnd(span, start);
  771. return span.Slice(start, length);
  772. }
  773. /// <summary>
  774. /// Removes all leading white-space characters from the span.
  775. /// </summary>
  776. /// <param name="span">The source span from which the characters are removed.</param>
  777. public static Span<char> TrimStart(this Span<char> span)
  778. => span.Slice(ClampStart(span));
  779. /// <summary>
  780. /// Removes all trailing white-space characters from the span.
  781. /// </summary>
  782. /// <param name="span">The source span from which the characters are removed.</param>
  783. public static Span<char> TrimEnd(this Span<char> span)
  784. => span.Slice(0, ClampEnd(span, 0));
  785. /// <summary>
  786. /// Delimits all leading occurrences of whitespace charecters from the span.
  787. /// </summary>
  788. /// <param name="span">The source span from which the characters are removed.</param>
  789. private static int ClampStart(ReadOnlySpan<char> span)
  790. {
  791. int start = 0;
  792. for (; start < span.Length; start++)
  793. {
  794. if (!char.IsWhiteSpace(span[start]))
  795. {
  796. break;
  797. }
  798. }
  799. return start;
  800. }
  801. /// <summary>
  802. /// Delimits all trailing occurrences of whitespace charecters from the span.
  803. /// </summary>
  804. /// <param name="span">The source span from which the characters are removed.</param>
  805. /// <param name="start">The start index from which to being searching.</param>
  806. private static int ClampEnd(ReadOnlySpan<char> span, int start)
  807. {
  808. // Initially, start==len==0. If ClampStart trims all, start==len
  809. Debug.Assert((uint)start <= span.Length);
  810. int end = span.Length - 1;
  811. for (; end >= start; end--)
  812. {
  813. if (!char.IsWhiteSpace(span[end]))
  814. {
  815. break;
  816. }
  817. }
  818. return end - start + 1;
  819. }
  820. }
  821. }