diff options
author | Elliott Hughes <enh@google.com> | 2019-11-19 15:28:24 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-11-19 15:28:24 +0000 |
commit | 43f9d5f34aa7d080f8321dc35f24c0d8296c6bcc (patch) | |
tree | 07e428d298c5d8fbb9a2251aabecfaa07798dd9a /base/strings.cpp | |
parent | ba5351692e005cfa016414fb5472a2a12f95e31d (diff) | |
parent | 908e0dfda568ddf0eb7f12e555655818241acda8 (diff) |
Merge "Add absl-like StringReplace."
Diffstat (limited to 'base/strings.cpp')
-rw-r--r-- | base/strings.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/base/strings.cpp b/base/strings.cpp index bb3167ef0a..40b2bf2705 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 |