Merge "Add String16::c_str and start using it." into main

This commit is contained in:
Tomasz Wasilczyk 2023-08-11 21:39:31 +00:00 committed by Gerrit Code Review
commit 067dbcc110
4 changed files with 17 additions and 17 deletions

View file

@ -26,7 +26,7 @@ namespace android {
static const StaticString16 emptyString(u""); static const StaticString16 emptyString(u"");
static inline char16_t* getEmptyString() { static inline char16_t* getEmptyString() {
return const_cast<char16_t*>(emptyString.string()); return const_cast<char16_t*>(emptyString.c_str());
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -112,10 +112,7 @@ String16::String16(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o)))
String16::String16(const char16_t* o, size_t len) : mString(allocFromUTF16(o, len)) {} String16::String16(const char16_t* o, size_t len) : mString(allocFromUTF16(o, len)) {}
String16::String16(const String8& o) String16::String16(const String8& o) : mString(allocFromUTF8(o.c_str(), o.size())) {}
: mString(allocFromUTF8(o.string(), o.size()))
{
}
String16::String16(const char* o) String16::String16(const char* o)
: mString(allocFromUTF8(o, strlen(o))) : mString(allocFromUTF8(o, strlen(o)))
@ -173,7 +170,7 @@ status_t String16::setTo(const String16& other, size_t len, size_t begin)
LOG_ALWAYS_FATAL("Not implemented"); LOG_ALWAYS_FATAL("Not implemented");
} }
return setTo(other.string()+begin, len); return setTo(other.c_str() + begin, len);
} }
status_t String16::setTo(const char16_t* other) status_t String16::setTo(const char16_t* other)
@ -200,7 +197,7 @@ status_t String16::setTo(const char16_t* other, size_t len)
} }
status_t String16::append(const String16& other) { status_t String16::append(const String16& other) {
return append(other.string(), other.size()); return append(other.c_str(), other.size());
} }
status_t String16::append(const char16_t* chrs, size_t otherLen) { status_t String16::append(const char16_t* chrs, size_t otherLen) {
@ -286,7 +283,7 @@ bool String16::startsWith(const String16& prefix) const
{ {
const size_t ps = prefix.size(); const size_t ps = prefix.size();
if (ps > size()) return false; if (ps > size()) return false;
return strzcmp16(mString, ps, prefix.string(), ps) == 0; return strzcmp16(mString, ps, prefix.c_str(), ps) == 0;
} }
bool String16::startsWith(const char16_t* prefix) const bool String16::startsWith(const char16_t* prefix) const

View file

@ -150,10 +150,7 @@ String8::String8(const char* o, size_t len)
} }
} }
String8::String8(const String16& o) String8::String8(const String16& o) : mString(allocFromUTF16(o.c_str(), o.size())) {}
: mString(allocFromUTF16(o.string(), o.size()))
{
}
String8::String8(const char16_t* o) String8::String8(const char16_t* o)
: mString(allocFromUTF16(o, strlen16(o))) : mString(allocFromUTF16(o, strlen16(o)))
@ -267,7 +264,7 @@ status_t String8::append(const String8& other)
return OK; return OK;
} }
return real_append(other.string(), otherLen); return real_append(other.c_str(), otherLen);
} }
status_t String8::append(const char* other) status_t String8::append(const char* other)

View file

@ -53,6 +53,7 @@ public:
~String16(); ~String16();
inline const char16_t* c_str() const;
inline const char16_t* string() const; inline const char16_t* string() const;
private: private:
@ -234,6 +235,11 @@ inline int strictly_order_type(const String16& lhs, const String16& rhs)
return compare_type(lhs, rhs) < 0; return compare_type(lhs, rhs) < 0;
} }
inline const char16_t* String16::c_str() const
{
return mString;
}
inline const char16_t* String16::string() const inline const char16_t* String16::string() const
{ {
return mString; return mString;
@ -241,7 +247,7 @@ inline const char16_t* String16::string() const
inline std::string String16::std_string(const String16& str) inline std::string String16::std_string(const String16& str)
{ {
return std::string(String8(str).string()); return std::string(String8(str).c_str());
} }
inline String16& String16::operator=(const String16& other) inline String16& String16::operator=(const String16& other)

View file

@ -193,14 +193,14 @@ public:
* replaces whatever was there before. * replaces whatever was there before.
*/ */
String8& appendPath(const char* leaf); String8& appendPath(const char* leaf);
String8& appendPath(const String8& leaf) { return appendPath(leaf.string()); } String8& appendPath(const String8& leaf) { return appendPath(leaf.c_str()); }
/* /*
* Like appendPath(), but does not affect this string. Returns a new one instead. * Like appendPath(), but does not affect this string. Returns a new one instead.
*/ */
String8 appendPathCopy(const char* leaf) const String8 appendPathCopy(const char* leaf) const
{ String8 p(*this); p.appendPath(leaf); return p; } { String8 p(*this); p.appendPath(leaf); return p; }
String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); } String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.c_str()); }
/* /*
* Converts all separators in this string to /, the default path separator. * Converts all separators in this string to /, the default path separator.
@ -255,7 +255,7 @@ inline const char* String8::string() const
inline std::string String8::std_string(const String8& str) inline std::string String8::std_string(const String8& str)
{ {
return std::string(str.string()); return std::string(str.c_str());
} }
inline size_t String8::size() const inline size_t String8::size() const