File indexing completed on 2025-01-18 09:42:45
0001
0002
0003
0004
0005
0006
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
0022
0023
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
0034
0035
0036
0037
0038
0039
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
0069
0070
0071
0072
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
0094
0095
0096
0097
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 }
0105 }
0106 }
0107
0108 #endif