From 74ebd1a50b102f6393f063a5f18d5cbc9288cc72 Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Mon, 20 Dec 2010 18:00:12 +0000 Subject: [PATCH] Added conversions from/to latin-1 (ISO-5589-1) in sf::Utf classes git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1758 4e206d99-4929-0410-ac5d-dfc041789085 --- include/SFML/System/Utf.hpp | 84 +++++++++++++++++++++++++++++++++++ include/SFML/System/Utf.inl | 88 +++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) diff --git a/include/SFML/System/Utf.hpp b/include/SFML/System/Utf.hpp index 1c212e2e4..bab27d67e 100644 --- a/include/SFML/System/Utf.hpp +++ b/include/SFML/System/Utf.hpp @@ -152,6 +152,20 @@ public : template static Out FromWide(In begin, In end, Out output); + //////////////////////////////////////////////////////////// + /// \brief Convert a latin-1 (ISO-5589-1) characters range to UTF-8 + /// + /// \param begin Iterator pointing to the beginning of the input sequence + /// \param end Iterator pointing to the end of the input sequence + /// \param output Iterator pointing to the beginning of the output sequence + /// \param locale Locale to use for conversion + /// + /// \return Iterator to the end of the output sequence which has been written + /// + //////////////////////////////////////////////////////////// + template + static Out FromLatin1(In begin, In end, Out output); + //////////////////////////////////////////////////////////// /// \brief Convert an UTF-8 characters range to ANSI characters /// @@ -184,6 +198,20 @@ public : template static Out ToWide(In begin, In end, Out output, wchar_t replacement = 0); + //////////////////////////////////////////////////////////// + /// \brief Convert an UTF-8 characters range to latin-1 (ISO-5589-1) characters + /// + /// \param begin Iterator pointing to the beginning of the input sequence + /// \param end Iterator pointing to the end of the input sequence + /// \param output Iterator pointing to the beginning of the output sequence + /// \param replacement Replacement for characters not convertible to wide (use 0 to skip them) + /// + /// \return Iterator to the end of the output sequence which has been written + /// + //////////////////////////////////////////////////////////// + template + static Out ToLatin1(In begin, In end, Out output, char replacement = 0); + //////////////////////////////////////////////////////////// /// \brief Convert a UTF-8 characters range to UTF-8 /// @@ -332,6 +360,20 @@ public : template static Out FromWide(In begin, In end, Out output); + //////////////////////////////////////////////////////////// + /// \brief Convert a latin-1 (ISO-5589-1) characters range to UTF-16 + /// + /// \param begin Iterator pointing to the beginning of the input sequence + /// \param end Iterator pointing to the end of the input sequence + /// \param output Iterator pointing to the beginning of the output sequence + /// \param locale Locale to use for conversion + /// + /// \return Iterator to the end of the output sequence which has been written + /// + //////////////////////////////////////////////////////////// + template + static Out FromLatin1(In begin, In end, Out output); + //////////////////////////////////////////////////////////// /// \brief Convert an UTF-16 characters range to ANSI characters /// @@ -364,6 +406,20 @@ public : template static Out ToWide(In begin, In end, Out output, wchar_t replacement = 0); + //////////////////////////////////////////////////////////// + /// \brief Convert an UTF-16 characters range to latin-1 (ISO-5589-1) characters + /// + /// \param begin Iterator pointing to the beginning of the input sequence + /// \param end Iterator pointing to the end of the input sequence + /// \param output Iterator pointing to the beginning of the output sequence + /// \param replacement Replacement for characters not convertible to wide (use 0 to skip them) + /// + /// \return Iterator to the end of the output sequence which has been written + /// + //////////////////////////////////////////////////////////// + template + static Out ToLatin1(In begin, In end, Out output, char replacement = 0); + //////////////////////////////////////////////////////////// /// \brief Convert a UTF-16 characters range to UTF-8 /// @@ -513,6 +569,20 @@ public : template static Out FromWide(In begin, In end, Out output); + //////////////////////////////////////////////////////////// + /// \brief Convert a latin-1 (ISO-5589-1) characters range to UTF-32 + /// + /// \param begin Iterator pointing to the beginning of the input sequence + /// \param end Iterator pointing to the end of the input sequence + /// \param output Iterator pointing to the beginning of the output sequence + /// \param locale Locale to use for conversion + /// + /// \return Iterator to the end of the output sequence which has been written + /// + //////////////////////////////////////////////////////////// + template + static Out FromLatin1(In begin, In end, Out output); + //////////////////////////////////////////////////////////// /// \brief Convert an UTF-32 characters range to ANSI characters /// @@ -545,6 +615,20 @@ public : template static Out ToWide(In begin, In end, Out output, wchar_t replacement = 0); + //////////////////////////////////////////////////////////// + /// \brief Convert an UTF-16 characters range to latin-1 (ISO-5589-1) characters + /// + /// \param begin Iterator pointing to the beginning of the input sequence + /// \param end Iterator pointing to the end of the input sequence + /// \param output Iterator pointing to the beginning of the output sequence + /// \param replacement Replacement for characters not convertible to wide (use 0 to skip them) + /// + /// \return Iterator to the end of the output sequence which has been written + /// + //////////////////////////////////////////////////////////// + template + static Out ToLatin1(In begin, In end, Out output, char replacement = 0); + //////////////////////////////////////////////////////////// /// \brief Convert a UTF-32 characters range to UTF-8 /// diff --git a/include/SFML/System/Utf.inl b/include/SFML/System/Utf.inl index 1b9d707af..12b886036 100644 --- a/include/SFML/System/Utf.inl +++ b/include/SFML/System/Utf.inl @@ -176,6 +176,19 @@ Out Utf<8>::FromWide(In begin, In end, Out output) } +//////////////////////////////////////////////////////////// +template +Out Utf<8>::FromLatin1(In begin, In end, Out output) +{ + // Latin-1 is directly compatible with Unicode encodings, + // and can thus be treated as (a sub-range of) UTF-32 + while (begin < end) + output = Encode(*begin++, output); + + return output; +} + + //////////////////////////////////////////////////////////// template Out Utf<8>::ToAnsi(In begin, In end, Out output, char replacement, const std::locale& locale) @@ -206,6 +219,23 @@ Out Utf<8>::ToWide(In begin, In end, Out output, wchar_t replacement) } +//////////////////////////////////////////////////////////// +template +Out Utf<8>::ToLatin1(In begin, In end, Out output, char replacement) +{ + // Latin-1 is directly compatible with Unicode encodings, + // and can thus be treated as (a sub-range of) UTF-32 + while (begin < end) + { + Uint32 codepoint; + begin = Decode(begin, end, codepoint); + *output++ = codepoint < 256 ? static_cast(codepoint) : replacement; + } + + return output; +} + + //////////////////////////////////////////////////////////// template Out Utf<8>::ToUtf8(In begin, In end, Out output) @@ -376,6 +406,19 @@ Out Utf<16>::FromWide(In begin, In end, Out output) } +//////////////////////////////////////////////////////////// +template +Out Utf<16>::FromLatin1(In begin, In end, Out output) +{ + // Latin-1 is directly compatible with Unicode encodings, + // and can thus be treated as (a sub-range of) UTF-32 + while (begin < end) + *output++ = *begin++; + + return output; +} + + //////////////////////////////////////////////////////////// template Out Utf<16>::ToAnsi(In begin, In end, Out output, char replacement, const std::locale& locale) @@ -406,6 +449,22 @@ Out Utf<16>::ToWide(In begin, In end, Out output, wchar_t replacement) } +//////////////////////////////////////////////////////////// +template +Out Utf<16>::ToLatin1(In begin, In end, Out output, char replacement) +{ + // Latin-1 is directly compatible with Unicode encodings, + // and can thus be treated as (a sub-range of) UTF-32 + while (begin < end) + { + *output++ = *begin < 256 ? static_cast(*begin) : replacement; + begin++; + } + + return output; +} + + //////////////////////////////////////////////////////////// template Out Utf<16>::ToUtf8(In begin, In end, Out output) @@ -503,6 +562,19 @@ Out Utf<32>::FromWide(In begin, In end, Out output) } +//////////////////////////////////////////////////////////// +template +Out Utf<32>::FromLatin1(In begin, In end, Out output) +{ + // Latin-1 is directly compatible with Unicode encodings, + // and can thus be treated as (a sub-range of) UTF-32 + while (begin < end) + *output++ = *begin++; + + return output; +} + + //////////////////////////////////////////////////////////// template Out Utf<32>::ToAnsi(In begin, In end, Out output, char replacement, const std::locale& locale) @@ -525,6 +597,22 @@ Out Utf<32>::ToWide(In begin, In end, Out output, wchar_t replacement) } +//////////////////////////////////////////////////////////// +template +Out Utf<32>::ToLatin1(In begin, In end, Out output, char replacement) +{ + // Latin-1 is directly compatible with Unicode encodings, + // and can thus be treated as (a sub-range of) UTF-32 + while (begin < end) + { + *output++ = *begin < 256 ? static_cast(*begin) : replacement; + begin++; + } + + return output; +} + + //////////////////////////////////////////////////////////// template Out Utf<32>::ToUtf8(In begin, In end, Out output)