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 if(input[n] ==
'\r' && n + 1 < input.size() && input[n + 1] ==
'\n') {
114 input.insert(n + 1, leader);
115 n += leader.size() + 1;
121CLI11_INLINE std::ostream &format_aliases(std::ostream &out,
const std::vector<std::string> &aliases, std::size_t wid) {
122 if(!aliases.empty()) {
123 out << std::setw(static_cast<int>(wid)) <<
" aliases: ";
125 for(
const auto &alias : aliases) {
131 out << detail::fix_newlines(
" ", alias);
138CLI11_INLINE
bool valid_name_string(
const std::string &str) {
139 if(str.empty() || !valid_first_char(str[0])) {
143 for(
auto c = str.begin() + 1; c != e; ++c)
144 if(!valid_later_char(*c))
149CLI11_INLINE std::string get_group_separators() {
150 std::string separators{
"_'"};
151#if CLI11_HAS_RTTI != 0
152 char group_separator = std::use_facet<std::numpunct<char>>(std::locale()).thousands_sep();
153 separators.push_back(group_separator);
158CLI11_INLINE std::string find_and_replace(std::string str, std::string from, std::string to) {
160 std::size_t start_pos = 0;
162 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
163 str.replace(start_pos, from.length(), to);
164 start_pos += to.length();
170CLI11_INLINE
void remove_default_flag_values(std::string &flags) {
171 auto loc = flags.find_first_of(
'{', 2);
172 while(loc != std::string::npos) {
173 auto finish = flags.find_first_of(
"},", loc + 1);
174 if((finish != std::string::npos) && (flags[finish] ==
'}')) {
175 flags.erase(flags.begin() +
static_cast<std::ptrdiff_t
>(loc),
176 flags.begin() +
static_cast<std::ptrdiff_t
>(finish) + 1);
178 loc = flags.find_first_of(
'{', loc + 1);
180 flags.erase(std::remove(flags.begin(), flags.end(),
'!'), flags.end());
183CLI11_INLINE std::ptrdiff_t
184find_member(std::string name,
const std::vector<std::string> &names,
bool ignore_case,
bool ignore_underscore) {
185 auto it = std::end(names);
187 if(ignore_underscore) {
188 name = detail::to_lower(detail::remove_underscore(name));
189 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
190 return detail::to_lower(detail::remove_underscore(local_name)) == name;
193 name = detail::to_lower(name);
194 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
195 return detail::to_lower(local_name) == name;
199 }
else if(ignore_underscore) {
200 name = detail::remove_underscore(name);
201 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
202 return detail::remove_underscore(local_name) == name;
205 it = std::find(std::begin(names), std::end(names), name);
208 return (it != std::end(names)) ? (it - std::begin(names)) : (-1);
211CLI11_MODULE_INLINE
const std::string &escapedChars() {
212 static const std::string s{
"\b\t\n\f\r\"\\"};
215CLI11_MODULE_INLINE
const std::string &escapedCharsCode() {
216 static const std::string s{
"btnfr\"\\"};
219CLI11_MODULE_INLINE
const std::string &bracketChars() {
220 static const std::string s{
"\"'`[(<{"};
223CLI11_MODULE_INLINE
const std::string &matchBracketChars() {
224 static const std::string s{
"\"'`])>}"};
228CLI11_INLINE
bool has_escapable_character(
const std::string &str) {
229 return (str.find_first_of(escapedChars()) != std::string::npos);
232CLI11_INLINE std::string add_escaped_characters(
const std::string &str) {
234 out.reserve(str.size() + 4);
236 auto sloc = escapedChars().find_first_of(s);
237 if(sloc != std::string::npos) {
239 out.push_back(escapedCharsCode()[sloc]);
247CLI11_INLINE std::uint32_t hexConvert(
char hc) {
249 if(hc >=
'0' && hc <=
'9') {
251 }
else if(hc >=
'A' && hc <=
'F') {
252 hcode = (hc -
'A' + 10);
253 }
else if(hc >=
'a' && hc <=
'f') {
254 hcode = (hc -
'a' + 10);
258 return static_cast<uint32_t
>(hcode);
261CLI11_INLINE
char make_char(std::uint32_t code) {
return static_cast<char>(
static_cast<unsigned char>(code)); }
263CLI11_INLINE
void append_codepoint(std::string &str, std::uint32_t code) {
265 str.push_back(
static_cast<char>(code));
266 }
else if(code < 0x800) {
268 str.push_back(make_char(0xC0 | code >> 6));
269 str.push_back(make_char(0x80 | (code & 0x3F)));
270 }
else if(code < 0x10000) {
271 if(0xD800 <= code && code <= 0xDFFF) {
272 throw std::invalid_argument(
"[0xD800, 0xDFFF] are not valid code points.");
275 str.push_back(make_char(0xE0 | code >> 12));
276 str.push_back(make_char(0x80 | (code >> 6 & 0x3F)));
277 str.push_back(make_char(0x80 | (code & 0x3F)));
278 }
else if(code < 0x110000) {
280 str.push_back(make_char(0xF0 | code >> 18));
281 str.push_back(make_char(0x80 | (code >> 12 & 0x3F)));
282 str.push_back(make_char(0x80 | (code >> 6 & 0x3F)));
283 str.push_back(make_char(0x80 | (code & 0x3F)));
285 throw std::invalid_argument(
"values above 0x10FFFF are not valid code points.");
289CLI11_INLINE std::string remove_escaped_characters(
const std::string &str) {
292 out.reserve(str.size());
293 for(
auto loc = str.begin(); loc < str.end(); ++loc) {
295 if(str.end() - loc < 2) {
296 throw std::invalid_argument(
"invalid escape sequence " + str);
298 auto ecloc = escapedCharsCode().find_first_of(*(loc + 1));
299 if(ecloc != std::string::npos) {
300 out.push_back(escapedChars()[ecloc]);
302 }
else if(*(loc + 1) ==
'u') {
304 if(str.end() - loc < 6) {
305 throw std::invalid_argument(
"unicode sequence must have 4 hex codes " + str);
307 std::uint32_t code{0};
308 std::uint32_t mplier{16 * 16 * 16};
309 for(
int ii = 2; ii < 6; ++ii) {
310 std::uint32_t res = hexConvert(*(loc + ii));
312 throw std::invalid_argument(
"unicode sequence must have 4 hex codes " + str);
314 code += res * mplier;
315 mplier = mplier / 16;
317 append_codepoint(out, code);
319 }
else if(*(loc + 1) ==
'U') {
321 if(str.end() - loc < 10) {
322 throw std::invalid_argument(
"unicode sequence must have 8 hex codes " + str);
324 std::uint32_t code{0};
325 std::uint32_t mplier{16 * 16 * 16 * 16 * 16 * 16 * 16};
326 for(
int ii = 2; ii < 10; ++ii) {
327 std::uint32_t res = hexConvert(*(loc + ii));
329 throw std::invalid_argument(
"unicode sequence must have 8 hex codes " + str);
331 code += res * mplier;
332 mplier = mplier / 16;
334 append_codepoint(out, code);
336 }
else if(*(loc + 1) ==
'0') {
340 throw std::invalid_argument(std::string(
"unrecognized escape sequence \\") + *(loc + 1) +
" in " + str);
349CLI11_INLINE std::size_t close_string_quote(
const std::string &str, std::size_t start,
char closure_char) {
351 for(loc = start + 1; loc < str.size(); ++loc) {
352 if(str[loc] == closure_char) {
355 if(str[loc] ==
'\\') {
363CLI11_INLINE std::size_t close_literal_quote(
const std::string &str, std::size_t start,
char closure_char) {
364 auto loc = str.find_first_of(closure_char, start + 1);
365 return (loc != std::string::npos ? loc : str.size());
368CLI11_INLINE std::size_t close_sequence(
const std::string &str, std::size_t start,
char closure_char) {
370 auto bracket_loc = matchBracketChars().find(closure_char);
371 switch(bracket_loc) {
373 return close_string_quote(str, start, closure_char);
376#if defined(_MSC_VER) && _MSC_VER < 1920
379 case std::string::npos:
381 return close_literal_quote(str, start, closure_char);
386 std::string closures(1, closure_char);
387 auto loc = start + 1;
389 while(loc < str.size()) {
390 if(str[loc] == closures.back()) {
392 if(closures.empty()) {
396 bracket_loc = bracketChars().find(str[loc]);
397 if(bracket_loc != std::string::npos) {
398 switch(bracket_loc) {
400 loc = close_string_quote(str, loc, str[loc]);
404 loc = close_literal_quote(str, loc, str[loc]);
407 closures.push_back(matchBracketChars()[bracket_loc]);
413 if(loc > str.size()) {
419CLI11_INLINE std::vector<std::string> split_up(std::string str,
char delimiter) {
421 auto find_ws = [delimiter](
char ch) {
422 return (delimiter ==
'\0') ? std::isspace<char>(ch, std::locale()) : (ch == delimiter);
426 std::vector<std::string> output;
427 while(!str.empty()) {
428 if(bracketChars().find_first_of(str[0]) != std::string::npos) {
429 auto bracketLoc = bracketChars().find_first_of(str[0]);
430 auto end = close_sequence(str, 0, matchBracketChars()[bracketLoc]);
431 if(end >= str.size()) {
432 output.push_back(std::move(str));
435 output.push_back(str.substr(0, end + 1));
442 char follow = str[end + 1];
443 bool skip_follow = find_ws(follow) || (bracketChars().find_first_of(follow) != std::string::npos);
444 auto next = skip_follow ? end + 2 : end + 1;
445 if(next < str.size()) {
446 str = str.substr(next);
453 auto it = std::find_if(std::begin(str), std::end(str), find_ws);
454 if(it != std::end(str)) {
455 std::string value = std::string(str.begin(), it);
456 output.push_back(value);
457 str = std::string(it + 1, str.end());
459 output.push_back(str);
468CLI11_INLINE std::size_t escape_detect(std::string &str, std::size_t offset) {
469 auto next = str[offset + 1];
470 if((next ==
'\"') || (next ==
'\'') || (next ==
'`')) {
475 auto astart = str.find_last_of(
"-/ \"\'`", offset - 1);
476 if(astart != std::string::npos) {
477 if(str[astart] == ((str[offset] ==
'=') ?
'-' :
'/'))
484CLI11_INLINE std::string binary_escape_string(
const std::string &string_to_escape,
bool force) {
486 std::string escaped_string{};
488 for(
char c : string_to_escape) {
491 if(isprint(
static_cast<unsigned char>(c)) == 0) {
492 std::stringstream stream;
496 stream << std::hex << static_cast<unsigned int>(
static_cast<unsigned char>(c));
497 std::string code = stream.str();
498 escaped_string += std::string(
"\\x") + (code.size() < 2 ?
"0" :
"") + code;
499 }
else if(c ==
'x' || c ==
'X') {
501 if(!escaped_string.empty() && escaped_string.back() ==
'\\') {
502 escaped_string += std::string(
"\\x") + (c ==
'x' ?
"78" :
"58");
504 escaped_string.push_back(c);
508 escaped_string.push_back(c);
511 if(escaped_string != string_to_escape || force) {
512 auto sqLoc = escaped_string.find(
'\'');
513 while(sqLoc != std::string::npos) {
514 escaped_string[sqLoc] =
'\\';
515 escaped_string.insert(sqLoc + 1,
"x27");
516 sqLoc = escaped_string.find(
'\'', sqLoc + 4);
518 escaped_string.insert(0,
"'B\"(");
519 escaped_string.push_back(
')');
520 escaped_string.push_back(
'"');
521 escaped_string.push_back(
'\'');
523 return escaped_string;
526CLI11_INLINE
bool is_binary_escaped_string(
const std::string &escaped_string) {
527 size_t ssize = escaped_string.size();
528 if(escaped_string.compare(0, 3,
"B\"(") == 0 && escaped_string.compare(ssize - 2, 2,
")\"") == 0) {
531 return (escaped_string.compare(0, 4,
"'B\"(") == 0 && escaped_string.compare(ssize - 3, 3,
")\"'") == 0);
534CLI11_INLINE std::string extract_binary_string(
const std::string &escaped_string) {
535 std::size_t start{0};
537 size_t ssize = escaped_string.size();
538 if(escaped_string.compare(0, 3,
"B\"(") == 0 && escaped_string.compare(ssize - 2, 2,
")\"") == 0) {
541 }
else if(escaped_string.compare(0, 4,
"'B\"(") == 0 && escaped_string.compare(ssize - 3, 3,
")\"'") == 0) {
547 return escaped_string;
549 std::string outstring;
551 outstring.reserve(ssize - start - tail);
552 std::size_t loc = start;
553 while(loc < ssize - tail) {
555 if(escaped_string[loc] ==
'\\' && (escaped_string[loc + 1] ==
'x' || escaped_string[loc + 1] ==
'X')) {
556 auto c1 = escaped_string[loc + 2];
557 auto c2 = escaped_string[loc + 3];
559 std::uint32_t res1 = hexConvert(c1);
560 std::uint32_t res2 = hexConvert(c2);
561 if(res1 <= 0x0F && res2 <= 0x0F) {
563 outstring.push_back(
static_cast<char>(res1 * 16 + res2));
567 outstring.push_back(escaped_string[loc]);
573CLI11_INLINE
void remove_quotes(std::vector<std::string> &args) {
574 for(
auto &arg : args) {
578 if(arg.front() ==
'\"' && arg.back() ==
'\"') {
581 arg = remove_escaped_characters(arg);
588CLI11_INLINE
void handle_secondary_array(std::string &str) {
589 if(str.size() >= 2 && str.front() ==
'[' && str.back() ==
']') {
591 std::string tstr{
"[["};
592 for(std::size_t ii = 1; ii < str.size(); ++ii) {
593 tstr.push_back(str[ii]);
594 tstr.push_back(str[ii]);
596 str = std::move(tstr);
601process_quoted_string(std::string &str,
char string_char,
char literal_char,
bool disable_secondary_array_processing) {
602 if(str.size() <= 1) {
605 if(detail::is_binary_escaped_string(str)) {
606 str = detail::extract_binary_string(str);
607 if(!disable_secondary_array_processing)
608 handle_secondary_array(str);
611 if(str.front() == string_char && str.back() == string_char) {
612 detail::remove_outer(str, string_char);
613 if(str.find_first_of(
'\\') != std::string::npos) {
614 str = detail::remove_escaped_characters(str);
616 if(!disable_secondary_array_processing)
617 handle_secondary_array(str);
620 if((str.front() == literal_char || str.front() ==
'`') && str.back() == str.front()) {
621 detail::remove_outer(str, str.front());
622 if(!disable_secondary_array_processing)
623 handle_secondary_array(str);
629CLI11_INLINE std::string get_environment_value(
const std::string &env_name) {
630 std::string ename_string;
634 char *buffer =
nullptr;
636 if(_dupenv_s(&buffer, &sz, env_name.c_str()) == 0 && buffer !=
nullptr) {
637 ename_string = std::string(buffer);
644 const char *buffer =
nullptr;
645 buffer = std::getenv(env_name.c_str());
646 if(buffer !=
nullptr) {
647 ename_string = std::string(buffer);
653CLI11_INLINE std::ostream &streamOutAsParagraph(std::ostream &out,
654 const std::string &text,
655 std::size_t paragraphWidth,
656 const std::string &linePrefix,
657 bool skipPrefixOnFirstLine) {
658 std::istringstream lss(text);
659 std::string line =
"";
660 while(std::getline(lss, line)) {
661 std::istringstream iss(line);
662 std::string word =
"";
663 std::size_t charsWritten = 0;
665 if(!skipPrefixOnFirstLine)
667 skipPrefixOnFirstLine =
false;
670 if(charsWritten > 0 && (word.length() + 1 + charsWritten > paragraphWidth)) {
671 out <<
'\n' << linePrefix;
674 if(charsWritten == 0) {
676 charsWritten += word.length();
679 charsWritten += word.length() + 1;