@@ -167,14 +167,14 @@ getenv_to_vec(const char *env_var_name) {
167
167
return std::nullopt ;
168
168
}
169
169
170
- auto is_quoted = [](std::string &str) {
171
- return (str.front () == ' \' ' && str.back () == ' \' ' ) ||
172
- (str.front () == ' "' && str.back () == ' "' );
170
+ auto is_quoted = [](const std::string &str) {
171
+ return str. size () >= 2 && ( (str.front () == ' \' ' && str.back () == ' \' ' ) ||
172
+ (str.front () == ' "' && str.back () == ' "' ) );
173
173
};
174
- auto has_colon = [](std::string &str) {
174
+ auto has_colon = [](const std::string &str) {
175
175
return str.find (' :' ) != std::string::npos;
176
176
};
177
- auto has_semicolon = [](std::string &str) {
177
+ auto has_semicolon = [](const std::string &str) {
178
178
return str.find (' ;' ) != std::string::npos;
179
179
};
180
180
@@ -188,8 +188,7 @@ getenv_to_vec(const char *env_var_name) {
188
188
}
189
189
190
190
if (is_quoted (value)) {
191
- value.erase (value.cbegin ());
192
- value.erase (value.cend () - 1 );
191
+ value = value.substr (1 , value.length () - 2 );
193
192
}
194
193
195
194
values_vec.push_back (value);
@@ -238,27 +237,29 @@ inline std::optional<EnvVarMap> getenv_to_map(const char *env_var_name,
238
237
return std::nullopt ;
239
238
}
240
239
241
- auto is_quoted = [](std::string &str) {
242
- return (str.front () == ' \' ' && str.back () == ' \' ' ) ||
243
- (str.front () == ' "' && str.back () == ' "' );
240
+ auto is_quoted = [](const std::string &str) {
241
+ return str. size () >= 2 && ( (str.front () == ' \' ' && str.back () == ' \' ' ) ||
242
+ (str.front () == ' "' && str.back () == ' "' ) );
244
243
};
245
- auto has_colon = [](std::string &str) {
244
+ auto has_colon = [](const std::string &str) {
246
245
return str.find (' :' ) != std::string::npos;
247
246
};
248
247
249
248
std::stringstream ss (*env_var);
250
249
std::string key_value;
251
250
while (std::getline (ss, key_value, main_delim)) {
252
- std::string key;
253
- std::string values;
254
- std::stringstream kv_ss (key_value);
255
-
256
251
if (reject_empty && !has_colon (key_value)) {
257
252
throw_wrong_format_map (env_var_name, *env_var);
258
253
}
259
254
260
- std::getline (kv_ss, key, key_value_delim);
261
- std::getline (kv_ss, values);
255
+ size_t colon_pos = key_value.find (key_value_delim);
256
+ if (colon_pos == std::string::npos) {
257
+ throw_wrong_format_map (env_var_name, *env_var);
258
+ }
259
+
260
+ std::string key = key_value.substr (0 , colon_pos);
261
+ std::string values = key_value.substr (colon_pos + 1 );
262
+
262
263
if (key.empty () || (reject_empty && values.empty ()) ||
263
264
(map.find (key) != map.end () && !allow_duplicate)) {
264
265
throw_wrong_format_map (env_var_name, *env_var);
@@ -269,18 +270,28 @@ inline std::optional<EnvVarMap> getenv_to_map(const char *env_var_name,
269
270
}
270
271
271
272
std::vector<std::string> values_vec;
272
- std::stringstream values_ss (values);
273
- std::string value;
274
- while (std::getline (values_ss, value, values_delim)) {
273
+
274
+ size_t start = 0 ;
275
+ size_t pos = 0 ;
276
+ while ((pos = values.find (values_delim, start)) != std::string::npos ||
277
+ start < values.length ()) {
278
+ // No delimiter found, process the last value
279
+ if (pos == std::string::npos) {
280
+ pos = values.length ();
281
+ }
282
+
283
+ std::string value = values.substr (start, pos - start);
275
284
if (value.empty () || (has_colon (value) && !is_quoted (value))) {
276
285
throw_wrong_format_map (env_var_name, *env_var);
277
286
}
278
287
if (is_quoted (value)) {
279
- value.erase (value.cbegin ());
280
- value.pop_back ();
288
+ value = value.substr (1 , value.length () - 2 );
281
289
}
282
- values_vec.push_back (value);
290
+ values_vec.push_back (std::move (value));
291
+
292
+ start = pos + 1 ;
283
293
}
294
+
284
295
if (map.find (key) != map.end ()) {
285
296
map[key].insert (map[key].end (), values_vec.begin (), values_vec.end ());
286
297
} else {
0 commit comments