|
|
@ -11,6 +11,7 @@ pub struct InputReader {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl InputReader {
|
|
|
|
impl InputReader {
|
|
|
|
|
|
|
|
/// Creates a new Input Reader
|
|
|
|
pub fn new<T: AsyncBufRead + Unpin + Send + 'static>(inner: T) -> Self {
|
|
|
|
pub fn new<T: AsyncBufRead + Unpin + Send + 'static>(inner: T) -> Self {
|
|
|
|
Self {
|
|
|
|
Self {
|
|
|
|
inner: Box::new(inner),
|
|
|
|
inner: Box::new(inner),
|
|
|
@ -22,13 +23,16 @@ impl InputReader {
|
|
|
|
/// Reads the next char consuming it in the process
|
|
|
|
/// Reads the next char consuming it in the process
|
|
|
|
#[inline]
|
|
|
|
#[inline]
|
|
|
|
pub async fn consume(&mut self) -> TapeResult<char> {
|
|
|
|
pub async fn consume(&mut self) -> TapeResult<char> {
|
|
|
|
self.read_next().await
|
|
|
|
self.seek_to(self.index + 1).await?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.read_current().await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the next char without forwarding
|
|
|
|
/// Returns the next char without forwarding
|
|
|
|
#[inline]
|
|
|
|
#[inline]
|
|
|
|
pub async fn peek(&mut self) -> TapeResult<char> {
|
|
|
|
pub async fn peek(&mut self) -> TapeResult<char> {
|
|
|
|
let char = self.read_next().await?;
|
|
|
|
self.seek_to(self.index + 1).await?;
|
|
|
|
|
|
|
|
let char = self.read_current().await?;
|
|
|
|
self.seek_to(self.index - 1).await?;
|
|
|
|
self.seek_to(self.index - 1).await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(char)
|
|
|
|
Ok(char)
|
|
|
@ -44,18 +48,10 @@ impl InputReader {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Reads the next char returning \x00 for EOF
|
|
|
|
/// Returns the previous char
|
|
|
|
async fn read_next(&mut self) -> TapeResult<char> {
|
|
|
|
#[inline]
|
|
|
|
self.seek_to(self.index + 1).await?;
|
|
|
|
pub async fn previous(&mut self) -> Option<char> {
|
|
|
|
let result = self
|
|
|
|
self.read_current().await.ok()
|
|
|
|
.buf
|
|
|
|
|
|
|
|
.get(self.index - 1..self.index)
|
|
|
|
|
|
|
|
.ok_or(TapeError::EOF)?
|
|
|
|
|
|
|
|
.chars()
|
|
|
|
|
|
|
|
.next()
|
|
|
|
|
|
|
|
.ok_or(TapeError::EOF);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Seeks to a given index
|
|
|
|
/// Seeks to a given index
|
|
|
@ -83,4 +79,20 @@ impl InputReader {
|
|
|
|
pub fn index(&self) -> usize {
|
|
|
|
pub fn index(&self) -> usize {
|
|
|
|
self.index
|
|
|
|
self.index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Reads the next char returning \x00 for EOF
|
|
|
|
|
|
|
|
async fn read_current(&mut self) -> TapeResult<char> {
|
|
|
|
|
|
|
|
if self.index == 0 {
|
|
|
|
|
|
|
|
return Err(TapeError::IndexError);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = self
|
|
|
|
|
|
|
|
.buf
|
|
|
|
|
|
|
|
.get(self.index - 1..self.index)
|
|
|
|
|
|
|
|
.ok_or(TapeError::EOF)?
|
|
|
|
|
|
|
|
.chars()
|
|
|
|
|
|
|
|
.next()
|
|
|
|
|
|
|
|
.ok_or(TapeError::EOF);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|