Skip to content

Instantly share code, notes, and snippets.

@pzol
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pzol/3802d216270b55c795df to your computer and use it in GitHub Desktop.
Save pzol/3802d216270b55c795df to your computer and use it in GitHub Desktop.
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