OmniSciDB  ca0c39ec8f
 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 2022 HEAVY.AI, 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 #include <optional>
23 #include <string_view>
24 
26 #endif // __CUDACC__
27 
28 #include <algorithm>
29 #include <iomanip>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 
34 #ifndef __CUDACC__
35 void apply_shim(std::string& result,
36  const boost::regex& reg_expr,
37  const std::function<void(std::string&, const boost::smatch&)>& shim_fn);
38 
39 // cat - Concatenate values of arbitrary types into a string.
40 template <typename... Ts>
41 std::string cat(Ts&&... args) {
42  std::ostringstream oss;
43  (oss << ... << std::forward<Ts>(args));
44  return oss.str();
45 }
46 #endif // __CUDACC__
47 
48 std::vector<std::pair<size_t, size_t>> find_string_literals(const std::string& query);
49 
50 // Replace passwords, keys, etc. in a sql query with 'XXXXXXXX'.
51 std::string hide_sensitive_data_from_query(std::string const& query_str);
52 
53 #ifndef __CUDACC__
54 std::optional<size_t> inside_string_literal(
55  const size_t start,
56  const size_t length,
57  const std::vector<std::pair<size_t, size_t>>& literal_positions);
58 #endif // __CUDACC__
59 
60 template <typename T>
61 std::string join(T const& container, std::string const& delim) {
62  std::stringstream ss;
63  if (!container.empty()) {
64  ss << *container.cbegin();
65  for (auto itr = std::next(container.cbegin()); itr != container.cend(); ++itr) {
66  ss << delim << *itr;
67  }
68  }
69  return ss.str();
70 }
71 
72 template <typename T>
73 std::string to_string(T&& v) {
74  std::ostringstream oss;
75  oss << v;
76  return oss.str();
77 }
78 
79 template <>
80 std::string to_string(char const*&& v);
81 
82 template <>
83 std::string to_string(std::string&& v);
84 
85 // NOTE(sy): to_upper/to_lower: As of Feb 2020, this is a solution recommended by Stack
86 // Overflow. Boost's to_upper_copy() is many times slower, maybe because it uses
87 // locale-aware std::toupper. Probably don't bother converting the input parameters to
88 // std::string_view because testing gave a small slowdown for std::string inputs and a
89 // small speedup for c-style string inputs for this usage.
90 
91 inline std::string to_upper(const std::string& str) {
92  auto str_uc = str;
93  std::transform(str_uc.begin(), str_uc.end(), str_uc.begin(), ::toupper);
94  return str_uc;
95 }
96 
97 inline std::string to_lower(const std::string& str) {
98  auto str_lc = str;
99  std::transform(str_lc.begin(), str_lc.end(), str_lc.begin(), ::tolower);
100  return str_lc;
101 }
102 
103 std::string generate_random_string(const size_t len);
104 
105 #ifndef __CUDACC__
106 std::vector<std::string> split(std::string_view str,
108  std::string_view delim = {},
109  std::optional<size_t> maxsplit = std::nullopt);
110 
112 std::string_view sv_strip(std::string_view str);
113 
115 std::string strip(std::string_view str);
116 
118 std::pair<std::string_view, const char*> substring(const std::string& str,
119  size_t substr_length);
120 #endif // __CUDACC__
121 
124  std::string& str) noexcept;
125 
127 #ifndef __CUDACC__
128 std::string simple_sanitize(const std::string& str);
129 #endif // __CUDACC__
130 
131 #ifndef __CUDACC__
132 std::string get_quoted_string(const std::string& filename,
134  char quote = '"',
135  char escape = '\\');
136 #endif // __CUDACC__
137 
138 #ifndef __CUDACC__
139 namespace {
140 
141 template <typename T>
142 inline decltype(auto) stringlike(T&& parm) {
143  // String.
144  if constexpr (std::is_base_of_v<std::string,
145  std::remove_reference_t<decltype(parm)>>) { // NOLINT
146  return std::forward<T>(parm);
147 
148  // Char Array.
149 
150  } else if constexpr (std::is_array_v<
151  std::remove_reference_t<decltype(parm)>>) { // NOLINT
152  return std::forward<T>(parm);
153 
154  // Char String.
155 
156  } else if constexpr (std::is_same_v<std::remove_reference_t<decltype(parm)>,
157  const char*> ||
158  std::is_same_v<std::remove_reference_t<decltype(parm)>,
159  char*>) { // NOLINT
160  return std::forward<T>(parm);
161 
162  // Integer or Floating Point.
163 
164  } else if constexpr (std::is_integral_v<std::remove_reference_t<decltype(parm)>> ||
165  std::is_floating_point_v<
166  std::remove_reference_t<decltype(parm)>>) { // NOLINT
167  return std::to_string(std::forward<T>(parm));
168  }
169 
170  // Unsupported type that will fail at compile-time.
171  else {
172  static_assert(std::is_base_of_v<void, decltype(parm)>);
173  return std::string(); // unreachable, but needed to avoid extra error messages
174  }
175 }
176 
177 } // anonymous namespace
178 
179 template <typename... Types>
180 std::string concat(Types&&... parms) {
181  struct Joiner {
182  Joiner() {}
183 
184  std::string txt;
185 
186  void append(std::string_view moretxt) { txt += moretxt; }
187  }; // struct Joiner
188  Joiner j{};
189  (j.append(stringlike(std::forward<Types>(parms))), ...);
190  return std::move(j.txt);
191 }
192 
193 template <typename... Types>
194 std::string concat_with(std::string_view with, Types&&... parms) {
195  struct JoinerWith {
196  JoinerWith(std::string_view join) : join(join), first(true) {}
197 
198  std::string_view join;
199  bool first;
200  std::string txt;
201 
202  void append(std::string_view moretxt) {
203  if (!first) {
204  txt += join;
205  } else {
206  first = false;
207  }
208  txt += moretxt;
209  }
210  }; // struct JoinerWith
211  JoinerWith j{with};
212  (j.append(stringlike(std::forward<Types>(parms))), ...);
213  return std::move(j.txt);
214 }
215 #endif // __CUDACC__
216 
217 #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:168
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_view sv_strip(std::string_view str)
return trimmed string_view
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)
OUTPUT transform(INPUT const &input, FUNC const &func)
Definition: misc.h:296
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
std::pair< std::string_view, const char * > substring(const std::string &str, size_t substr_length)
return substring of str with postfix if str.size() &gt; substr_length