summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Brown <jeffbrown@google.com>2014-06-09 21:04:59 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2014-06-09 21:04:59 +0000
commitcbe8464ddda27aed04129bb57282d4153d813993 (patch)
tree963e555235a799895035f470169c82e00a8e70e8
parentded895c033aa028ccd1e0b5b798e675ed1601044 (diff)
parent5ee915afe17b7190f992addc48cb53ed6371a68d (diff)
downloadsystem_core-cbe8464ddda27aed04129bb57282d4153d813993.zip
system_core-cbe8464ddda27aed04129bb57282d4153d813993.tar.gz
system_core-cbe8464ddda27aed04129bb57282d4153d813993.tar.bz2
Merge "Add a couple of useful string functions."
-rw-r--r--include/utils/String8.h13
-rw-r--r--libutils/String8.cpp24
2 files changed, 37 insertions, 0 deletions
diff --git a/include/utils/String8.h b/include/utils/String8.h
index ef59470..ecfcf10 100644
--- a/include/utils/String8.h
+++ b/include/utils/String8.h
@@ -130,11 +130,19 @@ public:
// start, or -1 if not found
ssize_t find(const char* other, size_t start = 0) const;
+ // return true if this string contains the specified substring
+ inline bool contains(const char* other) const;
+
+ // removes all occurrence of the specified substring
+ // returns true if any were found and removed
+ bool removeAll(const char* other);
+
void toLower();
void toLower(size_t start, size_t numChars);
void toUpper();
void toUpper(size_t start, size_t numChars);
+
/*
* These methods operate on the string as if it were a path name.
*/
@@ -280,6 +288,11 @@ inline const SharedBuffer* String8::sharedBuffer() const
return SharedBuffer::bufferFromData(mString);
}
+inline bool String8::contains(const char* other) const
+{
+ return find(other) >= 0;
+}
+
inline String8& String8::operator=(const String8& other)
{
setTo(other);
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());