2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
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)
8 #ifndef BOOST_NOWIDE_CONVERT_HPP_INCLUDED
9 #define BOOST_NOWIDE_CONVERT_HPP_INCLUDED
11 #include <boost/nowide/utf/convert.hpp>
18 /// Convert wide string (UTF-16/32) in range [begin,end) to NULL terminated narrow string (UTF-8)
19 /// stored in \a output of size \a output_size (including NULL)
21 /// If there is not enough room NULL is returned, else output is returned.
22 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
24 inline char* narrow(char* output, size_t output_size, const wchar_t* begin, const wchar_t* end)
26 return utf::convert_buffer(output, output_size, begin, end);
29 /// Convert NULL terminated wide string (UTF-16/32) to NULL terminated narrow string (UTF-8)
30 /// stored in \a output of size \a output_size (including NULL)
32 /// If there is not enough room NULL is returned, else output is returned.
33 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
35 inline char* narrow(char* output, size_t output_size, const wchar_t* source)
37 return narrow(output, output_size, source, source + utf::strlen(source));
41 /// Convert narrow string (UTF-8) in range [begin,end) to NULL terminated wide string (UTF-16/32)
42 /// stored in \a output of size \a output_size (including NULL)
44 /// If there is not enough room NULL is returned, else output is returned.
45 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
47 inline wchar_t* widen(wchar_t* output, size_t output_size, const char* begin, const char* end)
49 return utf::convert_buffer(output, output_size, begin, end);
52 /// Convert NULL terminated narrow string (UTF-8) to NULL terminated wide string (UTF-16/32)
53 /// most output_size (including NULL)
55 /// If there is not enough room NULL is returned, else output is returned.
56 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
58 inline wchar_t* widen(wchar_t* output, size_t output_size, const char* source)
60 return widen(output, output_size, source, source + utf::strlen(source));
64 /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
66 /// \param s Input string
67 /// \param count Number of characters to convert
68 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
70 inline std::string narrow(const wchar_t* s, size_t count)
72 return utf::convert_string<char>(s, s + count);
75 /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
77 /// \param s NULL terminated input string
78 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
80 inline std::string narrow(const wchar_t* s)
82 return narrow(s, utf::strlen(s));
85 /// Convert wide string (UTF-16/32) to narrow string (UTF-8).
87 /// \param s Input string
88 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
90 inline std::string narrow(const std::wstring& s)
92 return narrow(s.c_str(), s.size());
96 /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
98 /// \param s Input string
99 /// \param count Number of characters to convert
100 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
102 inline std::wstring widen(const char* s, size_t count)
104 return utf::convert_string<wchar_t>(s, s + count);
107 /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
109 /// \param s NULL terminated input string
110 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
112 inline std::wstring widen(const char* s)
114 return widen(s, utf::strlen(s));
117 /// Convert narrow string (UTF-8) to wide string (UTF-16/32).
119 /// \param s Input string
120 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
122 inline std::wstring widen(const std::string& s)
124 return widen(s.c_str(), s.size());
126 } // namespace nowide