//
// Copyright (c) 2015 Artyom Beilis (Tonkikh)
+// Copyright (c) 2020 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0. (See
-// accompanying file LICENSE_1_0.txt or copy at
+// accompanying file LICENSE or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_NOWIDE_UTF8_CODECVT_HPP_INCLUDED
#define BOOST_NOWIDE_UTF8_CODECVT_HPP_INCLUDED
-#include <boost/nowide/detail/utf.hpp>
#include <boost/nowide/replacement.hpp>
-#include <boost/cstdint.hpp>
-#include <boost/static_assert.hpp>
+#include <boost/nowide/utf/utf.hpp>
+#include <cstdint>
#include <locale>
namespace boost {
namespace nowide {
- // Make sure that mbstate can keep 16 bit of UTF-16 sequence
- BOOST_STATIC_ASSERT(sizeof(std::mbstate_t) >= 2);
+ static_assert(sizeof(std::mbstate_t) >= 2, "mbstate_t is to small to store an UTF-16 codepoint");
namespace detail {
// Avoid including cstring for std::memcpy
inline void copy_uint16_t(void* dst, const void* src)
cdst[0] = csrc[0];
cdst[1] = csrc[1];
}
- inline boost::uint16_t read_state(const std::mbstate_t& src)
+ inline std::uint16_t read_state(const std::mbstate_t& src)
{
- boost::uint16_t dst;
+ std::uint16_t dst;
copy_uint16_t(&dst, &src);
return dst;
}
- inline void write_state(std::mbstate_t& dst, const boost::uint16_t src)
+ inline void write_state(std::mbstate_t& dst, const std::uint16_t src)
{
copy_uint16_t(&dst, &src);
}
class BOOST_SYMBOL_VISIBLE utf8_codecvt<CharType, 2> : public std::codecvt<CharType, char, std::mbstate_t>
{
public:
- BOOST_STATIC_ASSERT_MSG(sizeof(CharType) >= 2, "CharType must be able to store UTF16 code point");
+ static_assert(sizeof(CharType) >= 2, "CharType must be able to store UTF16 code point");
utf8_codecvt(size_t refs = 0) : std::codecvt<CharType, char, std::mbstate_t>(refs)
{}
protected:
- typedef CharType uchar;
+ using uchar = CharType;
- virtual std::codecvt_base::result do_unshift(std::mbstate_t& s, char* from, char* /*to*/, char*& next) const
+ std::codecvt_base::result do_unshift(std::mbstate_t& s, char* from, char* /*to*/, char*& next) const override
{
if(detail::read_state(s) != 0)
return std::codecvt_base::error;
next = from;
return std::codecvt_base::ok;
}
- virtual int do_encoding() const throw()
+ int do_encoding() const noexcept override
{
return 0;
}
- virtual int do_max_length() const throw()
+ int do_max_length() const noexcept override
{
return 4;
}
- virtual bool do_always_noconv() const throw()
+ bool do_always_noconv() const noexcept override
{
return false;
}
- virtual int do_length(std::mbstate_t
+ int do_length(std::mbstate_t
#ifdef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
- const
+ const
#endif
- & std_state,
- const char* from,
- const char* from_end,
- size_t max) const
+ & std_state,
+ const char* from,
+ const char* from_end,
+ size_t max) const override
{
- boost::uint16_t state = detail::read_state(std_state);
+ std::uint16_t state = detail::read_state(std_state);
#ifndef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
const char* save_from = from;
#else
while(max > 0 && from < from_end)
{
const char* prev_from = from;
- boost::uint32_t ch = detail::utf::utf_traits<char>::decode(from, from_end);
- if(ch == detail::utf::illegal)
+ std::uint32_t ch = utf::utf_traits<char>::decode(from, from_end);
+ if(ch == utf::illegal)
{
ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
- } else if(ch == detail::utf::incomplete)
+ } else if(ch == utf::incomplete)
{
from = prev_from;
break;
#endif
}
- virtual std::codecvt_base::result do_in(std::mbstate_t& std_state,
- const char* from,
- const char* from_end,
- const char*& from_next,
- uchar* to,
- uchar* to_end,
- uchar*& to_next) const
+ std::codecvt_base::result do_in(std::mbstate_t& std_state,
+ const char* from,
+ const char* from_end,
+ const char*& from_next,
+ uchar* to,
+ uchar* to_end,
+ uchar*& to_next) const override
{
std::codecvt_base::result r = std::codecvt_base::ok;
//
// if 0 no code above >0xFFFF observed, of 1 a code above 0xFFFF observed
// and first pair is written, but no input consumed
- boost::uint16_t state = detail::read_state(std_state);
+ std::uint16_t state = detail::read_state(std_state);
while(to < to_end && from < from_end)
{
const char* from_saved = from;
- uint32_t ch = detail::utf::utf_traits<char>::decode(from, from_end);
+ uint32_t ch = utf::utf_traits<char>::decode(from, from_end);
- if(ch == detail::utf::illegal)
+ if(ch == utf::illegal)
{
ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
- } else if(ch == detail::utf::incomplete)
+ } else if(ch == utf::incomplete)
{
from = from_saved;
r = std::codecvt_base::partial;
// once again and then we would consume our input together with writing
// second surrogate pair
ch -= 0x10000;
- boost::uint16_t vh = static_cast<boost::uint16_t>(ch >> 10);
- boost::uint16_t vl = ch & 0x3FF;
- boost::uint16_t w1 = vh + 0xD800;
- boost::uint16_t w2 = vl + 0xDC00;
+ std::uint16_t vh = static_cast<std::uint16_t>(ch >> 10);
+ std::uint16_t vl = ch & 0x3FF;
+ std::uint16_t w1 = vh + 0xD800;
+ std::uint16_t w2 = vl + 0xDC00;
if(state == 0)
{
from = from_saved;
return r;
}
- virtual std::codecvt_base::result do_out(std::mbstate_t& std_state,
- const uchar* from,
- const uchar* from_end,
- const uchar*& from_next,
- char* to,
- char* to_end,
- char*& to_next) const
+ std::codecvt_base::result do_out(std::mbstate_t& std_state,
+ const uchar* from,
+ const uchar* from_end,
+ const uchar*& from_next,
+ char* to,
+ char* to_end,
+ char*& to_next) const override
{
std::codecvt_base::result r = std::codecvt_base::ok;
// mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
// State: state!=0 - a first surrogate pair was observed (state = first pair),
// we expect the second one to come and then zero the state
///
- boost::uint16_t state = detail::read_state(std_state);
+ std::uint16_t state = detail::read_state(std_state);
while(to < to_end && from < from_end)
{
- boost::uint32_t ch = 0;
+ std::uint32_t ch = 0;
if(state != 0)
{
// if the state indicates that 1st surrogate pair was written
// we should make sure that the second one that comes is actually
// second surrogate
- boost::uint16_t w1 = state;
- boost::uint16_t w2 = *from;
+ std::uint16_t w1 = state;
+ std::uint16_t w2 = *from;
// we don't forward from as writing may fail to incomplete or
// partial conversion
if(0xDC00 <= w2 && w2 <= 0xDFFF)
{
- boost::uint16_t vh = w1 - 0xD800;
- boost::uint16_t vl = w2 - 0xDC00;
+ std::uint16_t vh = w1 - 0xD800;
+ std::uint16_t vl = w2 - 0xDC00;
ch = ((uint32_t(vh) << 10) | vl) + 0x10000;
} else
{
// it into the state and consume it, note we don't
// go forward as it should be illegal so we increase
// the from pointer manually
- state = static_cast<boost::uint16_t>(ch);
+ state = static_cast<std::uint16_t>(ch);
from++;
continue;
} else if(0xDC00 <= ch && ch <= 0xDFFF)
ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
}
}
- if(!detail::utf::is_valid_codepoint(ch))
+ if(!utf::is_valid_codepoint(ch))
{
r = std::codecvt_base::error;
break;
}
- int len = detail::utf::utf_traits<char>::width(ch);
+ int len = utf::utf_traits<char>::width(ch);
if(to_end - to < len)
{
r = std::codecvt_base::partial;
break;
}
- to = detail::utf::utf_traits<char>::encode(ch, to);
+ to = utf::utf_traits<char>::encode(ch, to);
state = 0;
from++;
}
{}
protected:
- typedef CharType uchar;
+ using uchar = CharType;
- virtual std::codecvt_base::result do_unshift(std::mbstate_t& /*s*/, char* from, char* /*to*/, char*& next) const
+ std::codecvt_base::result
+ do_unshift(std::mbstate_t& /*s*/, char* from, char* /*to*/, char*& next) const override
{
next = from;
return std::codecvt_base::ok;
}
- virtual int do_encoding() const throw()
+ int do_encoding() const noexcept override
{
return 0;
}
- virtual int do_max_length() const throw()
+ int do_max_length() const noexcept override
{
return 4;
}
- virtual bool do_always_noconv() const throw()
+ bool do_always_noconv() const noexcept override
{
return false;
}
- virtual int do_length(std::mbstate_t
+ int do_length(std::mbstate_t
#ifdef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
- const
+ const
#endif
- & /*state*/,
- const char* from,
- const char* from_end,
- size_t max) const
+ & /*state*/,
+ const char* from,
+ const char* from_end,
+ size_t max) const override
{
#ifndef BOOST_NOWIDE_DO_LENGTH_MBSTATE_CONST
const char* start_from = from;
while(max > 0 && from < from_end)
{
const char* save_from = from;
- boost::uint32_t ch = detail::utf::utf_traits<char>::decode(from, from_end);
- if(ch == detail::utf::incomplete)
+ std::uint32_t ch = utf::utf_traits<char>::decode(from, from_end);
+ if(ch == utf::incomplete)
{
from = save_from;
break;
- } else if(ch == detail::utf::illegal)
+ } else if(ch == utf::illegal)
{
ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
}
#endif
}
- virtual std::codecvt_base::result do_in(std::mbstate_t& /*state*/,
- const char* from,
- const char* from_end,
- const char*& from_next,
- uchar* to,
- uchar* to_end,
- uchar*& to_next) const
+ std::codecvt_base::result do_in(std::mbstate_t& /*state*/,
+ const char* from,
+ const char* from_end,
+ const char*& from_next,
+ uchar* to,
+ uchar* to_end,
+ uchar*& to_next) const override
{
std::codecvt_base::result r = std::codecvt_base::ok;
{
const char* from_saved = from;
- uint32_t ch = detail::utf::utf_traits<char>::decode(from, from_end);
+ uint32_t ch = utf::utf_traits<char>::decode(from, from_end);
- if(ch == detail::utf::illegal)
+ if(ch == utf::illegal)
{
ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
- } else if(ch == detail::utf::incomplete)
+ } else if(ch == utf::incomplete)
{
r = std::codecvt_base::partial;
from = from_saved;
return r;
}
- virtual std::codecvt_base::result do_out(std::mbstate_t& /*std_state*/,
- const uchar* from,
- const uchar* from_end,
- const uchar*& from_next,
- char* to,
- char* to_end,
- char*& to_next) const
+ std::codecvt_base::result do_out(std::mbstate_t& /*std_state*/,
+ const uchar* from,
+ const uchar* from_end,
+ const uchar*& from_next,
+ char* to,
+ char* to_end,
+ char*& to_next) const override
{
std::codecvt_base::result r = std::codecvt_base::ok;
while(to < to_end && from < from_end)
{
- boost::uint32_t ch = 0;
+ std::uint32_t ch = 0;
ch = *from;
- if(!detail::utf::is_valid_codepoint(ch))
+ if(!utf::is_valid_codepoint(ch))
{
ch = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
}
- int len = detail::utf::utf_traits<char>::width(ch);
+ int len = utf::utf_traits<char>::width(ch);
if(to_end - to < len)
{
r = std::codecvt_base::partial;
break;
}
- to = detail::utf::utf_traits<char>::encode(ch, to);
+ to = utf::utf_traits<char>::encode(ch, to);
from++;
}
from_next = from;