20 #include <boost/algorithm/string/classification.hpp>
21 #include <boost/algorithm/string/split.hpp>
28 #include <boost/filesystem.hpp>
33 const boost::regex& reg_expr,
34 const std::function<
void(std::string&,
const boost::smatch&)>& shim_fn) {
37 auto start_it = result.cbegin();
38 auto end_it = result.cend();
40 if (!boost::regex_search(start_it, end_it, what, reg_expr)) {
43 const auto next_start =
46 start_it = result.cbegin() + *next_start;
48 shim_fn(result, what);
50 start_it = result.cbegin();
51 end_it = result.cend();
57 boost::regex literal_string_regex{R
"(([^']+)('(?:[^']+|'')+'))", boost::regex::perl};
59 auto it = query.begin();
61 std::vector<std::pair<size_t, size_t>> positions;
64 if (!boost::regex_search(it, query.end(), what, literal_string_regex)) {
67 }
catch (
const std::exception& e) {
68 LOG(
WARNING) <<
"Error processing literals: " << e.what()
69 <<
"\nContinuing query parse...";
79 positions.emplace_back(prev_it + what[1].length() - query.begin(),
86 constexpr std::regex::flag_type flags =
87 std::regex::ECMAScript | std::regex::icase | std::regex::optimize;
88 static const std::initializer_list<std::pair<std::regex, std::string>> rules{
90 R
"(\b((?:password|s3_access_key|s3_secret_key|s3_session_token|username|credential_string)\s*=\s*)'.+?')",
93 {std::regex(R
"((\\set_license\s+)\S+)", flags), "$1XXXXXXXX"}};
95 rules.begin(), rules.end(), query_str, [](
auto& str,
auto& rule) {
96 return std::regex_replace(str, rule.first, rule.second);
102 return std::string(v);
110 std::pair<std::string_view, const char*>
substring(
const std::string& str,
111 size_t substr_length) {
115 const auto str_size = str.size();
116 if (substr_length >= str_size) {
119 std::string_view substr(str.c_str(), substr_length);
120 return {substr,
"..."};
124 static char charset[] =
126 "abcdefghijklmnopqrstuvwxyz"
127 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
129 static std::mt19937 prng{std::random_device{}()};
130 static std::uniform_int_distribution<size_t> dist(0, strlen(charset) - 1);
134 for (
size_t i = 0; i < len; i++) {
135 str += charset[dist(prng)];
144 std::vector<std::string>
split(std::string_view str,
145 std::string_view delim,
146 std::optional<size_t> maxsplit) {
147 std::vector<std::string>
result;
150 if (!delim.empty()) {
151 std::string::size_type i = 0, j = 0;
152 while ((i = str.find(delim, i)) != std::string::npos &&
153 (!maxsplit || result.size() < maxsplit.value())) {
154 result.emplace_back(str, j, i - j);
158 result.emplace_back(str, j, std::string::npos);
164 std::string::size_type i = 0, j = 0;
165 for (; i < str.size(); ++i) {
167 if (!isspace(str[i])) {
173 if (isspace(str[i])) {
175 result.emplace_back(str, j, i - j);
178 if ((maxsplit && result.size() == maxsplit.value())) {
180 result.emplace_back(str, j, std::string::npos);
187 result.emplace_back(str, j, std::string::npos);
194 std::string::size_type i, j;
195 for (i = 0; i < str.size() && std::isspace(str[i]); ++i) {
197 for (j = str.size(); j > i && std::isspace(str[j - 1]); --j) {
199 return str.substr(i, j - i);
202 std::string
strip(std::string_view str) {
209 const std::vector<std::pair<size_t, size_t>>& literal_positions) {
210 const auto end = start + length;
211 for (
const auto& literal_position : literal_positions) {
212 if (literal_position.first <= start && end <= literal_position.second) {
213 return literal_position.second;
222 std::string& str) noexcept {
223 char inside_quote = 0;
224 bool previous_c_was_backslash =
false;
225 for (
auto& c : str) {
227 if (c ==
'\'' || c ==
'\"') {
229 if (!previous_c_was_backslash) {
231 if (inside_quote == c) {
234 }
else if (inside_quote == 0) {
239 }
else if (inside_quote == 0) {
241 if (c ==
'\n' || c ==
'\t' || c ==
'\r') {
249 previous_c_was_backslash = !previous_c_was_backslash;
251 previous_c_was_backslash =
false;
257 return (inside_quote == 0);
262 std::stringstream ss;
263 ss << std::quoted(filename, quote, escape);
270 auto sanitized_str{str};
271 for (
auto& c : sanitized_str) {
272 c = (c < 32) ?
' ' : c;
274 return sanitized_str;
DEVICE auto accumulate(ARGS &&...args)
std::string filename(char const *path)