1 // Copyright 2006-2016 Nemanja Trifunovic
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
11 The copyright notices in the Software and this entire statement, including
12 the above license grant, this restriction and the following disclaimer,
13 must be included in all copies of the Software, in whole or in part, and
14 all derivative works of the Software, unless such copies or derivative
15 works are solely in the form of machine-executable object code generated by
16 a source language processor.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
28 #ifndef UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
29 #define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
36 // Base for the exceptions that may be thrown from the library
37 class exception : public ::std::exception {
40 // Exceptions that may be thrown from the library functions.
41 class invalid_code_point : public exception {
44 invalid_code_point(uint32_t codepoint) : cp(codepoint) {}
45 virtual const char* what() const throw() { return "Invalid code point"; }
46 uint32_t code_point() const {return cp;}
49 class invalid_utf8 : public exception {
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;}
57 class invalid_utf16 : public exception {
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;}
65 class not_enough_room : public exception {
67 virtual const char* what() const throw() { return "Not enough space"; }
70 /// The library API - functions intended to be called by the users
72 template <typename octet_iterator>
73 octet_iterator append(uint32_t cp, octet_iterator result)
75 if (!utf8::internal::is_code_point_valid(cp))
76 throw invalid_code_point(cp);
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);
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);
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 throw not_enough_room();
111 case internal::INVALID_LEAD:
112 out = utf8::append (replacement, out);
115 case internal::INCOMPLETE_SEQUENCE:
116 case internal::OVERLONG_SEQUENCE:
117 case internal::INVALID_CODE_POINT:
118 out = utf8::append (replacement, out);
120 // just one replacement mark for the sequence
121 while (start != end && utf8::internal::is_trail(*start))
129 template <typename octet_iterator, typename output_iterator>
130 inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
132 static const uint32_t replacement_marker = utf8::internal::mask16(0xfffd);
133 return utf8::replace_invalid(start, end, out, replacement_marker);
136 template <typename octet_iterator>
137 uint32_t next(octet_iterator& it, octet_iterator end)
140 internal::utf_error err_code = utf8::internal::validate_next(it, end, cp);
142 case internal::UTF8_OK :
144 case internal::NOT_ENOUGH_ROOM :
145 throw not_enough_room();
146 case internal::INVALID_LEAD :
147 case internal::INCOMPLETE_SEQUENCE :
148 case internal::OVERLONG_SEQUENCE :
149 throw invalid_utf8(*it);
150 case internal::INVALID_CODE_POINT :
151 throw invalid_code_point(cp);
156 template <typename octet_iterator>
157 uint32_t peek_next(octet_iterator it, octet_iterator end)
159 return utf8::next(it, end);
162 template <typename octet_iterator>
163 uint32_t prior(octet_iterator& it, octet_iterator start)
165 // can't do much if it == start
167 throw not_enough_room();
169 octet_iterator end = it;
170 // Go back until we hit either a lead octet or start
171 while (utf8::internal::is_trail(*(--it)))
173 throw invalid_utf8(*it); // error - no lead byte in the sequence
174 return utf8::peek_next(it, end);
177 /// Deprecated in versions that include "prior"
178 template <typename octet_iterator>
179 uint32_t previous(octet_iterator& it, octet_iterator pass_start)
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);
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)
202 utf8::next(first, last);
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) {
210 uint32_t cp = utf8::internal::mask16(*start++);
211 // Take care of surrogate pairs first
212 if (utf8::internal::is_lead_surrogate(cp)) {
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;
218 throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
221 throw invalid_utf16(static_cast<uint16_t>(cp));
224 // Lone trail surrogate
225 else if (utf8::internal::is_trail_surrogate(cp))
226 throw invalid_utf16(static_cast<uint16_t>(cp));
228 result = utf8::append(cp, result);
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) {
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);
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)
252 result = utf8::append(*(start++), result);
257 template <typename octet_iterator, typename u32bit_iterator>
258 u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
261 (*result++) = utf8::next(start, end);
266 // The iterator class
267 template <typename octet_iterator>
268 class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
270 octet_iterator range_start;
271 octet_iterator range_end;
274 explicit iterator (const octet_iterator& octet_it,
275 const octet_iterator& rangestart,
276 const octet_iterator& rangeend) :
277 it(octet_it), range_start(rangestart), range_end(rangeend)
279 if (it < range_start || it > range_end)
280 throw std::out_of_range("Invalid utf-8 iterator position");
282 // the default "big three" are OK
283 octet_iterator base () const { return it; }
284 uint32_t operator * () const
286 octet_iterator temp = it;
287 return utf8::next(temp, range_end);
289 bool operator == (const iterator& rhs) const
291 if (range_start != rhs.range_start || range_end != rhs.range_end)
292 throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
293 return (it == rhs.it);
295 bool operator != (const iterator& rhs) const
297 return !(operator == (rhs));
299 iterator& operator ++ ()
301 utf8::next(it, range_end);
304 iterator operator ++ (int)
306 iterator temp = *this;
307 utf8::next(it, range_end);
310 iterator& operator -- ()
312 utf8::prior(it, range_start);
315 iterator operator -- (int)
317 iterator temp = *this;
318 utf8::prior(it, range_start);
325 #endif //header guard