|
||||
File indexing completed on 2025-01-18 09:42:45
0001 // 0002 // Copyright (c) 2012 Artyom Beilis (Tonkikh) 0003 // Copyright (c) 2020 Alexander Grund 0004 // 0005 // Distributed under the Boost Software License, Version 1.0. 0006 // https://www.boost.org/LICENSE_1_0.txt 0007 0008 #ifndef BOOST_NOWIDE_CONVERT_HPP_INCLUDED 0009 #define BOOST_NOWIDE_CONVERT_HPP_INCLUDED 0010 0011 #include <boost/nowide/detail/is_string_container.hpp> 0012 #include <boost/nowide/utf/convert.hpp> 0013 #include <string> 0014 0015 namespace boost { 0016 namespace nowide { 0017 0018 /// 0019 /// Convert wide string (UTF-16/32) in range [begin,end) to NULL terminated narrow string (UTF-8) 0020 /// stored in \a output of size \a output_size (including NULL) 0021 /// 0022 /// If there is not enough room NULL is returned, else output is returned. 0023 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER 0024 /// 0025 inline char* narrow(char* output, size_t output_size, const wchar_t* begin, const wchar_t* end) 0026 { 0027 return utf::convert_buffer(output, output_size, begin, end); 0028 } 0029 /// 0030 /// Convert NULL terminated wide string (UTF-16/32) to NULL terminated narrow string (UTF-8) 0031 /// stored in \a output of size \a output_size (including NULL) 0032 /// 0033 /// If there is not enough room NULL is returned, else output is returned. 0034 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER 0035 /// 0036 inline char* narrow(char* output, size_t output_size, const wchar_t* source) 0037 { 0038 return narrow(output, output_size, source, source + utf::strlen(source)); 0039 } 0040 0041 /// 0042 /// Convert narrow string (UTF-8) in range [begin,end) to NULL terminated wide string (UTF-16/32) 0043 /// stored in \a output of size \a output_size (including NULL) 0044 /// 0045 /// If there is not enough room NULL is returned, else output is returned. 0046 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER 0047 /// 0048 inline wchar_t* widen(wchar_t* output, size_t output_size, const char* begin, const char* end) 0049 { 0050 return utf::convert_buffer(output, output_size, begin, end); 0051 } 0052 /// 0053 /// Convert NULL terminated narrow string (UTF-8) to NULL terminated wide string (UTF-16/32) 0054 /// most output_size (including NULL) 0055 /// 0056 /// If there is not enough room NULL is returned, else output is returned. 0057 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER 0058 /// 0059 inline wchar_t* widen(wchar_t* output, size_t output_size, const char* source) 0060 { 0061 return widen(output, output_size, source, source + utf::strlen(source)); 0062 } 0063 0064 /// 0065 /// Convert wide string (UTF-16/32) to narrow string (UTF-8). 0066 /// 0067 /// \param s Input string 0068 /// \param count Number of characters to convert 0069 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER 0070 /// 0071 template<typename T_Char, typename = detail::requires_wide_char<T_Char>> 0072 inline std::string narrow(const T_Char* s, size_t count) 0073 { 0074 return utf::convert_string<char>(s, s + count); 0075 } 0076 /// 0077 /// Convert wide string (UTF-16/32) to narrow string (UTF-8). 0078 /// 0079 /// \param s NULL terminated input string 0080 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER 0081 /// 0082 template<typename T_Char, typename = detail::requires_wide_char<T_Char>> 0083 inline std::string narrow(const T_Char* s) 0084 { 0085 return narrow(s, utf::strlen(s)); 0086 } 0087 /// 0088 /// Convert wide string (UTF-16/32) to narrow string (UTF-8). 0089 /// 0090 /// \param s Input string 0091 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER 0092 /// 0093 template<typename StringOrStringView, typename = detail::requires_wide_string_container<StringOrStringView>> 0094 inline std::string narrow(const StringOrStringView& s) 0095 { 0096 return utf::convert_string<char>(s.data(), s.data() + s.size()); 0097 } 0098 0099 /// 0100 /// Convert narrow string (UTF-8) to wide string (UTF-16/32). 0101 /// 0102 /// \param s Input string 0103 /// \param count Number of characters to convert 0104 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER 0105 /// 0106 template<typename T_Char, typename = detail::requires_narrow_char<T_Char>> 0107 inline std::wstring widen(const T_Char* s, size_t count) 0108 { 0109 return utf::convert_string<wchar_t>(s, s + count); 0110 } 0111 /// 0112 /// Convert narrow string (UTF-8) to wide string (UTF-16/32). 0113 /// 0114 /// \param s NULL terminated input string 0115 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER 0116 /// 0117 template<typename T_Char, typename = detail::requires_narrow_char<T_Char>> 0118 inline std::wstring widen(const T_Char* s) 0119 { 0120 return widen(s, utf::strlen(s)); 0121 } 0122 /// 0123 /// Convert narrow string (UTF-8) to wide string (UTF-16/32). 0124 /// 0125 /// \param s Input string 0126 /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER 0127 /// 0128 template<typename StringOrStringView, typename = detail::requires_narrow_string_container<StringOrStringView>> 0129 inline std::wstring widen(const StringOrStringView& s) 0130 { 0131 return utf::convert_string<wchar_t>(s.data(), s.data() + s.size()); 0132 } 0133 } // namespace nowide 0134 } // namespace boost 0135 0136 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |