diff options
author | Jeff Brown <jeffbrown@google.com> | 2014-06-06 19:30:15 -0700 |
---|---|---|
committer | Jeff Brown <jeffbrown@google.com> | 2014-06-09 14:02:52 -0700 |
commit | 5ee915afe17b7190f992addc48cb53ed6371a68d (patch) | |
tree | b9d17977eac672201411fbb01c17bc4f16464aa5 /libutils | |
parent | 45e2756409bee6b766fd965bd945f9d42789fb33 (diff) | |
download | system_core-5ee915afe17b7190f992addc48cb53ed6371a68d.zip system_core-5ee915afe17b7190f992addc48cb53ed6371a68d.tar.gz system_core-5ee915afe17b7190f992addc48cb53ed6371a68d.tar.bz2 |
Add a couple of useful string functions.
Change-Id: I158f69917bab5f15482dd8f2b66b36a4cc0f11ad
Diffstat (limited to 'libutils')
-rw-r--r-- | libutils/String8.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/libutils/String8.cpp b/libutils/String8.cpp index 49340bb..9092cbc 100644 --- a/libutils/String8.cpp +++ b/libutils/String8.cpp @@ -408,6 +408,30 @@ ssize_t String8::find(const char* other, size_t start) const return p ? p-mString : -1; } +bool String8::removeAll(const char* other) { + ssize_t index = find(other); + if (index < 0) return false; + + char* buf = lockBuffer(size()); + if (!buf) return false; // out of memory + + size_t skip = strlen(other); + size_t len = size(); + size_t tail = index; + while (size_t(index) < len) { + ssize_t next = find(other, index + skip); + if (next < 0) { + next = len; + } + + memcpy(buf + tail, buf + index + skip, next - index - skip); + tail += next - index - skip; + index = next; + } + unlockBuffer(tail); + return true; +} + void String8::toLower() { toLower(0, size()); |