Debian Codegraph @

src/utils/param.rs

raw symbols SCIP
1 pub struct ParamParser<'a> {
2 s: &'a str,
3 idx: usize,
4 }
5
6 impl<'a> ParamParser<'a> {
7 pub fn new(s: &'a str) -> Self {
8 Self { s, idx: 0 }
9 }
10
11 // Returns `&str` from current index until space, comma, or slash is found.
12 // Ignore space, comma, or slash inside parentheses.
13 // Returns `None` if value not found.
14 pub fn value(&mut self) -> Option<&'a str> {
15 if self.is_end() {
16 return None;
17 }
18
19 match self.s.as_bytes()[self.idx] {
20 b' ' => return None,
21 b',' => return None,
22 b'/' => return None,
23 _ => (),
24 }
25
26 let start = self.idx;
27
28 // parenthesis depth
29 let mut nesting = 0i32;
30
31 while self.idx < self.s.len() {
32 let ch = self.s.as_bytes()[self.idx];
33 match ch {
34 b'(' => {
35 nesting += 1;
36 self.idx += 1;
37 }
38 b')' => {
39 if nesting > 0 {
40 nesting -= 1;
41 }
42 self.idx += 1;
43 }
44 b' ' | b',' | b'/' => {
45 if nesting == 0 {
46 // delimiter is *outside* parentheses
47 break;
48 }
49 self.idx += 1;
50 }
51 _ => self.idx += 1,
52 }
53 }
54
55 Some(&self.s[start..self.idx])
56 }
57
58 // Consume one or more spaces.
59 // Returns true if space is found, false otherwise.
60 pub fn space(&mut self) -> bool {
61 let mut found = false;
62 while self.idx < self.s.len() && self.s.as_bytes()[self.idx] == b' ' {
63 self.idx += 1;
64 found = true;
65 }
66 found
67 }
68
69 // Consume one or more spaces, or single comma.
70 // Spaces is allowed around comma.
71 // Returns true if one of them is found, false otherwise.
72 pub fn comma_or_space(&mut self) -> bool {
73 let mut found_comma = false;
74 let mut found_space = false;
75
76 while self.idx < self.s.len() {
77 let ch = self.s.as_bytes()[self.idx];
78 match ch {
79 b' ' => {
80 found_space = true;
81 self.idx += 1;
82 }
83 b',' => {
84 if found_comma {
85 break;
86 }
87 found_comma = true;
88 self.idx += 1;
89 }
90 _ => {
91 break;
92 }
93 }
94 }
95
96 found_comma || found_space
97 }
98
99 // Consume single comma or single slash.
100 // Spaces is allowed around comma or slash.
101 // Returns true if one of them is found, false otherwise.
102 pub fn comma_or_slash(&mut self) -> bool {
103 let mut found = false;
104
105 while self.idx < self.s.len() {
106 let ch = self.s.as_bytes()[self.idx];
107 match ch {
108 b' ' => {
109 self.idx += 1;
110 }
111 b',' | b'/' => {
112 if found {
113 break;
114 }
115 found = true;
116 self.idx += 1;
117 }
118 _ => {
119 break;
120 }
121 }
122 }
123
124 found
125 }
126
127 // Consume a single slash. Spaces is allowed around slash.
128 // Returns true if a slash is found, false otherwise.
129 pub fn slash(&mut self) -> bool {
130 let mut found = false;
131
132 while self.idx < self.s.len() {
133 let ch = self.s.as_bytes()[self.idx];
134 match ch {
135 b' ' => {
136 self.idx += 1;
137 }
138 b'/' => {
139 if found {
140 break;
141 }
142 found = true;
143 self.idx += 1;
144 }
145 _ => {
146 break;
147 }
148 }
149 }
150
151 found
152 }
153
154 // Returns true is we finished reading the str.
155 pub fn is_end(&self) -> bool {
156 self.idx >= self.s.len()
157 }
158 }
159
160 #[cfg(test)]
161 mod t {
162 use super::*;
163
164 #[test]
165 fn param_parser() {
166 let s = " ";
167 let mut p = ParamParser::new(s);
168 assert_eq!(p.is_end(), false);
169 assert!(p.space());
170 assert_eq!(p.space(), false);
171 assert!(p.is_end());
172
173 let s = "abc ";
174 let mut p = ParamParser::new(s);
175 assert_eq!(p.space(), false);
176 assert_eq!(p.is_end(), false);
177 assert_eq!(p.value(), Some("abc"));
178 assert!(p.space());
179 assert!(p.is_end());
180
181 let s = ",, , ";
182 let mut p = ParamParser::new(s);
183 assert!(p.comma_or_space());
184 assert!(p.comma_or_space());
185 assert!(p.comma_or_space());
186 assert_eq!(p.comma_or_space(), false);
187 assert!(p.is_end());
188
189 let s = "97,ab/5 / 10.7 ";
190 let mut p = ParamParser::new(s);
191 assert_eq!(p.slash(), false);
192 assert_eq!(p.value(), Some("97"));
193 assert_eq!(p.slash(), false);
194 assert!(p.comma_or_space());
195 assert_eq!(p.value(), Some("ab"));
196 assert!(p.slash());
197 assert_eq!(p.value(), Some("5"));
198 assert!(p.slash());
199 assert_eq!(p.value(), Some("10.7"));
200 assert!(p.space());
201 assert!(p.is_end());
202
203 let s = " ab(1 2,3),45 , xy cd / 10";
204 let mut p = ParamParser::new(s);
205 assert_eq!(p.value(), None);
206 assert!(p.space());
207 assert_eq!(p.space(), false);
208
209 assert_eq!(p.value(), Some("ab(1 2,3)"));
210 assert!(p.comma_or_space());
211
212 assert_eq!(p.value(), Some("45"));
213 assert!(p.comma_or_space());
214 assert_eq!(p.value(), Some("xy"));
215 assert!(p.comma_or_space());
216 assert_eq!(p.value(), Some("cd"));
217 assert!(p.comma_or_slash());
218 assert_eq!(p.is_end(), false);
219 assert_eq!(p.value(), Some("10"));
220 assert_eq!(p.space(), false);
221 assert_eq!(p.value(), None);
222 assert!(p.is_end());
223
224 let s = "2.53/9,dog cat,fx(1 2 (56, 78))";
225 let mut p = ParamParser::new(s);
226 assert_eq!(p.value(), Some("2.53"));
227 assert!(p.comma_or_slash());
228 assert_eq!(p.value(), Some("9"));
229 assert!(p.comma_or_space());
230 assert_eq!(p.value(), Some("dog"));
231 assert!(p.space());
232 assert_eq!(p.value(), Some("cat"));
233 assert!(p.comma_or_slash());
234 assert_eq!(p.value(), Some("fx(1 2 (56, 78))"));
235 assert_eq!(p.comma_or_slash(), false);
236 assert!(p.is_end());
237
238 let s = ") ( (9)) (";
239 let mut p = ParamParser::new(s);
240 assert_eq!(p.value(), Some(")"));
241 assert!(p.space());
242 assert_eq!(p.value(), Some("( (9))"));
243 assert!(p.space());
244 assert_eq!(p.value(), Some("("));
245 assert!(p.is_end());
246 }
247 }