]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/nowide/convert.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / nowide / convert.hpp
1 //
2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
3 // Copyright (c) 2020 Alexander Grund
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 #ifndef BOOST_NOWIDE_CONVERT_HPP_INCLUDED
10 #define BOOST_NOWIDE_CONVERT_HPP_INCLUDED
11
12 #include <boost/nowide/detail/is_string_container.hpp>
13 #include <boost/nowide/utf/convert.hpp>
14 #include <string>
15
16 namespace boost {
17 namespace nowide {
18
19 ///
20 /// Convert wide string (UTF-16/32) in range [begin,end) to NULL terminated narrow string (UTF-8)
21 /// stored in \a output of size \a output_size (including NULL)
22 ///
23 /// If there is not enough room NULL is returned, else output is returned.
24 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
25 ///
26 inline char* narrow(char* output, size_t output_size, const wchar_t* begin, const wchar_t* end)
27 {
28 return utf::convert_buffer(output, output_size, begin, end);
29 }
30 ///
31 /// Convert NULL terminated wide string (UTF-16/32) to NULL terminated narrow string (UTF-8)
32 /// stored in \a output of size \a output_size (including NULL)
33 ///
34 /// If there is not enough room NULL is returned, else output is returned.
35 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
36 ///
37 inline char* narrow(char* output, size_t output_size, const wchar_t* source)
38 {
39 return narrow(output, output_size, source, source + utf::strlen(source));
40 }
41
42 ///
43 /// Convert narrow string (UTF-8) in range [begin,end) to NULL terminated wide string (UTF-16/32)
44 /// stored in \a output of size \a output_size (including NULL)
45 ///
46 /// If there is not enough room NULL is returned, else output is returned.
47 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
48 ///
49 inline wchar_t* widen(wchar_t* output, size_t output_size, const char* begin, const char* end)
50 {
51 return utf::convert_buffer(output, output_size, begin, end);
52 }
53 ///
54 /// Convert NULL terminated narrow string (UTF-8) to NULL terminated wide string (UTF-16/32)
55 /// most output_size (including NULL)
56 ///
57 /// If there is not enough room NULL is returned, else output is returned.
58 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
59 ///
60 inline wchar_t* widen(wchar_t* output, size_t output_size, const char* source)
61 {
62 return widen(output, output_size, source, source + utf::strlen(source));
63 }
64
65 ///
66 /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
67 ///
68 /// \param s Input string
69 /// \param count Number of characters to convert
70 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
71 ///
72 template<typename T_Char, typename = detail::requires_wide_char<T_Char>>
73 inline std::string narrow(const T_Char* s, size_t count)
74 {
75 return utf::convert_string<char>(s, s + count);
76 }
77 ///
78 /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
79 ///
80 /// \param s NULL terminated input string
81 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
82 ///
83 template<typename T_Char, typename = detail::requires_wide_char<T_Char>>
84 inline std::string narrow(const T_Char* s)
85 {
86 return narrow(s, utf::strlen(s));
87 }
88 ///
89 /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
90 ///
91 /// \param s Input string
92 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
93 ///
94 template<typename StringOrStringView, typename = detail::requires_wide_string_container<StringOrStringView>>
95 inline std::string narrow(const StringOrStringView& s)
96 {
97 return utf::convert_string<char>(s.data(), s.data() + s.size());
98 }
99
100 ///
101 /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
102 ///
103 /// \param s Input string
104 /// \param count Number of characters to convert
105 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
106 ///
107 template<typename T_Char, typename = detail::requires_narrow_char<T_Char>>
108 inline std::wstring widen(const T_Char* s, size_t count)
109 {
110 return utf::convert_string<wchar_t>(s, s + count);
111 }
112 ///
113 /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
114 ///
115 /// \param s NULL terminated input string
116 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
117 ///
118 template<typename T_Char, typename = detail::requires_narrow_char<T_Char>>
119 inline std::wstring widen(const T_Char* s)
120 {
121 return widen(s, utf::strlen(s));
122 }
123 ///
124 /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
125 ///
126 /// \param s Input string
127 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
128 ///
129 template<typename StringOrStringView, typename = detail::requires_narrow_string_container<StringOrStringView>>
130 inline std::wstring widen(const StringOrStringView& s)
131 {
132 return utf::convert_string<wchar_t>(s.data(), s.data() + s.size());
133 }
134 } // namespace nowide
135 } // namespace boost
136
137 #endif