CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
StringTools_inl.hpp
1// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
2// under NSF AWARD 1414736 and by the respective contributors.
3// All rights reserved.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6
7#pragma once
8
9// IWYU pragma: private, include "CLI/CLI.hpp"
10
11// This include is only needed for IDEs to discover symbols
12#include "../StringTools.hpp"
13
14// [CLI11:public_includes:set]
15#include <cstdint>
16#include <string>
17#include <utility>
18#include <vector>
19// [CLI11:public_includes:end]
20
21namespace CLI {
22// [CLI11:string_tools_inl_hpp:verbatim]
23
24namespace detail {
25CLI11_INLINE std::vector<std::string> split(const std::string &s, char delim) {
26 std::vector<std::string> elems;
27 // Check to see if empty string, give consistent result
28 if(s.empty()) {
29 elems.emplace_back();
30 } else {
31 std::stringstream ss;
32 ss.str(s);
33 std::string item;
34 while(std::getline(ss, item, delim)) {
35 elems.push_back(item);
36 }
37 }
38 return elems;
39}
40
41CLI11_INLINE std::string &ltrim(std::string &str) {
42 auto it = std::find_if(str.begin(), str.end(), [](char ch) { return !std::isspace<char>(ch, std::locale()); });
43 str.erase(str.begin(), it);
44 return str;
45}
46
47CLI11_INLINE std::string &ltrim(std::string &str, const std::string &filter) {
48 auto it = std::find_if(str.begin(), str.end(), [&filter](char ch) { return filter.find(ch) == std::string::npos; });
49 str.erase(str.begin(), it);
50 return str;
51}
52
53CLI11_INLINE std::string &rtrim(std::string &str) {
54 auto it = std::find_if(str.rbegin(), str.rend(), [](char ch) { return !std::isspace<char>(ch, std::locale()); });
55 str.erase(it.base(), str.end());
56 return str;
57}
58
59CLI11_INLINE std::string &rtrim(std::string &str, const std::string &filter) {
60 auto it =
61 std::find_if(str.rbegin(), str.rend(), [&filter](char ch) { return filter.find(ch) == std::string::npos; });
62 str.erase(it.base(), str.end());
63 return str;
64}
65
66CLI11_INLINE std::string &remove_quotes(std::string &str) {
67 if(str.length() > 1 && (str.front() == '"' || str.front() == '\'' || str.front() == '`')) {
68 if(str.front() == str.back()) {
69 str.pop_back();
70 str.erase(str.begin(), str.begin() + 1);
71 }
72 }
73 return str;
74}
75
76CLI11_INLINE std::string &remove_outer(std::string &str, char key) {
77 if(str.length() > 1 && (str.front() == key)) {
78 if(str.front() == str.back()) {
79 str.pop_back();
80 str.erase(str.begin(), str.begin() + 1);
81 }
82 }
83 return str;
84}
85
86CLI11_INLINE std::string fix_newlines(const std::string &leader, std::string input) {
87 std::string::size_type n = 0;
88 while(n != std::string::npos && n < input.size()) {
89 n = input.find('\n', n);
90 if(n != std::string::npos) {
91 input = input.substr(0, n + 1) + leader + input.substr(n + 1);
92 n += leader.size();
93 }
94 }
95 return input;
96}
97
98CLI11_INLINE std::ostream &format_aliases(std::ostream &out, const std::vector<std::string> &aliases, std::size_t wid) {
99 if(!aliases.empty()) {
100 out << std::setw(static_cast<int>(wid)) << " aliases: ";
101 bool front = true;
102 for(const auto &alias : aliases) {
103 if(!front) {
104 out << ", ";
105 } else {
106 front = false;
107 }
108 out << detail::fix_newlines(" ", alias);
109 }
110 out << "\n";
111 }
112 return out;
113}
114
115CLI11_INLINE bool valid_name_string(const std::string &str) {
116 if(str.empty() || !valid_first_char(str[0])) {
117 return false;
118 }
119 auto e = str.end();
120 for(auto c = str.begin() + 1; c != e; ++c)
121 if(!valid_later_char(*c))
122 return false;
123 return true;
124}
125
126CLI11_INLINE std::string find_and_replace(std::string str, std::string from, std::string to) {
127
128 std::size_t start_pos = 0;
129
130 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
131 str.replace(start_pos, from.length(), to);
132 start_pos += to.length();
133 }
134
135 return str;
136}
137
138CLI11_INLINE void remove_default_flag_values(std::string &flags) {
139 auto loc = flags.find_first_of('{', 2);
140 while(loc != std::string::npos) {
141 auto finish = flags.find_first_of("},", loc + 1);
142 if((finish != std::string::npos) && (flags[finish] == '}')) {
143 flags.erase(flags.begin() + static_cast<std::ptrdiff_t>(loc),
144 flags.begin() + static_cast<std::ptrdiff_t>(finish) + 1);
145 }
146 loc = flags.find_first_of('{', loc + 1);
147 }
148 flags.erase(std::remove(flags.begin(), flags.end(), '!'), flags.end());
149}
150
151CLI11_INLINE std::ptrdiff_t
152find_member(std::string name, const std::vector<std::string> names, bool ignore_case, bool ignore_underscore) {
153 auto it = std::end(names);
154 if(ignore_case) {
155 if(ignore_underscore) {
156 name = detail::to_lower(detail::remove_underscore(name));
157 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
158 return detail::to_lower(detail::remove_underscore(local_name)) == name;
159 });
160 } else {
161 name = detail::to_lower(name);
162 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
163 return detail::to_lower(local_name) == name;
164 });
165 }
166
167 } else if(ignore_underscore) {
168 name = detail::remove_underscore(name);
169 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
170 return detail::remove_underscore(local_name) == name;
171 });
172 } else {
173 it = std::find(std::begin(names), std::end(names), name);
174 }
175
176 return (it != std::end(names)) ? (it - std::begin(names)) : (-1);
177}
178
179static const std::string escapedChars("\b\t\n\f\r\"\\");
180static const std::string escapedCharsCode("btnfr\"\\");
181static const std::string bracketChars{"\"'`[(<{"};
182static const std::string matchBracketChars("\"'`])>}");
183
184CLI11_INLINE bool has_escapable_character(const std::string &str) {
185 return (str.find_first_of(escapedChars) != std::string::npos);
186}
187
188CLI11_INLINE std::string add_escaped_characters(const std::string &str) {
189 std::string out;
190 out.reserve(str.size() + 4);
191 for(char s : str) {
192 auto sloc = escapedChars.find_first_of(s);
193 if(sloc != std::string::npos) {
194 out.push_back('\\');
195 out.push_back(escapedCharsCode[sloc]);
196 } else {
197 out.push_back(s);
198 }
199 }
200 return out;
201}
202
203CLI11_INLINE std::uint32_t hexConvert(char hc) {
204 int hcode{0};
205 if(hc >= '0' && hc <= '9') {
206 hcode = (hc - '0');
207 } else if(hc >= 'A' && hc <= 'F') {
208 hcode = (hc - 'A' + 10);
209 } else if(hc >= 'a' && hc <= 'f') {
210 hcode = (hc - 'a' + 10);
211 } else {
212 hcode = -1;
213 }
214 return static_cast<uint32_t>(hcode);
215}
216
217CLI11_INLINE char make_char(std::uint32_t code) { return static_cast<char>(static_cast<unsigned char>(code)); }
218
219CLI11_INLINE void append_codepoint(std::string &str, std::uint32_t code) {
220 if(code < 0x80) { // ascii code equivalent
221 str.push_back(static_cast<char>(code));
222 } else if(code < 0x800) { // \u0080 to \u07FF
223 // 110yyyyx 10xxxxxx; 0x3f == 0b0011'1111
224 str.push_back(make_char(0xC0 | code >> 6));
225 str.push_back(make_char(0x80 | (code & 0x3F)));
226 } else if(code < 0x10000) { // U+0800...U+FFFF
227 if(0xD800 <= code && code <= 0xDFFF) {
228 throw std::invalid_argument("[0xD800, 0xDFFF] are not valid UTF-8.");
229 }
230 // 1110yyyy 10yxxxxx 10xxxxxx
231 str.push_back(make_char(0xE0 | code >> 12));
232 str.push_back(make_char(0x80 | (code >> 6 & 0x3F)));
233 str.push_back(make_char(0x80 | (code & 0x3F)));
234 } else if(code < 0x110000) { // U+010000 ... U+10FFFF
235 // 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
236 str.push_back(make_char(0xF0 | code >> 18));
237 str.push_back(make_char(0x80 | (code >> 12 & 0x3F)));
238 str.push_back(make_char(0x80 | (code >> 6 & 0x3F)));
239 str.push_back(make_char(0x80 | (code & 0x3F)));
240 }
241}
242
243CLI11_INLINE std::string remove_escaped_characters(const std::string &str) {
244
245 std::string out;
246 out.reserve(str.size());
247 for(auto loc = str.begin(); loc < str.end(); ++loc) {
248 if(*loc == '\\') {
249 if(str.end() - loc < 2) {
250 throw std::invalid_argument("invalid escape sequence " + str);
251 }
252 auto ecloc = escapedCharsCode.find_first_of(*(loc + 1));
253 if(ecloc != std::string::npos) {
254 out.push_back(escapedChars[ecloc]);
255 ++loc;
256 } else if(*(loc + 1) == 'u') {
257 // must have 4 hex characters
258 if(str.end() - loc < 6) {
259 throw std::invalid_argument("unicode sequence must have 4 hex codes " + str);
260 }
261 std::uint32_t code{0};
262 std::uint32_t mplier{16 * 16 * 16};
263 for(int ii = 2; ii < 6; ++ii) {
264 std::uint32_t res = hexConvert(*(loc + ii));
265 if(res > 0x0F) {
266 throw std::invalid_argument("unicode sequence must have 4 hex codes " + str);
267 }
268 code += res * mplier;
269 mplier = mplier / 16;
270 }
271 append_codepoint(out, code);
272 loc += 5;
273 } else if(*(loc + 1) == 'U') {
274 // must have 8 hex characters
275 if(str.end() - loc < 10) {
276 throw std::invalid_argument("unicode sequence must have 8 hex codes " + str);
277 }
278 std::uint32_t code{0};
279 std::uint32_t mplier{16 * 16 * 16 * 16 * 16 * 16 * 16};
280 for(int ii = 2; ii < 10; ++ii) {
281 std::uint32_t res = hexConvert(*(loc + ii));
282 if(res > 0x0F) {
283 throw std::invalid_argument("unicode sequence must have 8 hex codes " + str);
284 }
285 code += res * mplier;
286 mplier = mplier / 16;
287 }
288 append_codepoint(out, code);
289 loc += 9;
290 } else if(*(loc + 1) == '0') {
291 out.push_back('\0');
292 ++loc;
293 } else {
294 throw std::invalid_argument(std::string("unrecognized escape sequence \\") + *(loc + 1) + " in " + str);
295 }
296 } else {
297 out.push_back(*loc);
298 }
299 }
300 return out;
301}
302
303CLI11_INLINE std::size_t close_string_quote(const std::string &str, std::size_t start, char closure_char) {
304 std::size_t loc{0};
305 for(loc = start + 1; loc < str.size(); ++loc) {
306 if(str[loc] == closure_char) {
307 break;
308 }
309 if(str[loc] == '\\') {
310 // skip the next character for escaped sequences
311 ++loc;
312 }
313 }
314 return loc;
315}
316
317CLI11_INLINE std::size_t close_literal_quote(const std::string &str, std::size_t start, char closure_char) {
318 auto loc = str.find_first_of(closure_char, start + 1);
319 return (loc != std::string::npos ? loc : str.size());
320}
321
322CLI11_INLINE std::size_t close_sequence(const std::string &str, std::size_t start, char closure_char) {
323
324 auto bracket_loc = matchBracketChars.find(closure_char);
325 switch(bracket_loc) {
326 case 0:
327 return close_string_quote(str, start, closure_char);
328 case 1:
329 case 2:
330#if defined(_MSC_VER) && _MSC_VER < 1920
331 case(std::size_t)-1:
332#else
333 case std::string::npos:
334#endif
335 return close_literal_quote(str, start, closure_char);
336 default:
337 break;
338 }
339
340 std::string closures(1, closure_char);
341 auto loc = start + 1;
342
343 while(loc < str.size()) {
344 if(str[loc] == closures.back()) {
345 closures.pop_back();
346 if(closures.empty()) {
347 return loc;
348 }
349 }
350 bracket_loc = bracketChars.find(str[loc]);
351 if(bracket_loc != std::string::npos) {
352 switch(bracket_loc) {
353 case 0:
354 loc = close_string_quote(str, loc, str[loc]);
355 break;
356 case 1:
357 case 2:
358 loc = close_literal_quote(str, loc, str[loc]);
359 break;
360 default:
361 closures.push_back(matchBracketChars[bracket_loc]);
362 break;
363 }
364 }
365 ++loc;
366 }
367 if(loc > str.size()) {
368 loc = str.size();
369 }
370 return loc;
371}
372
373CLI11_INLINE std::vector<std::string> split_up(std::string str, char delimiter) {
374
375 auto find_ws = [delimiter](char ch) {
376 return (delimiter == '\0') ? std::isspace<char>(ch, std::locale()) : (ch == delimiter);
377 };
378 trim(str);
379
380 std::vector<std::string> output;
381 while(!str.empty()) {
382 if(bracketChars.find_first_of(str[0]) != std::string::npos) {
383 auto bracketLoc = bracketChars.find_first_of(str[0]);
384 auto end = close_sequence(str, 0, matchBracketChars[bracketLoc]);
385 if(end >= str.size()) {
386 output.push_back(std::move(str));
387 str.clear();
388 } else {
389 output.push_back(str.substr(0, end + 1));
390 if(end + 2 < str.size()) {
391 str = str.substr(end + 2);
392 } else {
393 str.clear();
394 }
395 }
396
397 } else {
398 auto it = std::find_if(std::begin(str), std::end(str), find_ws);
399 if(it != std::end(str)) {
400 std::string value = std::string(str.begin(), it);
401 output.push_back(value);
402 str = std::string(it + 1, str.end());
403 } else {
404 output.push_back(str);
405 str.clear();
406 }
407 }
408 trim(str);
409 }
410 return output;
411}
412
413CLI11_INLINE std::size_t escape_detect(std::string &str, std::size_t offset) {
414 auto next = str[offset + 1];
415 if((next == '\"') || (next == '\'') || (next == '`')) {
416 auto astart = str.find_last_of("-/ \"\'`", offset - 1);
417 if(astart != std::string::npos) {
418 if(str[astart] == ((str[offset] == '=') ? '-' : '/'))
419 str[offset] = ' '; // interpret this as a space so the split_up works properly
420 }
421 }
422 return offset + 1;
423}
424
425CLI11_INLINE std::string binary_escape_string(const std::string &string_to_escape) {
426 // s is our escaped output string
427 std::string escaped_string{};
428 // loop through all characters
429 for(char c : string_to_escape) {
430 // check if a given character is printable
431 // the cast is necessary to avoid undefined behaviour
432 if(isprint(static_cast<unsigned char>(c)) == 0) {
433 std::stringstream stream;
434 // if the character is not printable
435 // we'll convert it to a hex string using a stringstream
436 // note that since char is signed we have to cast it to unsigned first
437 stream << std::hex << static_cast<unsigned int>(static_cast<unsigned char>(c));
438 std::string code = stream.str();
439 escaped_string += std::string("\\x") + (code.size() < 2 ? "0" : "") + code;
440 } else if(c == 'x' || c == 'X') {
441 // need to check for inadvertent binary sequences
442 if(!escaped_string.empty() && escaped_string.back() == '\\') {
443 escaped_string += std::string("\\x") + (c == 'x' ? "78" : "58");
444 } else {
445 escaped_string.push_back(c);
446 }
447
448 } else {
449 escaped_string.push_back(c);
450 }
451 }
452 if(escaped_string != string_to_escape) {
453 auto sqLoc = escaped_string.find('\'');
454 while(sqLoc != std::string::npos) {
455 escaped_string[sqLoc] = '\\';
456 escaped_string.insert(sqLoc + 1, "x27");
457 sqLoc = escaped_string.find('\'');
458 }
459 escaped_string.insert(0, "'B\"(");
460 escaped_string.push_back(')');
461 escaped_string.push_back('"');
462 escaped_string.push_back('\'');
463 }
464 return escaped_string;
465}
466
467CLI11_INLINE bool is_binary_escaped_string(const std::string &escaped_string) {
468 size_t ssize = escaped_string.size();
469 if(escaped_string.compare(0, 3, "B\"(") == 0 && escaped_string.compare(ssize - 2, 2, ")\"") == 0) {
470 return true;
471 }
472 return (escaped_string.compare(0, 4, "'B\"(") == 0 && escaped_string.compare(ssize - 3, 3, ")\"'") == 0);
473}
474
475CLI11_INLINE std::string extract_binary_string(const std::string &escaped_string) {
476 std::size_t start{0};
477 std::size_t tail{0};
478 size_t ssize = escaped_string.size();
479 if(escaped_string.compare(0, 3, "B\"(") == 0 && escaped_string.compare(ssize - 2, 2, ")\"") == 0) {
480 start = 3;
481 tail = 2;
482 } else if(escaped_string.compare(0, 4, "'B\"(") == 0 && escaped_string.compare(ssize - 3, 3, ")\"'") == 0) {
483 start = 4;
484 tail = 3;
485 }
486
487 if(start == 0) {
488 return escaped_string;
489 }
490 std::string outstring;
491
492 outstring.reserve(ssize - start - tail);
493 std::size_t loc = start;
494 while(loc < ssize - tail) {
495 // ssize-2 to skip )" at the end
496 if(escaped_string[loc] == '\\' && (escaped_string[loc + 1] == 'x' || escaped_string[loc + 1] == 'X')) {
497 auto c1 = escaped_string[loc + 2];
498 auto c2 = escaped_string[loc + 3];
499
500 std::uint32_t res1 = hexConvert(c1);
501 std::uint32_t res2 = hexConvert(c2);
502 if(res1 <= 0x0F && res2 <= 0x0F) {
503 loc += 4;
504 outstring.push_back(static_cast<char>(res1 * 16 + res2));
505 continue;
506 }
507 }
508 outstring.push_back(escaped_string[loc]);
509 ++loc;
510 }
511 return outstring;
512}
513
514CLI11_INLINE void remove_quotes(std::vector<std::string> &args) {
515 for(auto &arg : args) {
516 if(arg.front() == '\"' && arg.back() == '\"') {
517 remove_quotes(arg);
518 // only remove escaped for string arguments not literal strings
519 arg = remove_escaped_characters(arg);
520 } else {
521 remove_quotes(arg);
522 }
523 }
524}
525
526CLI11_INLINE void handle_secondary_array(std::string &str) {
527 if(str.size() >= 2 && str.front() == '[' && str.back() == ']') {
528 // handle some special array processing for arguments if it might be interpreted as a secondary array
529 std::string tstr{"[["};
530 for(std::size_t ii = 1; ii < str.size(); ++ii) {
531 tstr.push_back(str[ii]);
532 tstr.push_back(str[ii]);
533 }
534 str = std::move(tstr);
535 }
536}
537
538CLI11_INLINE bool process_quoted_string(std::string &str, char string_char, char literal_char) {
539 if(str.size() <= 1) {
540 return false;
541 }
542 if(detail::is_binary_escaped_string(str)) {
543 str = detail::extract_binary_string(str);
544 handle_secondary_array(str);
545 return true;
546 }
547 if(str.front() == string_char && str.back() == string_char) {
548 detail::remove_outer(str, string_char);
549 if(str.find_first_of('\\') != std::string::npos) {
550 str = detail::remove_escaped_characters(str);
551 }
552 handle_secondary_array(str);
553 return true;
554 }
555 if((str.front() == literal_char || str.front() == '`') && str.back() == str.front()) {
556 detail::remove_outer(str, str.front());
557 handle_secondary_array(str);
558 return true;
559 }
560 return false;
561}
562
563std::string get_environment_value(const std::string &env_name) {
564 char *buffer = nullptr;
565 std::string ename_string;
566
567#ifdef _MSC_VER
568 // Windows version
569 std::size_t sz = 0;
570 if(_dupenv_s(&buffer, &sz, env_name.c_str()) == 0 && buffer != nullptr) {
571 ename_string = std::string(buffer);
572 free(buffer);
573 }
574#else
575 // This also works on Windows, but gives a warning
576 buffer = std::getenv(env_name.c_str());
577 if(buffer != nullptr) {
578 ename_string = std::string(buffer);
579 }
580#endif
581 return ename_string;
582}
583
584CLI11_INLINE std::ostream &streamOutAsParagraph(std::ostream &out,
585 const std::string &text,
586 std::size_t paragraphWidth,
587 const std::string &linePrefix,
588 bool skipPrefixOnFirstLine) {
589 if(!skipPrefixOnFirstLine)
590 out << linePrefix; // First line prefix
591
592 std::istringstream lss(text);
593 std::string line = "";
594 while(std::getline(lss, line)) {
595 std::istringstream iss(line);
596 std::string word = "";
597 std::size_t charsWritten = 0;
598
599 while(iss >> word) {
600 if(word.length() + charsWritten > paragraphWidth) {
601 out << '\n' << linePrefix;
602 charsWritten = 0;
603 }
604
605 out << word << " ";
606 charsWritten += word.length() + 1;
607 }
608
609 if(!lss.eof())
610 out << '\n' << linePrefix;
611 }
612 return out;
613}
614
615} // namespace detail
616// [CLI11:string_tools_inl_hpp:end]
617} // namespace CLI