OmniSciDB  c07336695a
StringTransform.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SHARED_STRINGTRANSFORM_H
18 #define SHARED_STRINGTRANSFORM_H
19 
20 #include "Logger.h"
21 
22 #include <boost/regex.hpp>
23 
24 #include <algorithm>
25 #include <iomanip>
26 #include <sstream>
27 #include <string>
28 
29 void apply_shim(std::string& result,
30  const boost::regex& reg_expr,
31  const std::function<void(std::string&, const boost::smatch&)>& shim_fn);
32 
33 std::vector<std::pair<size_t, size_t>> find_string_literals(const std::string& query);
34 
35 // Replace passwords, keys, etc. in a sql query with 'XXXXXXXX'.
36 std::string hide_sensitive_data_from_query(std::string const& query_str);
37 
38 ssize_t inside_string_literal(
39  const size_t start,
40  const size_t length,
41  const std::vector<std::pair<size_t, size_t>>& literal_positions);
42 
43 template <typename T>
44 std::string join(T const& container, std::string const& delim) {
45  std::stringstream ss;
46  if (!container.empty()) {
47  ss << container.front();
48  for (auto itr = std::next(container.cbegin()); itr != container.cend(); ++itr) {
49  ss << delim << *itr;
50  }
51  }
52  return ss.str();
53 }
54 
55 template <typename T>
56 std::string to_string(T&& v) {
57  std::ostringstream oss;
58  oss << v;
59  return oss.str();
60 }
61 
62 template <>
63 std::string to_string(char const*&& v);
64 
65 template <>
66 std::string to_string(std::string&& v);
67 
68 inline std::string to_upper(const std::string& str) {
69  auto str_uc = str;
70  std::transform(str_uc.begin(), str_uc.end(), str_uc.begin(), ::toupper);
71  return str_uc;
72 }
73 
74 std::string generate_random_string(const size_t len);
75 
76 // split apart a string into a vector of substrings
77 std::vector<std::string> split(const std::string& str, const std::string& delim);
78 
79 // trim any whitespace from the left and right ends of a string
80 std::string strip(const std::string& str);
81 
82 // sanitize an SQL string
84  std::string& str) noexcept;
85 
86 #endif // SHARED_STRINGTRANSFORM_H
void apply_shim(std::string &result, const boost::regex &reg_expr, const std::function< void(std::string &, const boost::smatch &)> &shim_fn)
std::vector< std::string > split(const std::string &str, const std::string &delim)
std::string join(T const &container, std::string const &delim)
std::string hide_sensitive_data_from_query(std::string const &query_str)
std::string to_string(T &&v)
std::string generate_random_string(const size_t len)
T v(const TargetValue &r)
std::vector< std::pair< size_t, size_t > > find_string_literals(const std::string &query)
ssize_t inside_string_literal(const size_t start, const size_t length, const std::vector< std::pair< size_t, size_t >> &literal_positions)
bool remove_unquoted_newlines_linefeeds_and_tabs_from_sql_string(std::string &str) noexcept
std::string to_upper(const std::string &str)
std::string strip(const std::string &str)