class BasedSegmentBuilder extends SegmentBuilderBase<BasedSegmentBuilder> implements IBasedSegmentBuilder<BasedSegmentBuilder> {
    final @NotNull BasedSequence baseSeq;
    final @NotNull SegmentOptimizer optimizer;

    protected BasedSegmentBuilder(@NotNull BasedSequence baseSeq) {
        this(baseSeq, new CharRecoveryOptimizer(PositionAnchor.CURRENT));
    }

    protected BasedSegmentBuilder(@NotNull BasedSequence baseSeq, @NotNull SegmentOptimizer optimizer) {
        super();
        this.baseSeq = baseSeq.getBaseSequence();
        this.optimizer = optimizer;
    }

    protected BasedSegmentBuilder(@NotNull BasedSequence baseSeq, int options) {
        this(baseSeq, new CharRecoveryOptimizer(PositionAnchor.CURRENT), options);
    }

    protected BasedSegmentBuilder(@NotNull BasedSequence baseSeq, @NotNull SegmentOptimizer optimizer, int options) {
        super(options);
        this.baseSeq = baseSeq.getBaseSequence();
        this.optimizer = optimizer;
    }

    @Override
    public @NotNull BasedSequence getBaseSequence() {
        return baseSeq;
    }

    @Override
    protected Object[] optimizeText(@NotNull Object[] parts) {
        return optimizer.apply(baseSeq, parts);
    }

    @Override
    protected Object[] handleOverlap(@NotNull Object[] parts) {<FILL_FUNCTION_BODY>}

    @NotNull
    @Override
    public String toStringWithRangesVisibleWhitespace() {
        return super.toStringWithRangesVisibleWhitespace(baseSeq);
    }

    @NotNull
    @Override
    public String toStringWithRanges() {
        return super.toStringWithRanges(baseSeq);
    }

    @NotNull
    @Override
    public String toStringChars() {
        return super.toString(baseSeq);
    }

    @NotNull
    public static BasedSegmentBuilder emptyBuilder(@NotNull BasedSequence sequence) {
        return new BasedSegmentBuilder(sequence);
    }

    @NotNull
    public static BasedSegmentBuilder emptyBuilder(@NotNull BasedSequence sequence, int options) {
        return new BasedSegmentBuilder(sequence, options);
    }

    @NotNull
    public static BasedSegmentBuilder emptyBuilder(@NotNull BasedSequence sequence, @NotNull SegmentOptimizer optimizer) {
        return new BasedSegmentBuilder(sequence, optimizer);
    }

    @NotNull
    public static BasedSegmentBuilder emptyBuilder(@NotNull BasedSequence sequence, @NotNull SegmentOptimizer optimizer, int options) {
        return new BasedSegmentBuilder(sequence, optimizer, options);
    }
}

        // convert overlap to text from our base
        // range overlaps with last segment in the list
        Range lastSeg = (Range) parts[0];
        CharSequence text = (CharSequence) parts[1];
        Range range = (Range) parts[2];
        assert !lastSeg.isNull() && lastSeg.getEnd() > range.getStart();

        Range overlap;
        Range after = Range.NULL;

        if (range.getEnd() <= lastSeg.getStart()) {
            // the whole thing is before
            overlap = range;
        } else if (range.getStart() <= lastSeg.getStart()) {
            // part before, maybe some after
            overlap = Range.of(range.getStart(), Math.min(range.getEnd(), lastSeg.getEnd()));
            if (lastSeg.getEnd() < range.getEnd()) {
                after = Range.of(lastSeg.getEnd(), range.getEnd());
            }
        } else if (range.getEnd() <= lastSeg.getEnd()) {
            // all contained within
            overlap = range;
        } else {
            assert range.getStart() < lastSeg.getEnd();
            overlap = range.withEnd(lastSeg.getEnd());
            after = range.withStart(lastSeg.getEnd());
        }

        int overlapSpan = overlap.getSpan();
        assert overlapSpan + after.getSpan() == range.getSpan();

        // append overlap to text
        if (text.length() == 0) {
            parts[1] = baseSeq.subSequence(overlap.getStart(), overlap.getEnd()).toString();
        } else {
            parts[1] = text.toString() + baseSeq.subSequence(overlap.getStart(), overlap.getEnd()).toString();
        }
        parts[2] = after;

        return parts;
    
<methods>public @NotNull BasedSegmentBuilder append(@NotNull Range) ,public @NotNull BasedSegmentBuilder append(int, int) ,public @NotNull BasedSegmentBuilder append(@NotNull CharSequence) ,public @NotNull BasedSegmentBuilder append(char) ,public @NotNull BasedSegmentBuilder append(char, int) ,public @NotNull BasedSegmentBuilder appendAnchor(int) ,public @Nullable Range getBaseSubSequenceRange() ,public int getEndOffset() ,public int getEndOffsetIfNeeded() ,public int getOptions() ,public @NotNull Object getPart(int) ,public @NotNull Iterable<Seg> getSegments() ,public int getSpan() ,public int getStartOffset() ,public int getStartOffsetIfNeeded() ,public com.vladsch.flexmark.util.sequence.builder.SegmentStats getStats() ,public java.lang.CharSequence getText() ,public int getTextFirst256Length() ,public int getTextFirst256Segments() ,public int getTextLength() ,public int getTextSegments() ,public int getTextSpaceLength() ,public int getTextSpaceSegments() ,public boolean haveOffsets() ,public boolean isBaseSubSequenceRange() ,public boolean isEmpty() ,public boolean isIncludeAnchors() ,public boolean isTrackTextFirst256() ,public @NotNull Iterator<Object> iterator() ,public int length() ,public boolean needEndOffset() ,public boolean needStartOffset() ,public int noAnchorsSize() ,public int size() ,public @NotNull String toString(@NotNull CharSequence, @NotNull CharSequence, @NotNull CharSequence, @NotNull Function<CharSequence,CharSequence>) ,public @NotNull String toString(@NotNull CharSequence) ,public java.lang.String toString() ,public java.lang.String toStringPrep() ,public @NotNull String toStringWithRanges(@NotNull CharSequence) ,public @NotNull String toStringWithRangesVisibleWhitespace(@NotNull CharSequence) ,public void trimToSize() <variables>public static final int[] EMPTY_PARTS,public static final int MIN_PART_CAPACITY,protected int anchorsSize,protected int endOffset,protected int immutableOffset,protected int length,protected final non-sealed int options,protected @NotNull int [] parts,protected int partsSize,protected int startOffset,protected final non-sealed com.vladsch.flexmark.util.sequence.builder.SegmentStats stats,protected final java.lang.StringBuilder text,protected final non-sealed com.vladsch.flexmark.util.sequence.builder.SegmentStats textStats