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