12#include "../StringTools.hpp"
25CLI11_INLINE std::vector<std::string> split(
const std::string &s,
char delim) {
26 std::vector<std::string> elems;
34 while(std::getline(ss, item, delim)) {
35 elems.push_back(item);
41CLI11_INLINE std::string <rim(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);
47CLI11_INLINE std::string <rim(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);
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());
59CLI11_INLINE std::string &rtrim(std::string &str,
const std::string &filter) {
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());
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()) {
70 str.erase(str.begin(), str.begin() + 1);
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()) {
80 str.erase(str.begin(), str.begin() + 1);
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_first_of(
"\r\n", n);
90 if(n != std::string::npos) {
91 input = input.substr(0, n + 1) + leader + input.substr(n + 1);
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: ";
102 for(
const auto &alias : aliases) {
108 out << detail::fix_newlines(
" ", alias);
115CLI11_INLINE
bool valid_name_string(
const std::string &str) {
116 if(str.empty() || !valid_first_char(str[0])) {
120 for(
auto c = str.begin() + 1; c != e; ++c)
121 if(!valid_later_char(*c))
126CLI11_INLINE std::string get_group_separators() {
127 std::string separators{
"_'"};
128#if CLI11_HAS_RTTI != 0
129 char group_separator = std::use_facet<std::numpunct<char>>(std::locale()).thousands_sep();
130 separators.push_back(group_separator);
135CLI11_INLINE std::string find_and_replace(std::string str, std::string from, std::string to) {
137 std::size_t start_pos = 0;
139 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
140 str.replace(start_pos, from.length(), to);
141 start_pos += to.length();
147CLI11_INLINE
void remove_default_flag_values(std::string &flags) {
148 auto loc = flags.find_first_of(
'{', 2);
149 while(loc != std::string::npos) {
150 auto finish = flags.find_first_of(
"},", loc + 1);
151 if((finish != std::string::npos) && (flags[finish] ==
'}')) {
152 flags.erase(flags.begin() +
static_cast<std::ptrdiff_t
>(loc),
153 flags.begin() +
static_cast<std::ptrdiff_t
>(finish) + 1);
155 loc = flags.find_first_of(
'{', loc + 1);
157 flags.erase(std::remove(flags.begin(), flags.end(),
'!'), flags.end());
160CLI11_INLINE std::ptrdiff_t
161find_member(std::string name,
const std::vector<std::string> names,
bool ignore_case,
bool ignore_underscore) {
162 auto it = std::end(names);
164 if(ignore_underscore) {
165 name = detail::to_lower(detail::remove_underscore(name));
166 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
167 return detail::to_lower(detail::remove_underscore(local_name)) == name;
170 name = detail::to_lower(name);
171 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
172 return detail::to_lower(local_name) == name;
176 }
else if(ignore_underscore) {
177 name = detail::remove_underscore(name);
178 it = std::find_if(std::begin(names), std::end(names), [&name](std::string local_name) {
179 return detail::remove_underscore(local_name) == name;
182 it = std::find(std::begin(names), std::end(names), name);
185 return (it != std::end(names)) ? (it - std::begin(names)) : (-1);
188CLI11_MODULE_INLINE
const std::string &escapedChars() {
189 static const std::string s{
"\b\t\n\f\r\"\\"};
192CLI11_MODULE_INLINE
const std::string &escapedCharsCode() {
193 static const std::string s{
"btnfr\"\\"};
196CLI11_MODULE_INLINE
const std::string &bracketChars() {
197 static const std::string s{
"\"'`[(<{"};
200CLI11_MODULE_INLINE
const std::string &matchBracketChars() {
201 static const std::string s{
"\"'`])>}"};
213CLI11_INLINE
bool has_escapable_character(
const std::string &str) {
214 return (str.find_first_of(escapedChars()) != std::string::npos);
217CLI11_INLINE std::string add_escaped_characters(
const std::string &str) {
219 out.reserve(str.size() + 4);
221 auto sloc = escapedChars().find_first_of(s);
222 if(sloc != std::string::npos) {
224 out.push_back(escapedCharsCode()[sloc]);
232CLI11_INLINE std::uint32_t hexConvert(
char hc) {
234 if(hc >=
'0' && hc <=
'9') {
236 }
else if(hc >=
'A' && hc <=
'F') {
237 hcode = (hc -
'A' + 10);
238 }
else if(hc >=
'a' && hc <=
'f') {
239 hcode = (hc -
'a' + 10);
243 return static_cast<uint32_t
>(hcode);
246CLI11_INLINE
char make_char(std::uint32_t code) {
return static_cast<char>(
static_cast<unsigned char>(code)); }
248CLI11_INLINE
void append_codepoint(std::string &str, std::uint32_t code) {
250 str.push_back(
static_cast<char>(code));
251 }
else if(code < 0x800) {
253 str.push_back(make_char(0xC0 | code >> 6));
254 str.push_back(make_char(0x80 | (code & 0x3F)));
255 }
else if(code < 0x10000) {
256 if(0xD800 <= code && code <= 0xDFFF) {
257 throw std::invalid_argument(
"[0xD800, 0xDFFF] are not valid UTF-8.");
260 str.push_back(make_char(0xE0 | code >> 12));
261 str.push_back(make_char(0x80 | (code >> 6 & 0x3F)));
262 str.push_back(make_char(0x80 | (code & 0x3F)));
263 }
else if(code < 0x110000) {
265 str.push_back(make_char(0xF0 | code >> 18));
266 str.push_back(make_char(0x80 | (code >> 12 & 0x3F)));
267 str.push_back(make_char(0x80 | (code >> 6 & 0x3F)));
268 str.push_back(make_char(0x80 | (code & 0x3F)));
272CLI11_INLINE std::string remove_escaped_characters(
const std::string &str) {
275 out.reserve(str.size());
276 for(
auto loc = str.begin(); loc < str.end(); ++loc) {
278 if(str.end() - loc < 2) {
279 throw std::invalid_argument(
"invalid escape sequence " + str);
281 auto ecloc = escapedCharsCode().find_first_of(*(loc + 1));
282 if(ecloc != std::string::npos) {
283 out.push_back(escapedChars()[ecloc]);
285 }
else if(*(loc + 1) ==
'u') {
287 if(str.end() - loc < 6) {
288 throw std::invalid_argument(
"unicode sequence must have 4 hex codes " + str);
290 std::uint32_t code{0};
291 std::uint32_t mplier{16 * 16 * 16};
292 for(
int ii = 2; ii < 6; ++ii) {
293 std::uint32_t res = hexConvert(*(loc + ii));
295 throw std::invalid_argument(
"unicode sequence must have 4 hex codes " + str);
297 code += res * mplier;
298 mplier = mplier / 16;
300 append_codepoint(out, code);
302 }
else if(*(loc + 1) ==
'U') {
304 if(str.end() - loc < 10) {
305 throw std::invalid_argument(
"unicode sequence must have 8 hex codes " + str);
307 std::uint32_t code{0};
308 std::uint32_t mplier{16 * 16 * 16 * 16 * 16 * 16 * 16};
309 for(
int ii = 2; ii < 10; ++ii) {
310 std::uint32_t res = hexConvert(*(loc + ii));
312 throw std::invalid_argument(
"unicode sequence must have 8 hex codes " + str);
314 code += res * mplier;
315 mplier = mplier / 16;
317 append_codepoint(out, code);
319 }
else if(*(loc + 1) ==
'0') {
323 throw std::invalid_argument(std::string(
"unrecognized escape sequence \\") + *(loc + 1) +
" in " + str);
332CLI11_INLINE std::size_t close_string_quote(
const std::string &str, std::size_t start,
char closure_char) {
334 for(loc = start + 1; loc < str.size(); ++loc) {
335 if(str[loc] == closure_char) {
338 if(str[loc] ==
'\\') {
346CLI11_INLINE std::size_t close_literal_quote(
const std::string &str, std::size_t start,
char closure_char) {
347 auto loc = str.find_first_of(closure_char, start + 1);
348 return (loc != std::string::npos ? loc : str.size());
351CLI11_INLINE std::size_t close_sequence(
const std::string &str, std::size_t start,
char closure_char) {
353 auto bracket_loc = matchBracketChars().find(closure_char);
354 switch(bracket_loc) {
356 return close_string_quote(str, start, closure_char);
359#if defined(_MSC_VER) && _MSC_VER < 1920
362 case std::string::npos:
364 return close_literal_quote(str, start, closure_char);
369 std::string closures(1, closure_char);
370 auto loc = start + 1;
372 while(loc < str.size()) {
373 if(str[loc] == closures.back()) {
375 if(closures.empty()) {
379 bracket_loc = bracketChars().find(str[loc]);
380 if(bracket_loc != std::string::npos) {
381 switch(bracket_loc) {
383 loc = close_string_quote(str, loc, str[loc]);
387 loc = close_literal_quote(str, loc, str[loc]);
390 closures.push_back(matchBracketChars()[bracket_loc]);
396 if(loc > str.size()) {
402CLI11_INLINE std::vector<std::string> split_up(std::string str,
char delimiter) {
404 auto find_ws = [delimiter](
char ch) {
405 return (delimiter ==
'\0') ? std::isspace<char>(ch, std::locale()) : (ch == delimiter);
409 std::vector<std::string> output;
410 while(!str.empty()) {
411 if(bracketChars().find_first_of(str[0]) != std::string::npos) {
412 auto bracketLoc = bracketChars().find_first_of(str[0]);
413 auto end = close_sequence(str, 0, matchBracketChars()[bracketLoc]);
414 if(end >= str.size()) {
415 output.push_back(std::move(str));
418 output.push_back(str.substr(0, end + 1));
419 if(end + 2 < str.size()) {
420 str = str.substr(end + 2);
427 auto it = std::find_if(std::begin(str), std::end(str), find_ws);
428 if(it != std::end(str)) {
429 std::string value = std::string(str.begin(), it);
430 output.push_back(value);
431 str = std::string(it + 1, str.end());
433 output.push_back(str);
442CLI11_INLINE std::size_t escape_detect(std::string &str, std::size_t offset) {
443 auto next = str[offset + 1];
444 if((next ==
'\"') || (next ==
'\'') || (next ==
'`')) {
445 auto astart = str.find_last_of(
"-/ \"\'`", offset - 1);
446 if(astart != std::string::npos) {
447 if(str[astart] == ((str[offset] ==
'=') ?
'-' :
'/'))
454CLI11_INLINE std::string binary_escape_string(
const std::string &string_to_escape,
bool force) {
456 std::string escaped_string{};
458 for(
char c : string_to_escape) {
461 if(isprint(
static_cast<unsigned char>(c)) == 0) {
462 std::stringstream stream;
466 stream << std::hex << static_cast<unsigned int>(
static_cast<unsigned char>(c));
467 std::string code = stream.str();
468 escaped_string += std::string(
"\\x") + (code.size() < 2 ?
"0" :
"") + code;
469 }
else if(c ==
'x' || c ==
'X') {
471 if(!escaped_string.empty() && escaped_string.back() ==
'\\') {
472 escaped_string += std::string(
"\\x") + (c ==
'x' ?
"78" :
"58");
474 escaped_string.push_back(c);
478 escaped_string.push_back(c);
481 if(escaped_string != string_to_escape || force) {
482 auto sqLoc = escaped_string.find(
'\'');
483 while(sqLoc != std::string::npos) {
484 escaped_string[sqLoc] =
'\\';
485 escaped_string.insert(sqLoc + 1,
"x27");
486 sqLoc = escaped_string.find(
'\'');
488 escaped_string.insert(0,
"'B\"(");
489 escaped_string.push_back(
')');
490 escaped_string.push_back(
'"');
491 escaped_string.push_back(
'\'');
493 return escaped_string;
496CLI11_INLINE
bool is_binary_escaped_string(
const std::string &escaped_string) {
497 size_t ssize = escaped_string.size();
498 if(escaped_string.compare(0, 3,
"B\"(") == 0 && escaped_string.compare(ssize - 2, 2,
")\"") == 0) {
501 return (escaped_string.compare(0, 4,
"'B\"(") == 0 && escaped_string.compare(ssize - 3, 3,
")\"'") == 0);
504CLI11_INLINE std::string extract_binary_string(
const std::string &escaped_string) {
505 std::size_t start{0};
507 size_t ssize = escaped_string.size();
508 if(escaped_string.compare(0, 3,
"B\"(") == 0 && escaped_string.compare(ssize - 2, 2,
")\"") == 0) {
511 }
else if(escaped_string.compare(0, 4,
"'B\"(") == 0 && escaped_string.compare(ssize - 3, 3,
")\"'") == 0) {
517 return escaped_string;
519 std::string outstring;
521 outstring.reserve(ssize - start - tail);
522 std::size_t loc = start;
523 while(loc < ssize - tail) {
525 if(escaped_string[loc] ==
'\\' && (escaped_string[loc + 1] ==
'x' || escaped_string[loc + 1] ==
'X')) {
526 auto c1 = escaped_string[loc + 2];
527 auto c2 = escaped_string[loc + 3];
529 std::uint32_t res1 = hexConvert(c1);
530 std::uint32_t res2 = hexConvert(c2);
531 if(res1 <= 0x0F && res2 <= 0x0F) {
533 outstring.push_back(
static_cast<char>(res1 * 16 + res2));
537 outstring.push_back(escaped_string[loc]);
543CLI11_INLINE
void remove_quotes(std::vector<std::string> &args) {
544 for(
auto &arg : args) {
545 if(arg.front() ==
'\"' && arg.back() ==
'\"') {
548 arg = remove_escaped_characters(arg);
555CLI11_INLINE
void handle_secondary_array(std::string &str) {
556 if(str.size() >= 2 && str.front() ==
'[' && str.back() ==
']') {
558 std::string tstr{
"[["};
559 for(std::size_t ii = 1; ii < str.size(); ++ii) {
560 tstr.push_back(str[ii]);
561 tstr.push_back(str[ii]);
563 str = std::move(tstr);
568process_quoted_string(std::string &str,
char string_char,
char literal_char,
bool disable_secondary_array_processing) {
569 if(str.size() <= 1) {
572 if(detail::is_binary_escaped_string(str)) {
573 str = detail::extract_binary_string(str);
574 if(!disable_secondary_array_processing)
575 handle_secondary_array(str);
578 if(str.front() == string_char && str.back() == string_char) {
579 detail::remove_outer(str, string_char);
580 if(str.find_first_of(
'\\') != std::string::npos) {
581 str = detail::remove_escaped_characters(str);
583 if(!disable_secondary_array_processing)
584 handle_secondary_array(str);
587 if((str.front() == literal_char || str.front() ==
'`') && str.back() == str.front()) {
588 detail::remove_outer(str, str.front());
589 if(!disable_secondary_array_processing)
590 handle_secondary_array(str);
596std::string get_environment_value(
const std::string &env_name) {
597 std::string ename_string;
601 char *buffer =
nullptr;
603 if(_dupenv_s(&buffer, &sz, env_name.c_str()) == 0 && buffer !=
nullptr) {
604 ename_string = std::string(buffer);
611 const char *buffer =
nullptr;
612 buffer = std::getenv(env_name.c_str());
613 if(buffer !=
nullptr) {
614 ename_string = std::string(buffer);
620CLI11_INLINE std::ostream &streamOutAsParagraph(std::ostream &out,
621 const std::string &text,
622 std::size_t paragraphWidth,
623 const std::string &linePrefix,
624 bool skipPrefixOnFirstLine) {
625 if(!skipPrefixOnFirstLine)
628 std::istringstream lss(text);
629 std::string line =
"";
630 while(std::getline(lss, line)) {
631 std::istringstream iss(line);
632 std::string word =
"";
633 std::size_t charsWritten = 0;
636 if(charsWritten > 0 && (word.length() + 1 + charsWritten > paragraphWidth)) {
637 out <<
'\n' << linePrefix;
640 if(charsWritten == 0) {
642 charsWritten += word.length();
645 charsWritten += word.length() + 1;
650 out <<
'\n' << linePrefix;