pub struct Parser<'a> { /* private fields */ }
Expand description
A struct for gradually parsing data from a string from left to right.
Each time a method is called, the processed portion of the string is “consumed”, and future method calls will only consider the remainder of the string.
§Examples
use aoc::Parse;
let s = "move 10 from 271 to 3";
let mut parser = s.as_parser();
assert_eq!(parser.between(" ", " "), "10");
assert_eq!(parser.between(" ", " "), "271");
assert_eq!(parser.after(" "), "3");
Another way to do the same thing is:
use aoc::Parse;
let s = "move 10 from 271 to 3";
let mut parser = s.as_parser().skip(5);
assert_eq!(parser.before(" "), "10");
parser.skip(5);
assert_eq!(parser.before(" "), "271");
parser.skip(3);
assert_eq!(parser.rest(), "3");
Or alternatively:
use aoc::Parse;
let s = "move 10 from 271 to 3";
let mut parser = s.as_parser();
assert_eq!(parser.between("move ", " "), "10");
assert_eq!(parser.between("from ", " "), "271");
assert_eq!(parser.after("to "), "3");
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn skip(&mut self, n: usize) -> Self
pub fn skip(&mut self, n: usize) -> Self
Skips past the next n
bytes (ASCII characters) of the string.
Future method calls on self
will work on the remainder of the string.
Both mutates self
and returns a copy of self
after the mutation.
Panics if the string has less than n
bytes left.
§Examples
use aoc::Parse;
let s = "12345foo1234567bar";
let mut parser = s.as_parser().skip(5);
assert_eq!(parser.take(3), "foo");
parser.skip(7);
assert_eq!(parser.rest(), "bar");
Sourcepub fn take(&mut self, n: usize) -> &str
pub fn take(&mut self, n: usize) -> &str
Returns the next n
bytes (ASCII characters) of the string.
Future method calls on self
will then work on the remainder of the string.
Panics if the string has less than n
bytes left.
§Examples
use aoc::Parse;
let s = "12345foo1234567bar.";
let mut parser = s.as_parser().skip(5);
assert_eq!(parser.take(3), "foo");
parser.skip(7);
assert_eq!(parser.take(3), "bar");
Sourcepub fn rest(self) -> &'a str
pub fn rest(self) -> &'a str
Returns the remainder of the string, consuming self
.
§Examples
use aoc::Parse;
let s = "hello, world!";
let mut parser = s.as_parser();
parser.skip(7);
assert_eq!(parser.rest(), "world!");
Sourcepub fn before(&mut self, suffix: &str) -> &'a str
pub fn before(&mut self, suffix: &str) -> &'a str
Returns the slice of the string before the first occurrence of suffix
.
Future method calls on self
will then work on the remainder of the string after suffix
.
Panics if suffix
is not contained in the remainder of the string.
§Examples
use aoc::Parse;
let s = "move 10 from 271 to 3";
let mut parser = s.as_parser().skip(5);
assert_eq!(parser.before(" "), "10");
parser.skip(5);
assert_eq!(parser.before(" "), "271");
parser.skip(3);
assert_eq!(parser.rest(), "3");
Sourcepub fn after(self, prefix: &str) -> &'a str
pub fn after(self, prefix: &str) -> &'a str
Returns the slice of the string after the first occurrence of prefix
, consuming self
.
Panics if prefix
is not contained in the remainder of the string.
§Examples
use aoc::Parse;
let s = "move 10 from 271 to 3";
let mut parser = s.as_parser();
assert_eq!(parser.between(" ", " "), "10");
assert_eq!(parser.between(" ", " "), "271");
assert_eq!(parser.after(" "), "3");
Sourcepub fn between(&mut self, prefix: &str, suffix: &str) -> &'a str
pub fn between(&mut self, prefix: &str, suffix: &str) -> &'a str
Returns the slice of the string after the first occurrence of prefix
, and before the next occurrence of suffix
.
Future method calls on self
will then work on the remainder of the string after suffix
.
§Examples
use aoc::Parse;
let s = "move 10 from 271 to 3";
let mut parser = s.as_parser();
assert_eq!(parser.between("move ", " "), "10");
assert_eq!(parser.between("from ", " "), "271");
assert_eq!(parser.after("to "), "3");