2
0

SpanHelpers.T.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices; // Do not remove. This is necessary for netstandard, since this file is mirrored into corefx
  6. #if !netstandard
  7. using Internal.Runtime.CompilerServices;
  8. #endif
  9. namespace System
  10. {
  11. internal static partial class SpanHelpers // .T
  12. {
  13. public static int IndexOf<T>(ref T searchSpace, int searchSpaceLength, ref T value, int valueLength)
  14. where T : IEquatable<T>
  15. {
  16. Debug.Assert(searchSpaceLength >= 0);
  17. Debug.Assert(valueLength >= 0);
  18. if (valueLength == 0)
  19. return 0; // A zero-length sequence is always treated as "found" at the start of the search space.
  20. T valueHead = value;
  21. ref T valueTail = ref Unsafe.Add(ref value, 1);
  22. int valueTailLength = valueLength - 1;
  23. int index = 0;
  24. for (; ; )
  25. {
  26. Debug.Assert(0 <= index && index <= searchSpaceLength); // Ensures no deceptive underflows in the computation of "remainingSearchSpaceLength".
  27. int remainingSearchSpaceLength = searchSpaceLength - index - valueTailLength;
  28. if (remainingSearchSpaceLength <= 0)
  29. break; // The unsearched portion is now shorter than the sequence we're looking for. So it can't be there.
  30. // Do a quick search for the first element of "value".
  31. int relativeIndex = IndexOf(ref Unsafe.Add(ref searchSpace, index), valueHead, remainingSearchSpaceLength);
  32. if (relativeIndex == -1)
  33. break;
  34. index += relativeIndex;
  35. // Found the first element of "value". See if the tail matches.
  36. if (SequenceEqual(ref Unsafe.Add(ref searchSpace, index + 1), ref valueTail, valueTailLength))
  37. return index; // The tail matched. Return a successful find.
  38. index++;
  39. }
  40. return -1;
  41. }
  42. // Adapted from IndexOf(...)
  43. public unsafe static bool Contains<T>(ref T searchSpace, T value, int length)
  44. where T : IEquatable<T>
  45. {
  46. Debug.Assert(length >= 0);
  47. IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
  48. if (default(T) != null || (object)value != null)
  49. {
  50. while (length >= 8)
  51. {
  52. length -= 8;
  53. if (value.Equals(Unsafe.Add(ref searchSpace, index + 0)) ||
  54. value.Equals(Unsafe.Add(ref searchSpace, index + 1)) ||
  55. value.Equals(Unsafe.Add(ref searchSpace, index + 2)) ||
  56. value.Equals(Unsafe.Add(ref searchSpace, index + 3)) ||
  57. value.Equals(Unsafe.Add(ref searchSpace, index + 4)) ||
  58. value.Equals(Unsafe.Add(ref searchSpace, index + 5)) ||
  59. value.Equals(Unsafe.Add(ref searchSpace, index + 6)) ||
  60. value.Equals(Unsafe.Add(ref searchSpace, index + 7)))
  61. {
  62. goto Found;
  63. }
  64. index += 8;
  65. }
  66. if (length >= 4)
  67. {
  68. length -= 4;
  69. if (value.Equals(Unsafe.Add(ref searchSpace, index + 0)) ||
  70. value.Equals(Unsafe.Add(ref searchSpace, index + 1)) ||
  71. value.Equals(Unsafe.Add(ref searchSpace, index + 2)) ||
  72. value.Equals(Unsafe.Add(ref searchSpace, index + 3)))
  73. {
  74. goto Found;
  75. }
  76. index += 4;
  77. }
  78. while (length > 0)
  79. {
  80. length -= 1;
  81. if (value.Equals(Unsafe.Add(ref searchSpace, index)))
  82. goto Found;
  83. index += 1;
  84. }
  85. }
  86. else
  87. {
  88. byte* len = (byte*)length;
  89. for (index = (IntPtr)0; index.ToPointer() < len; index += 1)
  90. {
  91. if ((object)Unsafe.Add(ref searchSpace, index) is null)
  92. {
  93. goto Found;
  94. }
  95. }
  96. }
  97. return false;
  98. Found:
  99. return true;
  100. }
  101. public static unsafe int IndexOf<T>(ref T searchSpace, T value, int length)
  102. where T : IEquatable<T>
  103. {
  104. Debug.Assert(length >= 0);
  105. IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
  106. if (default(T) != null || (object)value != null)
  107. {
  108. while (length >= 8)
  109. {
  110. length -= 8;
  111. if (value.Equals(Unsafe.Add(ref searchSpace, index)))
  112. goto Found;
  113. if (value.Equals(Unsafe.Add(ref searchSpace, index + 1)))
  114. goto Found1;
  115. if (value.Equals(Unsafe.Add(ref searchSpace, index + 2)))
  116. goto Found2;
  117. if (value.Equals(Unsafe.Add(ref searchSpace, index + 3)))
  118. goto Found3;
  119. if (value.Equals(Unsafe.Add(ref searchSpace, index + 4)))
  120. goto Found4;
  121. if (value.Equals(Unsafe.Add(ref searchSpace, index + 5)))
  122. goto Found5;
  123. if (value.Equals(Unsafe.Add(ref searchSpace, index + 6)))
  124. goto Found6;
  125. if (value.Equals(Unsafe.Add(ref searchSpace, index + 7)))
  126. goto Found7;
  127. index += 8;
  128. }
  129. if (length >= 4)
  130. {
  131. length -= 4;
  132. if (value.Equals(Unsafe.Add(ref searchSpace, index)))
  133. goto Found;
  134. if (value.Equals(Unsafe.Add(ref searchSpace, index + 1)))
  135. goto Found1;
  136. if (value.Equals(Unsafe.Add(ref searchSpace, index + 2)))
  137. goto Found2;
  138. if (value.Equals(Unsafe.Add(ref searchSpace, index + 3)))
  139. goto Found3;
  140. index += 4;
  141. }
  142. while (length > 0)
  143. {
  144. if (value.Equals(Unsafe.Add(ref searchSpace, index)))
  145. goto Found;
  146. index += 1;
  147. length--;
  148. }
  149. }
  150. else
  151. {
  152. byte* len = (byte*)length;
  153. for (index = (IntPtr)0; index.ToPointer() < len; index += 1)
  154. {
  155. if ((object)Unsafe.Add(ref searchSpace, index) is null)
  156. {
  157. goto Found;
  158. }
  159. }
  160. }
  161. return -1;
  162. Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
  163. return (int)(byte*)index;
  164. Found1:
  165. return (int)(byte*)(index + 1);
  166. Found2:
  167. return (int)(byte*)(index + 2);
  168. Found3:
  169. return (int)(byte*)(index + 3);
  170. Found4:
  171. return (int)(byte*)(index + 4);
  172. Found5:
  173. return (int)(byte*)(index + 5);
  174. Found6:
  175. return (int)(byte*)(index + 6);
  176. Found7:
  177. return (int)(byte*)(index + 7);
  178. }
  179. public static int IndexOfAny<T>(ref T searchSpace, T value0, T value1, int length)
  180. where T : IEquatable<T>
  181. {
  182. Debug.Assert(length >= 0);
  183. T lookUp;
  184. int index = 0;
  185. if (default(T) != null || ((object)value0 != null && (object)value1 != null))
  186. {
  187. while ((length - index) >= 8)
  188. {
  189. lookUp = Unsafe.Add(ref searchSpace, index);
  190. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  191. goto Found;
  192. lookUp = Unsafe.Add(ref searchSpace, index + 1);
  193. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  194. goto Found1;
  195. lookUp = Unsafe.Add(ref searchSpace, index + 2);
  196. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  197. goto Found2;
  198. lookUp = Unsafe.Add(ref searchSpace, index + 3);
  199. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  200. goto Found3;
  201. lookUp = Unsafe.Add(ref searchSpace, index + 4);
  202. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  203. goto Found4;
  204. lookUp = Unsafe.Add(ref searchSpace, index + 5);
  205. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  206. goto Found5;
  207. lookUp = Unsafe.Add(ref searchSpace, index + 6);
  208. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  209. goto Found6;
  210. lookUp = Unsafe.Add(ref searchSpace, index + 7);
  211. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  212. goto Found7;
  213. index += 8;
  214. }
  215. if ((length - index) >= 4)
  216. {
  217. lookUp = Unsafe.Add(ref searchSpace, index);
  218. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  219. goto Found;
  220. lookUp = Unsafe.Add(ref searchSpace, index + 1);
  221. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  222. goto Found1;
  223. lookUp = Unsafe.Add(ref searchSpace, index + 2);
  224. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  225. goto Found2;
  226. lookUp = Unsafe.Add(ref searchSpace, index + 3);
  227. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  228. goto Found3;
  229. index += 4;
  230. }
  231. while (index < length)
  232. {
  233. lookUp = Unsafe.Add(ref searchSpace, index);
  234. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  235. goto Found;
  236. index++;
  237. }
  238. }
  239. else
  240. {
  241. for (index = 0; index < length; index++)
  242. {
  243. lookUp = Unsafe.Add(ref searchSpace, index);
  244. if ((object)lookUp is null)
  245. {
  246. if ((object)value0 is null || (object)value1 is null)
  247. {
  248. goto Found;
  249. }
  250. }
  251. else if (lookUp.Equals(value0) || lookUp.Equals(value1))
  252. {
  253. goto Found;
  254. }
  255. }
  256. }
  257. return -1;
  258. Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
  259. return index;
  260. Found1:
  261. return index + 1;
  262. Found2:
  263. return index + 2;
  264. Found3:
  265. return index + 3;
  266. Found4:
  267. return index + 4;
  268. Found5:
  269. return index + 5;
  270. Found6:
  271. return index + 6;
  272. Found7:
  273. return index + 7;
  274. }
  275. public static int IndexOfAny<T>(ref T searchSpace, T value0, T value1, T value2, int length)
  276. where T : IEquatable<T>
  277. {
  278. Debug.Assert(length >= 0);
  279. T lookUp;
  280. int index = 0;
  281. if (default(T) != null || ((object)value0 != null && (object)value1 != null && (object)value2 != null))
  282. {
  283. while ((length - index) >= 8)
  284. {
  285. lookUp = Unsafe.Add(ref searchSpace, index);
  286. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  287. goto Found;
  288. lookUp = Unsafe.Add(ref searchSpace, index + 1);
  289. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  290. goto Found1;
  291. lookUp = Unsafe.Add(ref searchSpace, index + 2);
  292. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  293. goto Found2;
  294. lookUp = Unsafe.Add(ref searchSpace, index + 3);
  295. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  296. goto Found3;
  297. lookUp = Unsafe.Add(ref searchSpace, index + 4);
  298. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  299. goto Found4;
  300. lookUp = Unsafe.Add(ref searchSpace, index + 5);
  301. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  302. goto Found5;
  303. lookUp = Unsafe.Add(ref searchSpace, index + 6);
  304. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  305. goto Found6;
  306. lookUp = Unsafe.Add(ref searchSpace, index + 7);
  307. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  308. goto Found7;
  309. index += 8;
  310. }
  311. if ((length - index) >= 4)
  312. {
  313. lookUp = Unsafe.Add(ref searchSpace, index);
  314. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  315. goto Found;
  316. lookUp = Unsafe.Add(ref searchSpace, index + 1);
  317. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  318. goto Found1;
  319. lookUp = Unsafe.Add(ref searchSpace, index + 2);
  320. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  321. goto Found2;
  322. lookUp = Unsafe.Add(ref searchSpace, index + 3);
  323. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  324. goto Found3;
  325. index += 4;
  326. }
  327. while (index < length)
  328. {
  329. lookUp = Unsafe.Add(ref searchSpace, index);
  330. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  331. goto Found;
  332. index++;
  333. }
  334. }
  335. else
  336. {
  337. for (index = 0; index < length; index++)
  338. {
  339. lookUp = Unsafe.Add(ref searchSpace, index);
  340. if ((object)lookUp is null)
  341. {
  342. if ((object)value0 is null || (object)value1 is null || (object)value2 is null)
  343. {
  344. goto Found;
  345. }
  346. }
  347. else if (lookUp.Equals(value0) || lookUp.Equals(value1) || lookUp.Equals(value2))
  348. {
  349. goto Found;
  350. }
  351. }
  352. }
  353. return -1;
  354. Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
  355. return index;
  356. Found1:
  357. return index + 1;
  358. Found2:
  359. return index + 2;
  360. Found3:
  361. return index + 3;
  362. Found4:
  363. return index + 4;
  364. Found5:
  365. return index + 5;
  366. Found6:
  367. return index + 6;
  368. Found7:
  369. return index + 7;
  370. }
  371. public static int IndexOfAny<T>(ref T searchSpace, int searchSpaceLength, ref T value, int valueLength)
  372. where T : IEquatable<T>
  373. {
  374. Debug.Assert(searchSpaceLength >= 0);
  375. Debug.Assert(valueLength >= 0);
  376. if (valueLength == 0)
  377. return 0; // A zero-length sequence is always treated as "found" at the start of the search space.
  378. int index = -1;
  379. for (int i = 0; i < valueLength; i++)
  380. {
  381. var tempIndex = IndexOf(ref searchSpace, Unsafe.Add(ref value, i), searchSpaceLength);
  382. if ((uint)tempIndex < (uint)index)
  383. {
  384. index = tempIndex;
  385. // Reduce space for search, cause we don't care if we find the search value after the index of a previously found value
  386. searchSpaceLength = tempIndex;
  387. if (index == 0)
  388. break;
  389. }
  390. }
  391. return index;
  392. }
  393. public static int LastIndexOf<T>(ref T searchSpace, int searchSpaceLength, ref T value, int valueLength)
  394. where T : IEquatable<T>
  395. {
  396. Debug.Assert(searchSpaceLength >= 0);
  397. Debug.Assert(valueLength >= 0);
  398. if (valueLength == 0)
  399. return 0; // A zero-length sequence is always treated as "found" at the start of the search space.
  400. T valueHead = value;
  401. ref T valueTail = ref Unsafe.Add(ref value, 1);
  402. int valueTailLength = valueLength - 1;
  403. int index = 0;
  404. for (; ; )
  405. {
  406. Debug.Assert(0 <= index && index <= searchSpaceLength); // Ensures no deceptive underflows in the computation of "remainingSearchSpaceLength".
  407. int remainingSearchSpaceLength = searchSpaceLength - index - valueTailLength;
  408. if (remainingSearchSpaceLength <= 0)
  409. break; // The unsearched portion is now shorter than the sequence we're looking for. So it can't be there.
  410. // Do a quick search for the first element of "value".
  411. int relativeIndex = LastIndexOf(ref searchSpace, valueHead, remainingSearchSpaceLength);
  412. if (relativeIndex == -1)
  413. break;
  414. // Found the first element of "value". See if the tail matches.
  415. if (SequenceEqual(ref Unsafe.Add(ref searchSpace, relativeIndex + 1), ref valueTail, valueTailLength))
  416. return relativeIndex; // The tail matched. Return a successful find.
  417. index += remainingSearchSpaceLength - relativeIndex;
  418. }
  419. return -1;
  420. }
  421. public static int LastIndexOf<T>(ref T searchSpace, T value, int length)
  422. where T : IEquatable<T>
  423. {
  424. Debug.Assert(length >= 0);
  425. if (default(T) != null || (object)value != null)
  426. {
  427. while (length >= 8)
  428. {
  429. length -= 8;
  430. if (value.Equals(Unsafe.Add(ref searchSpace, length + 7)))
  431. goto Found7;
  432. if (value.Equals(Unsafe.Add(ref searchSpace, length + 6)))
  433. goto Found6;
  434. if (value.Equals(Unsafe.Add(ref searchSpace, length + 5)))
  435. goto Found5;
  436. if (value.Equals(Unsafe.Add(ref searchSpace, length + 4)))
  437. goto Found4;
  438. if (value.Equals(Unsafe.Add(ref searchSpace, length + 3)))
  439. goto Found3;
  440. if (value.Equals(Unsafe.Add(ref searchSpace, length + 2)))
  441. goto Found2;
  442. if (value.Equals(Unsafe.Add(ref searchSpace, length + 1)))
  443. goto Found1;
  444. if (value.Equals(Unsafe.Add(ref searchSpace, length)))
  445. goto Found;
  446. }
  447. if (length >= 4)
  448. {
  449. length -= 4;
  450. if (value.Equals(Unsafe.Add(ref searchSpace, length + 3)))
  451. goto Found3;
  452. if (value.Equals(Unsafe.Add(ref searchSpace, length + 2)))
  453. goto Found2;
  454. if (value.Equals(Unsafe.Add(ref searchSpace, length + 1)))
  455. goto Found1;
  456. if (value.Equals(Unsafe.Add(ref searchSpace, length)))
  457. goto Found;
  458. }
  459. while (length > 0)
  460. {
  461. length--;
  462. if (value.Equals(Unsafe.Add(ref searchSpace, length)))
  463. goto Found;
  464. }
  465. }
  466. else
  467. {
  468. for (length--; length >= 0; length--)
  469. {
  470. if ((object)Unsafe.Add(ref searchSpace, length) is null)
  471. {
  472. goto Found;
  473. }
  474. }
  475. }
  476. return -1;
  477. Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
  478. return length;
  479. Found1:
  480. return length + 1;
  481. Found2:
  482. return length + 2;
  483. Found3:
  484. return length + 3;
  485. Found4:
  486. return length + 4;
  487. Found5:
  488. return length + 5;
  489. Found6:
  490. return length + 6;
  491. Found7:
  492. return length + 7;
  493. }
  494. public static int LastIndexOfAny<T>(ref T searchSpace, T value0, T value1, int length)
  495. where T : IEquatable<T>
  496. {
  497. Debug.Assert(length >= 0);
  498. T lookUp;
  499. if (default(T) != null || ((object)value0 != null && (object)value1 != null))
  500. {
  501. while (length >= 8)
  502. {
  503. length -= 8;
  504. lookUp = Unsafe.Add(ref searchSpace, length + 7);
  505. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  506. goto Found7;
  507. lookUp = Unsafe.Add(ref searchSpace, length + 6);
  508. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  509. goto Found6;
  510. lookUp = Unsafe.Add(ref searchSpace, length + 5);
  511. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  512. goto Found5;
  513. lookUp = Unsafe.Add(ref searchSpace, length + 4);
  514. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  515. goto Found4;
  516. lookUp = Unsafe.Add(ref searchSpace, length + 3);
  517. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  518. goto Found3;
  519. lookUp = Unsafe.Add(ref searchSpace, length + 2);
  520. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  521. goto Found2;
  522. lookUp = Unsafe.Add(ref searchSpace, length + 1);
  523. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  524. goto Found1;
  525. lookUp = Unsafe.Add(ref searchSpace, length);
  526. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  527. goto Found;
  528. }
  529. if (length >= 4)
  530. {
  531. length -= 4;
  532. lookUp = Unsafe.Add(ref searchSpace, length + 3);
  533. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  534. goto Found3;
  535. lookUp = Unsafe.Add(ref searchSpace, length + 2);
  536. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  537. goto Found2;
  538. lookUp = Unsafe.Add(ref searchSpace, length + 1);
  539. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  540. goto Found1;
  541. lookUp = Unsafe.Add(ref searchSpace, length);
  542. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  543. goto Found;
  544. }
  545. while (length > 0)
  546. {
  547. length--;
  548. lookUp = Unsafe.Add(ref searchSpace, length);
  549. if (value0.Equals(lookUp) || value1.Equals(lookUp))
  550. goto Found;
  551. }
  552. }
  553. else
  554. {
  555. for (length--; length >= 0; length--)
  556. {
  557. lookUp = Unsafe.Add(ref searchSpace, length);
  558. if ((object)lookUp is null)
  559. {
  560. if ((object)value0 is null || (object)value1 is null)
  561. {
  562. goto Found;
  563. }
  564. }
  565. else if (lookUp.Equals(value0) || lookUp.Equals(value1))
  566. {
  567. goto Found;
  568. }
  569. }
  570. }
  571. return -1;
  572. Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
  573. return length;
  574. Found1:
  575. return length + 1;
  576. Found2:
  577. return length + 2;
  578. Found3:
  579. return length + 3;
  580. Found4:
  581. return length + 4;
  582. Found5:
  583. return length + 5;
  584. Found6:
  585. return length + 6;
  586. Found7:
  587. return length + 7;
  588. }
  589. public static int LastIndexOfAny<T>(ref T searchSpace, T value0, T value1, T value2, int length)
  590. where T : IEquatable<T>
  591. {
  592. Debug.Assert(length >= 0);
  593. T lookUp;
  594. if (default(T) != null || ((object)value0 != null && (object)value1 != null))
  595. {
  596. while (length >= 8)
  597. {
  598. length -= 8;
  599. lookUp = Unsafe.Add(ref searchSpace, length + 7);
  600. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  601. goto Found7;
  602. lookUp = Unsafe.Add(ref searchSpace, length + 6);
  603. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  604. goto Found6;
  605. lookUp = Unsafe.Add(ref searchSpace, length + 5);
  606. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  607. goto Found5;
  608. lookUp = Unsafe.Add(ref searchSpace, length + 4);
  609. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  610. goto Found4;
  611. lookUp = Unsafe.Add(ref searchSpace, length + 3);
  612. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  613. goto Found3;
  614. lookUp = Unsafe.Add(ref searchSpace, length + 2);
  615. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  616. goto Found2;
  617. lookUp = Unsafe.Add(ref searchSpace, length + 1);
  618. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  619. goto Found1;
  620. lookUp = Unsafe.Add(ref searchSpace, length);
  621. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  622. goto Found;
  623. }
  624. if (length >= 4)
  625. {
  626. length -= 4;
  627. lookUp = Unsafe.Add(ref searchSpace, length + 3);
  628. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  629. goto Found3;
  630. lookUp = Unsafe.Add(ref searchSpace, length + 2);
  631. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  632. goto Found2;
  633. lookUp = Unsafe.Add(ref searchSpace, length + 1);
  634. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  635. goto Found1;
  636. lookUp = Unsafe.Add(ref searchSpace, length);
  637. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  638. goto Found;
  639. }
  640. while (length > 0)
  641. {
  642. length--;
  643. lookUp = Unsafe.Add(ref searchSpace, length);
  644. if (value0.Equals(lookUp) || value1.Equals(lookUp) || value2.Equals(lookUp))
  645. goto Found;
  646. }
  647. }
  648. else
  649. {
  650. for (length--; length >= 0; length--)
  651. {
  652. lookUp = Unsafe.Add(ref searchSpace, length);
  653. if ((object)lookUp is null)
  654. {
  655. if ((object)value0 is null || (object)value1 is null || (object)value2 is null)
  656. {
  657. goto Found;
  658. }
  659. }
  660. else if (lookUp.Equals(value0) || lookUp.Equals(value1) || lookUp.Equals(value2))
  661. {
  662. goto Found;
  663. }
  664. }
  665. }
  666. return -1;
  667. Found: // Workaround for https://github.com/dotnet/coreclr/issues/13549
  668. return length;
  669. Found1:
  670. return length + 1;
  671. Found2:
  672. return length + 2;
  673. Found3:
  674. return length + 3;
  675. Found4:
  676. return length + 4;
  677. Found5:
  678. return length + 5;
  679. Found6:
  680. return length + 6;
  681. Found7:
  682. return length + 7;
  683. }
  684. public static int LastIndexOfAny<T>(ref T searchSpace, int searchSpaceLength, ref T value, int valueLength)
  685. where T : IEquatable<T>
  686. {
  687. Debug.Assert(searchSpaceLength >= 0);
  688. Debug.Assert(valueLength >= 0);
  689. if (valueLength == 0)
  690. return 0; // A zero-length sequence is always treated as "found" at the start of the search space.
  691. int index = -1;
  692. for (int i = 0; i < valueLength; i++)
  693. {
  694. var tempIndex = LastIndexOf(ref searchSpace, Unsafe.Add(ref value, i), searchSpaceLength);
  695. if (tempIndex > index)
  696. index = tempIndex;
  697. }
  698. return index;
  699. }
  700. public static bool SequenceEqual<T>(ref T first, ref T second, int length)
  701. where T : IEquatable<T>
  702. {
  703. Debug.Assert(length >= 0);
  704. if (Unsafe.AreSame(ref first, ref second))
  705. goto Equal;
  706. IntPtr index = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
  707. T lookUp0;
  708. T lookUp1;
  709. while (length >= 8)
  710. {
  711. length -= 8;
  712. lookUp0 = Unsafe.Add(ref first, index);
  713. lookUp1 = Unsafe.Add(ref second, index);
  714. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  715. goto NotEqual;
  716. lookUp0 = Unsafe.Add(ref first, index + 1);
  717. lookUp1 = Unsafe.Add(ref second, index + 1);
  718. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  719. goto NotEqual;
  720. lookUp0 = Unsafe.Add(ref first, index + 2);
  721. lookUp1 = Unsafe.Add(ref second, index + 2);
  722. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  723. goto NotEqual;
  724. lookUp0 = Unsafe.Add(ref first, index + 3);
  725. lookUp1 = Unsafe.Add(ref second, index + 3);
  726. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  727. goto NotEqual;
  728. lookUp0 = Unsafe.Add(ref first, index + 4);
  729. lookUp1 = Unsafe.Add(ref second, index + 4);
  730. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  731. goto NotEqual;
  732. lookUp0 = Unsafe.Add(ref first, index + 5);
  733. lookUp1 = Unsafe.Add(ref second, index + 5);
  734. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  735. goto NotEqual;
  736. lookUp0 = Unsafe.Add(ref first, index + 6);
  737. lookUp1 = Unsafe.Add(ref second, index + 6);
  738. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  739. goto NotEqual;
  740. lookUp0 = Unsafe.Add(ref first, index + 7);
  741. lookUp1 = Unsafe.Add(ref second, index + 7);
  742. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  743. goto NotEqual;
  744. index += 8;
  745. }
  746. if (length >= 4)
  747. {
  748. length -= 4;
  749. lookUp0 = Unsafe.Add(ref first, index);
  750. lookUp1 = Unsafe.Add(ref second, index);
  751. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  752. goto NotEqual;
  753. lookUp0 = Unsafe.Add(ref first, index + 1);
  754. lookUp1 = Unsafe.Add(ref second, index + 1);
  755. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  756. goto NotEqual;
  757. lookUp0 = Unsafe.Add(ref first, index + 2);
  758. lookUp1 = Unsafe.Add(ref second, index + 2);
  759. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  760. goto NotEqual;
  761. lookUp0 = Unsafe.Add(ref first, index + 3);
  762. lookUp1 = Unsafe.Add(ref second, index + 3);
  763. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  764. goto NotEqual;
  765. index += 4;
  766. }
  767. while (length > 0)
  768. {
  769. lookUp0 = Unsafe.Add(ref first, index);
  770. lookUp1 = Unsafe.Add(ref second, index);
  771. if (!(lookUp0?.Equals(lookUp1) ?? (object)lookUp1 is null))
  772. goto NotEqual;
  773. index += 1;
  774. length--;
  775. }
  776. Equal:
  777. return true;
  778. NotEqual: // Workaround for https://github.com/dotnet/coreclr/issues/13549
  779. return false;
  780. }
  781. public static int SequenceCompareTo<T>(ref T first, int firstLength, ref T second, int secondLength)
  782. where T : IComparable<T>
  783. {
  784. Debug.Assert(firstLength >= 0);
  785. Debug.Assert(secondLength >= 0);
  786. var minLength = firstLength;
  787. if (minLength > secondLength)
  788. minLength = secondLength;
  789. for (int i = 0; i < minLength; i++)
  790. {
  791. T lookUp = Unsafe.Add(ref second, i);
  792. int result = (Unsafe.Add(ref first, i)?.CompareTo(lookUp) ?? (((object)lookUp is null) ? 0 : -1));
  793. if (result != 0)
  794. return result;
  795. }
  796. return firstLength.CompareTo(secondLength);
  797. }
  798. }
  799. }