Last active
August 29, 2015 14:11
-
-
Save pzol/3802d216270b55c795df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct StrSplits<'a> { | |
buf: &'a str, | |
finished: bool, | |
sep: char | |
} | |
impl<'a> StrSplits<'a> { | |
pub fn new(buf: &'a str) -> StrSplits<'a> { | |
StrSplits { buf: buf, finished: false, sep: '|' } | |
} | |
pub fn next(&mut self) -> Option<&'a str> { | |
if self.finished { return None }; | |
match self.buf.find(self.sep) { | |
Some(idx) => { | |
let current = Some(self.buf.slice_to(idx)); | |
self.buf = self.buf.slice_from(idx + 1); | |
current | |
}, | |
None => { | |
if self.finished { | |
None | |
} else { | |
self.finished = true; | |
Some(self.buf) | |
} | |
} | |
} | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::StrSplits; | |
#[test] | |
fn name() { | |
let ss = "foo|bar|baz".into_string(); | |
let mut slices : Vec<&str> = vec![]; | |
let mut splits = StrSplits::new(ss.as_slice()); | |
while let Some(field) = splits.next() { | |
slices.push(field); | |
} | |
panic!("slices {}", slices); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment