Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-09 08:14:24

0001 //
0002 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
0003 // Copyright (c) 2022-2024 Alexander Grund
0004 //
0005 // Distributed under the Boost Software License, Version 1.0.
0006 // https://www.boost.org/LICENSE_1_0.txt
0007 
0008 #ifndef BOOST_LOCALE_ENCODING_UTF_HPP_INCLUDED
0009 #define BOOST_LOCALE_ENCODING_UTF_HPP_INCLUDED
0010 
0011 #include <boost/locale/detail/allocator_traits.hpp>
0012 #include <boost/locale/encoding_errors.hpp>
0013 #include <boost/locale/utf.hpp>
0014 #include <boost/locale/util/string.hpp>
0015 #include <iterator>
0016 #include <memory>
0017 #include <type_traits>
0018 
0019 #ifdef BOOST_MSVC
0020 #    pragma warning(push)
0021 #    pragma warning(disable : 4275 4251 4231 4660)
0022 #endif
0023 
0024 namespace boost { namespace locale { namespace conv {
0025     /// \addtogroup codepage
0026     ///
0027     /// @{
0028 
0029     /// Convert a Unicode text in range [begin,end) to other Unicode encoding
0030     ///
0031     /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded)
0032     template<typename CharOut, typename CharIn, class Alloc = std::allocator<CharOut>>
0033     std::basic_string<CharOut, std::char_traits<CharOut>, Alloc>
0034     utf_to_utf(const CharIn* begin, const CharIn* end, method_type how = default_method, const Alloc& alloc = Alloc())
0035     {
0036         std::basic_string<CharOut, std::char_traits<CharOut>, Alloc> result(alloc);
0037         result.reserve(end - begin);
0038         auto inserter = std::back_inserter(result);
0039         while(begin != end) {
0040             const utf::code_point c = utf::utf_traits<CharIn>::decode(begin, end);
0041             if(c == utf::illegal || c == utf::incomplete) {
0042                 if(how == stop)
0043                     throw conversion_error();
0044             } else
0045                 utf::utf_traits<CharOut>::encode(c, inserter);
0046         }
0047         return result;
0048     }
0049 
0050     /// Convert a Unicode string \a str to other Unicode encoding.
0051     /// Invalid characters are skipped.
0052     template<typename CharOut, typename CharIn, class Alloc>
0053     std::basic_string<CharOut, std::char_traits<CharOut>, Alloc>
0054     utf_to_utf(const CharIn* begin, const CharIn* end, const Alloc& alloc)
0055     {
0056         return utf_to_utf<CharOut>(begin, end, skip, alloc);
0057     }
0058 
0059     /// Convert a Unicode NULL terminated string \a str to other Unicode encoding
0060     ///
0061     /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded)
0062     template<typename CharOut, typename CharIn, class Alloc = std::allocator<CharOut>>
0063     std::basic_string<CharOut, std::char_traits<CharOut>, Alloc>
0064     utf_to_utf(const CharIn* str, method_type how = default_method, const Alloc& alloc = Alloc())
0065     {
0066         return utf_to_utf<CharOut>(str, util::str_end(str), how, alloc);
0067     }
0068 
0069     /// Convert a Unicode string \a str to other Unicode encoding.
0070     /// Invalid characters are skipped.
0071     template<typename CharOut, typename CharIn, class Alloc>
0072 #ifndef BOOST_LOCALE_DOXYGEN
0073     detail::enable_if_allocator_for<Alloc,
0074                                     CharOut,
0075 #endif
0076                                     std::basic_string<CharOut, std::char_traits<CharOut>, Alloc>
0077 #ifndef BOOST_LOCALE_DOXYGEN
0078                                     >
0079 #endif
0080     utf_to_utf(const CharIn* str, const Alloc& alloc)
0081     {
0082         return utf_to_utf<CharOut>(str, skip, alloc);
0083     }
0084 
0085     /// Convert a Unicode string \a str to other Unicode encoding
0086     ///
0087     /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded)
0088     template<typename CharOut, typename CharIn, class Alloc>
0089 #ifndef BOOST_LOCALE_DOXYGEN
0090     detail::enable_if_allocator_for<
0091       Alloc,
0092       CharIn,
0093 #endif
0094       std::basic_string<CharOut, std::char_traits<CharOut>, detail::rebind_alloc<Alloc, CharOut>>
0095 #ifndef BOOST_LOCALE_DOXYGEN
0096       >
0097 #endif
0098     utf_to_utf(const std::basic_string<CharIn, std::char_traits<CharIn>, Alloc>& str, method_type how = default_method)
0099     {
0100         return utf_to_utf<CharOut>(str.c_str(),
0101                                    str.c_str() + str.size(),
0102                                    how,
0103                                    detail::rebind_alloc<Alloc, CharOut>(str.get_allocator()));
0104     }
0105 
0106     /// Convert a Unicode string \a str to other Unicode encoding
0107     ///
0108     /// \throws conversion_error: Conversion failed (e.g. \a how is \c stop and any character cannot be decoded)
0109     template<typename CharOut, typename CharIn, class AllocOut, class AllocIn>
0110 #ifndef BOOST_LOCALE_DOXYGEN
0111     detail::enable_if_allocator_for<AllocIn,
0112                                     CharIn,
0113 #endif
0114                                     std::basic_string<CharOut, std::char_traits<CharOut>, AllocOut>
0115 #ifndef BOOST_LOCALE_DOXYGEN
0116                                     >
0117 #endif
0118     utf_to_utf(const std::basic_string<CharIn, std::char_traits<CharIn>, AllocIn>& str,
0119                method_type how = default_method,
0120                const AllocOut& alloc = AllocOut())
0121     {
0122         return utf_to_utf<CharOut>(str.c_str(), str.c_str() + str.size(), how, alloc);
0123     }
0124 
0125     /// Convert a Unicode string \a str to other Unicode encoding.
0126     /// Invalid characters are skipped.
0127     template<typename CharOut, typename CharIn, class AllocOut, class AllocIn>
0128 #ifndef BOOST_LOCALE_DOXYGEN
0129     detail::enable_if_allocator_for2<AllocIn,
0130                                      CharIn,
0131                                      AllocOut,
0132                                      CharOut,
0133 #endif
0134                                      std::basic_string<CharOut, std::char_traits<CharOut>, AllocOut>
0135 #ifndef BOOST_LOCALE_DOXYGEN
0136                                      >
0137 #endif
0138     utf_to_utf(const std::basic_string<CharIn, std::char_traits<CharIn>, AllocIn>& str, const AllocOut& alloc)
0139     {
0140         return utf_to_utf<CharOut>(str, skip, alloc);
0141     }
0142 
0143     /// @}
0144 
0145 }}} // namespace boost::locale::conv
0146 
0147 #ifdef BOOST_MSVC
0148 #    pragma warning(pop)
0149 #endif
0150 
0151 #endif