statsig_rust/evaluation/user_agent_parsing/statsig_uaparser/
window_iter.rs1use std::collections::VecDeque;
2
3pub struct WindowIter<'a> {
4 iter: std::str::Split<'a, [char; 2]>,
5 window: VecDeque<&'a str>,
6}
7
8type Window<'a> = (
9 Option<&'a str>,
10 Option<&'a str>,
11 Option<&'a str>,
12 Option<&'a str>,
13);
14
15impl<'a> WindowIter<'a> {
16 pub fn new(input: &'a str) -> Self {
17 let mut iter: std::str::Split<'_, [char; 2]> = input.split([';', ' ']);
18 let mut window = VecDeque::new();
19
20 for _ in 0..4 {
21 if let Some(word) = iter.next() {
22 window.push_back(word);
23 }
24 }
25
26 Self { iter, window }
27 }
28
29 #[allow(clippy::get_first)]
30 pub fn get_window(&self) -> Window<'a> {
31 (
32 self.window.get(0).copied(),
33 self.window.get(1).copied(),
34 self.window.get(2).copied(),
35 self.window.get(3).copied(),
36 )
37 }
38
39 pub fn slide_window_by(&mut self, n: usize) {
40 for _ in 0..n {
41 self.window.pop_front();
42 if let Some(word) = self.iter.next() {
43 self.window.push_back(word);
44 }
45 }
46 }
47
48 pub fn is_empty(&self) -> bool {
49 self.window.is_empty()
50 }
51}