MCPcopy Index your code
hub / github.com/cloud-hypervisor/cloud-hypervisor / split_commas

Function split_commas

option_parser/src/lib.rs:87–121  ·  view source on GitHub ↗
(s: &str)

Source from the content-addressed store, hash-verified

85type OptionParserResult<T> = std::result::Result<T, OptionParserError>;
86
87fn split_commas(s: &str) -> OptionParserResult<Vec<String>> {
88 let mut list: Vec<String> = Vec::new();
89 let mut opened_brackets = 0u64;
90 let mut in_quotes = false;
91 let mut current = String::new();
92
93 for c in s.trim().chars() {
94 match c {
95 // In quotes, only '"' is special
96 '"' => in_quotes = !in_quotes,
97 _ if in_quotes => {}
98 '[' => opened_brackets += 1,
99 ']' => {
100 if opened_brackets < 1 {
101 return Err(OptionParserError::InvalidSyntax(s.to_owned()));
102 }
103 opened_brackets -= 1;
104 }
105 ',' if opened_brackets == 0 => {
106 list.push(current);
107 current = String::new();
108 continue;
109 }
110 _ => {}
111 }
112 current.push(c);
113 }
114 list.push(current);
115
116 if in_quotes || opened_brackets != 0 {
117 return Err(OptionParserError::InvalidSyntax(s.to_owned()));
118 }
119
120 Ok(list)
121}
122
123impl OptionParser {
124 /// Creates an empty `OptionParser` with no registered options.

Callers 4

parse_innerMethod · 0.85
from_strMethod · 0.85

Calls 2

newFunction · 0.85
pushMethod · 0.45