Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Foo {
- v: Vec<u8>,
- }
- impl<'a> Foo {
- fn do_foo(&'a mut self) -> Result<Option<&'a [u8]>, ()> {
- loop {
- self.v.extend_from_slice(b" [ eu ] ");
- if get_section_name(&self.v).is_some() {
- break;
- }
- };
- Ok(get_section_name(&self.v))
- }
- }
- fn strip(buf: &[u8]) -> &[u8] {
- let is_blank = |ch: &&u8| (**ch as char).is_ascii_whitespace();
- let i = buf.iter().take_while(is_blank).count();
- let j = buf[i..].iter().rev().take_while(is_blank).count();
- &buf[i..buf.len() - j]
- }
- fn get_section_name(buf: &[u8]) -> std::option::Option<&[u8]> {
- let buf = strip(buf);
- (buf.len() > 2 && buf[0] == b'[' && buf[buf.len()-1] == b']').then(
- || strip(&buf[1..buf.len()-1]))
- }
- fn main() {
- let mut foo = Foo{v: Vec::new()};
- println!("{:?}", foo.do_foo());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement