]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // |
2 | // Copyright (c) 2012 Artyom Beilis (Tonkikh) | |
1e59de90 | 3 | // Copyright (c) 2020 Alexander Grund |
f67539c2 TL |
4 | // |
5 | // Distributed under the Boost Software License, Version 1.0. (See | |
20effc67 | 6 | // accompanying file LICENSE or copy at |
f67539c2 TL |
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 | ||
1e59de90 | 12 | #include <boost/nowide/detail/is_string_container.hpp> |
20effc67 | 13 | #include <boost/nowide/utf/convert.hpp> |
f67539c2 TL |
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 | { | |
20effc67 | 28 | return utf::convert_buffer(output, output_size, begin, end); |
f67539c2 TL |
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 | { | |
20effc67 | 39 | return narrow(output, output_size, source, source + utf::strlen(source)); |
f67539c2 TL |
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 | { | |
20effc67 | 51 | return utf::convert_buffer(output, output_size, begin, end); |
f67539c2 TL |
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 | { | |
20effc67 | 62 | return widen(output, output_size, source, source + utf::strlen(source)); |
f67539c2 TL |
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 | /// | |
1e59de90 TL |
72 | template<typename T_Char, typename = detail::requires_wide_char<T_Char>> |
73 | inline std::string narrow(const T_Char* s, size_t count) | |
f67539c2 | 74 | { |
20effc67 | 75 | return utf::convert_string<char>(s, s + count); |
f67539c2 TL |
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 | /// | |
1e59de90 TL |
83 | template<typename T_Char, typename = detail::requires_wide_char<T_Char>> |
84 | inline std::string narrow(const T_Char* s) | |
f67539c2 | 85 | { |
20effc67 | 86 | return narrow(s, utf::strlen(s)); |
f67539c2 TL |
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 | /// | |
1e59de90 TL |
94 | template<typename StringOrStringView, typename = detail::requires_wide_string_container<StringOrStringView>> |
95 | inline std::string narrow(const StringOrStringView& s) | |
f67539c2 | 96 | { |
1e59de90 | 97 | return utf::convert_string<char>(s.data(), s.data() + s.size()); |
f67539c2 TL |
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 | /// | |
1e59de90 TL |
107 | template<typename T_Char, typename = detail::requires_narrow_char<T_Char>> |
108 | inline std::wstring widen(const T_Char* s, size_t count) | |
f67539c2 | 109 | { |
20effc67 | 110 | return utf::convert_string<wchar_t>(s, s + count); |
f67539c2 TL |
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 | /// | |
1e59de90 TL |
118 | template<typename T_Char, typename = detail::requires_narrow_char<T_Char>> |
119 | inline std::wstring widen(const T_Char* s) | |
f67539c2 | 120 | { |
20effc67 | 121 | return widen(s, utf::strlen(s)); |
f67539c2 TL |
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 | /// | |
1e59de90 TL |
129 | template<typename StringOrStringView, typename = detail::requires_narrow_string_container<StringOrStringView>> |
130 | inline std::wstring widen(const StringOrStringView& s) | |
f67539c2 | 131 | { |
1e59de90 | 132 | return utf::convert_string<wchar_t>(s.data(), s.data() + s.size()); |
f67539c2 TL |
133 | } |
134 | } // namespace nowide | |
135 | } // namespace boost | |
136 | ||
137 | #endif |