|
41 | 41 | namespace nvbench |
42 | 42 | { |
43 | 43 |
|
| 44 | +namespace |
| 45 | +{ |
| 46 | + |
| 47 | +#ifdef _WIN32 |
| 48 | + |
| 49 | +// Quote an argument with the backslash/double-quote rules used by |
| 50 | +// CommandLineToArgvW. This is not a full cmd.exe escaping layer; cmd.exe |
| 51 | +// expansion rules may still apply when pasted into a shell. |
| 52 | +std::string shell_quote(const std::string &arg) |
| 53 | +{ |
| 54 | + if (!arg.empty() && arg.find_first_of(" \t\n\v\"^&|<>()%!") == std::string::npos) |
| 55 | + { |
| 56 | + return arg; |
| 57 | + } |
| 58 | + |
| 59 | + // Follow the rules of CommandLineToArgvW: a run of backslashes is only special |
| 60 | + // when a double quote comes after it. |
| 61 | + std::string result; |
| 62 | + |
| 63 | + result.reserve((4 * arg.size()) + 2); |
| 64 | + result += '"'; |
| 65 | + for (auto iter = arg.begin(); iter != arg.end(); ++iter) |
| 66 | + { |
| 67 | + std::size_t num_backslashes = 0; |
| 68 | + while (iter != arg.end() && *iter == '\\') |
| 69 | + { |
| 70 | + ++num_backslashes; |
| 71 | + ++iter; |
| 72 | + } |
| 73 | + |
| 74 | + if (iter == arg.end()) |
| 75 | + { // Double the backslashes that come before the closing quote. |
| 76 | + result.append(num_backslashes * 2, '\\'); |
| 77 | + break; |
| 78 | + } |
| 79 | + |
| 80 | + if (*iter == '"') |
| 81 | + { // Double the backslashes that come before a quote, then escape the quote. |
| 82 | + result.append(num_backslashes * 2, '\\'); |
| 83 | + result += "\\\""; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + result.append(num_backslashes, '\\'); |
| 88 | + result += *iter; |
| 89 | + } |
| 90 | + } |
| 91 | + result += '"'; |
| 92 | + return result; |
| 93 | +} |
| 94 | + |
| 95 | +#else |
| 96 | + |
| 97 | +// Quote an argument for POSIX shells (sh, bash, zsh), so that the printed |
| 98 | +// command line can be copied and pasted. |
| 99 | +std::string shell_quote(const std::string &arg) |
| 100 | +{ |
| 101 | + if (!arg.empty() && arg.find_first_of(" \t\n\"'\\$`|&;<>()*?[]{}#~!") == std::string::npos) |
| 102 | + { |
| 103 | + return arg; |
| 104 | + } |
| 105 | + |
| 106 | + std::string result; |
| 107 | + |
| 108 | + result.reserve((4 * arg.size()) + 2); |
| 109 | + result += '\''; |
| 110 | + for (const char c : arg) |
| 111 | + { |
| 112 | + if (c == '\'') |
| 113 | + { // A single quote cannot appear inside single quotes; close, escape, reopen. |
| 114 | + result += "'\\''"; |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + result += c; |
| 119 | + } |
| 120 | + } |
| 121 | + result += '\''; |
| 122 | + return result; |
| 123 | +} |
| 124 | + |
| 125 | +#endif // _WIN32 |
| 126 | + |
| 127 | +std::size_t max_backtick_run(const std::string &str) |
| 128 | +{ |
| 129 | + std::size_t max_run{}; |
| 130 | + std::size_t current_run{}; |
| 131 | + for (const char c : str) |
| 132 | + { |
| 133 | + if (c == '`') |
| 134 | + { |
| 135 | + ++current_run; |
| 136 | + max_run = current_run > max_run ? current_run : max_run; |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + current_run = 0; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return max_run; |
| 145 | +} |
| 146 | + |
| 147 | +std::string markdown_code_fence(const std::string &contents) |
| 148 | +{ |
| 149 | + const auto fence_size = max_backtick_run(contents) + 1; |
| 150 | + return std::string(fence_size < 3 ? 3 : fence_size, '`'); |
| 151 | +} |
| 152 | + |
| 153 | +} // namespace |
| 154 | + |
| 155 | +void markdown_printer::do_print_argv() |
| 156 | +{ |
| 157 | + const auto &argv = this->get_raw_argv(); |
| 158 | + if (argv.empty()) |
| 159 | + { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + std::string command_line; |
| 164 | + for (std::size_t i = 0; i < argv.size(); ++i) |
| 165 | + { |
| 166 | + if (i != 0) |
| 167 | + { |
| 168 | + command_line += ' '; |
| 169 | + } |
| 170 | + command_line += shell_quote(argv[i]); |
| 171 | + } |
| 172 | + |
| 173 | + const auto fence = markdown_code_fence(command_line); |
| 174 | + |
| 175 | + fmt::memory_buffer buffer; |
| 176 | + fmt::format_to(fmt::appender(buffer), |
| 177 | + "# Command Line\n\n{}\n{}\n{}\n\n", |
| 178 | + fence, |
| 179 | + command_line, |
| 180 | + fence); |
| 181 | + m_ostream << fmt::to_string(buffer); |
| 182 | +} |
| 183 | + |
44 | 184 | void markdown_printer::do_print_device_info() |
45 | 185 | { |
46 | 186 | fmt::memory_buffer buffer; |
|
0 commit comments