1pub fn smart_split(text: String) -> Vec<String> {
2 let words = text.split_whitespace();
3 let mut result = Vec::new();
4 let mut word_finished = true;
5 for word in words {
6 if word_finished {
7 let count = word.matches('"').count();
8 let count2 = word.matches("'").count();
9 let count3 = word.matches("[").count() as isize - word.matches("]").count() as isize;
10 let count4 = word.matches("{").count() as isize - word.matches("}").count() as isize;
11 if count % 2 != 0 || count2 % 2 != 0 || count3 != 0 || count4 != 0 {
12 word_finished = false;
13 }
14 result.push(word.to_string());
15 } else {
16 if let Some(last) = result.last_mut() {
17 last.push_str(" ");
18 last.push_str(word);
19 let count = last.matches('"').count();
20 let count2 = last.matches("'").count();
21 let count3 =
22 last.matches("[").count() as isize - last.matches("]").count() as isize;
23 let count4 =
24 word.matches("{").count() as isize - word.matches("}").count() as isize;
25
26 if count % 2 != 0 && count2 % 2 != 0 && count3 == 0 && count4 == 0 {
27 word_finished = true;
28 }
29 }
30 }
31 }
32 return result;
33}
34
35#[cfg(test)]
36#[test]
37fn test_smart_split() {
38 let v = smart_split("text".to_string());
39 assert_eq!(v.len(), 1);
40 let v = smart_split("text word1 word2".to_string());
41 assert_eq!(v.len(), 3);
42 let v = smart_split("text 'word1 word2'".to_string());
43 assert_eq!(v.len(), 2);
44 assert_eq!(v.last().unwrap(), "'word1 word2'");
45 let v = smart_split(r#"text 'word1 "word2"'"#.to_string());
46 assert_eq!(v.len(), 2);
47 assert_eq!(v.last().unwrap(), r#"'word1 "word2"'"#);
48}