OmniSciDB  085a039ca4
 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 #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 strip(std::string_view str);
113 
115 std::pair<std::string_view, const char*> substring(const std::string& str,
116  size_t substr_length);
117 #endif // __CUDACC__
118 
121  std::string& str) noexcept;
122 
124 #ifndef __CUDACC__
125 std::string simple_sanitize(const std::string& str);
126 #endif // __CUDACC__
127 
128 #ifndef __CUDACC__
129 std::string get_quoted_string(const std::string& filename,
131  char quote = '"',
132  char escape = '\\');
133 #endif // __CUDACC__
134 
135 #ifndef __CUDACC__
136 namespace {
137 
138 template <typename T>
139 inline decltype(auto) stringlike(T&& parm) {
140  // String.
141  if constexpr (std::is_base_of_v<std::string,
142  std::remove_reference_t<decltype(parm)>>) { // NOLINT
143  return std::forward<T>(parm);
144 
145  // Char Array.
146 
147  } else if constexpr (std::is_array_v<
148  std::remove_reference_t<decltype(parm)>>) { // NOLINT
149  return std::forward<T>(parm);
150 
151  // Char String.
152 
153  } else if constexpr (std::is_same_v<std::remove_reference_t<decltype(parm)>,
154  const char*> ||
155  std::is_same_v<std::remove_reference_t<decltype(parm)>,
156  char*>) { // NOLINT
157  return std::forward<T>(parm);
158 
159  // Integer or Floating Point.
160 
161  } else if constexpr (std::is_integral_v<std::remove_reference_t<decltype(parm)>> ||
162  std::is_floating_point_v<
163  std::remove_reference_t<decltype(parm)>>) { // NOLINT
164  return std::to_string(std::forward<T>(parm));
165  }
166 
167  // Unsupported type that will fail at compile-time.
168  else {
169  static_assert(std::is_base_of_v<void, decltype(parm)>);
170  return std::string(); // unreachable, but needed to avoid extra error messages
171  }
172 }
173 
174 } // anonymous namespace
175 
176 template <typename... Types>
177 std::string concat(Types&&... parms) {
178  struct Joiner {
179  Joiner() {}
180 
181  std::string txt;
182 
183  void append(std::string_view moretxt) { txt += moretxt; }
184  }; // struct Joiner
185  Joiner j{};
186  (j.append(stringlike(std::forward<Types>(parms))), ...);
187  return std::move(j.txt);
188 }
189 
190 template <typename... Types>
191 std::string concat_with(std::string_view with, Types&&... parms) {
192  struct JoinerWith {
193  JoinerWith(std::string_view join) : join(join), first(true) {}
194 
195  std::string_view join;
196  bool first;
197  std::string txt;
198 
199  void append(std::string_view moretxt) {
200  if (!first) {
201  txt += join;
202  } else {
203  first = false;
204  }
205  txt += moretxt;
206  }
207  }; // struct JoinerWith
208  JoinerWith j{with};
209  (j.append(stringlike(std::forward<Types>(parms))), ...);
210  return std::move(j.txt);
211 }
212 #endif // __CUDACC__
213 
214 #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 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:297
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