Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:42:45

0001 //
0002 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
0003 // Copyright (c) 2020 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_NOWIDE_UTF_CONVERT_HPP_INCLUDED
0009 #define BOOST_NOWIDE_UTF_CONVERT_HPP_INCLUDED
0010 
0011 #include <boost/nowide/detail/is_string_container.hpp>
0012 #include <boost/nowide/replacement.hpp>
0013 #include <boost/nowide/utf/utf.hpp>
0014 #include <iterator>
0015 #include <string>
0016 
0017 namespace boost {
0018 namespace nowide {
0019     namespace utf {
0020 
0021         /// Return the length of the given string in code units.
0022         /// That is the number of elements of type Char until the first NULL character.
0023         /// Equivalent to `std::strlen(s)` but can handle wide-strings
0024         template<typename Char>
0025         size_t strlen(const Char* s)
0026         {
0027             const Char* end = s;
0028             while(*end)
0029                 end++;
0030             return end - s;
0031         }
0032 
0033         /// Convert a buffer of UTF sequences in the range [source_begin, source_end)
0034         /// from \a CharIn to \a CharOut to the output \a buffer of size \a buffer_size.
0035         ///
0036         /// \return original buffer containing the NULL terminated string or NULL
0037         ///
0038         /// If there is not enough room in the buffer NULL is returned, and the content of the buffer is undefined.
0039         /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
0040         template<typename CharOut, typename CharIn>
0041         CharOut*
0042         convert_buffer(CharOut* buffer, size_t buffer_size, const CharIn* source_begin, const CharIn* source_end)
0043         {
0044             CharOut* rv = buffer;
0045             if(buffer_size == 0)
0046                 return nullptr;
0047             buffer_size--;
0048             while(source_begin != source_end)
0049             {
0050                 code_point c = utf_traits<CharIn>::decode(source_begin, source_end);
0051                 if(c == illegal || c == incomplete)
0052                 {
0053                     c = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
0054                 }
0055                 size_t width = utf_traits<CharOut>::width(c);
0056                 if(buffer_size < width)
0057                 {
0058                     rv = nullptr;
0059                     break;
0060                 }
0061                 buffer = utf_traits<CharOut>::encode(c, buffer);
0062                 buffer_size -= width;
0063             }
0064             *buffer++ = 0;
0065             return rv;
0066         }
0067 
0068         /// Convert the UTF sequences in range [begin, end) from \a CharIn to \a CharOut
0069         /// and return it as a string
0070         ///
0071         /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
0072         /// \tparam CharOut Output character type
0073         template<typename CharOut, typename CharIn>
0074         std::basic_string<CharOut> convert_string(const CharIn* begin, const CharIn* end)
0075         {
0076             std::basic_string<CharOut> result;
0077             result.reserve(end - begin);
0078             using inserter_type = std::back_insert_iterator<std::basic_string<CharOut>>;
0079             inserter_type inserter(result);
0080             code_point c;
0081             while(begin != end)
0082             {
0083                 c = utf_traits<CharIn>::decode(begin, end);
0084                 if(c == illegal || c == incomplete)
0085                 {
0086                     c = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
0087                 }
0088                 utf_traits<CharOut>::encode(c, inserter);
0089             }
0090             return result;
0091         }
0092 
0093         /// Convert the UTF sequence in the input string from \a CharIn to \a CharOut
0094         /// and return it as a string
0095         ///
0096         /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
0097         /// \tparam CharOut Output character type
0098         template<typename CharOut, typename CharIn>
0099         std::basic_string<CharOut> convert_string(const std::basic_string<CharIn>& s)
0100         {
0101             return convert_string<CharOut>(s.data(), s.data() + s.size());
0102         }
0103 
0104     } // namespace utf
0105 } // namespace nowide
0106 } // namespace boost
0107 
0108 #endif