Added String::toUtf8/16/32 functions (#501)

This commit is contained in:
Laurent Gomila 2013-12-04 22:54:17 +01:00
parent 58f60f2279
commit 4a300547f3
2 changed files with 68 additions and 0 deletions

View File

@ -264,6 +264,39 @@ public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::wstring toWideString() const; std::wstring toWideString() const;
////////////////////////////////////////////////////////////
/// \brief Convert the unicode string to a UTF-8 string
///
/// \return Converted UTF-8 string
///
/// \see toUtf16, toUtf32
///
////////////////////////////////////////////////////////////
std::basic_string<Uint8> toUtf8() const;
////////////////////////////////////////////////////////////
/// \brief Convert the unicode string to a UTF-16 string
///
/// \return Converted UTF-16 string
///
/// \see toUtf8, toUtf32
///
////////////////////////////////////////////////////////////
std::basic_string<Uint16> toUtf16() const;
////////////////////////////////////////////////////////////
/// \brief Convert the unicode string to a UTF-32 string
///
/// This function doesn't perform any conversion, since the
/// string is already stored as UTF-32 internally.
///
/// \return Converted UTF-32 string
///
/// \see toUtf8, toUtf16
///
////////////////////////////////////////////////////////////
std::basic_string<Uint32> toUtf32() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Overload of assignment operator /// \brief Overload of assignment operator
/// ///

View File

@ -174,6 +174,41 @@ std::wstring String::toWideString() const
} }
////////////////////////////////////////////////////////////
std::basic_string<Uint8> String::toUtf8() const
{
// Prepare the output string
std::basic_string<Uint8> output;
output.reserve(m_string.length());
// Convert
Utf32::toUtf8(m_string.begin(), m_string.end(), std::back_inserter(output));
return output;
}
////////////////////////////////////////////////////////////
std::basic_string<Uint16> String::toUtf16() const
{
// Prepare the output string
std::basic_string<Uint16> output;
output.reserve(m_string.length());
// Convert
Utf32::toUtf16(m_string.begin(), m_string.end(), std::back_inserter(output));
return output;
}
////////////////////////////////////////////////////////////
std::basic_string<Uint32> String::toUtf32() const
{
return m_string;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
String& String::operator =(const String& right) String& String::operator =(const String& right)
{ {