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