Skip to content

Instantly share code, notes, and snippets.

@Songbird0
Created April 10, 2019 20:42
Show Gist options
  • Save Songbird0/8c7c0218b58a979cdc79a233beee213f to your computer and use it in GitHub Desktop.
Save Songbird0/8c7c0218b58a979cdc79a233beee213f to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate nom;
use std::fmt::Write;
fn get_all_a(input: &[u8]) -> nom::IResult<&[u8], Vec<&[u8]>, u32> {
let io: (&[u8], Vec<&[u8]>) = {
let mut a_list: Vec<&[u8]> = vec!();
let mut current_input: &[u8] = &b""[..];
let input_length = input.len();
let mut ptr: usize = 0;
while ptr < input_length {
let (i, o) = try_parse!(input,
tag!("a")
);
std::dbg!(i);
std::dbg!(o);
a_list.push(o);
current_input = i;
ptr += 1;
}
(current_input, a_list)
};
Ok(io)
}
fn main() {
let foo = &b"aaaaa"[..];
let (i, o) = get_all_a(foo)
.map_err(|e| {
match e {
nom::Err::Error(context) => {
match context {
nom::verbose_errors::Context::Code(i, e) => {
std::dbg!(i);
std::dbg!(e);
}
_ => unreachable!()
}
},
_ => unreachable!()
};
})
.unwrap();
std::dbg!(String::from_utf8_lossy(i));
let stringified_list: String = {
let iterator = o.iter();
let mut buffer: String = String::new();
for a in iterator {
write!(&mut buffer, "{}", String::from_utf8_lossy(a))
.expect("Something went wrong while buffering the iterator items");
}
buffer
};
assert_eq!(foo.len(), stringified_list.len());
std::dbg!(stringified_list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment