Samchon Framework for CPP  1.0.0
StringUtil.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <string>
5 #include <samchon/WeakString.hpp>
6 
7 #include <iostream>
8 #include <sstream>
9 #include <samchon/IndexPair.hpp>
10 #include <samchon/library/Math.hpp>
11 
12 namespace samchon
13 {
14 namespace library
15 {
37  class SAMCHON_FRAMEWORK_API StringUtil
38  {
39  public:
40  /* ----------------------------------------------------------------------
41  SUBSTITUTE
42  ---------------------------------------------------------------------- */
54  template <typename T, typename ... _Args>
55  static auto substitute(const std::string &format,
56  const T& val, const _Args& ... args) -> std::string
57  {
58  std::string &res = _substitute(format, val);
59 
60  return StringUtil::substitute(res, args...);
61  };
62  template <typename T> static auto substitute(const std::string &format, const T& val) -> std::string
63  {
64  return _substitute(format, val);
65  };
66 
80  template <typename T, typename ... _Args >
81  static auto substituteSQL(const std::string &format,
82  const T& value, const _Args& ... args) -> std::string
83  {
84  std::string &res = _substituteSQL(format, value);
85  return StringUtil::substituteSQL(res, args...);
86  };
87  template <typename T> static auto substituteSQL(const std::string &format, const T& value) -> std::string
88  {
89  return _substituteSQL(format, value);
90  };
91 
92  protected:
93  template <typename T> static auto _substitute(const std::string &format, const T& value) -> std::string
94  {
95  std::vector<std::string> &parenthesisArray = betweens(format, { (char)'{' }, { (char)'}' });
96  std::vector<long> vec;
97 
98  for (auto it = parenthesisArray.begin(); it != parenthesisArray.end(); it++)
99  if (isNumeric(*it) == true)
100  vec.push_back(stoi(*it));
101 
102  size_t index = (size_t)Math::minimum(vec).getValue();
103 
104  //replaceAll
105  std::string &to = toString(value);
106  return replaceAll(format, "{" + toString(index) + "}", to);
107  };
108  template <typename T> static auto _substituteSQL(const std::string &format, const T& value) -> std::string
109  {
110  std::vector<std::string> &parenthesisArray = betweens(format, "{", "}");
111  std::vector<long> vec;
112 
113  for (auto it = parenthesisArray.begin(); it != parenthesisArray.end(); it++)
114  if (isNumeric(*it) == true)
115  vec.push_back(stoi(*it));
116 
117  size_t index = (size_t)Math::minimum(vec).getValue();
118 
119  //replaceAll
120  std::string &to = toSQL(value);
121  return replaceAll(format, "{" + toString(index) + "}", to);
122  };
123 
124  /* ----------------------------------------------------------------------
125  SUBSTITUTE -> TO_STRING
126  ---------------------------------------------------------------------- */
127  template <typename T>
128  static auto toString(const T &val) -> std::string
129  {
130  std::basic_stringstream<char> stream;
131  stream << val;
132 
133  return stream.str();
134  };
135  template<> static auto toString(const WeakString &str) -> std::string;
136 
137  template <typename T>
138  static auto toSQL(const T &val) -> std::string
139  {
140  if (val == INT_MIN)
141  return "NULL";
142 
143  std::basic_stringstream<char> stream;
144  stream << val;
145 
146  return stream.str();
147  };
148  template<> static auto toSQL(const bool &flag) -> std::string;
149  template<> static auto toSQL(const char &val) -> std::string;
150  template<> static auto toSQL(const std::string &str) -> std::string;
151  template<> static auto toSQL(const WeakString &str) -> std::string;
152  static auto toSQL(const char *) -> std::string;
153 
154  public:
155  /* ----------------------------------------------------------------------
156  NUMBER-FORMAT
157  IN MONETARY UNIT, ADD DELIMETER ','
158  COLOR-FORMAT
159 
160  POSITIVE NUMBER IS RED,
161  NEGATIVE NUMBER IS BLUE
162  ZERO IS BLACK
163  ---------------------------------------------------------------------- */
170  static auto isNumeric(const std::string &str) -> bool;
171 
178  static auto toNumber(const std::string &str) -> double;
179 
191  static auto numberFormat(double val, int precision = 2) -> std::string;
192 
202  static auto percentFormat(double val, int precision = 2) -> std::string;
203 
219  static auto colorNumberFormat(double value, int precision = 2, double delimiter = 0.0) -> std::string;
220 
229  static auto colorPercentFormat(double value, int precision = 2, double delimiter = 0.0) -> std::string;
230 
231  /* ----------------------------------------------------------------------
232  TRIM -> WITH LTRIM & RTRIM
233  IT'S RIGHT, THE TRIM OF ORACLE
234  ---------------------------------------------------------------------- */
242  static auto trim(const std::string &val, const std::vector<std::string> &delims) -> std::string;
243 
251  static auto ltrim(const std::string &val, const std::vector<std::string> &delims) -> std::string;
252 
260  static auto rtrim(const std::string &val, const std::vector<std::string> &delims) -> std::string;
261 
262  static auto trim(const std::string &str) -> std::string;
263  static auto ltrim(const std::string &str) -> std::string;
264  static auto rtrim(const std::string &str) -> std::string;
265 
266  static auto trim(const std::string &str, const std::string &delim) -> std::string;
267  static auto ltrim(const std::string &str, const std::string &delim) -> std::string;
268  static auto rtrim(const std::string &str, const std::string &delim) -> std::string;
269 
270  /* ----------------------------------------------------------------------
271  EXTRACTORS
272  ---------------------------------------------------------------------- */
288  static auto finds(const std::string &str,
289  const std::vector<std::string> &delims, size_t startIndex = 0)->IndexPair<std::string>;
290 
306  static auto rfinds(const std::string &str,
307  const std::vector<std::string> &delims, size_t endIndex = SIZE_MAX)->IndexPair<std::string>;
308 
324  static auto substring(const std::string &str,
325  size_t startIndex, size_t endIndex = SIZE_MAX) -> std::string;
326 
346  static auto between(const std::string &str,
347  const std::string &start = "", const std::string &end = "") -> std::string;
348 
349  //TAB
357  static auto addTab(const std::string &str, size_t n = 1) -> std::string;
358 
359  //MULTIPLE STRINGS
368  static auto split(const std::string &str, const std::string &delim) -> std::vector<std::string>;
369 
388  static auto betweens
389  (
390  const std::string &str,
391  const std::string &start = "", const std::string &end = ""
392  ) -> std::vector<std::string>;
393 
394  /* ----------------------------------------------------------------------
395  REPLACERS
396  ---------------------------------------------------------------------- */
397  //ALPHABET-CONVERSION
404  static auto toLowerCase(const std::string &str) -> std::string;
405 
412  static auto toUpperCase(const std::string &str) -> std::string;
413 
422  static auto replaceAll
423  (
424  const std::string &str,
425  const std::string &before, const std::string &after
426  ) -> std::string;
427 
435  static auto replaceAll(const std::string &str,
436  const std::vector<std::pair<std::string, std::string>> &pairs) -> std::string;
437 
443  static auto removeHTMLSpaces(const std::string &) -> std::string;
444  };
445 };
446 };
static auto substituteSQL(const std::string &format, const T &value, const _Args &...args) -> std::string
Substitutes "{n}" tokens within the specified sql-string with the respective arguments passed in...
Definition: StringUtil.hpp:81
static auto substitute(const std::string &format, const T &val, const _Args &...args) -> std::string
Substitutes "{n}" tokens within the specified string with the respective arguments passed in...
Definition: StringUtil.hpp:55
static auto minimum(const _Cont &container) -> IndexPair< T >
Calculate minimum value with its index.
Definition: Math.hpp:143
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
A string class only references characeters, reference only.
Definition: WeakString.hpp:32
Utility class for string.
Definition: StringUtil.hpp:37