libutils: add String8 and String16 std_string utility methods

Add utility methods that convert String16 and String8 into
std::string.

Also, remove a repeated include of <utils/Unicode.h> in
String16.h, since it is not used in that header file,
and is already included in String16.cpp.

BUG: 27200800
Change-Id: I5238aeb70689499763060a99dff9950fbb7adb3e
TEST: libutils builds successfully.
This commit is contained in:
Samuel Tan 2016-02-16 14:20:05 -08:00
parent 95fd527065
commit 9ac4e005b4
3 changed files with 17 additions and 2 deletions

View file

@ -17,8 +17,10 @@
#ifndef ANDROID_STRING16_H
#define ANDROID_STRING16_H
#include <string> // for std::string
#include <utils/Errors.h>
#include <utils/Unicode.h>
#include <utils/String8.h>
#include <utils/TypeHelpers.h>
// ---------------------------------------------------------------------------
@ -65,6 +67,7 @@ public:
inline const char16_t* string() const;
static inline std::string std_string(const String16& str);
size_t size() const;
void setTo(const String16& other);
status_t setTo(const char16_t* other);
@ -142,6 +145,11 @@ inline const char16_t* String16::string() const
return mString;
}
inline std::string String16::std_string(const String16& str)
{
return std::string(String8(str).string());
}
inline String16& String16::operator=(const String16& other)
{
setTo(other);

View file

@ -17,6 +17,8 @@
#ifndef ANDROID_STRING8_H
#define ANDROID_STRING8_H
#include <string> // for std::string
#include <utils/Errors.h>
#include <utils/Unicode.h>
#include <utils/TypeHelpers.h>
@ -63,6 +65,7 @@ public:
static String8 formatV(const char* fmt, va_list args);
inline const char* string() const;
static inline std::string std_string(const String8& str);
inline size_t size() const;
inline size_t bytes() const;
inline bool isEmpty() const;
@ -261,6 +264,11 @@ inline const char* String8::string() const
return mString;
}
inline std::string String8::std_string(const String8& str)
{
return std::string(str.string());
}
inline size_t String8::size() const
{
return length();

View file

@ -18,7 +18,6 @@
#include <utils/Log.h>
#include <utils/Unicode.h>
#include <utils/String8.h>
#include <utils/threads.h>
#include <memory.h>