Skip to content

Commit

Permalink
Rename Buffer::trim_left and Buffer::trim_right
Browse files Browse the repository at this point in the history
Rename to trim_start and trim_end for consistency with String, and in
the spirit of their rename (not assuming RTL):
rust-lang/rust#30459
  • Loading branch information
sporksmith committed Apr 19, 2020
1 parent 72732f4 commit 0fb738e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
16 changes: 8 additions & 8 deletions src/parse_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ impl<'a> Buffer<'a> {
self.text.chars().next()
}

pub fn trim_left(&self) -> Buffer<'a> {
pub fn trim_start(&self) -> Buffer<'a> {
match self.text.find(|x: char| !x.is_whitespace()) {
Some(offset) => self.advance(offset),
None => self.advance(self.text.len()),
}
}

pub fn trim_right(&self) -> Buffer<'a> {
pub fn trim_end(&self) -> Buffer<'a> {
Buffer {
text: self.text.trim_end(),
..*self
}
}

pub fn trim(&self) -> Buffer<'a> {
self.trim_left().trim_right()
self.trim_start().trim_end()
}

pub fn space(&self) -> ParseSuccess<'a> {
let new_input = self.trim_left();
let new_input = self.trim_start();
if new_input == *self {
Err(self.expected("whitespace"))
} else {
Expand Down Expand Up @@ -211,15 +211,15 @@ mod test {
};

assert_eq!(
input.trim_left(),
input.trim_start(),
Buffer {
row: 1,
col: 4,
text: "Hello ",
}
);
assert_eq!(
input.trim_right(),
input.trim_end(),
Buffer {
row: 1,
col: 2,
Expand All @@ -236,8 +236,8 @@ mod test {
);

// Idempotent
assert_eq!(input.trim_left(), input.trim_left().trim_left());
assert_eq!(input.trim_right(), input.trim_right().trim_right());
assert_eq!(input.trim_start(), input.trim_start().trim_start());
assert_eq!(input.trim_end(), input.trim_end().trim_end());
assert_eq!(input.trim(), input.trim().trim());
}

Expand Down
22 changes: 11 additions & 11 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ fn parse_command_internal<'a>(buf: &Buffer<'a>) -> ParseResult<'a, Vec<String>>
while !buf.text.is_empty() {
let (new_buf, part) = parse_command_part(&buf)?;
output.push(part);
buf = new_buf.trim_left();
buf = new_buf.trim_start();
}
Ok((buf, output.into_iter().map(String::from).collect()))
}

fn parse_command_part<'a>(buf: &Buffer<'a>) -> ParseResult<'a, &'a str> {
let buf = buf.trim_left();
let buf = buf.trim_start();
match buf.peek() {
Some('\'') => buf.read_between('\'', '\''),
Some('"') => buf.read_between('"', '"'),
Expand Down Expand Up @@ -84,9 +84,9 @@ pub fn parse_config(input: &str) -> Result<Vec<FeedInfo>, ParseError> {

fn parse_line<'a>(buf: &Buffer<'a>) -> ParseResult<'a, FeedInfo> {
let (buf, name) = parse_name(buf)?;
let buf = buf.trim_left();
let buf = buf.trim_start();
let (buf, url) = parse_url(&buf)?;
let buf = buf.trim_left();
let buf = buf.trim_start();
let (buf, policies) = parse_policies(&buf)?;
Ok((
buf,
Expand All @@ -101,26 +101,26 @@ fn parse_line<'a>(buf: &Buffer<'a>) -> ParseResult<'a, FeedInfo> {
}

fn parse_name<'a>(buf: &Buffer<'a>) -> ParseResult<'a, &'a str> {
buf.trim_left().read_between('"', '"')
buf.trim_start().read_between('"', '"')
}

fn parse_url<'a>(buf: &Buffer<'a>) -> ParseResult<'a, &'a str> {
buf.trim_left().read_between('<', '>')
buf.trim_start().read_between('<', '>')
}

fn parse_policies<'a>(buf: &Buffer<'a>) -> ParseResult<'a, Vec<UpdateSpec>> {
let mut policies = Vec::new();
let mut buf = buf.trim_left();
let mut buf = buf.trim_start();
while buf.starts_with("@") {
let (inp, policy) = parse_policy(&buf)?;
policies.push(policy);
buf = inp.trim_left();
buf = inp.trim_start();
}
Ok((buf, policies))
}

fn parse_policy<'a>(buf: &Buffer<'a>) -> Result<(Buffer<'a>, UpdateSpec), ParseError> {
let buf = buf.trim_left().token("@")?.space()?;
let buf = buf.trim_start().token("@")?.space()?;

if buf.starts_with_no_case("on") {
let buf = buf.token_no_case("on")?.space()?;
Expand Down Expand Up @@ -185,7 +185,7 @@ fn parse_policy<'a>(buf: &Buffer<'a>) -> Result<(Buffer<'a>, UpdateSpec), ParseE
{
let (buf, count) = parse_number(&buf)?;
let buf = buf
.trim_left()
.trim_start()
.token_no_case("new")?
.space()?
.first_token_of_no_case(&["comics", "comic"])?
Expand All @@ -209,7 +209,7 @@ fn parse_policy<'a>(buf: &Buffer<'a>) -> Result<(Buffer<'a>, UpdateSpec), ParseE
}

fn parse_number<'a>(buf: &Buffer<'a>) -> ParseResult<'a, usize> {
let buf = buf.trim_left();
let buf = buf.trim_start();
let end = buf
.text
.find(|c: char| !c.is_digit(10))
Expand Down

0 comments on commit 0fb738e

Please sign in to comment.