1 #include <samchon/library/StringUtil.hpp> 14 template<>
auto StringUtil::toString(
const WeakString &str) ->
string 20 template<>
auto StringUtil::toSQL(
const bool &flag) ->
string 22 return toString(flag);
24 template<>
auto StringUtil::toSQL(
const char &val) ->
string 26 return toSQL(
string({val}));
28 auto StringUtil::toSQL(
const char *ptr) ->
string 30 return toSQL(
string(ptr));
33 template<>
auto StringUtil::toSQL(
const string &str) ->
string 37 template<>
auto StringUtil::toSQL(
const WeakString &wstr) ->
string 39 if (wstr.empty() ==
true)
43 if (wstr.find(
"'") != string::npos)
44 return "'" + wstr.replaceAll(
"'",
"''") +
"'";
46 return "'" + wstr.str() +
"'";
59 auto StringUtil::isNumeric(
const string &str) ->
bool 66 catch (
const std::exception &)
77 auto StringUtil::toNumber(
const string &str) ->
double 79 string &numStr = replaceAll(str,
",",
"");
83 auto StringUtil::numberFormat(
double val,
int precision) ->
string 88 else if (val == INT_MIN)
94 bool isNegative = (val < 0);
97 int cipher = (int)log10(val) + 1;
100 if (val != (
unsigned long long)val)
102 int pointValue = (int)round((val - (
unsigned long long)val) * pow(10.0, (
double)precision));
103 str =
"." + to_string(pointValue);
107 for (
int i = 0; i < cipher; i++)
109 int num = (int)((
unsigned long long)val % (
unsigned long long)pow(10.0, i + 1.0));
110 num = (int)(num / pow(10.0, (
double)i));
112 str = (char)(num +
'0') + str;
113 if((i + 1) % 3 == 0 && i < cipher - 1)
118 if(isNegative ==
true)
123 auto StringUtil::percentFormat(
double val,
int precision) ->
string 127 return numberFormat(val * 100, precision) +
"%";
130 auto StringUtil::colorNumberFormat(
double value,
int precision,
double delimiter) ->
string 134 if (value > delimiter) color =
"red";
135 else if (value == delimiter) color =
"black";
140 "<font color='{1}'>{2}</font>",
142 numberFormat(value, precision)
145 auto StringUtil::colorPercentFormat(
double value,
int precision,
double delimiter) ->
string 149 if (value > delimiter) color =
"red";
150 else if (value == delimiter) color =
"black";
155 "<font color='{1}'>{2}</font>",
157 percentFormat(value, precision)
165 auto StringUtil::trim(
const string &val,
const vector<string> &delims) ->
string 169 auto StringUtil::ltrim(
const string &val,
const vector<string> &delims) ->
string 173 auto StringUtil::rtrim(
const string &val,
const vector<string> &delims) ->
string 178 auto StringUtil::trim(
const string &str) ->
string 182 auto StringUtil::ltrim(
const string &str) ->
string 186 auto StringUtil::rtrim(
const string &str) ->
string 191 auto StringUtil::trim(
const string &str,
const string &delim) ->
string 195 auto StringUtil::ltrim(
const string &str,
const string &delim)->string
199 auto StringUtil::rtrim(
const string &str,
const string &delim)->string
208 auto StringUtil::finds(
const string &str,
214 auto StringUtil::rfinds(
const string &str,
222 auto StringUtil::substring(
const string &str,
223 size_t startIndex,
size_t endIndex) ->
string 227 auto StringUtil::between(
const string &str,
228 const string &start,
const string &end) ->
string 232 auto StringUtil::addTab(
const string &str,
size_t n) ->
string 234 vector<string> &lines = split(str,
"\n");
240 val.reserve(val.size() + lines.size());
243 for (i = 0; i < n; i++)
246 for (i = 0; i < lines.size(); i++)
247 val.append(tab + lines[i] + ((i == lines.size() - 1) ?
"" :
"\n"));
253 auto StringUtil::split(
const string &str,
const string &delim) -> vector<string>
257 vector<string> resArray(arr.size());
258 for (
size_t i = 0; i < arr.size(); i++)
259 resArray[i] = move(arr[i].str());
263 auto StringUtil::betweens(
const string &str,
264 const string &start,
const string &end) -> vector<string>
268 vector<string> resArray(arr.size());
269 for (
size_t i = 0; i < arr.size(); i++)
270 resArray[i] = move(arr[i].str());
278 auto StringUtil::toLowerCase(
const string &str) ->
string 282 auto StringUtil::toUpperCase(
const string &str) ->
string 287 auto StringUtil::replaceAll(
const string &str,
288 const string &before,
const string &after) ->
string 292 auto StringUtil::replaceAll(
const string &str,
293 const vector<pair<string, string>> &pairs) ->
string 297 auto StringUtil::removeHTMLSpaces(
const string &str) ->
string 299 vector<pair<string, string>> pairs =
305 return replaceAll(str, pairs);
auto toLowerCase() const -> std::string
Convert uppercase letters to lowercase.
auto getValue() -> T &
Get reference of value.
auto split(const WeakString &delim) const -> std::vector< WeakString >
Generates substrings.
auto ltrim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the beginning of the specified string.
auto rfinds(const std::vector< std::string > &delims, size_t endIndex=SIZE_MAX) const -> IndexPair< WeakString >
Finds last occurence in string.
auto getIndex() const -> size_t
Get index.
auto finds(const std::vector< std::string > &delims, size_t startIndex=0) const -> IndexPair< WeakString >
Finds first occurence in string.
auto toUpperCase() const -> std::string
Convert uppercase letters to lowercase.
auto trim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the beginning and end of the specified string.
auto substring(size_t startIndex, size_t size=SIZE_MAX) const -> WeakString
Generates a substring.
auto rtrim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the end of the specified string.
auto replaceAll(const WeakString &before, const WeakString &after) const -> std::string
Returns a string specified word is replaced.
A pair of index and its value(T)
auto betweens(const WeakString &start={}, const WeakString &end={}) const -> std::vector< WeakString >
Generates substrings.
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
Top level namespace of products built from samchon.
A string class only references characeters, reference only.