Function utils::grid::from_str_padded

source ·
pub fn from_str_padded<T: Clone>(
    input: &str,
    padding: usize,
    padding_value: T,
    func: impl FnMut(u8) -> Option<T>,
) -> Result<(usize, usize, Vec<T>), InputError>
Expand description

Parse 2D grid, adding padding around the edges.

Similar to from_str, but pads the edges of the parsed grid with padding rows and columns filled with the default value. This is helpful to avoid bounds checks when considering a location’s neighbors.

Returns (number of rows, number of columns, data) on success (row and column counts include the added padding).

§Examples

assert_eq!(
    from_str_padded("##.#\n#..#\n#.##", 2, false, |c| match c {
        b'#' => Some(true),
        b'.' => Some(false),
        _ => None,
    }).unwrap(),
    (7, 8, vec![
        false, false, false, false, false, false, false, false,
        false, false, false, false, false, false, false, false,
        false, false, true, true, false, true, false, false,
        false, false, true, false, false, true, false, false,
        false, false, true, false, true, true, false, false,
        false, false, false, false, false, false, false, false,
        false, false, false, false, false, false, false, false,
    ]),
);