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 UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE {
return "Invalid code point"; }
46 uint32_t code_point()
const {
return cp;}
53 virtual const char* what()
const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE {
return "Invalid UTF-8"; }
54 uint8_t utf8_octet()
const {
return u8;}
61 virtual const char* what()
const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE {
return "Invalid UTF-16"; }
62 uint16_t utf16_word()
const {
return u16;}
67 virtual const char* what()
const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE {
return "Not enough space"; }
72 template <
typename octet_iterator>
73 octet_iterator append(uint32_t cp, octet_iterator result)
75 if (!utf8::internal::is_code_point_valid(cp))
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>
99 output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
101 while (start != end) {
102 octet_iterator sequence_start = start;
103 internal::utf_error err_code = utf8::internal::validate_next(start, end);
105 case internal::UTF8_OK :
106 for (octet_iterator it = sequence_start; it != start; ++it)
109 case internal::NOT_ENOUGH_ROOM:
110 out = utf8::append (replacement, out);
113 case internal::INVALID_LEAD:
114 out = utf8::append (replacement, out);
117 case internal::INCOMPLETE_SEQUENCE:
118 case internal::OVERLONG_SEQUENCE:
119 case internal::INVALID_CODE_POINT:
120 out = utf8::append (replacement, out);
123 while (start != end && utf8::internal::is_trail(*start))
131 template <
typename octet_iterator,
typename output_iterator>
132 inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
134 static const uint32_t replacement_marker = utf8::internal::mask16(0xfffd);
135 return utf8::replace_invalid(start, end, out, replacement_marker);
138 template <
typename octet_iterator>
139 uint32_t next(octet_iterator& it, octet_iterator end)
142 internal::utf_error err_code = utf8::internal::validate_next(it, end, cp);
144 case internal::UTF8_OK :
146 case internal::NOT_ENOUGH_ROOM :
148 case internal::INVALID_LEAD :
149 case internal::INCOMPLETE_SEQUENCE :
150 case internal::OVERLONG_SEQUENCE :
152 case internal::INVALID_CODE_POINT :
158 template <
typename octet_iterator>
159 uint32_t peek_next(octet_iterator it, octet_iterator end)
161 return utf8::next(it, end);
164 template <
typename octet_iterator>
165 uint32_t prior(octet_iterator& it, octet_iterator start)
171 octet_iterator end = it;
173 while (utf8::internal::is_trail(*(--it)))
176 return utf8::peek_next(it, end);
179 template <
typename octet_iterator,
typename distance_type>
180 void advance (octet_iterator& it, distance_type n, octet_iterator end)
182 const distance_type zero(0);
185 for (distance_type i = n; i < zero; ++i)
186 utf8::prior(it, end);
189 for (distance_type i = zero; i < n; ++i)
194 template <
typename octet_iterator>
195 typename std::iterator_traits<octet_iterator>::difference_type
196 distance (octet_iterator first, octet_iterator last)
198 typename std::iterator_traits<octet_iterator>::difference_type dist;
199 for (dist = 0; first < last; ++dist)
200 utf8::next(first, last);
204 template <
typename u16bit_iterator,
typename octet_iterator>
205 octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
207 while (start != end) {
208 uint32_t cp = utf8::internal::mask16(*start++);
210 if (utf8::internal::is_lead_surrogate(cp)) {
212 uint32_t trail_surrogate = utf8::internal::mask16(*start++);
213 if (utf8::internal::is_trail_surrogate(trail_surrogate))
214 cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
223 else if (utf8::internal::is_trail_surrogate(cp))
226 result = utf8::append(cp, result);
231 template <
typename u16bit_iterator,
typename octet_iterator>
232 u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
234 while (start < end) {
235 uint32_t cp = utf8::next(start, end);
237 *result++ =
static_cast<uint16_t
>((cp >> 10) + internal::LEAD_OFFSET);
238 *result++ =
static_cast<uint16_t
>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
241 *result++ =
static_cast<uint16_t
>(cp);
246 template <
typename octet_iterator,
typename u32bit_iterator>
247 octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
250 result = utf8::append(*(start++), result);
255 template <
typename octet_iterator,
typename u32bit_iterator>
256 u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
259 (*result++) = utf8::next(start, end);
265 template <
typename octet_iterator>
268 octet_iterator range_start;
269 octet_iterator range_end;
271 typedef uint32_t value_type;
272 typedef uint32_t* pointer;
273 typedef uint32_t& reference;
274 typedef std::ptrdiff_t difference_type;
275 typedef std::bidirectional_iterator_tag iterator_category;
277 explicit iterator (
const octet_iterator& octet_it,
278 const octet_iterator& rangestart,
279 const octet_iterator& rangeend) :
280 it(octet_it), range_start(rangestart), range_end(rangeend)
282 if (it < range_start || it > range_end)
283 throw std::out_of_range(
"Invalid utf-8 iterator position");
286 octet_iterator base ()
const {
return it; }
287 uint32_t operator * ()
const 289 octet_iterator temp = it;
290 return utf8::next(temp, range_end);
292 bool operator == (
const iterator& rhs)
const 294 if (range_start != rhs.range_start || range_end != rhs.range_end)
295 throw std::logic_error(
"Comparing utf-8 iterators defined with different ranges");
296 return (it == rhs.it);
298 bool operator != (
const iterator& rhs)
const 300 return !(operator == (rhs));
304 utf8::next(it, range_end);
310 utf8::next(it, range_end);
315 utf8::prior(it, range_start);
321 utf8::prior(it, range_start);
328 #if UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later 330 #endif // C++ 11 or later 332 #endif //header guard
Definition: checked.h:266