| 565 | } |
| 566 | |
| 567 | #savePartialsBeforeReset(): void { |
| 568 | // Early return if no accumulators are active (common case) |
| 569 | if ( |
| 570 | this.#textStartIdx === -1 && |
| 571 | this.#cdataStartIdx === -1 && |
| 572 | this.#attrStartIdx === -1 && |
| 573 | this.#tagNameStartIdx === -1 && |
| 574 | this.#commentStartIdx === -1 && |
| 575 | this.#piTargetStartIdx === -1 && |
| 576 | this.#piContentStartIdx === -1 && |
| 577 | this.#attrNameStartIdx === -1 |
| 578 | ) { |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | // Cache private fields accessed multiple times |
| 583 | const buffer = this.#buffer; |
| 584 | const end = this.#bufferIndex; |
| 585 | |
| 586 | // --- Accumulators that need their data saved --- |
| 587 | // Text, tag name, attribute name, and PI target accumulators track |
| 588 | // [startIdx, bufferIndex) ranges that haven't been copied to their |
| 589 | // partial strings yet. Save that data before the buffer changes. |
| 590 | if (this.#textStartIdx !== -1) { |
| 591 | this.#textPartial += buffer.slice(this.#textStartIdx, end); |
| 592 | this.#textStartIdx = 0; |
| 593 | } |
| 594 | if (this.#tagNameStartIdx !== -1) { |
| 595 | this.#tagNamePartial += buffer.slice(this.#tagNameStartIdx, end); |
| 596 | this.#tagNameStartIdx = 0; |
| 597 | } |
| 598 | if (this.#attrNameStartIdx !== -1) { |
| 599 | this.#attrNamePartial += buffer.slice(this.#attrNameStartIdx, end); |
| 600 | this.#attrNameStartIdx = 0; |
| 601 | } |
| 602 | if (this.#piTargetStartIdx !== -1) { |
| 603 | this.#piTargetPartial += buffer.slice(this.#piTargetStartIdx, end); |
| 604 | this.#piTargetStartIdx = 0; |
| 605 | } |
| 606 | |
| 607 | // --- Accumulators that only need index reset --- |
| 608 | // Comment, CDATA, PI content, and attribute value accumulators save |
| 609 | // their data eagerly during batch scanning (#captureComment, etc.). |
| 610 | // At chunk boundaries their startIdx always equals bufferIndex (the |
| 611 | // main loop fully consumes the buffer), so the range is empty and |
| 612 | // there is nothing to copy — just reset the indices for the next chunk. |
| 613 | if (this.#cdataStartIdx !== -1) this.#cdataStartIdx = 0; |
| 614 | if (this.#commentStartIdx !== -1) this.#commentStartIdx = 0; |
| 615 | if (this.#piContentStartIdx !== -1) this.#piContentStartIdx = 0; |
| 616 | if (this.#attrStartIdx !== -1) this.#attrStartIdx = 0; |
| 617 | } |
| 618 | |
| 619 | #advanceWithCode(code: number): void { |
| 620 | if (this.#trackPosition) { |