[][src]Module asciifile::line_number_cache

Caches information about linebreaks that allows us to reconstruct the line number and character column in O(log N). This makes sense as we only request line numbers in a few cases (e.g. on error).

use asciifile::LineNumberCache;

let input = "asd\nqwer\nZU\nfgfh";
let positions = input
    .chars()
    .enumerate()
    .filter_map(
        |(offset, chr)| {
            if chr == '\n' {
                Some(offset)
            } else {
                None
            }
        },
    )
    .collect();
let cache = LineNumberCache::new(positions);

let actual: Vec<_> = input
    .chars()
    .enumerate()
    .map(|(offset, chr)| (chr, cache.row_and_column(offset)))
    .collect();

let expected = vec![
    ('a', (0, 0)),
    ('s', (0, 1)),
    ('d', (0, 2)),
    ('\n', (0, 3)),
    ('q', (1, 0)),
    ('w', (1, 1)),
    ('e', (1, 2)),
    ('r', (1, 3)),
    ('\n', (1, 4)),
    ('Z', (2, 0)),
    ('U', (2, 1)),
    ('\n', (2, 2)),
    ('f', (3, 0)),
    ('g', (3, 1)),
    ('f', (3, 2)),
    ('h', (3, 3)),
];

assert_eq!(actual, expected);

Structs

LineNumberCache