SpanHelpers.T.cs 37 KB

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