// // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) // Copyright (c) 2022-2024 Alexander Grund // // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #ifndef BOOST_LOCALE_ENCODING_UTF_HPP_INCLUDED #define BOOST_LOCALE_ENCODING_UTF_HPP_INCLUDED #include #include #include #include #include #include #include #ifdef BOOST_MSVC # pragma warning(push) # pragma warning(disable : 4275 4251 4231 4660) #endif namespace boost { namespace locale { namespace conv { /// \addtogroup codepage /// /// @{ /// Convert a Unicode text in range [begin,end) to other Unicode encoding /// /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded) template> std::basic_string, Alloc> utf_to_utf(const CharIn* begin, const CharIn* end, method_type how = default_method, const Alloc& alloc = Alloc()) { std::basic_string, Alloc> result(alloc); result.reserve(end - begin); auto inserter = std::back_inserter(result); while(begin != end) { const utf::code_point c = utf::utf_traits::decode(begin, end); if(c == utf::illegal || c == utf::incomplete) { if(how == stop) throw conversion_error(); } else utf::utf_traits::encode(c, inserter); } return result; } /// Convert a Unicode string \a str to other Unicode encoding. /// Invalid characters are skipped. template std::basic_string, Alloc> utf_to_utf(const CharIn* begin, const CharIn* end, const Alloc& alloc) { return utf_to_utf(begin, end, skip, alloc); } /// Convert a Unicode NULL terminated string \a str to other Unicode encoding /// /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded) template> std::basic_string, Alloc> utf_to_utf(const CharIn* str, method_type how = default_method, const Alloc& alloc = Alloc()) { return utf_to_utf(str, util::str_end(str), how, alloc); } /// Convert a Unicode string \a str to other Unicode encoding. /// Invalid characters are skipped. template #ifndef BOOST_LOCALE_DOXYGEN detail::enable_if_allocator_for, Alloc> #ifndef BOOST_LOCALE_DOXYGEN > #endif utf_to_utf(const CharIn* str, const Alloc& alloc) { return utf_to_utf(str, skip, alloc); } /// Convert a Unicode string \a str to other Unicode encoding /// /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded) template #ifndef BOOST_LOCALE_DOXYGEN detail::enable_if_allocator_for< Alloc, CharIn, #endif std::basic_string, detail::rebind_alloc> #ifndef BOOST_LOCALE_DOXYGEN > #endif utf_to_utf(const std::basic_string, Alloc>& str, method_type how = default_method) { return utf_to_utf(str.c_str(), str.c_str() + str.size(), how, detail::rebind_alloc(str.get_allocator())); } /// Convert a Unicode string \a str to other Unicode encoding /// /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded) template #ifndef BOOST_LOCALE_DOXYGEN detail::enable_if_allocator_for, AllocOut> #ifndef BOOST_LOCALE_DOXYGEN > #endif utf_to_utf(const std::basic_string, AllocIn>& str, method_type how = default_method, const AllocOut& alloc = AllocOut()) { return utf_to_utf(str.c_str(), str.c_str() + str.size(), how, alloc); } /// Convert a Unicode string \a str to other Unicode encoding. /// Invalid characters are skipped. template #ifndef BOOST_LOCALE_DOXYGEN detail::enable_if_allocator_for2, AllocOut> #ifndef BOOST_LOCALE_DOXYGEN > #endif utf_to_utf(const std::basic_string, AllocIn>& str, const AllocOut& alloc) { return utf_to_utf(str, skip, alloc); } /// @} }}} // namespace boost::locale::conv #ifdef BOOST_MSVC # pragma warning(pop) #endif #endif