Merge "Implement android::String access methods that avoid C string cast" into main

This commit is contained in:
Tomasz Wasilczyk 2023-09-12 20:43:20 +00:00 committed by Gerrit Code Review
commit 771bda6c0b
2 changed files with 66 additions and 1 deletions

View file

@ -24,6 +24,11 @@
#include <utils/String8.h>
#include <utils/TypeHelpers.h>
#if __has_include(<string_view>)
#include <string_view>
#define HAS_STRING_VIEW
#endif
// ---------------------------------------------------------------------------
namespace android {
@ -88,6 +93,7 @@ public:
bool startsWith(const char16_t* prefix) const;
bool contains(const char16_t* chrs) const;
inline bool contains(const String16& other) const;
status_t replaceAll(char16_t replaceThis,
char16_t withThis);
@ -110,6 +116,12 @@ public:
inline operator const char16_t*() const;
#ifdef HAS_STRING_VIEW
// Implicit cast to std::u16string is not implemented on purpose - u16string_view is much
// lighter and if one needs, they can still create u16string from u16string_view.
inline operator std::u16string_view() const;
#endif
// Static and non-static String16 behave the same for the users, so
// this method isn't of much use for the users. It is public for testing.
bool isStaticString() const;
@ -256,6 +268,11 @@ inline size_t String16::length() const
return size();
}
inline bool String16::contains(const String16& other) const
{
return contains(other.c_str());
}
inline String16& String16::operator=(const String16& other)
{
setTo(other);
@ -345,8 +362,15 @@ inline String16::operator const char16_t*() const
return mString;
}
inline String16::operator std::u16string_view() const
{
return {mString, length()};
}
} // namespace android
// ---------------------------------------------------------------------------
#undef HAS_STRING_VIEW
#endif // ANDROID_STRING16_H

View file

@ -18,7 +18,6 @@
#define ANDROID_STRING8_H
#include <iostream>
#include <string>
#include <utils/Errors.h>
#include <utils/Unicode.h>
@ -27,6 +26,16 @@
#include <string.h> // for strcmp
#include <stdarg.h>
#if __has_include(<string>)
#include <string>
#define HAS_STRING
#endif
#if __has_include(<string_view>)
#include <string_view>
#define HAS_STRING_VIEW
#endif
// ---------------------------------------------------------------------------
namespace android {
@ -109,6 +118,10 @@ public:
inline operator const char*() const;
#ifdef HAS_STRING_VIEW
inline explicit operator std::string_view() const;
#endif
char* lockBuffer(size_t size);
void unlockBuffer();
status_t unlockBuffer(size_t size);
@ -116,13 +129,16 @@ public:
// 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;
inline ssize_t find(const String8& other, size_t start = 0) const;
// return true if this string contains the specified substring
inline bool contains(const char* other) const;
inline bool contains(const String8& other) const;
// removes all occurrence of the specified substring
// returns true if any were found and removed
bool removeAll(const char* other);
inline bool removeAll(const String8& other);
void toLower();
@ -186,11 +202,26 @@ inline size_t String8::bytes() const
return length();
}
inline ssize_t String8::find(const String8& other, size_t start) const
{
return find(other.c_str(), start);
}
inline bool String8::contains(const char* other) const
{
return find(other) >= 0;
}
inline bool String8::contains(const String8& other) const
{
return contains(other.c_str());
}
inline bool String8::removeAll(const String8& other)
{
return removeAll(other.c_str());
}
inline String8& String8::operator=(const String8& other)
{
setTo(other);
@ -299,8 +330,18 @@ inline String8::operator const char*() const
return mString;
}
#ifdef HAS_STRING_VIEW
inline String8::operator std::string_view() const
{
return {mString, length()};
}
#endif
} // namespace android
// ---------------------------------------------------------------------------
#undef HAS_STRING
#undef HAS_STRING_VIEW
#endif // ANDROID_STRING8_H