7 #include <samchon/WeakString.hpp> 8 #include <samchon/IndexPair.hpp> 9 #include <samchon/library/Math.hpp> 53 template <
typename T,
typename ... _Args>
55 const T& val,
const _Args& ... args) -> std::string
57 std::string &res = _substitute(format, val);
61 template <
typename T>
static auto substitute(
const std::string &format,
const T& val) -> std::string
63 return _substitute(format, val);
79 template <
typename T,
typename ... _Args >
81 const T& value,
const _Args& ... args) -> std::string
83 std::string &res = _substituteSQL(format, value);
86 template <
typename T>
static auto substituteSQL(
const std::string &format,
const T& value) -> std::string
88 return _substituteSQL(format, value);
92 template <
typename T>
static auto _substitute(
const std::string &format,
const T& value) -> std::string
94 std::vector<std::string> &parenthesisArray =
betweens(format, { (char)
'{' }, { (char)
'}' });
95 std::vector<long> vec;
97 for (
auto it = parenthesisArray.begin(); it != parenthesisArray.end(); it++)
99 vec.push_back(stoi(*it));
104 std::string &to = toString(value);
105 return replaceAll(format,
"{" + std::to_string(index) +
"}", to);
108 template <
typename T>
static auto _substituteSQL(
const std::string &format,
const T& value) -> std::string
110 std::vector<std::string> &parenthesisArray =
betweens(format,
"{",
"}");
111 std::vector<long> vec;
113 for (
auto it = parenthesisArray.begin(); it != parenthesisArray.end(); it++)
115 vec.push_back(stoi(*it));
120 std::string &to = toSQL(value);
121 return replaceAll(format,
"{" + std::to_string(index) +
"}", to);
127 template <
typename T>
128 static auto toString(
const T &val) -> std::string
130 return std::to_string(val);
132 template<>
static auto toString(
const std::string &str) -> std::string
136 template<>
static auto toString(
const WeakString &str) -> std::string
140 static auto toString(
const char *ptr) -> std::string
145 template <
typename T>
146 static auto toSQL(
const T &val) -> std::string
151 return std::to_string(val);
153 template<>
static auto toSQL(
const bool &flag) -> std::string
155 return std::to_string(flag);
157 template<>
static auto toSQL(
const char &val) -> std::string
159 return toSQL(std::string({ val }));
161 template<>
static auto toSQL(
const std::string &str) -> std::string
165 template<>
static auto toSQL(
const WeakString &wstr) -> std::string
167 if (wstr.
empty() ==
true)
171 if (wstr.
find(
"'") != std::string::npos)
172 return "'" + wstr.
replaceAll(
"'",
"''") +
"'";
174 return "'" + wstr.
str() +
"'";
177 static auto toSQL(
const char *ptr) -> std::string
179 return toSQL(std::string(ptr));
205 catch (
const std::exception &)
223 static auto toNumber(
const std::string &str) ->
double 225 std::string &numStr =
replaceAll(str,
",",
"");
246 val = round(val * pow(10, precision));
247 val = val / pow(10, precision);
250 bool is_negative = (val < 0);
251 unsigned long long natural = (
unsigned long long)abs(val);
252 double fraction = abs(val) - (
unsigned long long)abs(val);
260 size_t cipher_count = (size_t)log10(natural) + 1;
262 for (
size_t i = 0; i <= cipher_count; i++)
264 size_t cipher = natural % (size_t)pow(10, i + 1);
265 cipher = (size_t)(cipher / pow(10, i));
267 if (i == cipher_count && cipher == 0)
271 if (i > 0 && i % 3 == 0)
275 str = std::to_string(cipher) + str;
280 if (is_negative ==
true)
284 if (precision > 0 && fraction != 0)
286 fraction = (double)(
unsigned long long)round(fraction * pow(10, precision));
287 size_t zeros = precision - (size_t)log10(fraction) - 1;
289 str +=
"." + std::string(zeros,
'0') + std::to_string((
unsigned long long)fraction);
325 static auto colorNumberFormat(
double value,
int precision = 2,
double delimiter = 0.0) -> std::string
329 if (value > delimiter) color =
"red";
330 else if (value == delimiter) color =
"black";
335 "<font color='{1}'>{2}</font>",
349 static auto colorPercentFormat(
double value,
int precision = 2,
double delimiter = 0.0) -> std::string
353 if (value > delimiter) color =
"red";
354 else if (value == delimiter) color =
"black";
359 "<font color='{1}'>{2}</font>",
376 static auto trim(
const std::string &val,
const std::vector<std::string> &delims) -> std::string
388 static auto ltrim(
const std::string &val,
const std::vector<std::string> &delims) -> std::string
400 static auto rtrim(
const std::string &val,
const std::vector<std::string> &delims) -> std::string
405 static auto trim(
const std::string &str) -> std::string
409 static auto ltrim(
const std::string &str) -> std::string
413 static auto rtrim(
const std::string &str) -> std::string
418 static auto trim(
const std::string &str,
const std::string &delim) -> std::string
422 static auto ltrim(
const std::string &str,
const std::string &delim) -> std::string
426 static auto rtrim(
const std::string &str,
const std::string &delim) -> std::string
449 static auto finds(
const std::string &str,
472 static auto rfinds(
const std::string &str,
496 size_t startIndex,
size_t endIndex = SIZE_MAX) -> std::string
521 const std::string &start =
"",
const std::string &end =
"") -> std::string
534 static auto addTab(
const std::string &str,
size_t n = 1) -> std::string
536 std::vector<std::string> &lines =
split(str,
"\n");
542 val.reserve(val.size() + lines.size());
545 for (i = 0; i < n; i++)
548 for (i = 0; i < lines.size(); i++)
549 val.append(tab + lines[i] + ((i == lines.size() - 1) ?
"" :
"\n"));
563 static auto split(
const std::string &str,
const std::string &delim) -> std::vector<std::string>
567 std::vector<std::string> resArray(arr.size());
568 for (
size_t i = 0; i < arr.size(); i++)
569 resArray[i] = move(arr[i].str());
594 const std::string &str,
595 const std::string &start =
"",
const std::string &end =
"" 596 ) -> std::vector<std::string>
600 std::vector<std::string> resArray(arr.size());
601 for (
size_t i = 0; i < arr.size(); i++)
602 resArray[i] = move(arr[i].str());
643 const std::string &str,
644 const std::string &before,
const std::string &after
658 const std::vector<std::pair<std::string, std::string>> &pairs) -> std::string
670 std::vector<std::pair<std::string, std::string>> pairs =
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...
static auto percentFormat(double val, int precision=2) -> std::string
Returns a percentage string converted from the number rounded off from specified precision with "...
auto str() const -> std::string
Get the string content.
static auto ltrim(const std::string &val, const std::vector< std::string > &delims) -> std::string
Removes all designated characters from the beginning of the specified string.
static auto substring(const std::string &str, size_t startIndex, size_t endIndex=SIZE_MAX) -> std::string
Generates a substring.
auto empty() const -> bool
Tests wheter string is emtpy.
auto rfinds(const std::vector< std::string > &delims, size_t endIndex=SIZE_MAX) const -> IndexPair< WeakString >
Finds last occurence in string.
static auto toNumber(const std::string &str) -> double
Number std::string to Number having ',' symbols.
static auto rfinds(const std::string &str, const std::vector< std::string > &delims, size_t endIndex=SIZE_MAX) -> IndexPair< std::string >
Finds last occurence in string.
auto getValue() -> T &
Get reference of value.
auto replaceAll(const WeakString &before, const WeakString &after) const -> std::string
Returns a string specified word is replaced.
static auto addTab(const std::string &str, size_t n=1) -> std::string
Adds tab() character to first position of each line.
static auto colorPercentFormat(double value, int precision=2, double delimiter=0.0) -> std::string
Returns a percentage string converted from the number rounded off from specified precision with "...
auto toLowerCase() const -> std::string
Convert uppercase letters to lowercase.
auto betweens(const WeakString &start={}, const WeakString &end={}) const -> std::vector< WeakString >
Generates substrings.
static auto finds(const std::string &str, const std::vector< std::string > &delims, size_t startIndex=0) -> IndexPair< std::string >
Finds first occurence in string.
auto finds(const std::vector< std::string > &delims, size_t startIndex=0) const -> IndexPair< WeakString >
Finds first occurence in string.
static auto replaceAll(const std::string &str, const std::vector< std::pair< std::string, std::string >> &pairs) -> std::string
Returns a string specified words are replaced.
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...
static auto split(const std::string &str, const std::string &delim) -> std::vector< std::string >
Generates substrings.
static auto minimum(const _Cont &container) -> IndexPair< T >
Calculate minimum value with its index.
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
static auto numberFormat(double val, int precision=2) -> std::string
auto substring(size_t startIndex, size_t endIndex=SIZE_MAX) const -> WeakString
Generates a substring.
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
static auto rtrim(const std::string &val, const std::vector< std::string > &delims) -> std::string
Removes all designated characters from the end of the specified string.
A pair of index and its value(T)
static auto isNumeric(const std::string &str) -> bool
Returns wheter the std::string represents Number or not .
static auto colorNumberFormat(double value, int precision=2, double delimiter=0.0) -> std::string
Returns a string converted from the number rounded off from specified precision with "...
static auto betweens(const std::string &str, const std::string &start="", const std::string &end="") -> std::vector< std::string >
Generates substrings.
auto yoUpperCase() const -> std::string
Convert uppercase letters to lowercase.
static auto replaceAll(const std::string &str, const std::string &before, const std::string &after) -> std::string
Returns a string specified word is replaced.
auto split(const WeakString &delim) const -> std::vector< WeakString >
Generates substrings.
static auto between(const std::string &str, const std::string &start="", const std::string &end="") -> std::string
Generate a substring.
static auto trim(const std::string &val, const std::vector< std::string > &delims) -> std::string
Removes all designated characters from the beginning and end of the specified string.
auto get_index() const -> size_t
Get index.
A string class only references characeters, reference only.
static auto removeHTMLSpaces(const std::string &str) -> std::string
Replace all HTML spaces to a literal space.
static auto toLowerCase(const std::string &str) -> std::string
Returns a string that all uppercase characters are converted to lowercase.
static auto yoUpperCase(const std::string &str) -> std::string
Utility class for string.