Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
checked.h
Go to the documentation of this file.
1// Copyright 2006 Nemanja Trifunovic
2
3/*
4Permission is hereby granted, free of charge, to any person or organization
5obtaining a copy of the software and accompanying documentation covered by
6this license (the "Software") to use, reproduce, display, distribute,
7execute, and transmit the Software, and to prepare derivative works of the
8Software, and to permit third-parties to whom the Software is furnished to
9do so, all subject to the following:
10
11The copyright notices in the Software and this entire statement, including
12the above license grant, this restriction and the following disclaimer,
13must be included in all copies of the Software, in whole or in part, and
14all derivative works of the Software, unless such copies or derivative
15works are solely in the form of machine-executable object code generated by
16a source language processor.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24DEALINGS IN THE SOFTWARE.
25*/
26
27
28#ifndef UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
29#define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
30
31#include "core.h"
32#include <stdexcept>
33
34namespace utf8
35{
36 // Base for the exceptions that may be thrown from the library
37 class exception : public ::std::exception {
38 };
39
40 // Exceptions that may be thrown from the library functions.
42 uint32_t cp;
43 public:
45 virtual const char* what() const throw() { return "Invalid code point"; }
46 uint32_t code_point() const {return cp;}
47 };
48
49 class invalid_utf8 : public exception {
50 uint8_t u8;
51 public:
52 invalid_utf8 (uint8_t u) : u8(u) {}
53 virtual const char* what() const throw() { return "Invalid UTF-8"; }
54 uint8_t utf8_octet() const {return u8;}
55 };
56
57 class invalid_utf16 : public exception {
58 uint16_t u16;
59 public:
60 invalid_utf16 (uint16_t u) : u16(u) {}
61 virtual const char* what() const throw() { return "Invalid UTF-16"; }
62 uint16_t utf16_word() const {return u16;}
63 };
64
65 class not_enough_room : public exception {
66 public:
67 virtual const char* what() const throw() { return "Not enough space"; }
68 };
69
71
72 template <typename octet_iterator>
73 octet_iterator append(uint32_t cp, octet_iterator result)
74 {
76 throw invalid_code_point(cp);
77
78 if (cp < 0x80) // one octet
79 *(result++) = static_cast<uint8_t>(cp);
80 else if (cp < 0x800) { // two octets
81 *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
82 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
83 }
84 else if (cp < 0x10000) { // three octets
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);
88 }
89 else { // four octets
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);
94 }
95 return result;
96 }
97
98 template <typename octet_iterator, typename output_iterator>
99 output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
100 {
101 while (start != end) {
102 octet_iterator sequence_start = start;
104 switch (err_code) {
105 case internal::UTF8_OK :
106 for (octet_iterator it = sequence_start; it != start; ++it)
107 *out++ = *it;
108 break;
110 throw not_enough_room();
112 out = utf8::append (replacement, out);
113 ++start;
114 break;
118 out = utf8::append (replacement, out);
119 ++start;
120 // just one replacement mark for the sequence
121 while (start != end && utf8::internal::is_trail(*start))
122 ++start;
123 break;
124 }
125 }
126 return out;
127 }
128
129 template <typename octet_iterator, typename output_iterator>
130 inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
131 {
132 static const uint32_t replacement_marker = utf8::internal::mask16(0xfffd);
133 return utf8::replace_invalid(start, end, out, replacement_marker);
134 }
135
136 template <typename octet_iterator>
137 uint32_t next(octet_iterator& it, octet_iterator end)
138 {
139 uint32_t cp = 0;
141 switch (err_code) {
142 case internal::UTF8_OK :
143 break;
145 throw not_enough_room();
149 throw invalid_utf8(*it);
151 throw invalid_code_point(cp);
152 }
153 return cp;
154 }
155
156 template <typename octet_iterator>
157 uint32_t peek_next(octet_iterator it, octet_iterator end)
158 {
159 return utf8::next(it, end);
160 }
161
162 template <typename octet_iterator>
163 uint32_t prior(octet_iterator& it, octet_iterator start)
164 {
165 // can't do much if it == start
166 if (it == start)
167 throw not_enough_room();
168
169 octet_iterator end = it;
170 // Go back until we hit either a lead octet or start
171 while (utf8::internal::is_trail(*(--it)))
172 if (it == start)
173 throw invalid_utf8(*it); // error - no lead byte in the sequence
174 return utf8::peek_next(it, end);
175 }
176
178 template <typename octet_iterator>
179 uint32_t previous(octet_iterator& it, octet_iterator pass_start)
180 {
181 octet_iterator end = it;
182 while (utf8::internal::is_trail(*(--it)))
183 if (it == pass_start)
184 throw invalid_utf8(*it); // error - no lead byte in the sequence
185 octet_iterator temp = it;
186 return utf8::next(temp, end);
187 }
188
189 template <typename octet_iterator, typename distance_type>
190 void advance (octet_iterator& it, distance_type n, octet_iterator end)
191 {
192 for (distance_type i = 0; i < n; ++i)
193 utf8::next(it, end);
194 }
195
196 template <typename octet_iterator>
197 typename std::iterator_traits<octet_iterator>::difference_type
198 distance (octet_iterator first, octet_iterator last)
199 {
200 typename std::iterator_traits<octet_iterator>::difference_type dist;
201 for (dist = 0; first < last; ++dist)
202 utf8::next(first, last);
203 return dist;
204 }
205
206 template <typename u16bit_iterator, typename octet_iterator>
207 octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
208 {
209 while (start != end) {
210 uint32_t cp = utf8::internal::mask16(*start++);
211 // Take care of surrogate pairs first
213 if (start != end) {
214 uint32_t trail_surrogate = utf8::internal::mask16(*start++);
215 if (utf8::internal::is_trail_surrogate(trail_surrogate))
216 cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
217 else
218 throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
219 }
220 else
221 throw invalid_utf16(static_cast<uint16_t>(cp));
222
223 }
224 // Lone trail surrogate
226 throw invalid_utf16(static_cast<uint16_t>(cp));
227
228 result = utf8::append(cp, result);
229 }
230 return result;
231 }
232
233 template <typename u16bit_iterator, typename octet_iterator>
234 u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
235 {
236 while (start != end) {
237 uint32_t cp = utf8::next(start, end);
238 if (cp > 0xffff) { //make a surrogate pair
239 *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
240 *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
241 }
242 else
243 *result++ = static_cast<uint16_t>(cp);
244 }
245 return result;
246 }
247
248 template <typename octet_iterator, typename u32bit_iterator>
249 octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
250 {
251 while (start != end)
252 result = utf8::append(*(start++), result);
253
254 return result;
255 }
256
257 template <typename octet_iterator, typename u32bit_iterator>
258 u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
259 {
260 while (start != end)
261 (*result++) = utf8::next(start, end);
262
263 return result;
264 }
265
266 // The iterator class
267 template <typename octet_iterator>
268 class iterator {
269 octet_iterator it;
270 octet_iterator range_start;
271 octet_iterator range_end;
272 public:
273
274 using iterator_category = std::bidirectional_iterator_tag;
276 using difference_type = std::ptrdiff_t;
279
281 explicit iterator (const octet_iterator& octet_it,
282 const octet_iterator& range_start,
283 const octet_iterator& range_end) :
284 it(octet_it), range_start(range_start), range_end(range_end)
285 {
286 if (it < range_start || it > range_end)
287 throw std::out_of_range("Invalid utf-8 iterator position");
288 }
289 // the default "big three" are OK
290 octet_iterator base () const { return it; }
292 {
293 octet_iterator temp = it;
294 return utf8::next(temp, range_end);
295 }
296 bool operator == (const iterator& rhs) const
297 {
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);
301 }
302 bool operator != (const iterator& rhs) const
303 {
304 return !(operator == (rhs));
305 }
307 {
308 utf8::next(it, range_end);
309 return *this;
310 }
312 {
313 iterator temp = *this;
314 utf8::next(it, range_end);
315 return temp;
316 }
318 {
319 utf8::prior(it, range_start);
320 return *this;
321 }
323 {
324 iterator temp = *this;
325 utf8::prior(it, range_start);
326 return temp;
327 }
328 }; // class iterator
329
330} // namespace utf8
331
332#endif //header guard
333
334
uint32_t code_point() const
Definition checked.h:46
invalid_code_point(uint32_t cp)
Definition checked.h:44
virtual const char * what() const
Definition checked.h:45
virtual const char * what() const
Definition checked.h:61
uint16_t utf16_word() const
Definition checked.h:62
invalid_utf16(uint16_t u)
Definition checked.h:60
invalid_utf8(uint8_t u)
Definition checked.h:52
uint8_t utf8_octet() const
Definition checked.h:54
virtual const char * what() const
Definition checked.h:53
uint32_t operator*() const
Definition checked.h:291
bool operator==(const iterator &rhs) const
Definition checked.h:296
octet_iterator base() const
Definition checked.h:290
iterator & operator--()
Definition checked.h:317
std::ptrdiff_t difference_type
Definition checked.h:276
uint32_t * pointer
Definition checked.h:277
iterator(const octet_iterator &octet_it, const octet_iterator &range_start, const octet_iterator &range_end)
Definition checked.h:281
uint32_t value_type
Definition checked.h:275
iterator & operator++()
Definition checked.h:306
std::bidirectional_iterator_tag iterator_category
Definition checked.h:274
bool operator!=(const iterator &rhs) const
Definition checked.h:302
uint32_t & reference
Definition checked.h:278
virtual const char * what() const
Definition checked.h:67
bool is_lead_surrogate(u16 cp)
Definition core.h:75
bool is_trail_surrogate(u16 cp)
Definition core.h:81
const uint32_t SURROGATE_OFFSET
Definition core.h:53
const uint16_t LEAD_OFFSET
Definition core.h:52
const uint16_t TRAIL_SURROGATE_MIN
Definition core.h:50
bool is_code_point_valid(u32 cp)
Definition core.h:93
@ INCOMPLETE_SEQUENCE
Definition core.h:134
@ OVERLONG_SEQUENCE
Definition core.h:134
@ INVALID_CODE_POINT
Definition core.h:134
@ NOT_ENOUGH_ROOM
Definition core.h:134
uint16_t mask16(u16_type oc)
Definition core.h:64
bool is_trail(octet_type oc)
Definition core.h:69
utf_error validate_next(octet_iterator &it, octet_iterator end, uint32_t &code_point)
Definition core.h:223
Definition checked.h:35
output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
Definition checked.h:99
octet_iterator append(uint32_t cp, octet_iterator result)
The library API - functions intended to be called by the users.
Definition checked.h:73
void advance(octet_iterator &it, distance_type n, octet_iterator end)
Definition checked.h:190
uint32_t peek_next(octet_iterator it, octet_iterator end)
Definition checked.h:157
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
Definition checked.h:249
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
Definition checked.h:234
u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result)
Definition checked.h:258
uint32_t prior(octet_iterator &it, octet_iterator start)
Definition checked.h:163
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
Definition checked.h:198
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
Definition checked.h:207
uint32_t previous(octet_iterator &it, octet_iterator pass_start)
Deprecated in versions that include "prior".
Definition checked.h:179
uint32_t next(octet_iterator &it, octet_iterator end)
Definition checked.h:137
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124