Add a couple of useful string functions. (DO NOT MERGE)
Change-Id: I158f69917bab5f15482dd8f2b66b36a4cc0f11ad
(cherry picked from commit 5ee915afe1)
This commit is contained in:
parent
08511d6e31
commit
c24b98c7d8
2 changed files with 37 additions and 0 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue