From 4a300547f3a220e181d4e8d5e46e3c794bb2fee4 Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Wed, 4 Dec 2013 22:54:17 +0100 Subject: [PATCH] Added String::toUtf8/16/32 functions (#501) --- include/SFML/System/String.hpp | 33 ++++++++++++++++++++++++++++++++ src/SFML/System/String.cpp | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/include/SFML/System/String.hpp b/include/SFML/System/String.hpp index dc200bfd9..8a0bcf8d0 100644 --- a/include/SFML/System/String.hpp +++ b/include/SFML/System/String.hpp @@ -264,6 +264,39 @@ public : //////////////////////////////////////////////////////////// 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 toUtf8() const; + + //////////////////////////////////////////////////////////// + /// \brief Convert the unicode string to a UTF-16 string + /// + /// \return Converted UTF-16 string + /// + /// \see toUtf8, toUtf32 + /// + //////////////////////////////////////////////////////////// + std::basic_string 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 toUtf32() const; + //////////////////////////////////////////////////////////// /// \brief Overload of assignment operator /// diff --git a/src/SFML/System/String.cpp b/src/SFML/System/String.cpp index b4ee1353f..f20f6a416 100644 --- a/src/SFML/System/String.cpp +++ b/src/SFML/System/String.cpp @@ -174,6 +174,41 @@ std::wstring String::toWideString() const } +//////////////////////////////////////////////////////////// +std::basic_string String::toUtf8() const +{ + // Prepare the output string + std::basic_string output; + output.reserve(m_string.length()); + + // Convert + Utf32::toUtf8(m_string.begin(), m_string.end(), std::back_inserter(output)); + + return output; +} + + +//////////////////////////////////////////////////////////// +std::basic_string String::toUtf16() const +{ + // Prepare the output string + std::basic_string output; + output.reserve(m_string.length()); + + // Convert + Utf32::toUtf16(m_string.begin(), m_string.end(), std::back_inserter(output)); + + return output; +} + + +//////////////////////////////////////////////////////////// +std::basic_string String::toUtf32() const +{ + return m_string; +} + + //////////////////////////////////////////////////////////// String& String::operator =(const String& right) {