2 // Copyright (c) 2015 Artyom Beilis (Tonkikh)
3 // Copyright (c) 2020 Alexander Grund
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_NOWIDE_UTF8_CODECVT_HPP_INCLUDED
10 #define BOOST_NOWIDE_UTF8_CODECVT_HPP_INCLUDED
12 #include <boost/nowide/replacement.hpp>
13 #include <boost/nowide/utf/utf.hpp>
20 static_assert(sizeof(std::mbstate_t) >= 2, "mbstate_t is to small to store an UTF-16 codepoint");
22 // Avoid including cstring for std::memcpy
23 inline void copy_uint16_t(void* dst, const void* src)
25 unsigned char* cdst = static_cast<unsigned char*>(dst);
26 const unsigned char* csrc = static_cast<const unsigned char*>(src);
30 inline std::uint16_t read_state(const std::mbstate_t& src)
33 copy_uint16_t(&dst, &src);
36 inline void write_state(std::mbstate_t& dst, const std::uint16_t src)
38 copy_uint16_t(&dst, &src);
42 #if defined _MSC_VER && _MSC_VER < 1700
43 // MSVC do_length is non-standard it counts wide characters instead of narrow and does not change mbstate
44 #define BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
47 /// std::codecvt implementation that converts between UTF-8 and UTF-16 or UTF-32
49 /// @tparam CharSize Determines the encoding: 2 for UTF-16, 4 for UTF-32
51 /// Invalid sequences are replaced by #BOOST_NOWIDE_REPLACEMENT_CHARACTER
52 /// A trailing incomplete sequence will result in a return value of std::codecvt::partial
53 template<typename CharType, int CharSize = sizeof(CharType)>
56 /// Specialization for the UTF-8 <-> UTF-16 variant of the std::codecvt implementation
57 template<typename CharType>
58 class BOOST_SYMBOL_VISIBLE utf8_codecvt<CharType, 2> : public std::codecvt<CharType, char, std::mbstate_t>
61 static_assert(sizeof(CharType) >= 2, "CharType must be able to store UTF16 code point");
63 utf8_codecvt(size_t refs = 0) : std::codecvt<CharType, char, std::mbstate_t>(refs)
67 using uchar = CharType;
69 std::codecvt_base::result do_unshift(std::mbstate_t& s, char* from, char* /*to*/, char*& next) const override
71 if(detail::read_state(s) != 0)
72 return std::codecvt_base::error;
74 return std::codecvt_base::ok;
76 int do_encoding() const noexcept override
80 int do_max_length() const noexcept override
84 bool do_always_noconv() const noexcept override
89 int do_length(std::mbstate_t
90 #ifdef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
96 size_t max) const override
98 std::uint16_t state = detail::read_state(std_state);
99 #ifndef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
100 const char* save_from = from;
102 size_t save_max = max;
104 while(max > 0 && from < from_end)
106 const char* prev_from = from;
107 std::uint32_t ch = utf::utf_traits<char>::decode(from, from_end);
108 if(ch == utf::illegal)
110 ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
111 } else if(ch == utf::incomplete)
129 #ifndef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
130 detail::write_state(std_state, state);
131 return static_cast<int>(from - save_from);
133 return static_cast<int>(save_max - max);
137 std::codecvt_base::result do_in(std::mbstate_t& std_state,
139 const char* from_end,
140 const char*& from_next,
143 uchar*& to_next) const override
145 std::codecvt_base::result r = std::codecvt_base::ok;
147 // mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
148 // according to standard. We use it to keep a flag 0/1 for surrogate pair writing
150 // if 0 no code above >0xFFFF observed, of 1 a code above 0xFFFF observed
151 // and first pair is written, but no input consumed
152 std::uint16_t state = detail::read_state(std_state);
153 while(to < to_end && from < from_end)
155 const char* from_saved = from;
157 uint32_t ch = utf::utf_traits<char>::decode(from, from_end);
159 if(ch == utf::illegal)
161 ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
162 } else if(ch == utf::incomplete)
165 r = std::codecvt_base::partial;
168 // Normal codepoints go directly to stream
171 *to++ = static_cast<CharType>(ch);
174 // for other codepoints we do following
176 // 1. We can't consume our input as we may find ourself
177 // in state where all input consumed but not all output written,i.e. only
178 // 1st pair is written
179 // 2. We only write first pair and mark this in the state, we also revert back
180 // the from pointer in order to make sure this codepoint would be read
181 // once again and then we would consume our input together with writing
182 // second surrogate pair
184 std::uint16_t vh = static_cast<std::uint16_t>(ch >> 10);
185 std::uint16_t vl = ch & 0x3FF;
186 std::uint16_t w1 = vh + 0xD800;
187 std::uint16_t w2 = vl + 0xDC00;
191 *to++ = static_cast<CharType>(w1);
195 *to++ = static_cast<CharType>(w2);
202 if(r == std::codecvt_base::ok && (from != from_end || state != 0))
203 r = std::codecvt_base::partial;
204 detail::write_state(std_state, state);
208 std::codecvt_base::result do_out(std::mbstate_t& std_state,
210 const uchar* from_end,
211 const uchar*& from_next,
214 char*& to_next) const override
216 std::codecvt_base::result r = std::codecvt_base::ok;
217 // mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
218 // according to standard. We assume that sizeof(mbstate_t) >=2 in order
219 // to be able to store first observed surrogate pair
221 // State: state!=0 - a first surrogate pair was observed (state = first pair),
222 // we expect the second one to come and then zero the state
224 std::uint16_t state = detail::read_state(std_state);
225 while(to < to_end && from < from_end)
227 std::uint32_t ch = 0;
230 // if the state indicates that 1st surrogate pair was written
231 // we should make sure that the second one that comes is actually
233 std::uint16_t w1 = state;
234 std::uint16_t w2 = *from;
235 // we don't forward from as writing may fail to incomplete or
236 // partial conversion
237 if(0xDC00 <= w2 && w2 <= 0xDFFF)
239 std::uint16_t vh = w1 - 0xD800;
240 std::uint16_t vl = w2 - 0xDC00;
241 ch = ((uint32_t(vh) << 10) | vl) + 0x10000;
244 ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
249 if(0xD800 <= ch && ch <= 0xDBFF)
251 // if this is a first surrogate pair we put
252 // it into the state and consume it, note we don't
253 // go forward as it should be illegal so we increase
254 // the from pointer manually
255 state = static_cast<std::uint16_t>(ch);
258 } else if(0xDC00 <= ch && ch <= 0xDFFF)
260 // if we observe second surrogate pair and
261 // first only may be expected we should break from the loop with error
262 // as it is illegal input
263 ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
266 if(!utf::is_valid_codepoint(ch))
268 r = std::codecvt_base::error;
271 int len = utf::utf_traits<char>::width(ch);
272 if(to_end - to < len)
274 r = std::codecvt_base::partial;
277 to = utf::utf_traits<char>::encode(ch, to);
283 if(r == std::codecvt_base::ok && (from != from_end || state != 0))
284 r = std::codecvt_base::partial;
285 detail::write_state(std_state, state);
290 /// Specialization for the UTF-8 <-> UTF-32 variant of the std::codecvt implementation
291 template<typename CharType>
292 class BOOST_SYMBOL_VISIBLE utf8_codecvt<CharType, 4> : public std::codecvt<CharType, char, std::mbstate_t>
295 utf8_codecvt(size_t refs = 0) : std::codecvt<CharType, char, std::mbstate_t>(refs)
299 using uchar = CharType;
301 std::codecvt_base::result
302 do_unshift(std::mbstate_t& /*s*/, char* from, char* /*to*/, char*& next) const override
305 return std::codecvt_base::ok;
307 int do_encoding() const noexcept override
311 int do_max_length() const noexcept override
315 bool do_always_noconv() const noexcept override
320 int do_length(std::mbstate_t
321 #ifdef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
326 const char* from_end,
327 size_t max) const override
329 #ifndef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
330 const char* start_from = from;
332 size_t save_max = max;
335 while(max > 0 && from < from_end)
337 const char* save_from = from;
338 std::uint32_t ch = utf::utf_traits<char>::decode(from, from_end);
339 if(ch == utf::incomplete)
343 } else if(ch == utf::illegal)
345 ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
349 #ifndef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
350 return from - start_from;
352 return save_max - max;
356 std::codecvt_base::result do_in(std::mbstate_t& /*state*/,
358 const char* from_end,
359 const char*& from_next,
362 uchar*& to_next) const override
364 std::codecvt_base::result r = std::codecvt_base::ok;
366 while(to < to_end && from < from_end)
368 const char* from_saved = from;
370 uint32_t ch = utf::utf_traits<char>::decode(from, from_end);
372 if(ch == utf::illegal)
374 ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
375 } else if(ch == utf::incomplete)
377 r = std::codecvt_base::partial;
385 if(r == std::codecvt_base::ok && from != from_end)
386 r = std::codecvt_base::partial;
390 std::codecvt_base::result do_out(std::mbstate_t& /*std_state*/,
392 const uchar* from_end,
393 const uchar*& from_next,
396 char*& to_next) const override
398 std::codecvt_base::result r = std::codecvt_base::ok;
399 while(to < to_end && from < from_end)
401 std::uint32_t ch = 0;
403 if(!utf::is_valid_codepoint(ch))
405 ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
407 int len = utf::utf_traits<char>::width(ch);
408 if(to_end - to < len)
410 r = std::codecvt_base::partial;
413 to = utf::utf_traits<char>::encode(ch, to);
418 if(r == std::codecvt_base::ok && from != from_end)
419 r = std::codecvt_base::partial;
424 } // namespace nowide