From 62d55223b26d7549d8583866961a8f14177fc169 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Tue, 11 Jun 2013 00:09:37 +0900 Subject: [PATCH 1/2] Added conversion from String to const wchar_t* --- taglib/toolkit/tstring.cpp | 5 +++++ taglib/toolkit/tstring.h | 11 ++++++++++- tests/test_string.cpp | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index d28855b8..c83118d1 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -312,6 +312,11 @@ const char *String::toCString(bool unicode) const return d->cstring.c_str(); } +const wchar_t *String::toCWString() const +{ + return d->data.c_str(); +} + String::Iterator String::begin() { return d->data.begin(); diff --git a/taglib/toolkit/tstring.h b/taglib/toolkit/tstring.h index d353bcd9..569650eb 100644 --- a/taglib/toolkit/tstring.h +++ b/taglib/toolkit/tstring.h @@ -205,7 +205,16 @@ namespace TagLib { * where memory is critical. */ const char *toCString(bool unicode = false) const; - + + /*! + * Returns a pointer to the wide char version of the TagLib string. The string + * is encoded in UTF-16(without BOM/CPU byte order). + * + * /note This returns a pointer to the String's internal data without any + * conversions. + */ + const wchar_t *toCWString() const; + /*! * Returns an iterator pointing to the beginning of the string. */ diff --git a/tests/test_string.cpp b/tests/test_string.cpp index f2976919..a815a0b4 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -72,6 +72,9 @@ public: String unicode2(unicode.to8Bit(true), String::UTF8); CPPUNIT_ASSERT(unicode == unicode2); + String unicode3(L"\u65E5\u672C\u8A9E"); + CPPUNIT_ASSERT(*(unicode3.toCWString() + 1) == L'\u672C'); + CPPUNIT_ASSERT(strcmp(String::number(0).toCString(), "0") == 0); CPPUNIT_ASSERT(strcmp(String::number(12345678).toCString(), "12345678") == 0); CPPUNIT_ASSERT(strcmp(String::number(-12345678).toCString(), "-12345678") == 0); From 75b685fa53bb9c7ca258eafb3641f46e6b2546e6 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Mon, 24 Jun 2013 01:13:35 +0900 Subject: [PATCH 2/2] Updated the related comments --- taglib/toolkit/tstring.h | 48 ++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/taglib/toolkit/tstring.h b/taglib/toolkit/tstring.h index 569650eb..57945bee 100644 --- a/taglib/toolkit/tstring.h +++ b/taglib/toolkit/tstring.h @@ -178,40 +178,56 @@ namespace TagLib { virtual ~String(); /*! - * If \a unicode if false (the default) this will return a \e Latin1 encoded - * std::string. If it is true the returned std::wstring will be UTF-8 - * encoded. + * Returns a deep copy of this String as an std::string. The returned string + * is encoded in UTF8 if \a unicode is true, otherwise Latin1. + * + * \see toCString() */ std::string to8Bit(bool unicode = false) const; /*! - * Returns a wstring version of the TagLib string as a wide string. + * Returns a deep copy of this String as a wstring. The returned string is + * encoded in UTF-16 (without BOM/CPU byte order). + * + * \see toCWString() */ wstring toWString() const; /*! - * Creates and returns a C-String based on the data. This string is still - * owned by the String (class) and as such should not be deleted by the user. + * Creates and returns a standard C-style (null-terminated) version of this + * String. The returned string is encoded in UTF8 if \a unicode is true, + * otherwise Latin1. + * + * The returned string is still owned by this String and should not be deleted + * by the user. * - * If \a unicode if false (the default) this string will be encoded in - * \e Latin1. If it is true the returned C-String will be UTF-8 encoded. + * The returned pointer remains valid until this String instance is destroyed + * or toCString() is called again. * - * This string remains valid until the String instance is destroyed or - * another export method is called. - * - * \warning This however has the side effect that this C-String will remain - * in memory in addition to other memory that is consumed by the + * \warning This however has the side effect that the returned string will remain + * in memory in addition to other memory that is consumed by this * String instance. So, this method should not be used on large strings or - * where memory is critical. + * where memory is critical. Consider using to8Bit() instead to avoid it. + * + * \see to8Bit() */ const char *toCString(bool unicode = false) const; /*! - * Returns a pointer to the wide char version of the TagLib string. The string - * is encoded in UTF-16(without BOM/CPU byte order). + * Returns a standard C-style (null-terminated) wide character version of + * this String. The returned string is encoded in UTF-16 (without BOM/CPU byte + * order). + * + * The returned string is still owned by this String and should not be deleted + * by the user. + * + * The returned pointer remains valid until this String instance is destroyed + * or any other method of this String is called. * * /note This returns a pointer to the String's internal data without any * conversions. + * + * \see toWString() */ const wchar_t *toCWString() const;