Skip to main content

compute_char_offsets

Function compute_char_offsets 

Source
pub fn compute_char_offsets(text: &str, slabs: &mut [Slab])
Expand description

Compute character offsets for a batch of slabs from the same document.

Builds a byte-to-char mapping in a single O(n) pass over the source text, then fills char_start/char_end on each slab. This is faster than per-slab computation when there are many slabs.

§Example

use code_chunker::{compute_char_offsets, Slab};

let text = "Hello 日本語 world";
let mut slabs = vec![
    Slab::new("Hello ", 0, 6, 0),
    Slab::new("日本語", 6, 15, 1),
];
compute_char_offsets(text, &mut slabs);

assert_eq!(slabs[0].char_start, Some(0));
assert_eq!(slabs[1].char_start, Some(6));
assert_eq!(slabs[1].char_end, Some(9));