From ed1c17a236e0e1fd9fc3eab626bbce5d67c3bc2c Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Sat, 24 Aug 2013 14:45:59 +0200 Subject: [PATCH] Added String::fromUtf8/16/32 functions (#196) --- include/SFML/System/String.hpp | 49 +++++++++++++++++++++++++++++++ include/SFML/System/String.inl | 53 ++++++++++++++++++++++++++++++++++ src/SFML/System/CMakeLists.txt | 1 + 3 files changed, 103 insertions(+) create mode 100644 include/SFML/System/String.inl diff --git a/include/SFML/System/String.hpp b/include/SFML/System/String.hpp index 080a8099..dc200bfd 100644 --- a/include/SFML/System/String.hpp +++ b/include/SFML/System/String.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include @@ -155,6 +156,52 @@ public : //////////////////////////////////////////////////////////// String(const String& copy); + //////////////////////////////////////////////////////////// + /// \brief Create a new sf::String from a UTF-8 encoded string + /// + /// \param begin Forward iterator to the begining of the UTF-8 sequence + /// \param end Forward iterator to the end of the UTF-8 sequence + /// + /// \return A sf::String containing the source string + /// + /// \see fromUtf16, fromUtf32 + /// + //////////////////////////////////////////////////////////// + template + static String fromUtf8(T begin, T end); + + //////////////////////////////////////////////////////////// + /// \brief Create a new sf::String from a UTF-16 encoded string + /// + /// \param begin Forward iterator to the begining of the UTF-16 sequence + /// \param end Forward iterator to the end of the UTF-16 sequence + /// + /// \return A sf::String containing the source string + /// + /// \see fromUtf8, fromUtf32 + /// + //////////////////////////////////////////////////////////// + template + static String fromUtf16(T begin, T end); + + //////////////////////////////////////////////////////////// + /// \brief Create a new sf::String from a UTF-32 encoded string + /// + /// This function is provided for consistency, it is equivalent to + /// using the constructors that takes a const sf::Uint32* or + /// a std::basic_string. + /// + /// \param begin Forward iterator to the begining of the UTF-32 sequence + /// \param end Forward iterator to the end of the UTF-32 sequence + /// + /// \return A sf::String containing the source string + /// + /// \see fromUtf8, fromUtf16 + /// + //////////////////////////////////////////////////////////// + template + static String fromUtf32(T begin, T end); + //////////////////////////////////////////////////////////// /// \brief Implicit cast operator to std::string (ANSI string) /// @@ -487,6 +534,8 @@ SFML_SYSTEM_API bool operator >=(const String& left, const String& right); //////////////////////////////////////////////////////////// SFML_SYSTEM_API String operator +(const String& left, const String& right); +#include + } // namespace sf diff --git a/include/SFML/System/String.inl b/include/SFML/System/String.inl new file mode 100644 index 00000000..c3e36336 --- /dev/null +++ b/include/SFML/System/String.inl @@ -0,0 +1,53 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////// +template +String String::fromUtf8(T begin, T end) +{ + String string; + Utf8::toUtf32(begin, end, std::back_inserter(string.m_string)); + return string; +} + + +//////////////////////////////////////////////////////////// +template +String String::fromUtf16(T begin, T end) +{ + String string; + Utf16::toUtf32(begin, end, std::back_inserter(string.m_string)); + return string; +} + + +//////////////////////////////////////////////////////////// +template +String String::fromUtf32(T begin, T end) +{ + String string; + string.m_string.assign(begin, end); + return string; +} diff --git a/src/SFML/System/CMakeLists.txt b/src/SFML/System/CMakeLists.txt index 5ef91395..0c62aa12 100644 --- a/src/SFML/System/CMakeLists.txt +++ b/src/SFML/System/CMakeLists.txt @@ -19,6 +19,7 @@ set(SRC ${INCROOT}/Sleep.hpp ${SRCROOT}/String.cpp ${INCROOT}/String.hpp + ${INCROOT}/String.inl ${SRCROOT}/Thread.cpp ${INCROOT}/Thread.hpp ${INCROOT}/Thread.inl