27 #include <boost/filesystem.hpp>
32 const boost::regex& reg_expr,
33 const std::function<
void(std::string&,
const boost::smatch&)>& shim_fn) {
36 auto start_it = result.cbegin();
37 auto end_it = result.cend();
39 if (!boost::regex_search(start_it, end_it, what, reg_expr)) {
42 const auto next_start =
45 start_it = result.cbegin() + *next_start;
47 shim_fn(result, what);
49 start_it = result.cbegin();
50 end_it = result.cend();
58 boost::regex literal_string_regex{R
"(([^']+)('(?:[^']+|'')*'))", boost::regex::perl};
60 auto it = query.begin();
62 std::vector<std::pair<size_t, size_t>> positions;
65 if (!boost::regex_search(it, query.end(), what, literal_string_regex)) {
68 }
catch (
const std::exception& e) {
73 std::ostringstream oss;
74 oss <<
"Detecting an error while processing string literal regex search: "
76 throw std::runtime_error(oss.str());
81 positions.emplace_back(prev_it + what[1].length() - query.begin(),
88 constexpr std::regex::flag_type flags =
89 std::regex::ECMAScript | std::regex::icase | std::regex::optimize;
90 static const std::initializer_list<std::pair<std::regex, std::string>> rules{
92 R
"(\b((?:password|s3_access_key|s3_secret_key|s3_session_token|username|credential_string)\s*=\s*)'.+?')",
95 {std::regex(R
"((\\set_license\s+)\S+)", flags), "$1XXXXXXXX"}};
97 rules.begin(), rules.end(), query_str, [](
auto& str,
auto& rule) {
98 return std::regex_replace(str, rule.first, rule.second);
103 const size_t units_per_k_unit{1024};
104 const std::vector<std::string> byte_units = {
" bytes",
"KB",
"MB",
"GB",
"TB",
"PB"};
105 const std::vector<size_t> bytes_per_scale_unit = {size_t(1),
112 if (bytes < units_per_k_unit) {
116 const size_t byte_scale = log(bytes) / log(units_per_k_unit);
119 const size_t scaled_bytes_left_of_decimal = bytes / bytes_per_scale_unit[byte_scale];
120 const size_t scaled_bytes_right_of_decimal = bytes % bytes_per_scale_unit[byte_scale];
121 const size_t fractional_digits =
static_cast<double>(scaled_bytes_right_of_decimal) /
122 bytes_per_scale_unit[byte_scale] * 100.;
129 return std::string(v);
137 std::pair<std::string_view, const char*>
substring(
const std::string& str,
138 size_t substr_length) {
142 const auto str_size = str.size();
143 if (substr_length >= str_size) {
146 std::string_view substr(str.c_str(), substr_length);
147 return {substr,
"..."};
151 static char charset[] =
153 "abcdefghijklmnopqrstuvwxyz"
154 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
156 static std::mt19937 prng{std::random_device{}()};
157 static std::uniform_int_distribution<size_t> dist(0, strlen(charset) - 1);
161 for (
size_t i = 0; i < len; i++) {
162 str += charset[dist(prng)];
171 std::vector<std::string>
split(std::string_view str,
172 std::string_view delim,
173 std::optional<size_t> maxsplit) {
174 std::vector<std::string>
result;
177 if (!delim.empty()) {
178 std::string::size_type i = 0, j = 0;
179 while ((i = str.find(delim, i)) != std::string::npos &&
180 (!maxsplit || result.size() < maxsplit.value())) {
181 result.emplace_back(str, j, i - j);
185 result.emplace_back(str, j, std::string::npos);
191 std::string::size_type i = 0, j = 0;
192 for (; i < str.size(); ++i) {
194 if (!isspace(str[i])) {
200 if (isspace(str[i])) {
202 result.emplace_back(str, j, i - j);
205 if ((maxsplit && result.size() == maxsplit.value())) {
207 result.emplace_back(str, j, std::string::npos);
214 result.emplace_back(str, j, std::string::npos);
221 std::string::size_type i, j;
222 for (i = 0; i < str.size() && std::isspace(str[i]); ++i) {
224 for (j = str.size(); j > i && std::isspace(str[j - 1]); --j) {
226 return str.substr(i, j - i);
229 std::string
strip(std::string_view str) {
236 std::vector<std::pair<size_t, size_t>>
const& literal_positions) {
237 const auto end = start + length;
238 for (
const auto& literal_position : literal_positions) {
239 if (literal_position.first <= start && end <= literal_position.second) {
240 return literal_position.second;
249 std::string& str) noexcept {
250 char inside_quote = 0;
251 bool previous_c_was_backslash =
false;
252 for (
auto& c : str) {
254 if (c ==
'\'' || c ==
'\"') {
256 if (!previous_c_was_backslash) {
258 if (inside_quote == c) {
261 }
else if (inside_quote == 0) {
266 }
else if (inside_quote == 0) {
268 if (c ==
'\n' || c ==
'\t' || c ==
'\r') {
276 previous_c_was_backslash = !previous_c_was_backslash;
278 previous_c_was_backslash =
false;
284 return (inside_quote == 0);
289 std::stringstream ss;
290 ss << std::quoted(filename, quote, escape);
297 auto sanitized_str{str};
298 for (
auto& c : sanitized_str) {
299 c = (c < 32) ?
' ' : c;
301 return sanitized_str;
DEVICE auto accumulate(ARGS &&...args)
std::string filename(char const *path)