Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

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.
6// https://www.boost.org/LICENSE_1_0.txt
7
8#ifndef BOOST_NOWIDE_CONVERT_HPP_INCLUDED
9#define BOOST_NOWIDE_CONVERT_HPP_INCLUDED
10
11#include <boost/nowide/detail/is_string_container.hpp>
12#include <boost/nowide/utf/convert.hpp>
13#include <string>
14
15namespace boost {
16namespace nowide {
17
25 inline char* narrow(char* output, size_t output_size, const wchar_t* begin, const wchar_t* end)
26 {
27 return utf::convert_buffer(output, output_size, begin, end);
28 }
36 inline char* narrow(char* output, size_t output_size, const wchar_t* source)
37 {
38 return narrow(output, output_size, source, source + utf::strlen(source));
39 }
40
48 inline wchar_t* widen(wchar_t* output, size_t output_size, const char* begin, const char* end)
49 {
50 return utf::convert_buffer(output, output_size, begin, end);
51 }
59 inline wchar_t* widen(wchar_t* output, size_t output_size, const char* source)
60 {
61 return widen(output, output_size, source, source + utf::strlen(source));
62 }
63
71 template<typename T_Char, typename = detail::requires_wide_char<T_Char>>
72 inline std::string narrow(const T_Char* s, size_t count)
73 {
74 return utf::convert_string<char>(s, s + count);
75 }
82 template<typename T_Char, typename = detail::requires_wide_char<T_Char>>
83 inline std::string narrow(const T_Char* s)
84 {
85 return narrow(s, utf::strlen(s));
86 }
93 template<typename StringOrStringView, typename = detail::requires_wide_string_container<StringOrStringView>>
94 inline std::string narrow(const StringOrStringView& s)
95 {
96 return utf::convert_string<char>(s.data(), s.data() + s.size());
97 }
98
106 template<typename T_Char, typename = detail::requires_narrow_char<T_Char>>
107 inline std::wstring widen(const T_Char* s, size_t count)
108 {
109 return utf::convert_string<wchar_t>(s, s + count);
110 }
117 template<typename T_Char, typename = detail::requires_narrow_char<T_Char>>
118 inline std::wstring widen(const T_Char* s)
119 {
120 return widen(s, utf::strlen(s));
121 }
128 template<typename StringOrStringView, typename = detail::requires_narrow_string_container<StringOrStringView>>
129 inline std::wstring widen(const StringOrStringView& s)
130 {
131 return utf::convert_string<wchar_t>(s.data(), s.data() + s.size());
132 }
133} // namespace nowide
134} // namespace boost
135
136#endif
CharOut * convert_buffer(CharOut *buffer, size_t buffer_size, const CharIn *source_begin, const CharIn *source_end)
Definition: convert.hpp:42
size_t strlen(const Char *s)
Definition: convert.hpp:25
wchar_t * widen(wchar_t *output, size_t output_size, const char *begin, const char *end)
Definition: convert.hpp:48
char * narrow(char *output, size_t output_size, const wchar_t *begin, const wchar_t *end)
Definition: convert.hpp:25