StringTools/Src/StringTools.cpp

156 lines
3.7 KiB
C++
Raw Normal View History

2021-11-20 19:24:35 +01:00
#include "StringTools.h"
#include <sstream>
2022-02-12 18:19:17 +01:00
std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst) {
2021-11-20 19:24:35 +01:00
std::stringstream ss;
for (std::size_t i = 0; i < str.length(); i++)
{
if (str[i] != find)
ss << str[i];
else
ss << subst;
}
return ss.str();
}
2022-02-12 18:19:17 +01:00
std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst) {
2021-11-20 19:24:35 +01:00
if (find.length() == 0)
return str;
std::stringstream ss;
std::size_t posFound = 0;
std::size_t lastFound = 0;
while (posFound != std::string::npos)
{
lastFound = posFound;
posFound = str.find(find, posFound);
if (posFound != std::string::npos)
{
ss << str.substr(lastFound, posFound - lastFound) << subst;
posFound += find.length();
}
else
{
ss << str.substr(lastFound, (str.length()) - lastFound);
}
}
return ss.str();
}
2022-02-12 18:19:17 +01:00
std::string StringTools::Replace(const std::string& str, const char find, const char subst) {
2021-11-20 19:24:35 +01:00
std::stringstream ss;
ss << subst;
return Replace(str, find, ss.str());
}
2022-02-12 18:19:17 +01:00
std::string StringTools::Replace(const std::string& str, const std::string& find, const char subst) {
2021-11-20 19:24:35 +01:00
std::stringstream ss;
ss << subst;
return Replace(str, find, ss.str());
2021-12-04 20:18:09 +01:00
}
2022-02-12 18:19:17 +01:00
std::string StringTools::Lower(const std::string& str) {
2021-12-04 20:18:09 +01:00
std::stringstream ss;
for (std::size_t i = 0; i < str.size(); i++)
{
const char c = str[i];
// Quick-accept: regular letters
if ((c >= 'A') && (c <= 'Z'))
2021-12-05 17:57:12 +01:00
ss << (char)(c | 32);
2021-12-04 20:18:09 +01:00
2022-02-11 01:12:19 +01:00
// Else: keep the character as is
2021-12-04 20:18:09 +01:00
else ss << c;
}
return ss.str();
}
2021-12-04 20:29:17 +01:00
2022-02-12 18:19:17 +01:00
std::string StringTools::Upper(const std::string& str) {
2021-12-04 20:29:17 +01:00
std::stringstream ss;
for (std::size_t i = 0; i < str.size(); i++)
{
const char c = str[i];
// Quick-accept: regular letters
if ((c >= 'a') && (c <= 'z'))
2021-12-05 17:57:12 +01:00
ss << (char)(c & ~32);
2021-12-04 20:29:17 +01:00
2022-02-11 01:12:19 +01:00
// Else: keep the character as is
2021-12-04 20:29:17 +01:00
else ss << c;
}
return ss.str();
}
2022-03-13 15:52:53 +01:00
std::vector<std::string> StringTools::Split(const std::string& str, const std::string& seperator) {
std::vector<std::string> toRet;
// Quick-accept: str length is 0
if (str.length() == 0)
toRet.push_back("");
2022-03-13 15:52:53 +01:00
// Quick-accept: seperator length is 0
else if (seperator.length() == 0) {
2022-03-13 15:52:53 +01:00
for (const char c : str)
toRet.push_back(std::string(&c, (&c) + 1));
}
else {
std::size_t idx = 0;
while (idx != std::string::npos) {
std::size_t lastIdx = idx;
idx = str.find(seperator, idx);
2022-03-13 15:52:53 +01:00
// Grab our substring until the next finding of sep
if (idx != std::string::npos) {
toRet.push_back(str.substr(
lastIdx,
idx - lastIdx
));
2022-03-13 15:52:53 +01:00
idx += seperator.length();
}
// No more seperator found. Grab the rest until the end of the string
else {
toRet.push_back(str.substr(
lastIdx
));
}
2022-03-13 15:52:53 +01:00
}
}
return toRet;
}
2022-03-14 12:17:15 +01:00
std::string StringTools::PadLeft(const std::string& str, const char pad, const std::size_t len) {
std::stringstream ss;
for (std::size_t i = str.length(); i < len; i++)
ss << pad;
ss << str;
return ss.str();
}
std::string StringTools::PadRight(const std::string& str, const char pad, const std::size_t len) {
std::stringstream ss;
ss << str;
for (std::size_t i = str.length(); i < len; i++)
ss << pad;
return ss.str();
}