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>
268 class iterator :
public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
275 const octet_iterator& range_start,
276 const octet_iterator& range_end) :
277 it(octet_it), range_start(range_start), range_end(range_end)
279 if (it < range_start || it > range_end)
280 throw std::out_of_range(
"Invalid utf-8 iterator position");
283 octet_iterator
base ()
const {
return it; }
286 octet_iterator temp =
it;
292 throw std::logic_error(
"Comparing utf-8 iterators defined with different ranges");
293 return (it == rhs.
it);
325 #endif //header guard
uint16_t utf16_word() const
Definition: checked.h:62
utf_error
Definition: core.h:134
virtual const char * what() const
Definition: checked.h:67
virtual const char * what() const
Definition: checked.h:61
u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result)
Definition: checked.h:258
utf_error validate_next(octet_iterator &it, octet_iterator end, uint32_t &code_point)
Definition: core.h:223
uint32_t next(octet_iterator &it, octet_iterator end)
Definition: checked.h:137
const uint16_t TRAIL_SURROGATE_MIN
Definition: core.h:50
invalid_utf16(uint16_t u)
Definition: checked.h:60
uint32_t peek_next(octet_iterator it, octet_iterator end)
Definition: checked.h:157
octet_iterator base() const
Definition: checked.h:283
bool is_lead_surrogate(u16 cp)
Definition: core.h:75
bool is_trail_surrogate(u16 cp)
Definition: core.h:81
invalid_utf8(uint8_t u)
Definition: checked.h:52
invalid_code_point(uint32_t cp)
Definition: checked.h:44
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
Definition: checked.h:207
void advance(octet_iterator &it, distance_type n, octet_iterator end)
Definition: checked.h:190
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
Definition: checked.h:249
uint16_t u16
Definition: checked.h:58
uint8_t u8
Definition: checked.h:50
unsigned char uint8_t
Definition: core.h:38
bool is_code_point_valid(u32 cp)
Definition: core.h:93
Definition: checked.h:268
octet_iterator range_start
Definition: checked.h:270
const uint32_t SURROGATE_OFFSET
Definition: core.h:53
uint16_t mask16(u16_type oc)
Definition: core.h:64
uint32_t previous(octet_iterator &it, octet_iterator pass_start)
Deprecated in versions that include "prior".
Definition: checked.h:179
unsigned short uint16_t
Definition: core.h:39
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
Definition: checked.h:198
uint32_t prior(octet_iterator &it, octet_iterator start)
Definition: checked.h:163
iterator & operator--()
Definition: checked.h:310
iterator & operator++()
Definition: checked.h:299
uint8_t utf8_octet() const
Definition: checked.h:54
uint32_t operator*() const
Definition: checked.h:284
virtual const char * what() const
Definition: checked.h:53
octet_iterator it
Definition: checked.h:269
iterator(const octet_iterator &octet_it, const octet_iterator &range_start, const octet_iterator &range_end)
Definition: checked.h:274
octet_iterator range_end
Definition: checked.h:271
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
Definition: checked.h:234
iterator()
Definition: checked.h:273
uint32_t cp
Definition: checked.h:42
output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
Definition: checked.h:99
unsigned int uint32_t
Definition: core.h:40
bool is_trail(octet_type oc)
Definition: core.h:69
bool operator==(const iterator &rhs) const
Definition: checked.h:289
const uint16_t LEAD_OFFSET
Definition: core.h:52
uint32_t code_point() const
Definition: checked.h:46
bool operator!=(const iterator &rhs) const
Definition: checked.h:295
octet_iterator append(uint32_t cp, octet_iterator result)
The library API - functions intended to be called by the users.
Definition: checked.h:73
virtual const char * what() const
Definition: checked.h:45