1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace System.Buffers
{
#if __MonoCS__
public ref partial struct SequenceReader<T> where T : IEquatable<T>
#else
public ref partial struct SequenceReader<T> where T : unmanaged, IEquatable<T>
#endif
{
/// <summary>
/// Try to read everything up to the given <paramref name="delimiter"/>.
/// </summary>
/// <param name="span">The read data, if any.</param>
/// <param name="delimiter">The delimiter to look for.</param>
/// <param name="advancePastDelimiter">True to move past the <paramref name="delimiter"/> if found.</param>
/// <returns>True if the <paramref name="delimiter"/> was found.</returns>
public bool TryReadTo(out ReadOnlySpan<T> span, T delimiter, bool advancePastDelimiter = true)
{
ReadOnlySpan<T> remaining = UnreadSpan;
int index = remaining.IndexOf(delimiter);
if (index != -1)
{
span = index == 0 ? default : remaining.Slice(0, index);
AdvanceCurrentSpan(index + (advancePastDelimiter ? 1 : 0));
return true;
}
return TryReadToSlow(out span, delimiter, advancePastDelimiter);
}
private bool TryReadToSlow(out ReadOnlySpan<T> span, T delimiter, bool advancePastDelimiter)
{
if (!TryReadToInternal(out ReadOnlySequence<T> sequence, delimiter, advancePastDelimiter, CurrentSpan.Length - CurrentSpanIndex))
{
span = default;
return false;
}
span = sequence.IsSingleSegment ? sequence.First.Span : sequence.ToArray();
return true;
}
/// <summary>
/// Try to read everything up to the given <paramref name="delimiter"/>, ignoring delimiters that are
/// preceded by <paramref name="delimiterEscape"/>.
/// </summary>
/// <param name="span">The read data, if any.</param>
/// <param name="delimiter">The delimiter to look for.</param>
/// <param name="delimiterEscape">If found prior to <paramref name="delimiter"/> it will skip that occurrence.</param>
/// <param name="advancePastDelimiter">True to move past the <paramref name="delimiter"/> if found.</param>
/// <returns>True if the <paramref name="delimiter"/> was found.</returns>
public bool TryReadTo(out ReadOnlySpan<T> span, T delimiter, T delimiterEscape, bool advancePastDelimiter = true)
{
ReadOnlySpan<T> remaining = UnreadSpan;
int index = remaining.IndexOf(delimiter);
if ((index > 0 && !remaining[index - 1].Equals(delimiterEscape)) || index == 0)
{
span = remaining.Slice(0, index);
AdvanceCurrentSpan(index + (advancePastDelimiter ? 1 : 0));
return true;
}
// This delimiter might be skipped, go down the slow path
return TryReadToSlow(out span, delimiter, delimiterEscape, index, advancePastDelimiter);
}
private bool TryReadToSlow(out ReadOnlySpan<T> span, T delimiter, T delimiterEscape, int index, bool advancePastDelimiter)
{
if (!TryReadToSlow(out ReadOnlySequence<T> sequence, delimiter, delimiterEscape, index, advancePastDelimiter))
{
span = default;
return false;
}
Debug.Assert(sequence.Length > 0);
span = sequence.IsSingleSegment ? sequence.First.Span : sequence.ToArray();
return true;
}
private bool TryReadToSlow(out ReadOnlySequence<T> sequence, T delimiter, T delimiterEscape, int index, bool advancePastDelimiter)
{
SequenceReader<T> copy = this;
ReadOnlySpan<T> remaining = UnreadSpan;
bool priorEscape = false;
do
{
if (index >= 0)
{
if (index == 0 && priorEscape)
{
// We were in the escaped state, so skip this delimiter
priorEscape = false;
Advance(index + 1);
remaining = UnreadSpan;
goto Continue;
}
else if (index > 0 && remaining[index - 1].Equals(delimiterEscape))
{
// This delimiter might be skipped
// Count our escapes
int escapeCount = 1;
int i = index - 2;
for (; i >= 0; i--)
{
if (!remaining[i].Equals(delimiterEscape))
break;
}
if (i < 0 && priorEscape)
{
// Started and ended with escape, increment once more
escapeCount++;
}
escapeCount += index - 2 - i;
if ((escapeCount & 1) != 0)
{
// An odd escape count means we're currently escaped,
// skip the delimiter and reset escaped state.
Advance(index + 1);
priorEscape = false;
remaining = UnreadSpan;
goto Continue;
}
}
// Found the delimiter. Move to it, slice, then move past it.
AdvanceCurrentSpan(index);
sequence = Sequence.Slice(copy.Position, Position);
if (advancePastDelimiter)
{
Advance(1);
}
return true;
}
else
{
// No delimiter, need to check the end of the span for odd number of escapes then advance
if (remaining.Length > 0 && remaining[remaining.Length - 1].Equals(delimiterEscape))
{
int escapeCount = 1;
int i = remaining.Length - 2;
for (; i >= 0; i--)
{
if (!remaining[i].Equals(delimiterEscape))
break;
}
escapeCount += remaining.Length - 2 - i;
if (i < 0 && priorEscape)
priorEscape = (escapeCount & 1) == 0; // equivalent to incrementing escapeCount before setting priorEscape
else
priorEscape = (escapeCount & 1) != 0;
}
else
{
priorEscape = false;
}
}
// Nothing in the current span, move to the end, checking for the skip delimiter
AdvanceCurrentSpan(remaining.Length);
remaining = CurrentSpan;
Continue:
index = remaining.IndexOf(delimiter);
} while (!End);
// Didn't find anything, reset our original state.
this = copy;
sequence = default;
return false;
}
/// <summary>
/// Try to read everything up to the given <paramref name="delimiter"/>.
/// </summary>
/// <param name="sequence">The read data, if any.</param>
/// <param name="delimiter">The delimiter to look for.</param>
/// <param name="advancePastDelimiter">True to move past the <paramref name="delimiter"/> if found.</param>
/// <returns>True if the <paramref name="delimiter"/> was found.</returns>
public bool TryReadTo(out ReadOnlySequence<T> sequence, T delimiter, bool advancePastDelimiter = true)
{
return TryReadToInternal(out sequence, delimiter, advancePastDelimiter);
}
private bool TryReadToInternal(out ReadOnlySequence<T> sequence, T delimiter, bool advancePastDelimiter, int skip = 0)
{
Debug.Assert(skip >= 0);
SequenceReader<T> copy = this;
if (skip > 0)
Advance(skip);
ReadOnlySpan<T> remaining = UnreadSpan;
while (_moreData)
{
int index = remaining.IndexOf(delimiter);
if (index != -1)
{
// Found the delimiter. Move to it, slice, then move past it.
if (index > 0)
{
AdvanceCurrentSpan(index);
}
sequence = Sequence.Slice(copy.Position, Position);
if (advancePastDelimiter)
{
Advance(1);
}
return true;
}
AdvanceCurrentSpan(remaining.Length);
remaining = CurrentSpan;
}
// Didn't find anything, reset our original state.
this = copy;
sequence = default;
return false;
}
/// <summary>
/// Try to read everything up to the given <paramref name="delimiter"/>, ignoring delimiters that are
/// preceded by <paramref name="delimiterEscape"/>.
/// </summary>
/// <param name="sequence">The read data, if any.</param>
/// <param name="delimiter">The delimiter to look for.</param>
/// <param name="delimiterEscape">If found prior to <paramref name="delimiter"/> it will skip that occurrence.</param>
/// <param name="advancePastDelimiter">True to move past the <paramref name="delimiter"/> if found.</param>
/// <returns>True if the <paramref name="delimiter"/> was found.</returns>
public bool TryReadTo(out ReadOnlySequence<T> sequence, T delimiter, T delimiterEscape, bool advancePastDelimiter = true)
{
SequenceReader<T> copy = this;
ReadOnlySpan<T> remaining = UnreadSpan;
bool priorEscape = false;
while (_moreData)
{
int index = remaining.IndexOf(delimiter);
if (index != -1)
{
if (index == 0 && priorEscape)
{
// We were in the escaped state, so skip this delimiter
priorEscape = false;
Advance(index + 1);
remaining = UnreadSpan;
continue;
}
else if (index > 0 && remaining[index - 1].Equals(delimiterEscape))
{
// This delimiter might be skipped
// Count our escapes
int escapeCount = 0;
for (int i = index; i > 0 && remaining[i - 1].Equals(delimiterEscape); i--, escapeCount++)
;
if (escapeCount == index && priorEscape)
{
// Started and ended with escape, increment once more
escapeCount++;
}
priorEscape = false;
if ((escapeCount & 1) != 0)
{
// Odd escape count means we're in the escaped state, so skip this delimiter
Advance(index + 1);
remaining = UnreadSpan;
continue;
}
}
// Found the delimiter. Move to it, slice, then move past it.
if (index > 0)
{
Advance(index);
}
sequence = Sequence.Slice(copy.Position, Position);
if (advancePastDelimiter)
{
Advance(1);
}
return true;
}
// No delimiter, need to check the end of the span for odd number of escapes then advance
{
int escapeCount = 0;
for (int i = remaining.Length; i > 0 && remaining[i - 1].Equals(delimiterEscape); i--, escapeCount++)
;
if (priorEscape && escapeCount == remaining.Length)
{
escapeCount++;
}
priorEscape = escapeCount % 2 != 0;
}
// Nothing in the current span, move to the end, checking for the skip delimiter
Advance(remaining.Length);
remaining = CurrentSpan;
}
// Didn't find anything, reset our original state.
this = copy;
sequence = default;
return false;
}
/// <summary>
/// Try to read everything up to the given <paramref name="delimiters"/>.
/// </summary>
/// <param name="span">The read data, if any.</param>
/// <param name="delimiters">The delimiters to look for.</param>
/// <param name="advancePastDelimiter">True to move past the first found instance of any of the given <paramref name="delimiters"/>.</param>
/// <returns>True if any of the <paramref name="delimiters"/> were found.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadToAny(out ReadOnlySpan<T> span, ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true)
{
ReadOnlySpan<T> remaining = UnreadSpan;
int index = delimiters.Length == 2
? remaining.IndexOfAny(delimiters[0], delimiters[1])
: remaining.IndexOfAny(delimiters);
if (index != -1)
{
span = remaining.Slice(0, index);
Advance(index + (advancePastDelimiter ? 1 : 0));
return true;
}
return TryReadToAnySlow(out span, delimiters, advancePastDelimiter);
}
private bool TryReadToAnySlow(out ReadOnlySpan<T> span, ReadOnlySpan<T> delimiters, bool advancePastDelimiter)
{
if (!TryReadToAnyInternal(out ReadOnlySequence<T> sequence, delimiters, advancePastDelimiter, CurrentSpan.Length - CurrentSpanIndex))
{
span = default;
return false;
}
span = sequence.IsSingleSegment ? sequence.First.Span : sequence.ToArray();
return true;
}
/// <summary>
/// Try to read everything up to the given <paramref name="delimiters"/>.
/// </summary>
/// <param name="sequence">The read data, if any.</param>
/// <param name="delimiters">The delimiters to look for.</param>
/// <param name="advancePastDelimiter">True to move past the first found instance of any of the given <paramref name="delimiters"/>.</param>
/// <returns>True if any of the <paramref name="delimiters"/> were found.</returns>
public bool TryReadToAny(out ReadOnlySequence<T> sequence, ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true)
{
return TryReadToAnyInternal(out sequence, delimiters, advancePastDelimiter);
}
private bool TryReadToAnyInternal(out ReadOnlySequence<T> sequence, ReadOnlySpan<T> delimiters, bool advancePastDelimiter, int skip = 0)
{
SequenceReader<T> copy = this;
if (skip > 0)
Advance(skip);
ReadOnlySpan<T> remaining = UnreadSpan;
while (!End)
{
int index = delimiters.Length == 2
? remaining.IndexOfAny(delimiters[0], delimiters[1])
: remaining.IndexOfAny(delimiters);
if (index != -1)
{
// Found one of the delimiters. Move to it, slice, then move past it.
if (index > 0)
{
AdvanceCurrentSpan(index);
}
sequence = Sequence.Slice(copy.Position, Position);
if (advancePastDelimiter)
{
Advance(1);
}
return true;
}
Advance(remaining.Length);
remaining = CurrentSpan;
}
// Didn't find anything, reset our original state.
this = copy;
sequence = default;
return false;
}
/// <summary>
/// Try to read data until the entire given <paramref name="delimiter"/> matches.
/// </summary>
/// <param name="sequence">The read data, if any.</param>
/// <param name="delimiter">The multi (T) delimiter.</param>
/// <param name="advancePastDelimiter">True to move past the <paramref name="delimiter"/> if found.</param>
/// <returns>True if the <paramref name="delimiter"/> was found.</returns>
public bool TryReadTo(out ReadOnlySequence<T> sequence, ReadOnlySpan<T> delimiter, bool advancePastDelimiter = true)
{
if (delimiter.Length == 0)
{
sequence = default;
return true;
}
SequenceReader<T> copy = this;
bool advanced = false;
while (!End)
{
if (!TryReadTo(out sequence, delimiter[0], advancePastDelimiter: false))
{
this = copy;
return false;
}
if (delimiter.Length == 1)
{
if (advancePastDelimiter)
{
Advance(1);
}
return true;
}
if (IsNext(delimiter))
{
// Probably a faster way to do this, potentially by avoiding the Advance in the previous TryReadTo call
if (advanced)
{
sequence = copy.Sequence.Slice(copy.Consumed, Consumed - copy.Consumed);
}
if (advancePastDelimiter)
{
Advance(delimiter.Length);
}
return true;
}
else
{
Advance(1);
advanced = true;
}
}
this = copy;
sequence = default;
return false;
}
/// <summary>
/// Advance until the given <paramref name="delimiter"/>, if found.
/// </summary>
/// <param name="delimiter">The delimiter to search for.</param>
/// <param name="advancePastDelimiter">True to move past the <paramref name="delimiter"/> if found.</param>
/// <returns>True if the given <paramref name="delimiter"/> was found.</returns>
public bool TryAdvanceTo(T delimiter, bool advancePastDelimiter = true)
{
ReadOnlySpan<T> remaining = UnreadSpan;
int index = remaining.IndexOf(delimiter);
if (index != -1)
{
Advance(advancePastDelimiter ? index + 1 : index);
return true;
}
return TryReadToInternal(out _, delimiter, advancePastDelimiter);
}
/// <summary>
/// Advance until any of the given <paramref name="delimiters"/>, if found.
/// </summary>
/// <param name="delimiters">The delimiters to search for.</param>
/// <param name="advancePastDelimiter">True to move past the first found instance of any of the given <paramref name="delimiters"/>.</param>
/// <returns>True if any of the given <paramref name="delimiters"/> were found.</returns>
public bool TryAdvanceToAny(ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true)
{
ReadOnlySpan<T> remaining = UnreadSpan;
int index = remaining.IndexOfAny(delimiters);
if (index != -1)
{
AdvanceCurrentSpan(index + (advancePastDelimiter ? 1 : 0));
return true;
}
return TryReadToAnyInternal(out _, delimiters, advancePastDelimiter);
}
/// <summary>
/// Advance past consecutive instances of the given <paramref name="value"/>.
/// </summary>
/// <returns>How many positions the reader has been advanced.</returns>
public long AdvancePast(T value)
{
long start = Consumed;
do
{
// Advance past all matches in the current span
int i;
for (i = CurrentSpanIndex; i < CurrentSpan.Length && CurrentSpan[i].Equals(value); i++)
{
}
int advanced = i - CurrentSpanIndex;
if (advanced == 0)
{
// Didn't advance at all in this span, exit.
break;
}
AdvanceCurrentSpan(advanced);
// If we're at postion 0 after advancing and not at the End,
// we're in a new span and should continue the loop.
} while (CurrentSpanIndex == 0 && !End);
return Consumed - start;
}
/// <summary>
/// Skip consecutive instances of any of the given <paramref name="values"/>.
/// </summary>
/// <returns>How many positions the reader has been advanced.</returns>
public long AdvancePastAny(ReadOnlySpan<T> values)
{
long start = Consumed;
do
{
// Advance past all matches in the current span
int i;
for (i = CurrentSpanIndex; i < CurrentSpan.Length && values.IndexOf(CurrentSpan[i]) != -1; i++)
{
}
int advanced = i - CurrentSpanIndex;
if (advanced == 0)
{
// Didn't advance at all in this span, exit.
break;
}
AdvanceCurrentSpan(advanced);
// If we're at postion 0 after advancing and not at the End,
// we're in a new span and should continue the loop.
} while (CurrentSpanIndex == 0 && !End);
return Consumed - start;
}
/// <summary>
/// Advance past consecutive instances of any of the given values.
/// </summary>
/// <returns>How many positions the reader has been advanced.</returns>
public long AdvancePastAny(T value0, T value1, T value2, T value3)
{
long start = Consumed;
do
{
// Advance past all matches in the current span
int i;
for (i = CurrentSpanIndex; i < CurrentSpan.Length; i++)
{
T value = CurrentSpan[i];
if (!value.Equals(value0) && !value.Equals(value1) && !value.Equals(value2) && !value.Equals(value3))
{
break;
}
}
int advanced = i - CurrentSpanIndex;
if (advanced == 0)
{
// Didn't advance at all in this span, exit.
break;
}
AdvanceCurrentSpan(advanced);
// If we're at postion 0 after advancing and not at the End,
// we're in a new span and should continue the loop.
} while (CurrentSpanIndex == 0 && !End);
return Consumed - start;
}
/// <summary>
/// Advance past consecutive instances of any of the given values.
/// </summary>
/// <returns>How many positions the reader has been advanced.</returns>
public long AdvancePastAny(T value0, T value1, T value2)
{
long start = Consumed;
do
{
// Advance past all matches in the current span
int i;
for (i = CurrentSpanIndex; i < CurrentSpan.Length; i++)
{
T value = CurrentSpan[i];
if (!value.Equals(value0) && !value.Equals(value1) && !value.Equals(value2))
{
break;
}
}
int advanced = i - CurrentSpanIndex;
if (advanced == 0)
{
// Didn't advance at all in this span, exit.
break;
}
AdvanceCurrentSpan(advanced);
// If we're at postion 0 after advancing and not at the End,
// we're in a new span and should continue the loop.
} while (CurrentSpanIndex == 0 && !End);
return Consumed - start;
}
/// <summary>
/// Advance past consecutive instances of any of the given values.
/// </summary>
/// <returns>How many positions the reader has been advanced.</returns>
public long AdvancePastAny(T value0, T value1)
{
long start = Consumed;
do
{
// Advance past all matches in the current span
int i;
for (i = CurrentSpanIndex; i < CurrentSpan.Length; i++)
{
T value = CurrentSpan[i];
if (!value.Equals(value0) && !value.Equals(value1))
{
break;
}
}
int advanced = i - CurrentSpanIndex;
if (advanced == 0)
{
// Didn't advance at all in this span, exit.
break;
}
AdvanceCurrentSpan(advanced);
// If we're at postion 0 after advancing and not at the End,
// we're in a new span and should continue the loop.
} while (CurrentSpanIndex == 0 && !End);
return Consumed - start;
}
/// <summary>
/// Check to see if the given <paramref name="next"/> value is next.
/// </summary>
/// <param name="next">The value to compare the next items to.</param>
/// <param name="advancePast">Move past the <paramref name="next"/> value if found.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsNext(T next, bool advancePast = false)
{
if (End)
return false;
if (CurrentSpan[CurrentSpanIndex].Equals(next))
{
if (advancePast)
{
AdvanceCurrentSpan(1);
}
return true;
}
return false;
}
/// <summary>
/// Check to see if the given <paramref name="next"/> values are next.
/// </summary>
/// <param name="next">The span to compare the next items to.</param>
/// <param name="advancePast">Move past the <paramref name="next"/> values if found.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsNext(ReadOnlySpan<T> next, bool advancePast = false)
{
ReadOnlySpan<T> unread = UnreadSpan;
if (unread.StartsWith(next))
{
if (advancePast)
{
AdvanceCurrentSpan(next.Length);
}
return true;
}
// Only check the slow path if there wasn't enough to satisfy next
return unread.Length < next.Length && IsNextSlow(next, advancePast);
}
private unsafe bool IsNextSlow(ReadOnlySpan<T> next, bool advancePast)
{
ReadOnlySpan<T> currentSpan = UnreadSpan;
// We should only come in here if we need more data than we have in our current span
Debug.Assert(currentSpan.Length < next.Length);
int fullLength = next.Length;
SequencePosition nextPosition = _nextPosition;
while (next.StartsWith(currentSpan))
{
if (next.Length == currentSpan.Length)
{
// Fully matched
if (advancePast)
{
Advance(fullLength);
}
return true;
}
// Need to check the next segment
while (true)
{
if (!Sequence.TryGet(ref nextPosition, out ReadOnlyMemory<T> nextSegment, advance: true))
{
// Nothing left
return false;
}
if (nextSegment.Length > 0)
{
next = next.Slice(currentSpan.Length);
currentSpan = nextSegment.Span;
if (currentSpan.Length > next.Length)
{
currentSpan = currentSpan.Slice(0, next.Length);
}
break;
}
}
}
return false;
}
}
}
|