Boost.Locale
encoding_utf.hpp
1//
2// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3// Copyright (c) 2022-2024 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_LOCALE_ENCODING_UTF_HPP_INCLUDED
9#define BOOST_LOCALE_ENCODING_UTF_HPP_INCLUDED
10
11#include <boost/locale/detail/allocator_traits.hpp>
12#include <boost/locale/encoding_errors.hpp>
13#include <boost/locale/utf.hpp>
14#include <boost/locale/util/string.hpp>
15#include <iterator>
16#include <memory>
17#include <type_traits>
18
19#ifdef BOOST_MSVC
20# pragma warning(push)
21# pragma warning(disable : 4275 4251 4231 4660)
22#endif
23
24namespace boost { namespace locale { namespace conv {
28
32 template<typename CharOut, typename CharIn, class Alloc = std::allocator<CharOut>>
33 std::basic_string<CharOut, std::char_traits<CharOut>, Alloc>
34 utf_to_utf(const CharIn* begin, const CharIn* end, method_type how = default_method, const Alloc& alloc = Alloc())
35 {
36 std::basic_string<CharOut, std::char_traits<CharOut>, Alloc> result(alloc);
37 result.reserve(end - begin);
38 auto inserter = std::back_inserter(result);
39 while(begin != end) {
41 if(c == utf::illegal || c == utf::incomplete) {
42 if(how == stop)
43 throw conversion_error();
44 } else
46 }
47 return result;
48 }
49
52 template<typename CharOut, typename CharIn, class Alloc>
53 std::basic_string<CharOut, std::char_traits<CharOut>, Alloc>
54 utf_to_utf(const CharIn* begin, const CharIn* end, const Alloc& alloc)
55 {
56 return utf_to_utf<CharOut>(begin, end, skip, alloc);
57 }
58
62 template<typename CharOut, typename CharIn, class Alloc = std::allocator<CharOut>>
63 std::basic_string<CharOut, std::char_traits<CharOut>, Alloc>
64 utf_to_utf(const CharIn* str, method_type how = default_method, const Alloc& alloc = Alloc())
65 {
66 return utf_to_utf<CharOut>(str, util::str_end(str), how, alloc);
67 }
68
71 template<typename CharOut, typename CharIn, class Alloc>
72#ifndef BOOST_LOCALE_DOXYGEN
73 detail::enable_if_allocator_for<Alloc,
74 CharOut,
75#endif
76 std::basic_string<CharOut, std::char_traits<CharOut>, Alloc>
77#ifndef BOOST_LOCALE_DOXYGEN
78 >
79#endif
80 utf_to_utf(const CharIn* str, const Alloc& alloc)
81 {
82 return utf_to_utf<CharOut>(str, skip, alloc);
83 }
84
88 template<typename CharOut, typename CharIn, class Alloc>
89#ifndef BOOST_LOCALE_DOXYGEN
90 detail::enable_if_allocator_for<
91 Alloc,
92 CharIn,
93#endif
94 std::basic_string<CharOut, std::char_traits<CharOut>, detail::rebind_alloc<Alloc, CharOut>>
95#ifndef BOOST_LOCALE_DOXYGEN
96 >
97#endif
98 utf_to_utf(const std::basic_string<CharIn, std::char_traits<CharIn>, Alloc>& str, method_type how = default_method)
99 {
100 return utf_to_utf<CharOut>(str.c_str(),
101 str.c_str() + str.size(),
102 how,
103 detail::rebind_alloc<Alloc, CharOut>(str.get_allocator()));
104 }
105
109 template<typename CharOut, typename CharIn, class AllocOut, class AllocIn>
110#ifndef BOOST_LOCALE_DOXYGEN
111 detail::enable_if_allocator_for<AllocIn,
112 CharIn,
113#endif
114 std::basic_string<CharOut, std::char_traits<CharOut>, AllocOut>
115#ifndef BOOST_LOCALE_DOXYGEN
116 >
117#endif
118 utf_to_utf(const std::basic_string<CharIn, std::char_traits<CharIn>, AllocIn>& str,
120 const AllocOut& alloc = AllocOut())
121 {
122 return utf_to_utf<CharOut>(str.c_str(), str.c_str() + str.size(), how, alloc);
123 }
124
127 template<typename CharOut, typename CharIn, class AllocOut, class AllocIn>
128#ifndef BOOST_LOCALE_DOXYGEN
129 detail::enable_if_allocator_for2<AllocIn,
130 CharIn,
131 AllocOut,
132 CharOut,
133#endif
134 std::basic_string<CharOut, std::char_traits<CharOut>, AllocOut>
135#ifndef BOOST_LOCALE_DOXYGEN
136 >
137#endif
138 utf_to_utf(const std::basic_string<CharIn, std::char_traits<CharIn>, AllocIn>& str, const AllocOut& alloc)
139 {
140 return utf_to_utf<CharOut>(str, skip, alloc);
141 }
142
144
145}}} // namespace boost::locale::conv
146
147#ifdef BOOST_MSVC
148# pragma warning(pop)
149#endif
150
151#endif
The exception that is thrown in case of conversion error.
Definition encoding_errors.hpp:25
std::basic_string< CharOut, std::char_traits< CharOut >, Alloc > utf_to_utf(const CharIn *begin, const CharIn *end, method_type how=default_method, const Alloc &alloc=Alloc())
Definition encoding_utf.hpp:34
method_type
enum that defines conversion policy
Definition encoding_errors.hpp:41
@ stop
Stop conversion and throw conversion_error.
Definition encoding_errors.hpp:43
@ default_method
Default method - skip.
Definition encoding_errors.hpp:44
@ skip
Skip illegal/unconvertible characters.
Definition encoding_errors.hpp:42
constexpr code_point illegal
Special constant that defines illegal code point.
Definition utf.hpp:22
uint32_t code_point
The integral type that can hold a Unicode code point.
Definition utf.hpp:19
constexpr code_point incomplete
Special constant that defines incomplete code point.
Definition utf.hpp:24
Char * str_end(Char *str)
Return the end of a C-string, i.e. the pointer to the trailing NULL byte.
Definition string.hpp:16
static code_point decode(Iterator &p, Iterator e)
static Iterator encode(code_point value, Iterator out)