OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #ifndef __CUDACC__
21 #include <boost/config.hpp>
22 #if defined(_WIN32) && !defined(WIN32_LEAN_AND_MEAN)
23 // boost/regex.hpp on win32 includes Windows.h
24 // and we need to clean up macros such as ERROR and GetObject
25 #define WIN32_LEAN_AND_MEAN
26 #endif
27 
28 #include <boost/regex.hpp>
29 
30 #if defined(_WIN32)
32 #undef WIN32_LEAN_AND_MEAN
33 #endif
34 
35 #include <optional>
36 #include <string_view>
37 #endif // __CUDACC__
38 
39 #include <algorithm>
40 #include <iomanip>
41 #include <sstream>
42 #include <string>
43 #include <vector>
44 
45 #ifndef __CUDACC__
46 void apply_shim(std::string& result,
47  const boost::regex& reg_expr,
48  const std::function<void(std::string&, const boost::smatch&)>& shim_fn);
49 
50 // cat - Concatenate values of arbitrary types into a string.
51 template <typename... Ts>
52 std::string cat(Ts&&... args) {
53  std::ostringstream oss;
54  (oss << ... << std::forward<Ts>(args));
55  return oss.str();
56 }
57 #endif // __CUDACC__
58 
59 std::vector<std::pair<size_t, size_t>> find_string_literals(const std::string& query);
60 
61 // Replace passwords, keys, etc. in a sql query with 'XXXXXXXX'.
62 std::string hide_sensitive_data_from_query(std::string const& query_str);
63 
64 #ifndef __CUDACC__
65 std::optional<size_t> inside_string_literal(
66  const size_t start,
67  const size_t length,
68  const std::vector<std::pair<size_t, size_t>>& literal_positions);
69 #endif // __CUDACC__
70 
71 template <typename T>
72 std::string join(T const& container, std::string const& delim) {
73  std::stringstream ss;
74  if (!container.empty()) {
75  ss << *container.cbegin();
76  for (auto itr = std::next(container.cbegin()); itr != container.cend(); ++itr) {
77  ss << delim << *itr;
78  }
79  }
80  return ss.str();
81 }
82 
83 template <typename T>
84 std::string to_string(T&& v) {
85  std::ostringstream oss;
86  oss << v;
87  return oss.str();
88 }
89 
90 template <>
91 std::string to_string(char const*&& v);
92 
93 template <>
94 std::string to_string(std::string&& v);
95 
96 // NOTE(sy): to_upper/to_lower: As of Feb 2020, this is a solution recommended by Stack
97 // Overflow. Boost's to_upper_copy() is many times slower, maybe because it uses
98 // locale-aware std::toupper. Probably don't bother converting the input parameters to
99 // std::string_view because testing gave a small slowdown for std::string inputs and a
100 // small speedup for c-style string inputs for this usage.
101 
102 inline std::string to_upper(const std::string& str) {
103  auto str_uc = str;
104  std::transform(str_uc.begin(), str_uc.end(), str_uc.begin(), ::toupper);
105  return str_uc;
106 }
107 
108 inline std::string to_lower(const std::string& str) {
109  auto str_lc = str;
110  std::transform(str_lc.begin(), str_lc.end(), str_lc.begin(), ::tolower);
111  return str_lc;
112 }
113 
114 std::string generate_random_string(const size_t len);
115 
116 #ifndef __CUDACC__
117 std::vector<std::string> split(std::string_view str,
119  std::string_view delim = {},
120  std::optional<size_t> maxsplit = std::nullopt);
121 
123 std::string strip(std::string_view str);
124 #endif // __CUDACC__
125 
128  std::string& str) noexcept;
129 
131 #ifndef __CUDACC__
132 std::string simple_sanitize(const std::string& str);
133 #endif // __CUDACC__
134 
135 #ifndef __CUDACC__
136 std::string get_quoted_string(const std::string& filename,
138  char quote = '"',
139  char escape = '\\');
140 #endif // __CUDACC__
141 
142 #ifndef __CUDACC__
143 namespace {
144 
145 template <typename T>
146 inline decltype(auto) stringlike(T&& parm) {
147  // String.
148  if constexpr (std::is_base_of_v<std::string,
149  std::remove_reference_t<decltype(parm)>>) { // NOLINT
150  return std::forward<T>(parm);
151 
152  // Char Array.
153 
154  } else if constexpr (std::is_array_v<
155  std::remove_reference_t<decltype(parm)>>) { // NOLINT
156  return std::forward<T>(parm);
157 
158  // Char String.
159 
160  } else if constexpr (std::is_same_v<std::remove_reference_t<decltype(parm)>,
161  const char*> ||
162  std::is_same_v<std::remove_reference_t<decltype(parm)>,
163  char*>) { // NOLINT
164  return std::forward<T>(parm);
165 
166  // Integer or Floating Point.
167 
168  } else if constexpr (std::is_integral_v<std::remove_reference_t<decltype(parm)>> ||
169  std::is_floating_point_v<
170  std::remove_reference_t<decltype(parm)>>) { // NOLINT
171  return std::to_string(std::forward<T>(parm));
172  }
173 
174  // Unsupported type that will fail at compile-time.
175  else {
176  static_assert(std::is_base_of_v<void, decltype(parm)>);
177  return std::string(); // unreachable, but needed to avoid extra error messages
178  }
179 }
180 
181 } // anonymous namespace
182 
183 template <typename... Types>
184 std::string concat(Types&&... parms) {
185  struct Joiner {
186  Joiner() {}
187 
188  std::string txt;
189 
190  void append(std::string_view moretxt) { txt += moretxt; }
191  }; // struct Joiner
192  Joiner j{};
193  (j.append(stringlike(std::forward<Types>(parms))), ...);
194  return std::move(j.txt);
195 }
196 
197 template <typename... Types>
198 std::string concat_with(std::string_view with, Types&&... parms) {
199  struct JoinerWith {
200  JoinerWith(std::string_view join) : join(join), first(true) {}
201 
202  std::string_view join;
203  bool first;
204  std::string txt;
205 
206  void append(std::string_view moretxt) {
207  if (!first) {
208  txt += join;
209  } else {
210  first = false;
211  }
212  txt += moretxt;
213  }
214  }; // struct JoinerWith
215  JoinerWith j{with};
216  (j.append(stringlike(std::forward<Types>(parms))), ...);
217  return std::move(j.txt);
218 }
219 #endif // __CUDACC__
220 
221 #endif // SHARED_STRINGTRANSFORM_H
std::string hide_sensitive_data_from_query(std::string const &query_str)
std::string to_lower(const std::string &str)
std::vector< std::pair< size_t, size_t > > find_string_literals(const std::string &query)
size_t append(FILE *f, const size_t size, const int8_t *buf)
Appends the specified number of bytes to the end of the file f from buf.
Definition: File.cpp:159
std::optional< size_t > inside_string_literal(const size_t start, const size_t length, const std::vector< std::pair< size_t, size_t >> &literal_positions)
std::string cat(Ts &&...args)
std::string strip(std::string_view str)
trim any whitespace from the left and right ends of a string
std::string concat_with(std::string_view with, Types &&...parms)
std::string join(T const &container, std::string const &delim)
std::string concat(Types &&...parms)
std::string simple_sanitize(const std::string &str)
simple sanitize string (replace control characters with space)
std::string to_string(char const *&&v)
std::vector< std::string > split(std::string_view str, std::string_view delim, std::optional< size_t > maxsplit)
split apart a string into a vector of substrings
std::string get_quoted_string(const std::string &filename, char quote, char escape)
Quote a string while escaping any existing quotes in the string.
std::string generate_random_string(const size_t len)
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)
bool remove_unquoted_newlines_linefeeds_and_tabs_from_sql_string(std::string &str) noexcept
sanitize an SQL string