Function line_to_byte_offset

Source
pub fn line_to_byte_offset<K, I>(
    bytes: I,
    line: usize,
) -> Result<usize, ParseError>
where K: Borrow<u8> + Eq, I: Iterator<Item = K>,
Expand description

Returns the byte offset of the starting character of line within the iterator.

Lines are assumed to be 1 indexed, and offsets are 0 indexed.

§Errors

Will return an ParseError::NotFound if the line cannot be found.

§Example

use device_tree_source::line_to_byte_offset;
use device_tree_source::ParseError;

let string = "Howdy\nHow goes it\n\nI'm doing fine";

assert_eq!(line_to_byte_offset(string.as_bytes().iter(), 1), Ok(0));
assert_eq!(line_to_byte_offset(string.as_bytes().iter(), 3), Ok(18));
assert_eq!(line_to_byte_offset(string.as_bytes().iter(), 5), Err(ParseError::NotFound));