OmniSciDB  a47db9e897
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 // NOTE: check for C++17 is required here because Parser/ is pinned to C++14.
30 #if __cplusplus >= 201703L
31 #include <string_view>
32 #endif // __cplusplus >= 201703L
33 
34 void apply_shim(std::string& result,
35  const boost::regex& reg_expr,
36  const std::function<void(std::string&, const boost::smatch&)>& shim_fn);
37 
38 std::vector<std::pair<size_t, size_t>> find_string_literals(const std::string& query);
39 
40 // Replace passwords, keys, etc. in a sql query with 'XXXXXXXX'.
41 std::string hide_sensitive_data_from_query(std::string const& query_str);
42 
43 ssize_t inside_string_literal(
44  const size_t start,
45  const size_t length,
46  const std::vector<std::pair<size_t, size_t>>& literal_positions);
47 
48 template <typename T>
49 std::string join(T const& container, std::string const& delim) {
50  std::stringstream ss;
51  if (!container.empty()) {
52  ss << container.front();
53  for (auto itr = std::next(container.cbegin()); itr != container.cend(); ++itr) {
54  ss << delim << *itr;
55  }
56  }
57  return ss.str();
58 }
59 
60 template <typename T>
61 std::string to_string(T&& v) {
62  std::ostringstream oss;
63  oss << v;
64  return oss.str();
65 }
66 
67 template <>
68 std::string to_string(char const*&& v);
69 
70 template <>
71 std::string to_string(std::string&& v);
72 
73 inline std::string to_upper(const std::string& str) {
74  auto str_uc = str;
75  std::transform(str_uc.begin(), str_uc.end(), str_uc.begin(), ::toupper);
76  return str_uc;
77 }
78 
79 std::string generate_random_string(const size_t len);
80 
82 std::vector<std::string> split(const std::string& str, const std::string& delim);
83 
85 std::string strip(const std::string& str);
86 
89  std::string& str) noexcept;
90 
91 // NOTE: this check for C++17 is required because Parser/ is pinned to C++14.
92 #if __cplusplus >= 201703L
93 namespace {
94 
95 template <typename T>
96 inline decltype(auto) stringlike(T&& parm) {
97  // String.
98  if constexpr (std::is_base_of_v<std::string, std::remove_reference_t<decltype(parm)>>) {
99  return std::forward<T>(parm);
100  }
101 
102  // Char Array.
103  else if constexpr (std::is_array_v<std::remove_reference_t<decltype(parm)>>) {
104  return std::forward<T>(parm);
105  }
106 
107  // Char String.
108  else if constexpr (std::is_same_v<std::remove_reference_t<decltype(parm)>,
109  const char*> ||
110  std::is_same_v<std::remove_reference_t<decltype(parm)>, char*>) {
111  return std::forward<T>(parm);
112  }
113 
114  // Integer or Floating Point.
115  else if constexpr (std::is_integral_v<std::remove_reference_t<decltype(parm)>> ||
116  std::is_floating_point_v<std::remove_reference_t<decltype(parm)>>) {
117  return std::to_string(std::forward<T>(parm));
118  }
119 
120  // Unsupported type that will fail at compile-time.
121  else {
122  static_assert(std::is_base_of_v<void, decltype(parm)>);
123  return std::string(); // unreachable, but needed to avoid extra error messages
124  }
125 }
126 
127 } // anonymous namespace
128 
129 template <typename... Types>
130 std::string concat(Types&&... parms) {
131  struct Joiner {
132  Joiner() {}
133 
134  std::string txt;
135 
136  void append(std::string_view moretxt) { txt += moretxt; }
137  }; // struct Joiner
138  Joiner j{};
139  (j.append(stringlike(std::forward<Types>(parms))), ...);
140  return std::move(j.txt);
141 }
142 
143 template <typename... Types>
144 std::string concat_with(std::string_view with, Types&&... parms) {
145  struct JoinerWith {
146  JoinerWith(std::string_view join) : join(join), first(true) {}
147 
148  std::string_view join;
149  bool first;
150  std::string txt;
151 
152  void append(std::string_view moretxt) {
153  if (!first) {
154  txt += join;
155  } else {
156  first = false;
157  }
158  txt += moretxt;
159  }
160  }; // struct JoinerWith
161  JoinerWith j{with};
162  (j.append(stringlike(std::forward<Types>(parms))), ...);
163  return std::move(j.txt);
164 }
165 #endif // __cplusplus >= 201703L
166 
167 #endif // SHARED_STRINGTRANSFORM_H
std::string hide_sensitive_data_from_query(std::string const &query_str)
ssize_t inside_string_literal(const size_t start, const size_t length, const std::vector< std::pair< size_t, size_t >> &literal_positions)
std::vector< std::pair< size_t, size_t > > find_string_literals(const std::string &query)
std::string strip(const std::string &str)
trim any whitespace from the left and right ends of a string
std::string join(T const &container, std::string const &delim)
std::string to_string(char const *&&v)
std::string generate_random_string(const size_t len)
size_t append(FILE *f, const size_t size, int8_t *buf)
Appends the specified number of bytes to the end of the file f from buf.
Definition: File.cpp:136
bool g_enable_smem_group_by true
std::string to_upper(const std::string &str)
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)
split apart a string into a vector of substrings
bool remove_unquoted_newlines_linefeeds_and_tabs_from_sql_string(std::string &str) noexcept
sanitize an SQL string