25 #include <boost/filesystem.hpp> 30 const boost::regex& reg_expr,
31 const std::function<
void(std::string&,
const boost::smatch&)>& shim_fn) {
34 auto start_it = result.cbegin();
35 auto end_it = result.cend();
37 if (!boost::regex_search(start_it, end_it, what, reg_expr)) {
40 const auto next_start =
43 start_it = result.cbegin() + *next_start;
45 shim_fn(result, what);
47 start_it = result.cbegin();
48 end_it = result.cend();
54 boost::regex literal_string_regex{R
"(([^']+)('(?:[^']+|'')+'))", boost::regex::perl}; 56 auto it = query.begin();
58 std::vector<std::pair<size_t, size_t>> positions;
60 if (!boost::regex_search(it, query.end(), what, literal_string_regex)) {
66 positions.emplace_back(prev_it + what[1].length() - query.begin(),
73 constexpr std::regex::flag_type flags =
74 std::regex::ECMAScript | std::regex::icase | std::regex::optimize;
75 static const std::initializer_list<std::pair<std::regex, std::string>> rules{
76 {std::regex(R
"(\b((?:password|s3_access_key|s3_secret_key)\s*=\s*)'.+?')", flags), 78 {std::regex(R
"((\\set_license\s+)\S+)", flags), "$1XXXXXXXX"}};
80 rules.begin(), rules.end(), query_str, [](
auto& str,
auto& rule) {
81 return std::regex_replace(str, rule.first, rule.second);
87 return std::string(v);
96 static char charset[] =
98 "abcdefghijklmnopqrstuvwxyz" 99 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
101 static std::mt19937 prng{std::random_device{}()};
102 static std::uniform_int_distribution<size_t> dist(0, strlen(charset) - 1);
106 for (
size_t i = 0; i < len; i++) {
107 str += charset[dist(prng)];
116 std::vector<std::string>
split(std::string_view str,
117 std::string_view delim,
118 std::optional<size_t> maxsplit) {
119 std::vector<std::string>
result;
122 if (!delim.empty()) {
123 std::string::size_type i = 0, j = 0;
124 while ((i = str.find(delim, i)) != std::string::npos &&
125 (!maxsplit || result.size() < maxsplit.value())) {
126 result.emplace_back(str, j, i - j);
130 result.emplace_back(str, j, std::string::npos);
136 std::string::size_type i = 0, j = 0;
137 for (; i < str.size(); ++i) {
139 if (!isspace(str[i])) {
145 if (isspace(str[i])) {
147 result.emplace_back(str, j, i - j);
150 if ((maxsplit && result.size() == maxsplit.value())) {
152 result.emplace_back(str, j, std::string::npos);
159 result.emplace_back(str, j, std::string::npos);
165 std::string
strip(std::string_view str) {
166 std::string::size_type i, j;
167 for (i = 0; i < str.size() && std::isspace(str[i]); ++i) {
169 for (j = str.size(); j > i && std::isspace(str[j - 1]); --j) {
171 return std::string(str.substr(i, j - i));
177 const std::vector<std::pair<size_t, size_t>>& literal_positions) {
178 const auto end = start + length;
179 for (
const auto& literal_position : literal_positions) {
180 if (literal_position.first <= start &&
end <= literal_position.second) {
181 return literal_position.second;
190 std::string& str) noexcept {
191 char inside_quote = 0;
192 bool previous_c_was_backslash =
false;
193 for (
auto& c : str) {
195 if (c ==
'\'' || c ==
'\"') {
197 if (!previous_c_was_backslash) {
199 if (inside_quote == c) {
202 }
else if (inside_quote == 0) {
207 }
else if (inside_quote == 0) {
209 if (c ==
'\n' || c ==
'\t' || c ==
'\r') {
217 previous_c_was_backslash = !previous_c_was_backslash;
219 previous_c_was_backslash =
false;
225 return (inside_quote == 0);
230 std::stringstream ss;
231 ss << std::quoted(filename, quote, escape);
std::string filename(char const *path)
DEVICE auto accumulate(ARGS &&... args)