Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ReplaceString utility #1507

Merged
merged 2 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Tests/src/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,6 @@ TEST_CASE("String replace")
REQUIRE(osmscout::ReplaceString("", "a", "b").empty());
REQUIRE(osmscout::ReplaceString("abc", "", "b")=="abc");
REQUIRE(osmscout::ReplaceString("abcabc", "a", "A")=="AbcAbc");
REQUIRE(osmscout::ReplaceString("abcdef", "ef", "X")=="abcdX");
REQUIRE(osmscout::ReplaceString("abcdef", "ab", "X")=="Xcdef");
}
23 changes: 15 additions & 8 deletions libosmscout/src/osmscout/util/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,23 @@ namespace osmscout {
if (search.empty()) {
return in;
}
auto arr=SplitString(in, search);
std::ostringstream buff;
for (auto it=arr.begin(); it!=arr.end();) {
buff << *it;
++it;
if (it!=arr.end()) {
buff << replacement;

std::ostringstream result;
std::string remaining=in;

while (!remaining.empty()) {
std::string::size_type pos = remaining.find(search);
if (pos == std::string::npos) {
result << remaining;
break;
} else {
result << remaining.substr(0, pos);
result << replacement;
remaining.erase(0, pos + search.length());
}
}
return buff.str();

return result.str();
}

std::optional<std::pair<std::string,std::string>> SplitStringToPair(const std::string& str,
Expand Down