Skip to content

Commit

Permalink
extend for sections
Browse files Browse the repository at this point in the history
  • Loading branch information
30bit committed Apr 17, 2024
1 parent 1bf1fd3 commit d323a19
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
56 changes: 28 additions & 28 deletions src/expr.rs
Expand Up @@ -53,7 +53,7 @@ fn inner_parse<S: State<T>, T>(
let range = expr_captures.name("f").unwrap().range();
let prev_text = src[prev_expr_end..range.start].trim();
if !prev_text.is_empty() {
output_buf.push(None, prev_text);
output_buf.push_str(None, prev_text);
}
prev_expr_end = range.end;

Expand All @@ -73,7 +73,7 @@ fn inner_parse<S: State<T>, T>(
}
let last_text = src[prev_expr_end..].trim();
if !last_text.is_empty() {
output_buf.push(None, last_text);
output_buf.push_str(None, last_text);
}
}

Expand Down Expand Up @@ -190,18 +190,28 @@ impl<T> Sections<T> {
}

#[inline]
pub fn push(&mut self, p: Option<T>, s: &str) {
pub fn push_str(&mut self, p: Option<T>, s: &str) {
self.push_fmt(p, format_args!("{s}"))
}

pub fn push_fmt(&mut self, p: Option<T>, s: fmt::Arguments) {
if !self.buf.is_empty() {
self.additional_offsets.push(self.buf.len() as u32);
}
self.buf.write_fmt(s).unwrap();
self.extend_fmt(s);
self.payload.push(p);
}

#[inline]
pub fn extend_str(&mut self, s: &str) {
self.extend_fmt(format_args!("{s}"));
}

#[inline]
pub fn extend_fmt(&mut self, s: fmt::Arguments) {
self.buf.write_fmt(s).unwrap();
}

pub fn truncate(&mut self, len: u32) {
self.payload.truncate(len as usize);
let buf_len = len.checked_sub(1).map_or(0, |i| {
Expand Down Expand Up @@ -229,18 +239,8 @@ pub struct Output<'a, T> {

impl<'a, T> Output<'a, T> {
#[inline]
pub fn len(&self) -> u32 {
self.buf.len()
}

#[inline]
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}

#[inline]
pub fn push(&mut self, p: Option<T>, s: &str) {
self.buf.push(p, s)
pub fn push_str(&mut self, p: Option<T>, s: &str) {
self.buf.push_str(p, s)
}

#[inline]
Expand Down Expand Up @@ -423,19 +423,19 @@ mod tests {
#[test]
fn sections() {
let mut s = Sections::<()>::new();
s.push(None, "1");
s.push_str(None, "1");
assert_eq!("1", s.buf);
assert_eq!(&[0u32; 0], &*s.additional_offsets);
s.push(None, "2");
s.push_str(None, "2");
assert_eq!("12", s.buf);
assert_eq!(&[1], &*s.additional_offsets);
s.push(None, "3");
s.push_str(None, "3");
assert_eq!("123", s.buf);
assert_eq!(&[1, 2], &*s.additional_offsets);
s.push(None, "4");
s.push_str(None, "4");
assert_eq!("1234", s.buf);
assert_eq!(&[1, 2, 3], &*s.additional_offsets);
s.push(None, "5");
s.push_str(None, "5");
assert_eq!("12345", s.buf);
assert_eq!(&[1, 2, 3, 4], &*s.additional_offsets);
s.truncate(2);
Expand All @@ -447,9 +447,9 @@ mod tests {
#[test]
fn args_mut() {
let mut s = Sections::<()>::new();
s.push(None, "1");
s.push(None, "2");
s.push(None, "3");
s.push_str(None, "1");
s.push_str(None, "2");
s.push_str(None, "3");
let mut d = Sections::<()>::new();
let mut o = vec![];
let mut args = ArgsMut {
Expand All @@ -460,14 +460,14 @@ mod tests {
assert_eq!(&[0], &**args.offsets);
assert_eq!("123", args.sections.buf);
assert_eq!(&[1, 2], &*args.sections.additional_offsets);
s.push(None, "4");
s.push(None, "5");
s.push_str(None, "4");
s.push_str(None, "5");
args.extract(&mut s, 0);
assert_eq!(&[0, 3], &**args.offsets);
assert_eq!("12345", args.sections.buf);
assert_eq!(&[1, 2, 3, 4], &*args.sections.additional_offsets);
s.push(None, "_");
s.push(None, "6");
s.push_str(None, "_");
s.push_str(None, "6");
args.extract(&mut s, 1);
assert_eq!(&[0, 3, 5], &**args.offsets);
assert_eq!("123456", args.sections.buf);
Expand Down
8 changes: 4 additions & 4 deletions tests/expr.rs
Expand Up @@ -12,11 +12,11 @@ fn to_string<'a>(sections: impl IntoIterator<Item = (Option<&'a ()>, &'a str)>)
impl expr::State<()> for State {
fn apply(&mut self, prompt: &str, args: expr::Args<()>, mut output: expr::Output<()>) {
match prompt {
"uppercase" => output.push(
"uppercase" => output.push_str(
None,
&to_string(args.iter().flat_map(|arg| arg.iter())).to_uppercase(),
),
"lowercase" => output.push(
"lowercase" => output.push_str(
None,
&to_string(args.iter().flat_map(|arg| arg.iter())).to_lowercase(),
),
Expand All @@ -36,9 +36,9 @@ impl expr::State<()> for State {
})
.collect();
s.pop();
output.push(None, &s)
output.push_str(None, &s)
}
"greet" => output.push(None, "Hello, world!"),
"greet" => output.push_str(None, "Hello, world!"),
_ => (),
}
}
Expand Down

0 comments on commit d323a19

Please sign in to comment.