2009-03-04 04:31:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ANDROID_STRING8_H
|
|
|
|
#define ANDROID_STRING8_H
|
|
|
|
|
|
|
|
#include <utils/Errors.h>
|
2010-11-09 23:37:23 +01:00
|
|
|
#include <utils/SharedBuffer.h>
|
|
|
|
#include <utils/Unicode.h>
|
2012-03-16 22:45:49 +01:00
|
|
|
#include <utils/TypeHelpers.h>
|
2009-03-04 04:31:44 +01:00
|
|
|
|
2010-11-09 23:37:23 +01:00
|
|
|
#include <string.h> // for strcmp
|
2010-11-11 01:03:06 +01:00
|
|
|
#include <stdarg.h>
|
2009-06-30 13:40:42 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
namespace android {
|
|
|
|
|
2010-11-09 23:37:23 +01:00
|
|
|
class String16;
|
2009-03-04 04:31:44 +01:00
|
|
|
class TextOutput;
|
|
|
|
|
2009-06-30 13:40:42 +02:00
|
|
|
//! This is a string holding UTF-8 characters. Does not allow the value more
|
|
|
|
// than 0x10FFFF, which is not valid unicode codepoint.
|
2009-03-04 04:31:44 +01:00
|
|
|
class String8
|
|
|
|
{
|
|
|
|
public:
|
2013-05-09 01:04:13 +02:00
|
|
|
/* use String8(StaticLinkage) if you're statically linking against
|
|
|
|
* libutils and declaring an empty static String8, e.g.:
|
|
|
|
*
|
|
|
|
* static String8 sAStaticEmptyString(String8::kEmptyString);
|
|
|
|
* static String8 sAnotherStaticEmptyString(sAStaticEmptyString);
|
|
|
|
*/
|
|
|
|
enum StaticLinkage { kEmptyString };
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
String8();
|
2013-05-09 01:04:13 +02:00
|
|
|
explicit String8(StaticLinkage);
|
2009-03-04 04:31:44 +01:00
|
|
|
String8(const String8& o);
|
|
|
|
explicit String8(const char* o);
|
|
|
|
explicit String8(const char* o, size_t numChars);
|
|
|
|
|
|
|
|
explicit String8(const String16& o);
|
|
|
|
explicit String8(const char16_t* o);
|
|
|
|
explicit String8(const char16_t* o, size_t numChars);
|
2009-06-30 13:40:42 +02:00
|
|
|
explicit String8(const char32_t* o);
|
|
|
|
explicit String8(const char32_t* o, size_t numChars);
|
2009-03-04 04:31:44 +01:00
|
|
|
~String8();
|
2010-12-02 22:50:46 +01:00
|
|
|
|
|
|
|
static inline const String8 empty();
|
|
|
|
|
|
|
|
static String8 format(const char* fmt, ...) __attribute__((format (printf, 1, 2)));
|
|
|
|
static String8 formatV(const char* fmt, va_list args);
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
inline const char* string() const;
|
|
|
|
inline size_t size() const;
|
|
|
|
inline size_t length() const;
|
|
|
|
inline size_t bytes() const;
|
2010-09-13 02:55:08 +02:00
|
|
|
inline bool isEmpty() const;
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
inline const SharedBuffer* sharedBuffer() const;
|
|
|
|
|
2010-09-13 02:55:08 +02:00
|
|
|
void clear();
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
void setTo(const String8& other);
|
|
|
|
status_t setTo(const char* other);
|
|
|
|
status_t setTo(const char* other, size_t numChars);
|
|
|
|
status_t setTo(const char16_t* other, size_t numChars);
|
2009-06-30 13:40:42 +02:00
|
|
|
status_t setTo(const char32_t* other,
|
|
|
|
size_t length);
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
status_t append(const String8& other);
|
|
|
|
status_t append(const char* other);
|
|
|
|
status_t append(const char* other, size_t numChars);
|
|
|
|
|
2010-08-11 23:46:32 +02:00
|
|
|
status_t appendFormat(const char* fmt, ...)
|
|
|
|
__attribute__((format (printf, 2, 3)));
|
2010-11-11 01:03:06 +01:00
|
|
|
status_t appendFormatV(const char* fmt, va_list args);
|
2010-07-16 08:54:05 +02:00
|
|
|
|
2009-06-30 13:40:42 +02:00
|
|
|
// Note that this function takes O(N) time to calculate the value.
|
|
|
|
// No cache value is stored.
|
|
|
|
size_t getUtf32Length() const;
|
|
|
|
int32_t getUtf32At(size_t index,
|
|
|
|
size_t *next_index) const;
|
2010-11-09 23:37:23 +01:00
|
|
|
void getUtf32(char32_t* dst) const;
|
2009-06-30 13:40:42 +02:00
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
inline String8& operator=(const String8& other);
|
|
|
|
inline String8& operator=(const char* other);
|
|
|
|
|
|
|
|
inline String8& operator+=(const String8& other);
|
|
|
|
inline String8 operator+(const String8& other) const;
|
|
|
|
|
|
|
|
inline String8& operator+=(const char* other);
|
|
|
|
inline String8 operator+(const char* other) const;
|
|
|
|
|
|
|
|
inline int compare(const String8& other) const;
|
|
|
|
|
|
|
|
inline bool operator<(const String8& other) const;
|
|
|
|
inline bool operator<=(const String8& other) const;
|
|
|
|
inline bool operator==(const String8& other) const;
|
|
|
|
inline bool operator!=(const String8& other) const;
|
|
|
|
inline bool operator>=(const String8& other) const;
|
|
|
|
inline bool operator>(const String8& other) const;
|
|
|
|
|
|
|
|
inline bool operator<(const char* other) const;
|
|
|
|
inline bool operator<=(const char* other) const;
|
|
|
|
inline bool operator==(const char* other) const;
|
|
|
|
inline bool operator!=(const char* other) const;
|
|
|
|
inline bool operator>=(const char* other) const;
|
|
|
|
inline bool operator>(const char* other) const;
|
|
|
|
|
|
|
|
inline operator const char*() const;
|
|
|
|
|
|
|
|
char* lockBuffer(size_t size);
|
|
|
|
void unlockBuffer();
|
|
|
|
status_t unlockBuffer(size_t size);
|
|
|
|
|
|
|
|
// return the index of the first byte of other in this at or after
|
|
|
|
// start, or -1 if not found
|
|
|
|
ssize_t find(const char* other, size_t start = 0) const;
|
|
|
|
|
|
|
|
void toLower();
|
|
|
|
void toLower(size_t start, size_t numChars);
|
|
|
|
void toUpper();
|
|
|
|
void toUpper(size_t start, size_t numChars);
|
2009-06-30 13:40:42 +02:00
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
/*
|
|
|
|
* These methods operate on the string as if it were a path name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the filename field to a specific value.
|
|
|
|
*
|
|
|
|
* Normalizes the filename, removing a trailing '/' if present.
|
|
|
|
*/
|
|
|
|
void setPathName(const char* name);
|
|
|
|
void setPathName(const char* name, size_t numChars);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get just the filename component.
|
|
|
|
*
|
|
|
|
* "/tmp/foo/bar.c" --> "bar.c"
|
|
|
|
*/
|
|
|
|
String8 getPathLeaf(void) const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the last (file name) component, leaving just the directory
|
|
|
|
* name.
|
|
|
|
*
|
|
|
|
* "/tmp/foo/bar.c" --> "/tmp/foo"
|
|
|
|
* "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
|
|
|
|
* "bar.c" --> ""
|
|
|
|
*/
|
|
|
|
String8 getPathDir(void) const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the front (root dir) component. Optionally also return the
|
|
|
|
* remaining components.
|
|
|
|
*
|
|
|
|
* "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
|
|
|
|
* "/tmp" --> "tmp" (remain = "")
|
|
|
|
* "bar.c" --> "bar.c" (remain = "")
|
|
|
|
*/
|
|
|
|
String8 walkPath(String8* outRemains = NULL) const;
|
|
|
|
|
|
|
|
/*
|
2011-03-14 19:32:29 +01:00
|
|
|
* Return the filename extension. This is the last '.' and any number
|
|
|
|
* of characters that follow it. The '.' is included in case we
|
2009-03-04 04:31:44 +01:00
|
|
|
* decide to expand our definition of what constitutes an extension.
|
|
|
|
*
|
|
|
|
* "/tmp/foo/bar.c" --> ".c"
|
|
|
|
* "/tmp" --> ""
|
|
|
|
* "/tmp/foo.bar/baz" --> ""
|
|
|
|
* "foo.jpeg" --> ".jpeg"
|
|
|
|
* "foo." --> ""
|
|
|
|
*/
|
|
|
|
String8 getPathExtension(void) const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the path without the extension. Rules for what constitutes
|
|
|
|
* an extension are described in the comment for getPathExtension().
|
|
|
|
*
|
|
|
|
* "/tmp/foo/bar.c" --> "/tmp/foo/bar"
|
|
|
|
*/
|
|
|
|
String8 getBasePath(void) const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a component to the pathname. We guarantee that there is
|
|
|
|
* exactly one path separator between the old path and the new.
|
|
|
|
* If there is no existing name, we just copy the new name in.
|
|
|
|
*
|
|
|
|
* If leaf is a fully qualified path (i.e. starts with '/', it
|
|
|
|
* replaces whatever was there before.
|
|
|
|
*/
|
|
|
|
String8& appendPath(const char* leaf);
|
|
|
|
String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like appendPath(), but does not affect this string. Returns a new one instead.
|
|
|
|
*/
|
|
|
|
String8 appendPathCopy(const char* leaf) const
|
|
|
|
{ String8 p(*this); p.appendPath(leaf); return p; }
|
|
|
|
String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts all separators in this string to /, the default path separator.
|
|
|
|
*
|
|
|
|
* If the default OS separator is backslash, this converts all
|
|
|
|
* backslashes to slashes, in-place. Otherwise it does nothing.
|
|
|
|
* Returns self.
|
|
|
|
*/
|
|
|
|
String8& convertToResPath();
|
|
|
|
|
|
|
|
private:
|
|
|
|
status_t real_append(const char* other, size_t numChars);
|
|
|
|
char* find_extension(void) const;
|
|
|
|
|
|
|
|
const char* mString;
|
|
|
|
};
|
|
|
|
|
2012-03-16 22:45:49 +01:00
|
|
|
// String8 can be trivially moved using memcpy() because moving does not
|
|
|
|
// require any change to the underlying SharedBuffer contents or reference count.
|
|
|
|
ANDROID_TRIVIAL_MOVE_TRAIT(String8)
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// No user servicable parts below.
|
|
|
|
|
|
|
|
inline int compare_type(const String8& lhs, const String8& rhs)
|
|
|
|
{
|
|
|
|
return lhs.compare(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int strictly_order_type(const String8& lhs, const String8& rhs)
|
|
|
|
{
|
|
|
|
return compare_type(lhs, rhs) < 0;
|
|
|
|
}
|
|
|
|
|
2010-12-02 22:50:46 +01:00
|
|
|
inline const String8 String8::empty() {
|
|
|
|
return String8();
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
inline const char* String8::string() const
|
|
|
|
{
|
|
|
|
return mString;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t String8::length() const
|
|
|
|
{
|
|
|
|
return SharedBuffer::sizeFromData(mString)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t String8::size() const
|
|
|
|
{
|
|
|
|
return length();
|
|
|
|
}
|
|
|
|
|
2010-09-13 02:55:08 +02:00
|
|
|
inline bool String8::isEmpty() const
|
|
|
|
{
|
|
|
|
return length() == 0;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
inline size_t String8::bytes() const
|
|
|
|
{
|
|
|
|
return SharedBuffer::sizeFromData(mString)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const SharedBuffer* String8::sharedBuffer() const
|
|
|
|
{
|
|
|
|
return SharedBuffer::bufferFromData(mString);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String8& String8::operator=(const String8& other)
|
|
|
|
{
|
|
|
|
setTo(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String8& String8::operator=(const char* other)
|
|
|
|
{
|
|
|
|
setTo(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String8& String8::operator+=(const String8& other)
|
|
|
|
{
|
|
|
|
append(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String8 String8::operator+(const String8& other) const
|
|
|
|
{
|
2010-08-06 01:21:23 +02:00
|
|
|
String8 tmp(*this);
|
2009-03-04 04:31:44 +01:00
|
|
|
tmp += other;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String8& String8::operator+=(const char* other)
|
|
|
|
{
|
|
|
|
append(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String8 String8::operator+(const char* other) const
|
|
|
|
{
|
2010-08-06 01:21:23 +02:00
|
|
|
String8 tmp(*this);
|
2009-03-04 04:31:44 +01:00
|
|
|
tmp += other;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int String8::compare(const String8& other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other.mString);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator<(const String8& other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other.mString) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator<=(const String8& other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other.mString) <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator==(const String8& other) const
|
|
|
|
{
|
2010-10-21 02:06:28 +02:00
|
|
|
return strcmp(mString, other.mString) == 0;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator!=(const String8& other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other.mString) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator>=(const String8& other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other.mString) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator>(const String8& other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other.mString) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator<(const char* other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator<=(const char* other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other) <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator==(const char* other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator!=(const char* other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator>=(const char* other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool String8::operator>(const char* other) const
|
|
|
|
{
|
|
|
|
return strcmp(mString, other) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String8::operator const char*() const
|
|
|
|
{
|
|
|
|
return mString;
|
|
|
|
}
|
|
|
|
|
2009-06-30 13:40:42 +02:00
|
|
|
} // namespace android
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif // ANDROID_STRING8_H
|