mirror of
https://github.com/hyprwm/hyprutils.git
synced 2024-12-22 09:39:49 +01:00
string: add replaceInString
This commit is contained in:
parent
138408125c
commit
f73a28ca03
4 changed files with 16 additions and 1 deletions
|
@ -6,5 +6,6 @@ namespace Hyprutils {
|
|||
// trims beginning and end of whitespace characters
|
||||
std::string trim(const std::string& in);
|
||||
bool isNumber(const std::string& str, bool allowfloat = false);
|
||||
void replaceInString(std::string& string, const std::string& what, const std::string& to);
|
||||
};
|
||||
};
|
|
@ -56,3 +56,13 @@ bool Hyprutils::String::isNumber(const std::string& str, bool allowfloat) {
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Hyprutils::String::replaceInString(std::string& string, const std::string& what, const std::string& to) {
|
||||
if (string.empty())
|
||||
return;
|
||||
size_t pos = 0;
|
||||
while ((pos = string.find(what, pos)) != std::string::npos) {
|
||||
string.replace(pos, what.length(), to);
|
||||
pos += to.length();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,4 +17,4 @@ namespace Colors {
|
|||
ret = 1; \
|
||||
} else { \
|
||||
std::cout << Colors::GREEN << "Passed " << Colors::RESET << #expr << ". Got " << val << "\n"; \
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,5 +34,9 @@ int main(int argc, char** argv, char** envp) {
|
|||
EXPECT(list[0], "hello");
|
||||
EXPECT(list[1], "world!");
|
||||
|
||||
std::string hello = "hello world!";
|
||||
replaceInString(hello, "hello", "hi");
|
||||
EXPECT(hello, "hi world!");
|
||||
|
||||
return ret;
|
||||
}
|
Loading…
Reference in a new issue