OmniSciDB  72c90bc290
 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 std::string format_num_bytes(const size_t bytes);
54 
55 #ifndef __CUDACC__
56 std::optional<size_t> inside_string_literal(
57  const size_t start,
58  const size_t length,
59  const std::vector<std::pair<size_t, size_t>>& literal_positions);
60 #endif // __CUDACC__
61 
62 template <typename T>
63 std::string join(T const& container, std::string const& delim) {
64  std::stringstream ss;
65  if (!container.empty()) {
66  ss << *container.cbegin();
67  for (auto itr = std::next(container.cbegin()); itr != container.cend(); ++itr) {
68  ss << delim << *itr;
69  }
70  }
71  return ss.str();
72 }
73 
74 template <typename T>
75 std::string to_string(T&& v) {
76  std::ostringstream oss;
77  oss << v;
78  return oss.str();
79 }
80 
81 template <>
82 std::string to_string(char const*&& v);
83 
84 template <>
85 std::string to_string(std::string&& v);
86 
87 // NOTE(sy): to_upper/to_lower: As of Feb 2020, this is a solution recommended by Stack
88 // Overflow. Boost's to_upper_copy() is many times slower, maybe because it uses
89 // locale-aware std::toupper. Probably don't bother converting the input parameters to
90 // std::string_view because testing gave a small slowdown for std::string inputs and a
91 // small speedup for c-style string inputs for this usage.
92 
93 inline std::string to_upper(const std::string& str) {
94  auto str_uc = str;
95  std::transform(str_uc.begin(), str_uc.end(), str_uc.begin(), ::toupper);
96  return str_uc;
97 }
98 
99 inline std::string to_lower(const std::string& str) {
100  auto str_lc = str;
101  std::transform(str_lc.begin(), str_lc.end(), str_lc.begin(), ::tolower);
102  return str_lc;
103 }
104 
105 std::string generate_random_string(const size_t len);
106 
107 #ifndef __CUDACC__
108 std::vector<std::string> split(std::string_view str,
110  std::string_view delim = {},
111  std::optional<size_t> maxsplit = std::nullopt);
112 
114 std::string_view sv_strip(std::string_view str);
115 
117 std::string strip(std::string_view str);
118 
120 std::pair<std::string_view, const char*> substring(const std::string& str,
121  size_t substr_length);
122 #endif // __CUDACC__
123 
126  std::string& str) noexcept;
127 
129 #ifndef __CUDACC__
130 std::string simple_sanitize(const std::string& str);
131 #endif // __CUDACC__
132 
133 #ifndef __CUDACC__
134 std::string get_quoted_string(const std::string& filename,
136  char quote = '"',
137  char escape = '\\');
138 #endif // __CUDACC__
139 
140 #ifndef __CUDACC__
141 namespace {
142 
143 template <typename T>
144 inline decltype(auto) stringlike(T&& parm) {
145  // String.
146  if constexpr (std::is_base_of_v<std::string,
147  std::remove_reference_t<decltype(parm)>>) { // NOLINT
148  return std::forward<T>(parm);
149 
150  // Char Array.
151 
152  } else if constexpr (std::is_array_v<
153  std::remove_reference_t<decltype(parm)>>) { // NOLINT
154  return std::forward<T>(parm);
155 
156  // Char String.
157 
158  } else if constexpr (std::is_same_v<std::remove_reference_t<decltype(parm)>,
159  const char*> ||
160  std::is_same_v<std::remove_reference_t<decltype(parm)>,
161  char*>) { // NOLINT
162  return std::forward<T>(parm);
163 
164  // Integer or Floating Point.
165 
166  } else if constexpr (std::is_integral_v<std::remove_reference_t<decltype(parm)>> ||
167  std::is_floating_point_v<
168  std::remove_reference_t<decltype(parm)>>) { // NOLINT
169  return std::to_string(std::forward<T>(parm));
170  }
171 
172  // Unsupported type that will fail at compile-time.
173  else {
174  static_assert(std::is_base_of_v<void, decltype(parm)>);
175  return std::string(); // unreachable, but needed to avoid extra error messages
176  }
177 }
178 
179 } // anonymous namespace
180 
181 template <typename... Types>
182 std::string concat(Types&&... parms) {
183  struct Joiner {
184  Joiner() {}
185 
186  std::string txt;
187 
188  void append(std::string_view moretxt) { txt += moretxt; }
189  }; // struct Joiner
190  Joiner j{};
191  (j.append(stringlike(std::forward<Types>(parms))), ...);
192  return std::move(j.txt);
193 }
194 
195 template <typename... Types>
196 std::string concat_with(std::string_view with, Types&&... parms) {
197  struct JoinerWith {
198  JoinerWith(std::string_view join) : join(join), first(true) {}
199 
200  std::string_view join;
201  bool first;
202  std::string txt;
203 
204  void append(std::string_view moretxt) {
205  if (!first) {
206  txt += join;
207  } else {
208  first = false;
209  }
210  txt += moretxt;
211  }
212  }; // struct JoinerWith
213  JoinerWith j{with};
214  (j.append(stringlike(std::forward<Types>(parms))), ...);
215  return std::move(j.txt);
216 }
217 #endif // __CUDACC__
218 
219 #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:158
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:320
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::string format_num_bytes(const size_t bytes)
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