Created
February 13, 2023 19:20
-
-
Save nathanbabcock/a7ed7bf7404e443d388baf491900e96c to your computer and use it in GitHub Desktop.
Rust comma iterator (w/ parenthesis support)
This file contains 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
use std::str::Chars; | |
/// An iterator over comma-separated values, **ignoring commas inside parentheses**. | |
/// | |
/// ## Examples | |
/// | |
/// ```rust | |
/// use ffmpeg_sidecar::util::CommaIter; | |
/// | |
/// let string = "foo(bar,baz),quux"; | |
/// let mut iter = CommaIter::new(string); | |
/// | |
/// assert_eq!(iter.next(), Some("foo(bar,baz)")); | |
/// assert_eq!(iter.next(), Some("quux")); | |
/// assert_eq!(iter.next(), None); | |
/// ``` | |
pub struct CommaIter<'a> { | |
chars: Chars<'a>, | |
} | |
impl<'a> CommaIter<'a> { | |
pub fn new(string: &'a str) -> Self { | |
Self { | |
chars: string.chars(), | |
} | |
} | |
} | |
impl<'a> Iterator for CommaIter<'a> { | |
type Item = &'a str; | |
/// Return the next comma-separated section, not including the comma. | |
fn next(&mut self) -> Option<Self::Item> { | |
let chars_clone = self.chars.clone(); | |
let mut i = 0; | |
while let Some(char) = self.chars.next() { | |
match char { | |
'(' => { | |
// advance until closing paren (only handles one level nesting) | |
while let Some(close_paren) = self.chars.next() { | |
i += 1; | |
if close_paren == ')' { | |
break; | |
} | |
} | |
} | |
',' => break, | |
_ => {} | |
} | |
i += 1; | |
} | |
match i { | |
0 => None, | |
_ => Some(&chars_clone.as_str()[..i]), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment