]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/nowide/utf/convert.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / nowide / utf / convert.hpp
1 //
2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_NOWIDE_UTF_CONVERT_HPP_INCLUDED
9 #define BOOST_NOWIDE_UTF_CONVERT_HPP_INCLUDED
10
11 #include <boost/nowide/replacement.hpp>
12 #include <boost/nowide/utf/utf.hpp>
13 #include <iterator>
14 #include <string>
15
16 namespace boost {
17 namespace nowide {
18 namespace utf {
19 ///
20 /// Convert a buffer of UTF sequences in the range [source_begin, source_end)
21 /// from \tparam CharIn to \tparam CharOut to the output \a buffer of size \a buffer_size.
22 ///
23 /// \return original buffer containing the NULL terminated string or NULL
24 ///
25 /// If there is not enough room in the buffer NULL is returned, and the content of the buffer is undefined.
26 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
27 ///
28 template<typename CharOut, typename CharIn>
29 CharOut*
30 convert_buffer(CharOut* buffer, size_t buffer_size, const CharIn* source_begin, const CharIn* source_end)
31 {
32 CharOut* rv = buffer;
33 if(buffer_size == 0)
34 return 0;
35 buffer_size--;
36 while(source_begin != source_end)
37 {
38 code_point c = utf_traits<CharIn>::decode(source_begin, source_end);
39 if(c == illegal || c == incomplete)
40 {
41 c = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
42 }
43 size_t width = utf_traits<CharOut>::width(c);
44 if(buffer_size < width)
45 {
46 rv = NULL;
47 break;
48 }
49 buffer = utf_traits<CharOut>::encode(c, buffer);
50 buffer_size -= width;
51 }
52 *buffer++ = 0;
53 return rv;
54 }
55
56 ///
57 /// Convert the UTF sequences in range [begin, end) from \tparam CharIn to \tparam CharOut
58 /// and return it as a string
59 ///
60 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
61 ///
62 template<typename CharOut, typename CharIn>
63 std::basic_string<CharOut> convert_string(const CharIn* begin, const CharIn* end)
64 {
65 std::basic_string<CharOut> result;
66 result.reserve(end - begin);
67 using inserter_type = std::back_insert_iterator<std::basic_string<CharOut>>;
68 inserter_type inserter(result);
69 code_point c;
70 while(begin != end)
71 {
72 c = utf_traits<CharIn>::decode(begin, end);
73 if(c == illegal || c == incomplete)
74 {
75 c = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
76 }
77 utf_traits<CharOut>::encode(c, inserter);
78 }
79 return result;
80 }
81
82 /// Return the length of the given string in code units.
83 /// That is the number of elements of type Char until the first NULL character
84 /// Equivalent to `std::strlen(s)` but can handle wide-strings
85 template<typename Char>
86 size_t strlen(const Char* s)
87 {
88 const Char* end = s;
89 while(*end)
90 end++;
91 return end - s;
92 }
93
94 } // namespace utf
95 } // namespace nowide
96 } // namespace boost
97
98 #endif