From 908e0dfda568ddf0eb7f12e555655818241acda8 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 18 Nov 2019 16:01:21 -0800 Subject: [PATCH] Add absl-like StringReplace. Needed for cli-test. Test: treehugger Change-Id: Ib1fd01ef7f3e54e5778cc548dd789b5fcfcb7bd9 --- base/include/android-base/strings.h | 5 ++++ base/strings.cpp | 19 +++++++++++++ base/strings_test.cpp | 43 +++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/base/include/android-base/strings.h b/base/include/android-base/strings.h index b1c22c9f7..14d534a3b 100644 --- a/base/include/android-base/strings.h +++ b/base/include/android-base/strings.h @@ -85,5 +85,10 @@ inline bool ConsumeSuffix(std::string_view* s, std::string_view suffix) { return true; } +// Replaces `from` with `to` in `s`, once if `all == false`, or as many times as +// there are matches if `all == true`. +[[nodiscard]] std::string StringReplace(std::string_view s, std::string_view from, + std::string_view to, bool all); + } // namespace base } // namespace android diff --git a/base/strings.cpp b/base/strings.cpp index bb3167ef0..40b2bf270 100644 --- a/base/strings.cpp +++ b/base/strings.cpp @@ -116,5 +116,24 @@ bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs) { return lhs.size() == rhs.size() && strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0; } +std::string StringReplace(std::string_view s, std::string_view from, std::string_view to, + bool all) { + if (from.empty()) return std::string(s); + + std::string result; + std::string_view::size_type start_pos = 0; + do { + std::string_view::size_type pos = s.find(from, start_pos); + if (pos == std::string_view::npos) break; + + result.append(s.data() + start_pos, pos - start_pos); + result.append(to.data(), to.size()); + + start_pos = pos + from.size(); + } while (all); + result.append(s.data() + start_pos, s.size() - start_pos); + return result; +} + } // namespace base } // namespace android diff --git a/base/strings_test.cpp b/base/strings_test.cpp index ca3c0b83e..5ae309446 100644 --- a/base/strings_test.cpp +++ b/base/strings_test.cpp @@ -311,3 +311,46 @@ TEST(strings, ConsumeSuffix) { ASSERT_TRUE(android::base::ConsumeSuffix(&s, ".bar")); ASSERT_EQ("foo", s); } + +TEST(strings, StringReplace_false) { + // No change. + ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", false)); + ASSERT_EQ("", android::base::StringReplace("", "z", "Z", false)); + ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", false)); + + // Equal lengths. + ASSERT_EQ("Abcabc", android::base::StringReplace("abcabc", "a", "A", false)); + ASSERT_EQ("aBcabc", android::base::StringReplace("abcabc", "b", "B", false)); + ASSERT_EQ("abCabc", android::base::StringReplace("abcabc", "c", "C", false)); + + // Longer replacement. + ASSERT_EQ("foobcabc", android::base::StringReplace("abcabc", "a", "foo", false)); + ASSERT_EQ("afoocabc", android::base::StringReplace("abcabc", "b", "foo", false)); + ASSERT_EQ("abfooabc", android::base::StringReplace("abcabc", "c", "foo", false)); + + // Shorter replacement. + ASSERT_EQ("xxyz", android::base::StringReplace("abcxyz", "abc", "x", false)); + ASSERT_EQ("axyz", android::base::StringReplace("abcxyz", "bcx", "x", false)); + ASSERT_EQ("abcx", android::base::StringReplace("abcxyz", "xyz", "x", false)); +} + +TEST(strings, StringReplace_true) { + // No change. + ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", true)); + ASSERT_EQ("", android::base::StringReplace("", "z", "Z", true)); + ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", true)); + + // Equal lengths. + ASSERT_EQ("AbcAbc", android::base::StringReplace("abcabc", "a", "A", true)); + ASSERT_EQ("aBcaBc", android::base::StringReplace("abcabc", "b", "B", true)); + ASSERT_EQ("abCabC", android::base::StringReplace("abcabc", "c", "C", true)); + + // Longer replacement. + ASSERT_EQ("foobcfoobc", android::base::StringReplace("abcabc", "a", "foo", true)); + ASSERT_EQ("afoocafooc", android::base::StringReplace("abcabc", "b", "foo", true)); + ASSERT_EQ("abfooabfoo", android::base::StringReplace("abcabc", "c", "foo", true)); + + // Shorter replacement. + ASSERT_EQ("xxyzx", android::base::StringReplace("abcxyzabc", "abc", "x", true)); + ASSERT_EQ("", android::base::StringReplace("", "abc", "x", true)); +}