1 // Copyright 2006 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 cp) : cp(cp) {}
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, typename output_iterator>
73 output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
75 while (start != end) {
76 octet_iterator sequence_start = start;
77 internal::utf_error err_code = internal::validate_next(start, end);
79 case internal::UTF8_OK :
80 for (octet_iterator it = sequence_start; it != start; ++it)
83 case internal::NOT_ENOUGH_ROOM:
84 throw not_enough_room();
85 case internal::INVALID_LEAD:
86 append (replacement, out);
89 case internal::INCOMPLETE_SEQUENCE:
90 case internal::OVERLONG_SEQUENCE:
91 case internal::INVALID_CODE_POINT:
92 append (replacement, out);
94 // just one replacement mark for the sequence
95 while (internal::is_trail(*start) && start != end)
103 template <typename octet_iterator, typename output_iterator>
104 inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
106 static const uint32_t replacement_marker = internal::mask16(0xfffd);
107 return replace_invalid(start, end, out, replacement_marker);
110 template <typename octet_iterator>
111 octet_iterator append(uint32_t cp, octet_iterator result)
113 if (!internal::is_code_point_valid(cp))
114 throw invalid_code_point(cp);
116 if (cp < 0x80) // one octet
117 *(result++) = static_cast<uint8_t>(cp);
118 else if (cp < 0x800) { // two octets
119 *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
120 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
122 else if (cp < 0x10000) { // three octets
123 *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
124 *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
125 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
127 else { // four octets
128 *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
129 *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
130 *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
131 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
136 template <typename octet_iterator>
137 uint32_t next(octet_iterator& it, octet_iterator end)
140 internal::utf_error err_code = 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 next(it, end);
162 template <typename octet_iterator>
163 uint32_t prior(octet_iterator& it, octet_iterator start)
165 octet_iterator end = it;
166 while (internal::is_trail(*(--it)))
168 throw invalid_utf8(*it); // error - no lead byte in the sequence
169 octet_iterator temp = it;
170 return next(temp, end);
173 /// Deprecated in versions that include "prior"
174 template <typename octet_iterator>
175 uint32_t previous(octet_iterator& it, octet_iterator pass_start)
177 octet_iterator end = it;
178 while (internal::is_trail(*(--it)))
179 if (it == pass_start)
180 throw invalid_utf8(*it); // error - no lead byte in the sequence
181 octet_iterator temp = it;
182 return next(temp, end);
185 template <typename octet_iterator, typename distance_type>
186 void advance (octet_iterator& it, distance_type n, octet_iterator end)
188 for (distance_type i = 0; i < n; ++i)
192 template <typename octet_iterator>
193 typename std::iterator_traits<octet_iterator>::difference_type
194 distance (octet_iterator first, octet_iterator last)
196 typename std::iterator_traits<octet_iterator>::difference_type dist;
197 for (dist = 0; first < last; ++dist)
202 template <typename u16bit_iterator, typename octet_iterator>
203 octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
205 while (start != end) {
206 uint32_t cp = internal::mask16(*start++);
207 // Take care of surrogate pairs first
208 if (internal::is_lead_surrogate(cp)) {
210 uint32_t trail_surrogate = internal::mask16(*start++);
211 if (internal::is_trail_surrogate(trail_surrogate))
212 cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
214 throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
217 throw invalid_utf16(static_cast<uint16_t>(cp));
220 // Lone trail surrogate
221 else if (internal::is_trail_surrogate(cp))
222 throw invalid_utf16(static_cast<uint16_t>(cp));
224 result = append(cp, result);
229 template <typename u16bit_iterator, typename octet_iterator>
230 u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
232 while (start != end) {
233 uint32_t cp = next(start, end);
234 if (cp > 0xffff) { //make a surrogate pair
235 *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
236 *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
239 *result++ = static_cast<uint16_t>(cp);
244 template <typename octet_iterator, typename u32bit_iterator>
245 octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
248 result = append(*(start++), result);
253 template <typename octet_iterator, typename u32bit_iterator>
254 u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
257 (*result++) = next(start, end);
262 // The iterator class
263 template <typename octet_iterator>
264 class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
266 octet_iterator range_start;
267 octet_iterator range_end;
270 explicit iterator (const octet_iterator& octet_it,
271 const octet_iterator& range_start,
272 const octet_iterator& range_end) :
273 it(octet_it), range_start(range_start), range_end(range_end)
275 if (it < range_start || it > range_end)
276 throw std::out_of_range("Invalid utf-8 iterator position");
278 // the default "big three" are OK
279 octet_iterator base () const { return it; }
280 uint32_t operator * () const
282 octet_iterator temp = it;
283 return next(temp, range_end);
285 bool operator == (const iterator& rhs) const
287 if (range_start != rhs.range_start || range_end != rhs.range_end)
288 throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
289 return (it == rhs.it);
291 bool operator != (const iterator& rhs) const
293 return !(operator == (rhs));
295 iterator& operator ++ ()
300 iterator operator ++ (int)
302 iterator temp = *this;
306 iterator& operator -- ()
308 prior(it, range_start);
311 iterator operator -- (int)
313 iterator temp = *this;
314 prior(it, range_start);
321 #endif //header guard