mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Create reusable line reader
This commit is contained in:
@@ -4,6 +4,33 @@ use std::io::Read;
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
pub struct LineIter<'a> {
|
||||||
|
reader: BufReader<&'a mut dyn Read>,
|
||||||
|
buffer: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> LineIter<'a> {
|
||||||
|
pub fn new(input: &'a mut dyn Read) -> Self {
|
||||||
|
Self {
|
||||||
|
reader: BufReader::new(input),
|
||||||
|
buffer: String::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the next line, or None
|
||||||
|
///
|
||||||
|
/// This is deliberately not an [Iterator] impl as those cannot hand out references to self.
|
||||||
|
pub fn next(&mut self) -> Option<&str> {
|
||||||
|
self.buffer.clear();
|
||||||
|
|
||||||
|
if matches!(self.reader.read_line(&mut self.buffer), Ok(n) if n > 0) {
|
||||||
|
Some(self.buffer.trim_end_matches('\n'))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Line-based iterator/parser
|
/// Line-based iterator/parser
|
||||||
///
|
///
|
||||||
/// For each line of the input, attempt to parse it as the requested type. Iteration is stopped on
|
/// For each line of the input, attempt to parse it as the requested type. Iteration is stopped on
|
||||||
@@ -13,33 +40,23 @@ pub struct LineParser<'a, I>
|
|||||||
where
|
where
|
||||||
I: FromStr,
|
I: FromStr,
|
||||||
{
|
{
|
||||||
reader: BufReader<&'a mut dyn Read>,
|
iter: LineIter<'a>,
|
||||||
buffer: String,
|
|
||||||
_data: PhantomData<I>,
|
_data: PhantomData<I>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, I: FromStr> LineParser<'a, I> {
|
impl<'a, I: FromStr> LineParser<'a, I> {
|
||||||
pub fn new(input: &'a mut dyn Read) -> Self {
|
pub fn new(input: &'a mut dyn Read) -> Self {
|
||||||
Self {
|
Self {
|
||||||
reader: BufReader::new(input),
|
iter: LineIter::new(input),
|
||||||
buffer: String::new(),
|
|
||||||
_data: PhantomData,
|
_data: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_line(&mut self) -> Option<&str> {
|
|
||||||
self.buffer.clear();
|
|
||||||
|
|
||||||
self.reader.read_line(&mut self.buffer).ok()?;
|
|
||||||
|
|
||||||
Some(self.buffer.trim())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, I: FromStr> Iterator for LineParser<'a, I> {
|
impl<'a, I: FromStr> Iterator for LineParser<'a, I> {
|
||||||
type Item = I;
|
type Item = I;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
self.next_line()?.parse().ok()
|
self.iter.next()?.parse().ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::io::BufRead;
|
|
||||||
use std::io::BufReader;
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
|
use crate::common::LineIter;
|
||||||
|
|
||||||
enum Dir {
|
enum Dir {
|
||||||
Up,
|
Up,
|
||||||
Down,
|
Down,
|
||||||
@@ -9,13 +9,11 @@ enum Dir {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_input(input: &mut dyn Read) -> Vec<(Dir, i32)> {
|
fn parse_input(input: &mut dyn Read) -> Vec<(Dir, i32)> {
|
||||||
let mut reader = BufReader::new(input);
|
let mut reader = LineIter::new(input);
|
||||||
let mut buffer = String::new();
|
|
||||||
|
|
||||||
let mut moves = Vec::new();
|
let mut moves = Vec::new();
|
||||||
|
|
||||||
while matches!(reader.read_line(&mut buffer), Ok(n) if n > 0) {
|
while let Some(line) = reader.next() {
|
||||||
let (dir, amount) = buffer.trim().split_once(" ").unwrap();
|
let (dir, amount) = line.split_once(" ").unwrap();
|
||||||
|
|
||||||
let dir = match dir {
|
let dir = match dir {
|
||||||
"up" => Dir::Up,
|
"up" => Dir::Up,
|
||||||
@@ -25,8 +23,6 @@ fn parse_input(input: &mut dyn Read) -> Vec<(Dir, i32)> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
moves.push((dir, amount.parse().unwrap()));
|
moves.push((dir, amount.parse().unwrap()));
|
||||||
|
|
||||||
buffer.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
moves
|
moves
|
||||||
|
|||||||
Reference in New Issue
Block a user