Skip to content

Commit

Permalink
Merge "Implement android::String access methods that avoid C string c…
Browse files Browse the repository at this point in the history
…ast" into main am: 771bda6 am: 2b3630b am: 1bdf8d7

Original change: https://android-review.googlesource.com/c/platform/system/core/+/2748673

Change-Id: I5a5469ef20fec147c8c9dadd6fa202f78e51fc3e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
  • Loading branch information
twasilczyk authored and android-build-merge-worker-robot committed Sep 12, 2023
2 parents 9996ca7 + 1bdf8d7 commit 25c2b50
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
24 changes: 24 additions & 0 deletions libutils/include/utils/String16.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -88,6 +93,7 @@ class String16
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);
Expand All @@ -110,6 +116,12 @@ class String16

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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
43 changes: 42 additions & 1 deletion libutils/include/utils/String8.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#define ANDROID_STRING8_H

#include <iostream>
#include <string>

#include <utils/Errors.h>
#include <utils/Unicode.h>
Expand All @@ -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 {
Expand Down Expand Up @@ -109,20 +118,27 @@ class String8

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);

// 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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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

0 comments on commit 25c2b50

Please sign in to comment.