| 432 | } |
| 433 | |
| 434 | CLI11_INLINE std::string binary_escape_string(const std::string &string_to_escape, bool force) { |
| 435 | // s is our escaped output string |
| 436 | std::string escaped_string{}; |
| 437 | // loop through all characters |
| 438 | for(char c : string_to_escape) { |
| 439 | // check if a given character is printable |
| 440 | // the cast is necessary to avoid undefined behaviour |
| 441 | if(isprint(static_cast<unsigned char>(c)) == 0) { |
| 442 | std::stringstream stream; |
| 443 | // if the character is not printable |
| 444 | // we'll convert it to a hex string using a stringstream |
| 445 | // note that since char is signed we have to cast it to unsigned first |
| 446 | stream << std::hex << static_cast<unsigned int>(static_cast<unsigned char>(c)); |
| 447 | std::string code = stream.str(); |
| 448 | escaped_string += std::string("\\x") + (code.size() < 2 ? "0" : "") + code; |
| 449 | } else if(c == 'x' || c == 'X') { |
| 450 | // need to check for inadvertent binary sequences |
| 451 | if(!escaped_string.empty() && escaped_string.back() == '\\') { |
| 452 | escaped_string += std::string("\\x") + (c == 'x' ? "78" : "58"); |
| 453 | } else { |
| 454 | escaped_string.push_back(c); |
| 455 | } |
| 456 | |
| 457 | } else { |
| 458 | escaped_string.push_back(c); |
| 459 | } |
| 460 | } |
| 461 | if(escaped_string != string_to_escape || force) { |
| 462 | auto sqLoc = escaped_string.find('\''); |
| 463 | while(sqLoc != std::string::npos) { |
| 464 | escaped_string[sqLoc] = '\\'; |
| 465 | escaped_string.insert(sqLoc + 1, "x27"); |
| 466 | sqLoc = escaped_string.find('\''); |
| 467 | } |
| 468 | escaped_string.insert(0, "'B\"("); |
| 469 | escaped_string.push_back(')'); |
| 470 | escaped_string.push_back('"'); |
| 471 | escaped_string.push_back('\''); |
| 472 | } |
| 473 | return escaped_string; |
| 474 | } |
| 475 | |
| 476 | CLI11_INLINE bool is_binary_escaped_string(const std::string &escaped_string) { |
| 477 | size_t ssize = escaped_string.size(); |
nothing calls this directly
no outgoing calls
no test coverage detected