12#include "../StringTools.hpp"
35CLI11_INLINE
bool isalpha(
const std::string &str) {
36 return std::all_of(str.begin(), str.end(), [](
char c) { return std::isalpha(c, std::locale()); });
39CLI11_INLINE std::string to_lower(std::string str) {
40 std::transform(std::begin(str), std::end(str), std::begin(str), [](
const std::string::value_type &x) {
41 return std::tolower(x, std::locale());
46CLI11_INLINE std::vector<std::string> split(
const std::string &s,
char delim) {
47 std::vector<std::string> elems;
53 std::size_t start = 0;
56 while((end = s.find(delim, start)) != std::string::npos) {
57 elems.push_back(s.substr(start, end - start));
60 elems.push_back(s.substr(start));
64CLI11_INLINE std::string <rim(std::string &str) {
65 auto it = std::find_if(str.begin(), str.end(), [](
char ch) { return !std::isspace<char>(ch, std::locale()); });
66 str.erase(str.begin(), it);
70CLI11_INLINE std::string <rim(std::string &str,
const std::string &filter) {
71 auto it = std::find_if(str.begin(), str.end(), [&filter](
char ch) { return filter.find(ch) == std::string::npos; });
72 str.erase(str.begin(), it);
76CLI11_INLINE std::string &rtrim(std::string &str) {
77 auto it = std::find_if(str.rbegin(), str.rend(), [](
char ch) { return !std::isspace<char>(ch, std::locale()); });
78 str.erase(it.base(), str.end());
82CLI11_INLINE std::string &rtrim(std::string &str,
const std::string &filter) {
84 std::find_if(str.rbegin(), str.rend(), [&filter](
char ch) { return filter.find(ch) == std::string::npos; });
85 str.erase(it.base(), str.end());
89CLI11_INLINE std::string &remove_quotes(std::string &str) {
90 if(!str.empty() && (str.front() ==
'"' || str.front() ==
'\'' || str.front() ==
'`')) {
91 remove_outer(str, str.front());
96CLI11_INLINE std::string &remove_outer(std::string &str,
char key) {
97 if(str.length() > 1 && (str.front() == key)) {
98 if(str.front() == str.back()) {
100 str.erase(str.begin(), str.begin() + 1);
106CLI11_INLINE std::string fix_newlines(
const std::string &leader, std::string input) {
107 std::string::size_type n = 0;
108 while(n != std::string::npos && n < input.size()) {
109 n = input.find_first_of(
"\r\n", n);
110 if(n != std::string::npos) {
111 input.insert(n + 1, leader);
112 n += leader.size() + 1;
118CLI11_INLINE std::ostream &format_aliases(std::ostream &out,
const std::vector<std::string> &aliases, std::size_t wid) {
119 if(!aliases.empty()) {
120 out << std::setw(static_cast<int>(wid)) <<
" aliases: ";
122 for(
const auto &alias : aliases) {
128 out << detail::fix_newlines(
" ", alias);
135CLI11_INLINE
bool valid_name_string(
const std::string &str) {
136 if(str.empty() || !valid_first_char(str[0])) {
140 for(
auto c = str.begin() + 1; c != e; ++c)
141 if(!valid_later_char(*c))
146CLI11_INLINE std::string get_group_separators() {
147 std::string separators{
"_'"};
148#if CLI11_HAS_RTTI != 0
149 char group_separator = std::use_facet<std::numpunct<char>>(std::locale()).thousands_sep();
150 separators.push_back(group_separator);
155CLI11_INLINE std::string find_and_replace(std::string str, std::string from, std::string to) {
157 std::size_t start_pos = 0;
159 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
160 str.replace(start_pos, from.length(), to);
161 start_pos += to.length();
167CLI11_INLINE
void remove_default_flag_values(std::string &flags) {
168 auto loc = flags.find_first_of(
'{', 2);
169 while(loc != std::string::npos) {
170 auto finish = flags.find_first_of(
"},", loc + 1);
171 if((finish != std::string::npos) && (flags[finish] ==
'}')) {
172 flags.erase(flags.begin() +
static_cast<std::ptrdiff_t
>(loc),
173 flags.begin() +
static_cast<std::ptrdiff_t
>(finish) + 1);
175 loc = flags.find_first_of(
'{', loc + 1);
177 flags.erase(std::remove(flags.begin(), flags.end(),
'!'), flags.end());
180CLI11_INLINE std::ptrdiff_t
181find_member(std::string name,
const std::vector<std::string> &names,
bool ignore_case,
bool ignore_underscore) {
182 auto it = std::end(names);
184 if(ignore_underscore) {
185 name = detail::to_lower(detail::remove_underscore(name));
186 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
187 return detail::to_lower(detail::remove_underscore(local_name)) == name;
190 name = detail::to_lower(name);
191 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
192 return detail::to_lower(local_name) == name;
196 }
else if(ignore_underscore) {
197 name = detail::remove_underscore(name);
198 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
199 return detail::remove_underscore(local_name) == name;
202 it = std::find(std::begin(names), std::end(names), name);
205 return (it != std::end(names)) ? (it - std::begin(names)) : (-1);
208CLI11_MODULE_INLINE
const std::string &escapedChars() {
209 static const std::string s{
"\b\t\n\f\r\"\\"};
212CLI11_MODULE_INLINE
const std::string &escapedCharsCode() {
213 static const std::string s{
"btnfr\"\\"};
216CLI11_MODULE_INLINE
const std::string &bracketChars() {
217 static const std::string s{
"\"'`[(<{"};
220CLI11_MODULE_INLINE
const std::string &matchBracketChars() {
221 static const std::string s{
"\"'`])>}"};
225CLI11_INLINE
bool has_escapable_character(
const std::string &str) {
226 return (str.find_first_of(escapedChars()) != std::string::npos);
229CLI11_INLINE std::string add_escaped_characters(
const std::string &str) {
231 out.reserve(str.size() + 4);
233 auto sloc = escapedChars().find_first_of(s);
234 if(sloc != std::string::npos) {
236 out.push_back(escapedCharsCode()[sloc]);
244CLI11_INLINE std::uint32_t hexConvert(
char hc) {
246 if(hc >=
'0' && hc <=
'9') {
248 }
else if(hc >=
'A' && hc <=
'F') {
249 hcode = (hc -
'A' + 10);
250 }
else if(hc >=
'a' && hc <=
'f') {
251 hcode = (hc -
'a' + 10);
255 return static_cast<uint32_t
>(hcode);
258CLI11_INLINE
char make_char(std::uint32_t code) {
return static_cast<char>(
static_cast<unsigned char>(code)); }
260CLI11_INLINE
void append_codepoint(std::string &str, std::uint32_t code) {
262 str.push_back(
static_cast<char>(code));
263 }
else if(code < 0x800) {
265 str.push_back(make_char(0xC0 | code >> 6));
266 str.push_back(make_char(0x80 | (code & 0x3F)));
267 }
else if(code < 0x10000) {
268 if(0xD800 <= code && code <= 0xDFFF) {
269 throw std::invalid_argument(
"[0xD800, 0xDFFF] are not valid code points.");
272 str.push_back(make_char(0xE0 | code >> 12));
273 str.push_back(make_char(0x80 | (code >> 6 & 0x3F)));
274 str.push_back(make_char(0x80 | (code & 0x3F)));
275 }
else if(code < 0x110000) {
277 str.push_back(make_char(0xF0 | code >> 18));
278 str.push_back(make_char(0x80 | (code >> 12 & 0x3F)));
279 str.push_back(make_char(0x80 | (code >> 6 & 0x3F)));
280 str.push_back(make_char(0x80 | (code & 0x3F)));
282 throw std::invalid_argument(
"values above 0x10FFFF are not valid code points.");
286CLI11_INLINE std::string remove_escaped_characters(
const std::string &str) {
289 out.reserve(str.size());
290 for(
auto loc = str.begin(); loc < str.end(); ++loc) {
292 if(str.end() - loc < 2) {
293 throw std::invalid_argument(
"invalid escape sequence " + str);
295 auto ecloc = escapedCharsCode().find_first_of(*(loc + 1));
296 if(ecloc != std::string::npos) {
297 out.push_back(escapedChars()[ecloc]);
299 }
else if(*(loc + 1) ==
'u') {
301 if(str.end() - loc < 6) {
302 throw std::invalid_argument(
"unicode sequence must have 4 hex codes " + str);
304 std::uint32_t code{0};
305 std::uint32_t mplier{16 * 16 * 16};
306 for(
int ii = 2; ii < 6; ++ii) {
307 std::uint32_t res = hexConvert(*(loc + ii));
309 throw std::invalid_argument(
"unicode sequence must have 4 hex codes " + str);
311 code += res * mplier;
312 mplier = mplier / 16;
314 append_codepoint(out, code);
316 }
else if(*(loc + 1) ==
'U') {
318 if(str.end() - loc < 10) {
319 throw std::invalid_argument(
"unicode sequence must have 8 hex codes " + str);
321 std::uint32_t code{0};
322 std::uint32_t mplier{16 * 16 * 16 * 16 * 16 * 16 * 16};
323 for(
int ii = 2; ii < 10; ++ii) {
324 std::uint32_t res = hexConvert(*(loc + ii));
326 throw std::invalid_argument(
"unicode sequence must have 8 hex codes " + str);
328 code += res * mplier;
329 mplier = mplier / 16;
331 append_codepoint(out, code);
333 }
else if(*(loc + 1) ==
'0') {
337 throw std::invalid_argument(std::string(
"unrecognized escape sequence \\") + *(loc + 1) +
" in " + str);
346CLI11_INLINE std::size_t close_string_quote(
const std::string &str, std::size_t start,
char closure_char) {
348 for(loc = start + 1; loc < str.size(); ++loc) {
349 if(str[loc] == closure_char) {
352 if(str[loc] ==
'\\') {
360CLI11_INLINE std::size_t close_literal_quote(
const std::string &str, std::size_t start,
char closure_char) {
361 auto loc = str.find_first_of(closure_char, start + 1);
362 return (loc != std::string::npos ? loc : str.size());
365CLI11_INLINE std::size_t close_sequence(
const std::string &str, std::size_t start,
char closure_char) {
367 auto bracket_loc = matchBracketChars().find(closure_char);
368 switch(bracket_loc) {
370 return close_string_quote(str, start, closure_char);
373#if defined(_MSC_VER) && _MSC_VER < 1920
376 case std::string::npos:
378 return close_literal_quote(str, start, closure_char);
383 std::string closures(1, closure_char);
384 auto loc = start + 1;
386 while(loc < str.size()) {
387 if(str[loc] == closures.back()) {
389 if(closures.empty()) {
393 bracket_loc = bracketChars().find(str[loc]);
394 if(bracket_loc != std::string::npos) {
395 switch(bracket_loc) {
397 loc = close_string_quote(str, loc, str[loc]);
401 loc = close_literal_quote(str, loc, str[loc]);
404 closures.push_back(matchBracketChars()[bracket_loc]);
410 if(loc > str.size()) {
416CLI11_INLINE std::vector<std::string> split_up(std::string str,
char delimiter) {
418 auto find_ws = [delimiter](
char ch) {
419 return (delimiter ==
'\0') ? std::isspace<char>(ch, std::locale()) : (ch == delimiter);
423 std::vector<std::string> output;
424 while(!str.empty()) {
425 if(bracketChars().find_first_of(str[0]) != std::string::npos) {
426 auto bracketLoc = bracketChars().find_first_of(str[0]);
427 auto end = close_sequence(str, 0, matchBracketChars()[bracketLoc]);
428 if(end >= str.size()) {
429 output.push_back(std::move(str));
432 output.push_back(str.substr(0, end + 1));
439 char follow = str[end + 1];
440 bool skip_follow = find_ws(follow) || (bracketChars().find_first_of(follow) != std::string::npos);
441 auto next = skip_follow ? end + 2 : end + 1;
442 if(next < str.size()) {
443 str = str.substr(next);
450 auto it = std::find_if(std::begin(str), std::end(str), find_ws);
451 if(it != std::end(str)) {
452 std::string value = std::string(str.begin(), it);
453 output.push_back(value);
454 str = std::string(it + 1, str.end());
456 output.push_back(str);
465CLI11_INLINE std::size_t escape_detect(std::string &str, std::size_t offset) {
466 auto next = str[offset + 1];
467 if((next ==
'\"') || (next ==
'\'') || (next ==
'`')) {
472 auto astart = str.find_last_of(
"-/ \"\'`", offset - 1);
473 if(astart != std::string::npos) {
474 if(str[astart] == ((str[offset] ==
'=') ?
'-' :
'/'))
481CLI11_INLINE std::string binary_escape_string(
const std::string &string_to_escape,
bool force) {
483 std::string escaped_string{};
485 for(
char c : string_to_escape) {
488 if(isprint(
static_cast<unsigned char>(c)) == 0) {
489 std::stringstream stream;
493 stream << std::hex << static_cast<unsigned int>(
static_cast<unsigned char>(c));
494 std::string code = stream.str();
495 escaped_string += std::string(
"\\x") + (code.size() < 2 ?
"0" :
"") + code;
496 }
else if(c ==
'x' || c ==
'X') {
498 if(!escaped_string.empty() && escaped_string.back() ==
'\\') {
499 escaped_string += std::string(
"\\x") + (c ==
'x' ?
"78" :
"58");
501 escaped_string.push_back(c);
505 escaped_string.push_back(c);
508 if(escaped_string != string_to_escape || force) {
509 auto sqLoc = escaped_string.find(
'\'');
510 while(sqLoc != std::string::npos) {
511 escaped_string[sqLoc] =
'\\';
512 escaped_string.insert(sqLoc + 1,
"x27");
513 sqLoc = escaped_string.find(
'\'', sqLoc + 4);
515 escaped_string.insert(0,
"'B\"(");
516 escaped_string.push_back(
')');
517 escaped_string.push_back(
'"');
518 escaped_string.push_back(
'\'');
520 return escaped_string;
523CLI11_INLINE
bool is_binary_escaped_string(
const std::string &escaped_string) {
524 size_t ssize = escaped_string.size();
525 if(escaped_string.compare(0, 3,
"B\"(") == 0 && escaped_string.compare(ssize - 2, 2,
")\"") == 0) {
528 return (escaped_string.compare(0, 4,
"'B\"(") == 0 && escaped_string.compare(ssize - 3, 3,
")\"'") == 0);
531CLI11_INLINE std::string extract_binary_string(
const std::string &escaped_string) {
532 std::size_t start{0};
534 size_t ssize = escaped_string.size();
535 if(escaped_string.compare(0, 3,
"B\"(") == 0 && escaped_string.compare(ssize - 2, 2,
")\"") == 0) {
538 }
else if(escaped_string.compare(0, 4,
"'B\"(") == 0 && escaped_string.compare(ssize - 3, 3,
")\"'") == 0) {
544 return escaped_string;
546 std::string outstring;
548 outstring.reserve(ssize - start - tail);
549 std::size_t loc = start;
550 while(loc < ssize - tail) {
552 if(escaped_string[loc] ==
'\\' && (escaped_string[loc + 1] ==
'x' || escaped_string[loc + 1] ==
'X')) {
553 auto c1 = escaped_string[loc + 2];
554 auto c2 = escaped_string[loc + 3];
556 std::uint32_t res1 = hexConvert(c1);
557 std::uint32_t res2 = hexConvert(c2);
558 if(res1 <= 0x0F && res2 <= 0x0F) {
560 outstring.push_back(
static_cast<char>(res1 * 16 + res2));
564 outstring.push_back(escaped_string[loc]);
570CLI11_INLINE
void remove_quotes(std::vector<std::string> &args) {
571 for(
auto &arg : args) {
575 if(arg.front() ==
'\"' && arg.back() ==
'\"') {
578 arg = remove_escaped_characters(arg);
585CLI11_INLINE
void handle_secondary_array(std::string &str) {
586 if(str.size() >= 2 && str.front() ==
'[' && str.back() ==
']') {
588 std::string tstr{
"[["};
589 for(std::size_t ii = 1; ii < str.size(); ++ii) {
590 tstr.push_back(str[ii]);
591 tstr.push_back(str[ii]);
593 str = std::move(tstr);
598process_quoted_string(std::string &str,
char string_char,
char literal_char,
bool disable_secondary_array_processing) {
599 if(str.size() <= 1) {
602 if(detail::is_binary_escaped_string(str)) {
603 str = detail::extract_binary_string(str);
604 if(!disable_secondary_array_processing)
605 handle_secondary_array(str);
608 if(str.front() == string_char && str.back() == string_char) {
609 detail::remove_outer(str, string_char);
610 if(str.find_first_of(
'\\') != std::string::npos) {
611 str = detail::remove_escaped_characters(str);
613 if(!disable_secondary_array_processing)
614 handle_secondary_array(str);
617 if((str.front() == literal_char || str.front() ==
'`') && str.back() == str.front()) {
618 detail::remove_outer(str, str.front());
619 if(!disable_secondary_array_processing)
620 handle_secondary_array(str);
626CLI11_INLINE std::string get_environment_value(
const std::string &env_name) {
627 std::string ename_string;
631 char *buffer =
nullptr;
633 if(_dupenv_s(&buffer, &sz, env_name.c_str()) == 0 && buffer !=
nullptr) {
634 ename_string = std::string(buffer);
641 const char *buffer =
nullptr;
642 buffer = std::getenv(env_name.c_str());
643 if(buffer !=
nullptr) {
644 ename_string = std::string(buffer);
650CLI11_INLINE std::ostream &streamOutAsParagraph(std::ostream &out,
651 const std::string &text,
652 std::size_t paragraphWidth,
653 const std::string &linePrefix,
654 bool skipPrefixOnFirstLine) {
655 std::istringstream lss(text);
656 std::string line =
"";
657 while(std::getline(lss, line)) {
658 std::istringstream iss(line);
659 std::string word =
"";
660 std::size_t charsWritten = 0;
662 if(!skipPrefixOnFirstLine)
664 skipPrefixOnFirstLine =
false;
667 if(charsWritten > 0 && (word.length() + 1 + charsWritten > paragraphWidth)) {
668 out <<
'\n' << linePrefix;
671 if(charsWritten == 0) {
673 charsWritten += word.length();
676 charsWritten += word.length() + 1;