diff options
author | Elliott Hughes <enh@google.com> | 2019-05-03 09:02:45 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2019-05-03 12:49:31 -0700 |
commit | b4dc7be6c57a911c8f496af188a15efdeb8ec2bc (patch) | |
tree | 3e2ffd39f262073f43528c25845860dcb46b43d2 /base/include/android-base/strings.h | |
parent | 56311071fec472ab546a6722295e84e4b4920c85 (diff) |
libbase: add ConsumePrefix/ConsumeSuffix.
adb was already using ConsumePrefix, and now we have another would-be
user in cutils. (There appears to be one place in adb that should use
ConsumeSuffix, so I'm assuming we'll want that sooner or later.)
I've kept these inline because adb and google3's versions both were, and
I'm easily led.
Test: treehugger
Change-Id: I29d99032f6f6ccbfaefece59725db8afb02a4c87
Diffstat (limited to 'base/include/android-base/strings.h')
-rw-r--r-- | base/include/android-base/strings.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/base/include/android-base/strings.h b/base/include/android-base/strings.h index 8e9716f9f..b1c22c9f7 100644 --- a/base/include/android-base/strings.h +++ b/base/include/android-base/strings.h @@ -18,6 +18,7 @@ #include <sstream> #include <string> +#include <string_view> #include <vector> namespace android { @@ -68,5 +69,21 @@ bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix); // Tests whether 'lhs' equals 'rhs', ignoring case. bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs); +// Removes `prefix` from the start of the given string and returns true (if +// it was present), false otherwise. +inline bool ConsumePrefix(std::string_view* s, std::string_view prefix) { + if (!StartsWith(*s, prefix)) return false; + s->remove_prefix(prefix.size()); + return true; +} + +// Removes `suffix` from the end of the given string and returns true (if +// it was present), false otherwise. +inline bool ConsumeSuffix(std::string_view* s, std::string_view suffix) { + if (!EndsWith(*s, suffix)) return false; + s->remove_suffix(suffix.size()); + return true; +} + } // namespace base } // namespace android |