From cb58eb9f04999914cb79d0b42ac4c8189e0bb802 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Mon, 19 Feb 2024 19:21:00 +0100 Subject: [PATCH] Add return value to ReplaceAll() Returns true of the string was modified and false otherwise --- lib/utils/utility.cpp | 7 +++++-- lib/utils/utility.hpp | 2 +- tests/mocks/utility.cpp | 7 +++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/utils/utility.cpp b/lib/utils/utility.cpp index cd890de8..ce9a2005 100644 --- a/lib/utils/utility.cpp +++ b/lib/utils/utility.cpp @@ -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 GetJsonField(const std::string &jsonStr, diff --git a/lib/utils/utility.hpp b/lib/utils/utility.hpp index bb28558f..788886a4 100644 --- a/lib/utils/utility.hpp +++ b/lib/utils/utility.hpp @@ -16,7 +16,7 @@ EXPORT std::pair 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 GetJsonField(const std::string &json, const std::string &id); diff --git a/tests/mocks/utility.cpp b/tests/mocks/utility.cpp index ff365918..e815415e 100644 --- a/tests/mocks/utility.cpp +++ b/tests/mocks/utility.cpp @@ -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 GetJsonField(const std::string &jsonStr,