33 namespace string_utils
37 std::string
shortenString(
const std::string&
string,
const size_t maxLength)
39 if (
string.length() <= maxLength)
42 const float spacePerPart = (maxLength -
ELLIPSIS.length()) / 2.f;
43 const auto beforeEllipsis =
string.substr(0, std::ceil(spacePerPart));
44 const auto afterEllipsis =
string.substr(
string.size() - std::floor(spacePerPart));
46 return beforeEllipsis +
ELLIPSIS + afterEllipsis;
51 std::size_t pos = input.find(toReplace);
52 if (pos == std::string::npos)
54 return input.replace(pos, toReplace.length(), replaceWith);
59 if (camelCase.empty())
62 std::string str(1, ::tolower(camelCase[0]));
63 for (
auto it = camelCase.begin() + 1; it != camelCase.end(); ++it)
65 if (isupper(*it) && *(it - 1) !=
'-' && islower(*(it - 1)))
70 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
76 std::string camel = separated;
78 for (
size_t x = 0; x < camel.length(); x++)
80 if (camel[x] == separator)
82 std::string tempString = camel.substr(x + 1, 1);
84 std::transform(tempString.begin(), tempString.end(), tempString.begin(), ::toupper);
87 camel.insert(x, tempString);
93 std::string
join(
const std::vector<std::string>&
strings,
const std::string& joinWith)
95 const size_t numStrings =
strings.size();
101 for (
size_t i = 1; i < numStrings; i++)
108 std::transform(input.begin(), input.end(), input.begin(), ::tolower);
114 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
int ch) { return !std::isspace(ch); }));
119 s.erase(std::find_if(s.rbegin(), s.rend(), [](
int ch) { return !std::isspace(ch); }).base(), s.end());
128 std::vector<std::string>
split(
const std::string& s,
char delim)
130 std::stringstream ss(s);
132 std::vector<std::string> elems;
133 while (std::getline(ss, item, delim))
134 elems.push_back(std::move(item));
std::string join(const std::vector< std::string > &strings, const std::string &joinWith)
std::string separatedToCamelCase(const std::string &separated, const char separator)
void rtrim(std::string &s)
void ltrim(std::string &s)
void trim(std::string &s)
std::string shortenString(const std::string &string, const size_t maxLength)
std::string replaceFirstOccurrence(std::string input, const std::string &toReplace, const std::string &replaceWith)
const std::string ELLIPSIS("...")
std::string camelCaseToSeparated(const std::string &camelCase, const char separator)
std::vector< std::string > split(const std::string &s, char delim)
std::string toLowercase(std::string input)