Add return value to ReplaceAll()

Returns true of the string was modified and false otherwise
This commit is contained in:
WarmUpTill 2024-02-19 19:21:00 +01:00 committed by WarmUpTill
parent b803c0ad75
commit cb58eb9f04
3 changed files with 11 additions and 5 deletions

View File

@ -21,17 +21,20 @@ bool DoubleEquals(double left, double right, double epsilon)
return (fabs(left - right) < epsilon);
}
void ReplaceAll(std::string &str, const std::string &from,
bool ReplaceAll(std::string &str, const std::string &from,
const std::string &to)
{
if (from.empty()) {
return;
return false;
}
bool somethingWasReplaced = false;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
somethingWasReplaced = true;
}
return somethingWasReplaced;
}
std::optional<std::string> GetJsonField(const std::string &jsonStr,

View File

@ -16,7 +16,7 @@ EXPORT std::pair<int, int> GetCursorPos();
EXPORT bool DoubleEquals(double left, double right, double epsilon);
void ReplaceAll(std::string &str, const std::string &from,
bool ReplaceAll(std::string &str, const std::string &from,
const std::string &to);
EXPORT std::optional<std::string> GetJsonField(const std::string &json,
const std::string &id);

View File

@ -14,17 +14,20 @@ bool DoubleEquals(double left, double right, double epsilon)
return (fabs(left - right) < epsilon);
}
void ReplaceAll(std::string &str, const std::string &from,
bool ReplaceAll(std::string &str, const std::string &from,
const std::string &to)
{
if (from.empty()) {
return;
return false;
}
size_t start_pos = 0;
bool somethingWasReplaced = false;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
somethingWasReplaced = true;
}
return somethingWasReplaced;
}
std::optional<std::string> GetJsonField(const std::string &jsonStr,