Skip to main content

logical_offset_to_byte

Function logical_offset_to_byte 

Source
pub fn logical_offset_to_byte(
    plain_text: &str,
    _images: &[ImageAnchor],
    char_offset: i64,
) -> u32
Expand description

Translate a logical character offset (counting text characters AND image positions interleaved by their byte_offset) into a UTF-8 byte offset within plain_text. Used by writer use cases to map a document-space char position to the byte position where text edits should land in block.plain_text.

Each image already occupies exactly one character of plain_text: the U+FFFC OBJECT REPLACEMENT CHARACTER that insert_image mirrors into the rope, which ImageAnchor::byte_offset points at. So a logical offset is a character offset and this is a plain char→byte mapping.

images is retained in the signature — and deliberately unused — because getting this wrong is silent and expensive, and callers pass it naturally. The previous implementation walked the anchor list in addition to char_indices(), so every image advanced the logical counter twice. That dates from the pre-rope model, where an anchor genuinely contributed no bytes; once the sentinel went into the rope the two representations started double-counting. The visible effects: selecting across an image returned the wrong text (an extra sentinel, a missing character), and deleting a range containing one removed too little.