]> git.donarmstrong.com Git - flightcrew.git/blob - src/utf8-cpp/utf8/checked.h
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / utf8-cpp / utf8 / checked.h
1 // Copyright 2006 Nemanja Trifunovic
2
3 /*
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:
10
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.
17
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.
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
34 namespace 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.
41     class invalid_code_point : public exception {
42         uint32_t cp;
43     public:
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;}
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
70     /// The library API - functions intended to be called by the users
71
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)
74     {
75         while (start != end) {
76             octet_iterator sequence_start = start;
77             internal::utf_error err_code = internal::validate_next(start, end);
78             switch (err_code) {
79                 case internal::UTF8_OK :
80                     for (octet_iterator it = sequence_start; it != start; ++it)
81                         *out++ = *it;
82                     break;
83                 case internal::NOT_ENOUGH_ROOM:
84                     throw not_enough_room();
85                 case internal::INVALID_LEAD:
86                     append (replacement, out);
87                     ++start;
88                     break;
89                 case internal::INCOMPLETE_SEQUENCE:
90                 case internal::OVERLONG_SEQUENCE:
91                 case internal::INVALID_CODE_POINT:
92                     append (replacement, out);
93                     ++start;
94                     // just one replacement mark for the sequence
95                     while (internal::is_trail(*start) && start != end)
96                         ++start;
97                     break;
98             }
99         }
100         return out;
101     }
102
103     template <typename octet_iterator, typename output_iterator>
104     inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
105     {
106         static const uint32_t replacement_marker = internal::mask16(0xfffd);
107         return replace_invalid(start, end, out, replacement_marker);
108     }
109
110     template <typename octet_iterator>
111     octet_iterator append(uint32_t cp, octet_iterator result)
112     {
113         if (!internal::is_code_point_valid(cp))
114             throw invalid_code_point(cp);
115
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);
121         }
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);
126         }
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);
132         }
133         return result;
134     }
135
136     template <typename octet_iterator>
137     uint32_t next(octet_iterator& it, octet_iterator end)
138     {
139         uint32_t cp = 0;
140         internal::utf_error err_code = internal::validate_next(it, end, &cp);
141         switch (err_code) {
142             case internal::UTF8_OK :
143                 break;
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);
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 next(it, end);
160     }
161
162     template <typename octet_iterator>
163     uint32_t prior(octet_iterator& it, octet_iterator start)
164     {
165         octet_iterator end = it;
166         while (internal::is_trail(*(--it)))
167             if (it < start)
168                 throw invalid_utf8(*it); // error - no lead byte in the sequence
169         octet_iterator temp = it;
170         return next(temp, end);
171     }
172
173     /// Deprecated in versions that include "prior"
174     template <typename octet_iterator>
175     uint32_t previous(octet_iterator& it, octet_iterator pass_start)
176     {
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);
183     }
184
185     template <typename octet_iterator, typename distance_type>
186     void advance (octet_iterator& it, distance_type n, octet_iterator end)
187     {
188         for (distance_type i = 0; i < n; ++i)
189             next(it, end);
190     }
191
192     template <typename octet_iterator>
193     typename std::iterator_traits<octet_iterator>::difference_type
194     distance (octet_iterator first, octet_iterator last)
195     {
196         typename std::iterator_traits<octet_iterator>::difference_type dist;
197         for (dist = 0; first < last; ++dist)
198             next(first, last);
199         return dist;
200     }
201
202     template <typename u16bit_iterator, typename octet_iterator>
203     octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
204     {
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)) {
209                 if (start != end) {
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;
213                     else
214                         throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
215                 }
216                 else
217                     throw invalid_utf16(static_cast<uint16_t>(cp));
218
219             }
220             // Lone trail surrogate
221             else if (internal::is_trail_surrogate(cp))
222                 throw invalid_utf16(static_cast<uint16_t>(cp));
223
224             result = append(cp, result);
225         }
226         return result;
227     }
228
229     template <typename u16bit_iterator, typename octet_iterator>
230     u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
231     {
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);
237             }
238             else
239                 *result++ = static_cast<uint16_t>(cp);
240         }
241         return result;
242     }
243
244     template <typename octet_iterator, typename u32bit_iterator>
245     octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
246     {
247         while (start != end)
248             result = append(*(start++), result);
249
250         return result;
251     }
252
253     template <typename octet_iterator, typename u32bit_iterator>
254     u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
255     {
256         while (start != end)
257             (*result++) = next(start, end);
258
259         return result;
260     }
261
262     // The iterator class
263     template <typename octet_iterator>
264     class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
265       octet_iterator it;
266       octet_iterator range_start;
267       octet_iterator range_end;
268       public:
269       iterator () {};
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)
274       {
275           if (it < range_start || it > range_end)
276               throw std::out_of_range("Invalid utf-8 iterator position");
277       }
278       // the default "big three" are OK
279       octet_iterator base () const { return it; }
280       uint32_t operator * () const
281       {
282           octet_iterator temp = it;
283           return next(temp, range_end);
284       }
285       bool operator == (const iterator& rhs) const
286       {
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);
290       }
291       bool operator != (const iterator& rhs) const
292       {
293           return !(operator == (rhs));
294       }
295       iterator& operator ++ ()
296       {
297           next(it, range_end);
298           return *this;
299       }
300       iterator operator ++ (int)
301       {
302           iterator temp = *this;
303           next(it, range_end);
304           return temp;
305       }
306       iterator& operator -- ()
307       {
308           prior(it, range_start);
309           return *this;
310       }
311       iterator operator -- (int)
312       {
313           iterator temp = *this;
314           prior(it, range_start);
315           return temp;
316       }
317     }; // class iterator
318
319 } // namespace utf8
320
321 #endif //header guard
322
323