use crate::{
Character,
TextInput,
};
use pups_core::Input;
use std::marker::PhantomData;
pub struct Text<'input> {
buffer: &'input str,
byte_offset: usize,
}
impl<'input> Text<'input> {
fn from_string(string: &'input str) -> Self {
Self { buffer: string, byte_offset: 0 }
}
}
impl<'input> Input for Text<'input> {
type Item = char;
fn advance(&mut self) {
if let Some (character) = self.peek() {
self.byte_offset += character.length();
}
}
fn cursor(&self) -> usize { self.byte_offset }
fn set_cursor(&mut self, cursor: usize) { self.byte_offset = cursor; }
fn next(&mut self) -> Option<Self::Item> {
if let Some (character) = self.peek() {
self.byte_offset += character.length();
Some (character)
} else { None }
}
fn peek(&self) -> Option<Self::Item> { char::next_in(&self.buffer[self.byte_offset..]) }
}
impl<'input> TextInput for Text<'input> {
fn starts_with(&self, string: &str) -> bool
{ self.buffer[self.byte_offset..].starts_with(string) }
fn skip_bytes(&mut self, byte_count: usize) { self.set_cursor(self.byte_offset + byte_count) }
}