pub struct Detector<'a> { /* private fields */ }
Expand description
Text reader detector
§Examples
use text_reader::TextReader;
use text_reader::Detector;
let text = r#""typeA""#;
let mut reader = TextReader::new(text);
let mut rets = Vec::new();
while reader.has_next() {
match reader.next() {
Some('"') => {
let mut detector = reader.detector();
rets.push('"');
if detector.next_text("type").yes() {
detector.rollback();
rets.push('t');
}
continue;
}
Some(ch) => {
rets.push(ch);
continue;
}
None => {}
}
}
let ret = rets.iter().collect::<String>();
println!("{}", ret); // "ttypeA"
Implementations§
Source§impl<'a> Detector<'a>
impl<'a> Detector<'a>
Sourcepub fn new(reader: &'a mut TextReader) -> Self
pub fn new(reader: &'a mut TextReader) -> Self
Sourcepub fn next_char(&mut self, ch: char) -> &mut Self
pub fn next_char(&mut self, ch: char) -> &mut Self
Detect next char
§Examples
use text_reader::TextReader;
let mut reader = TextReader::new("abc");
let mut detector = reader.detector();
detector.next_char('a');
Sourcepub fn next_text<S: AsRef<OsStr>>(&mut self, text: S) -> &mut Self
pub fn next_text<S: AsRef<OsStr>>(&mut self, text: S) -> &mut Self
Detect next string
§Examples
use text_reader::TextReader;
let mut reader = TextReader::new("abc");
let mut detector = reader.detector();
detector.next_text("ab");
Sourcepub fn yes(&mut self) -> bool
pub fn yes(&mut self) -> bool
Detect result is true
§Examples
use text_reader::TextReader;
let mut vec = Vec::new();
let mut reader = TextReader::new("abc");
while reader.has_next() {
match reader.next() {
Some(ch) => {
let mut detector = reader.detector();
if detector.next_text("bc").yes() {
println!("Detect ab");
}
vec.push(ch);
},
None => {}
}
}
println!("{}", vec.iter().collect::<String>()); // a
Sourcepub fn rollback(&mut self) -> &mut Self
pub fn rollback(&mut self) -> &mut Self
Rollback detector If detect success detector not back position. if want, use rollback function to reset reader position
§Examples
use text_reader::TextReader;
let mut vec = Vec::new();
let mut reader = TextReader::new("abc");
while reader.has_next() {
match reader.next() {
Some(ch) => {
let mut detector = reader.detector();
if detector.next_text("bc").yes() {
detector.rollback();
println!("Detect ab");
}
vec.push(ch);
},
None => {}
}
}
println!("{}", vec.iter().collect::<String>()); // abc
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Detector<'a>
impl<'a> RefUnwindSafe for Detector<'a>
impl<'a> Send for Detector<'a>
impl<'a> Sync for Detector<'a>
impl<'a> Unpin for Detector<'a>
impl<'a> !UnwindSafe for Detector<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more