12#include "../StringTools.hpp"
25CLI11_INLINE std::vector<std::string> split(
const std::string &s,
char delim) {
26 std::vector<std::string> elems;
32 std::size_t start = 0;
35 while((end = s.find(delim, start)) != std::string::npos) {
36 elems.push_back(s.substr(start, end - start));
39 elems.push_back(s.substr(start));
43CLI11_INLINE std::string <rim(std::string &str) {
44 auto it = std::find_if(str.begin(), str.end(), [](
char ch) { return !std::isspace<char>(ch, std::locale()); });
45 str.erase(str.begin(), it);
49CLI11_INLINE std::string <rim(std::string &str,
const std::string &filter) {
50 auto it = std::find_if(str.begin(), str.end(), [&filter](
char ch) { return filter.find(ch) == std::string::npos; });
51 str.erase(str.begin(), it);
55CLI11_INLINE std::string &rtrim(std::string &str) {
56 auto it = std::find_if(str.rbegin(), str.rend(), [](
char ch) { return !std::isspace<char>(ch, std::locale()); });
57 str.erase(it.base(), str.end());
61CLI11_INLINE std::string &rtrim(std::string &str,
const std::string &filter) {
63 std::find_if(str.rbegin(), str.rend(), [&filter](
char ch) { return filter.find(ch) == std::string::npos; });
64 str.erase(it.base(), str.end());
68CLI11_INLINE std::string &remove_quotes(std::string &str) {
69 if(!str.empty() && (str.front() ==
'"' || str.front() ==
'\'' || str.front() ==
'`')) {
70 remove_outer(str, str.front());
75CLI11_INLINE std::string &remove_outer(std::string &str,
char key) {
76 if(str.length() > 1 && (str.front() == key)) {
77 if(str.front() == str.back()) {
79 str.erase(str.begin(), str.begin() + 1);
85CLI11_INLINE std::string fix_newlines(
const std::string &leader, std::string input) {
86 std::string::size_type n = 0;
87 while(n != std::string::npos && n < input.size()) {
88 n = input.find_first_of(
"\r\n", n);
89 if(n != std::string::npos) {
90 input.insert(n + 1, leader);
91 n += leader.size() + 1;
97CLI11_INLINE std::ostream &format_aliases(std::ostream &out,
const std::vector<std::string> &aliases, std::size_t wid) {
98 if(!aliases.empty()) {
99 out << std::setw(static_cast<int>(wid)) <<
" aliases: ";
101 for(
const auto &alias : aliases) {
107 out << detail::fix_newlines(
" ", alias);
114CLI11_INLINE
bool valid_name_string(
const std::string &str) {
115 if(str.empty() || !valid_first_char(str[0])) {
119 for(
auto c = str.begin() + 1; c != e; ++c)
120 if(!valid_later_char(*c))
125CLI11_INLINE std::string get_group_separators() {
126 std::string separators{
"_'"};
127#if CLI11_HAS_RTTI != 0
128 char group_separator = std::use_facet<std::numpunct<char>>(std::locale()).thousands_sep();
129 separators.push_back(group_separator);
134CLI11_INLINE std::string find_and_replace(std::string str, std::string from, std::string to) {
136 std::size_t start_pos = 0;
138 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
139 str.replace(start_pos, from.length(), to);
140 start_pos += to.length();
146CLI11_INLINE
void remove_default_flag_values(std::string &flags) {
147 auto loc = flags.find_first_of(
'{', 2);
148 while(loc != std::string::npos) {
149 auto finish = flags.find_first_of(
"},", loc + 1);
150 if((finish != std::string::npos) && (flags[finish] ==
'}')) {
151 flags.erase(flags.begin() +
static_cast<std::ptrdiff_t
>(loc),
152 flags.begin() +
static_cast<std::ptrdiff_t
>(finish) + 1);
154 loc = flags.find_first_of(
'{', loc + 1);
156 flags.erase(std::remove(flags.begin(), flags.end(),
'!'), flags.end());
159CLI11_INLINE std::ptrdiff_t
160find_member(std::string name,
const std::vector<std::string> &names,
bool ignore_case,
bool ignore_underscore) {
161 auto it = std::end(names);
163 if(ignore_underscore) {
164 name = detail::to_lower(detail::remove_underscore(name));
165 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
166 return detail::to_lower(detail::remove_underscore(local_name)) == name;
169 name = detail::to_lower(name);
170 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
171 return detail::to_lower(local_name) == name;
175 }
else if(ignore_underscore) {
176 name = detail::remove_underscore(name);
177 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
178 return detail::remove_underscore(local_name) == name;
181 it = std::find(std::begin(names), std::end(names), name);
184 return (it != std::end(names)) ? (it - std::begin(names)) : (-1);
187CLI11_MODULE_INLINE
const std::string &escapedChars() {
188 static const std::string s{
"\b\t\n\f\r\"\\"};
191CLI11_MODULE_INLINE
const std::string &escapedCharsCode() {
192 static const std::string s{
"btnfr\"\\"};
195CLI11_MODULE_INLINE
const std::string &bracketChars() {
196 static const std::string s{
"\"'`[(<{"};
199CLI11_MODULE_INLINE
const std::string &matchBracketChars() {
200 static const std::string s{
"\"'`])>}"};
204CLI11_INLINE
bool has_escapable_character(
const std::string &str) {
205 return (str.find_first_of(escapedChars()) != std::string::npos);
208CLI11_INLINE std::string add_escaped_characters(
const std::string &str) {
210 out.reserve(str.size() + 4);
212 auto sloc = escapedChars().find_first_of(s);
213 if(sloc != std::string::npos) {
215 out.push_back(escapedCharsCode()[sloc]);
223CLI11_INLINE std::uint32_t hexConvert(
char hc) {
225 if(hc >=
'0' && hc <=
'9') {
227 }
else if(hc >=
'A' && hc <=
'F') {
228 hcode = (hc -
'A' + 10);
229 }
else if(hc >=
'a' && hc <=
'f') {
230 hcode = (hc -
'a' + 10);
234 return static_cast<uint32_t
>(hcode);
237CLI11_INLINE
char make_char(std::uint32_t code) {
return static_cast<char>(
static_cast<unsigned char>(code)); }
239CLI11_INLINE
void append_codepoint(std::string &str, std::uint32_t code) {
241 str.push_back(
static_cast<char>(code));
242 }
else if(code < 0x800) {
244 str.push_back(make_char(0xC0 | code >> 6));
245 str.push_back(make_char(0x80 | (code & 0x3F)));
246 }
else if(code < 0x10000) {
247 if(0xD800 <= code && code <= 0xDFFF) {
248 throw std::invalid_argument(
"[0xD800, 0xDFFF] are not valid code points.");
251 str.push_back(make_char(0xE0 | code >> 12));
252 str.push_back(make_char(0x80 | (code >> 6 & 0x3F)));
253 str.push_back(make_char(0x80 | (code & 0x3F)));
254 }
else if(code < 0x110000) {
256 str.push_back(make_char(0xF0 | code >> 18));
257 str.push_back(make_char(0x80 | (code >> 12 & 0x3F)));
258 str.push_back(make_char(0x80 | (code >> 6 & 0x3F)));
259 str.push_back(make_char(0x80 | (code & 0x3F)));
261 throw std::invalid_argument(
"values above 0x10FFFF are not valid code points.");
265CLI11_INLINE std::string remove_escaped_characters(
const std::string &str) {
268 out.reserve(str.size());
269 for(
auto loc = str.begin(); loc < str.end(); ++loc) {
271 if(str.end() - loc < 2) {
272 throw std::invalid_argument(
"invalid escape sequence " + str);
274 auto ecloc = escapedCharsCode().find_first_of(*(loc + 1));
275 if(ecloc != std::string::npos) {
276 out.push_back(escapedChars()[ecloc]);
278 }
else if(*(loc + 1) ==
'u') {
280 if(str.end() - loc < 6) {
281 throw std::invalid_argument(
"unicode sequence must have 4 hex codes " + str);
283 std::uint32_t code{0};
284 std::uint32_t mplier{16 * 16 * 16};
285 for(
int ii = 2; ii < 6; ++ii) {
286 std::uint32_t res = hexConvert(*(loc + ii));
288 throw std::invalid_argument(
"unicode sequence must have 4 hex codes " + str);
290 code += res * mplier;
291 mplier = mplier / 16;
293 append_codepoint(out, code);
295 }
else if(*(loc + 1) ==
'U') {
297 if(str.end() - loc < 10) {
298 throw std::invalid_argument(
"unicode sequence must have 8 hex codes " + str);
300 std::uint32_t code{0};
301 std::uint32_t mplier{16 * 16 * 16 * 16 * 16 * 16 * 16};
302 for(
int ii = 2; ii < 10; ++ii) {
303 std::uint32_t res = hexConvert(*(loc + ii));
305 throw std::invalid_argument(
"unicode sequence must have 8 hex codes " + str);
307 code += res * mplier;
308 mplier = mplier / 16;
310 append_codepoint(out, code);
312 }
else if(*(loc + 1) ==
'0') {
316 throw std::invalid_argument(std::string(
"unrecognized escape sequence \\") + *(loc + 1) +
" in " + str);
325CLI11_INLINE std::size_t close_string_quote(
const std::string &str, std::size_t start,
char closure_char) {
327 for(loc = start + 1; loc < str.size(); ++loc) {
328 if(str[loc] == closure_char) {
331 if(str[loc] ==
'\\') {
339CLI11_INLINE std::size_t close_literal_quote(
const std::string &str, std::size_t start,
char closure_char) {
340 auto loc = str.find_first_of(closure_char, start + 1);
341 return (loc != std::string::npos ? loc : str.size());
344CLI11_INLINE std::size_t close_sequence(
const std::string &str, std::size_t start,
char closure_char) {
346 auto bracket_loc = matchBracketChars().find(closure_char);
347 switch(bracket_loc) {
349 return close_string_quote(str, start, closure_char);
352#if defined(_MSC_VER) && _MSC_VER < 1920
355 case std::string::npos:
357 return close_literal_quote(str, start, closure_char);
362 std::string closures(1, closure_char);
363 auto loc = start + 1;
365 while(loc < str.size()) {
366 if(str[loc] == closures.back()) {
368 if(closures.empty()) {
372 bracket_loc = bracketChars().find(str[loc]);
373 if(bracket_loc != std::string::npos) {
374 switch(bracket_loc) {
376 loc = close_string_quote(str, loc, str[loc]);
380 loc = close_literal_quote(str, loc, str[loc]);
383 closures.push_back(matchBracketChars()[bracket_loc]);
389 if(loc > str.size()) {
395CLI11_INLINE std::vector<std::string> split_up(std::string str,
char delimiter) {
397 auto find_ws = [delimiter](
char ch) {
398 return (delimiter ==
'\0') ? std::isspace<char>(ch, std::locale()) : (ch == delimiter);
402 std::vector<std::string> output;
403 while(!str.empty()) {
404 if(bracketChars().find_first_of(str[0]) != std::string::npos) {
405 auto bracketLoc = bracketChars().find_first_of(str[0]);
406 auto end = close_sequence(str, 0, matchBracketChars()[bracketLoc]);
407 if(end >= str.size()) {
408 output.push_back(std::move(str));
411 output.push_back(str.substr(0, end + 1));
418 char follow = str[end + 1];
419 bool skip_follow = find_ws(follow) || (bracketChars().find_first_of(follow) != std::string::npos);
420 auto next = skip_follow ? end + 2 : end + 1;
421 if(next < str.size()) {
422 str = str.substr(next);
429 auto it = std::find_if(std::begin(str), std::end(str), find_ws);
430 if(it != std::end(str)) {
431 std::string value = std::string(str.begin(), it);
432 output.push_back(value);
433 str = std::string(it + 1, str.end());
435 output.push_back(str);
444CLI11_INLINE std::size_t escape_detect(std::string &str, std::size_t offset) {
445 auto next = str[offset + 1];
446 if((next ==
'\"') || (next ==
'\'') || (next ==
'`')) {
451 auto astart = str.find_last_of(
"-/ \"\'`", offset - 1);
452 if(astart != std::string::npos) {
453 if(str[astart] == ((str[offset] ==
'=') ?
'-' :
'/'))
460CLI11_INLINE std::string binary_escape_string(
const std::string &string_to_escape,
bool force) {
462 std::string escaped_string{};
464 for(
char c : string_to_escape) {
467 if(isprint(
static_cast<unsigned char>(c)) == 0) {
468 std::stringstream stream;
472 stream << std::hex << static_cast<unsigned int>(
static_cast<unsigned char>(c));
473 std::string code = stream.str();
474 escaped_string += std::string(
"\\x") + (code.size() < 2 ?
"0" :
"") + code;
475 }
else if(c ==
'x' || c ==
'X') {
477 if(!escaped_string.empty() && escaped_string.back() ==
'\\') {
478 escaped_string += std::string(
"\\x") + (c ==
'x' ?
"78" :
"58");
480 escaped_string.push_back(c);
484 escaped_string.push_back(c);
487 if(escaped_string != string_to_escape || force) {
488 auto sqLoc = escaped_string.find(
'\'');
489 while(sqLoc != std::string::npos) {
490 escaped_string[sqLoc] =
'\\';
491 escaped_string.insert(sqLoc + 1,
"x27");
492 sqLoc = escaped_string.find(
'\'', sqLoc + 4);
494 escaped_string.insert(0,
"'B\"(");
495 escaped_string.push_back(
')');
496 escaped_string.push_back(
'"');
497 escaped_string.push_back(
'\'');
499 return escaped_string;
502CLI11_INLINE
bool is_binary_escaped_string(
const std::string &escaped_string) {
503 size_t ssize = escaped_string.size();
504 if(escaped_string.compare(0, 3,
"B\"(") == 0 && escaped_string.compare(ssize - 2, 2,
")\"") == 0) {
507 return (escaped_string.compare(0, 4,
"'B\"(") == 0 && escaped_string.compare(ssize - 3, 3,
")\"'") == 0);
510CLI11_INLINE std::string extract_binary_string(
const std::string &escaped_string) {
511 std::size_t start{0};
513 size_t ssize = escaped_string.size();
514 if(escaped_string.compare(0, 3,
"B\"(") == 0 && escaped_string.compare(ssize - 2, 2,
")\"") == 0) {
517 }
else if(escaped_string.compare(0, 4,
"'B\"(") == 0 && escaped_string.compare(ssize - 3, 3,
")\"'") == 0) {
523 return escaped_string;
525 std::string outstring;
527 outstring.reserve(ssize - start - tail);
528 std::size_t loc = start;
529 while(loc < ssize - tail) {
531 if(escaped_string[loc] ==
'\\' && (escaped_string[loc + 1] ==
'x' || escaped_string[loc + 1] ==
'X')) {
532 auto c1 = escaped_string[loc + 2];
533 auto c2 = escaped_string[loc + 3];
535 std::uint32_t res1 = hexConvert(c1);
536 std::uint32_t res2 = hexConvert(c2);
537 if(res1 <= 0x0F && res2 <= 0x0F) {
539 outstring.push_back(
static_cast<char>(res1 * 16 + res2));
543 outstring.push_back(escaped_string[loc]);
549CLI11_INLINE
void remove_quotes(std::vector<std::string> &args) {
550 for(
auto &arg : args) {
554 if(arg.front() ==
'\"' && arg.back() ==
'\"') {
557 arg = remove_escaped_characters(arg);
564CLI11_INLINE
void handle_secondary_array(std::string &str) {
565 if(str.size() >= 2 && str.front() ==
'[' && str.back() ==
']') {
567 std::string tstr{
"[["};
568 for(std::size_t ii = 1; ii < str.size(); ++ii) {
569 tstr.push_back(str[ii]);
570 tstr.push_back(str[ii]);
572 str = std::move(tstr);
577process_quoted_string(std::string &str,
char string_char,
char literal_char,
bool disable_secondary_array_processing) {
578 if(str.size() <= 1) {
581 if(detail::is_binary_escaped_string(str)) {
582 str = detail::extract_binary_string(str);
583 if(!disable_secondary_array_processing)
584 handle_secondary_array(str);
587 if(str.front() == string_char && str.back() == string_char) {
588 detail::remove_outer(str, string_char);
589 if(str.find_first_of(
'\\') != std::string::npos) {
590 str = detail::remove_escaped_characters(str);
592 if(!disable_secondary_array_processing)
593 handle_secondary_array(str);
596 if((str.front() == literal_char || str.front() ==
'`') && str.back() == str.front()) {
597 detail::remove_outer(str, str.front());
598 if(!disable_secondary_array_processing)
599 handle_secondary_array(str);
605CLI11_INLINE std::string get_environment_value(
const std::string &env_name) {
606 std::string ename_string;
610 char *buffer =
nullptr;
612 if(_dupenv_s(&buffer, &sz, env_name.c_str()) == 0 && buffer !=
nullptr) {
613 ename_string = std::string(buffer);
620 const char *buffer =
nullptr;
621 buffer = std::getenv(env_name.c_str());
622 if(buffer !=
nullptr) {
623 ename_string = std::string(buffer);
629CLI11_INLINE std::ostream &streamOutAsParagraph(std::ostream &out,
630 const std::string &text,
631 std::size_t paragraphWidth,
632 const std::string &linePrefix,
633 bool skipPrefixOnFirstLine) {
634 std::istringstream lss(text);
635 std::string line =
"";
636 while(std::getline(lss, line)) {
637 std::istringstream iss(line);
638 std::string word =
"";
639 std::size_t charsWritten = 0;
641 if(!skipPrefixOnFirstLine)
643 skipPrefixOnFirstLine =
false;
646 if(charsWritten > 0 && (word.length() + 1 + charsWritten > paragraphWidth)) {
647 out <<
'\n' << linePrefix;
650 if(charsWritten == 0) {
652 charsWritten += word.length();
655 charsWritten += word.length() + 1;