28#ifndef UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
29#define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
45 virtual const char*
what()
const throw() {
return "Invalid code point"; }
53 virtual const char*
what()
const throw() {
return "Invalid UTF-8"; }
61 virtual const char*
what()
const throw() {
return "Invalid UTF-16"; }
67 virtual const char*
what()
const throw() {
return "Not enough space"; }
72 template <
typename octet_iterator>
79 *(result++) =
static_cast<uint8_t>(cp);
80 else if (cp < 0x800) {
81 *(result++) =
static_cast<uint8_t>((cp >> 6) | 0xc0);
82 *(result++) =
static_cast<uint8_t>((cp & 0x3f) | 0x80);
84 else if (cp < 0x10000) {
85 *(result++) =
static_cast<uint8_t>((cp >> 12) | 0xe0);
86 *(result++) =
static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
87 *(result++) =
static_cast<uint8_t>((cp & 0x3f) | 0x80);
90 *(result++) =
static_cast<uint8_t>((cp >> 18) | 0xf0);
91 *(result++) =
static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
92 *(result++) =
static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
93 *(result++) =
static_cast<uint8_t>((cp & 0x3f) | 0x80);
98 template <
typename octet_iterator,
typename output_iterator>
101 while (start != end) {
102 octet_iterator sequence_start = start;
106 for (octet_iterator it = sequence_start; it != start; ++it)
129 template <
typename octet_iterator,
typename output_iterator>
130 inline output_iterator
replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
136 template <
typename octet_iterator>
156 template <
typename octet_iterator>
162 template <
typename octet_iterator>
169 octet_iterator end = it;
178 template <
typename octet_iterator>
181 octet_iterator end = it;
183 if (it == pass_start)
185 octet_iterator temp = it;
189 template <
typename octet_iterator,
typename distance_type>
190 void advance (octet_iterator& it, distance_type n, octet_iterator end)
192 for (distance_type i = 0; i < n; ++i)
196 template <
typename octet_iterator>
197 typename std::iterator_traits<octet_iterator>::difference_type
198 distance (octet_iterator first, octet_iterator last)
200 typename std::iterator_traits<octet_iterator>::difference_type dist;
201 for (dist = 0; first < last; ++dist)
206 template <
typename u16bit_iterator,
typename octet_iterator>
207 octet_iterator
utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
209 while (start != end) {
233 template <
typename u16bit_iterator,
typename octet_iterator>
234 u16bit_iterator
utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
236 while (start != end) {
243 *result++ =
static_cast<uint16_t>(cp);
248 template <
typename octet_iterator,
typename u32bit_iterator>
249 octet_iterator
utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
257 template <
typename octet_iterator,
typename u32bit_iterator>
258 u32bit_iterator
utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
267 template <
typename octet_iterator>
270 octet_iterator range_start;
271 octet_iterator range_end;
282 const octet_iterator& range_start,
283 const octet_iterator& range_end) :
284 it(octet_it), range_start(range_start), range_end(range_end)
286 if (it < range_start || it > range_end)
287 throw std::out_of_range(
"Invalid utf-8 iterator position");
290 octet_iterator
base ()
const {
return it; }
293 octet_iterator temp = it;
298 if (range_start != rhs.range_start || range_end != rhs.range_end)
299 throw std::logic_error(
"Comparing utf-8 iterators defined with different ranges");
300 return (it == rhs.it);
uint32_t code_point() const
invalid_code_point(uint32_t cp)
virtual const char * what() const
virtual const char * what() const
uint16_t utf16_word() const
invalid_utf16(uint16_t u)
uint8_t utf8_octet() const
virtual const char * what() const
uint32_t operator*() const
bool operator==(const iterator &rhs) const
octet_iterator base() const
std::ptrdiff_t difference_type
iterator(const octet_iterator &octet_it, const octet_iterator &range_start, const octet_iterator &range_end)
std::bidirectional_iterator_tag iterator_category
bool operator!=(const iterator &rhs) const
virtual const char * what() const
bool is_lead_surrogate(u16 cp)
bool is_trail_surrogate(u16 cp)
const uint32_t SURROGATE_OFFSET
const uint16_t LEAD_OFFSET
const uint16_t TRAIL_SURROGATE_MIN
bool is_code_point_valid(u32 cp)
uint16_t mask16(u16_type oc)
bool is_trail(octet_type oc)
utf_error validate_next(octet_iterator &it, octet_iterator end, uint32_t &code_point)
output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
octet_iterator append(uint32_t cp, octet_iterator result)
The library API - functions intended to be called by the users.
void advance(octet_iterator &it, distance_type n, octet_iterator end)
uint32_t peek_next(octet_iterator it, octet_iterator end)
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result)
uint32_t prior(octet_iterator &it, octet_iterator start)
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
uint32_t previous(octet_iterator &it, octet_iterator pass_start)
Deprecated in versions that include "prior".
uint32_t next(octet_iterator &it, octet_iterator end)