drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
checked.h
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
29#pragma once
30
31#include <drizzled/utf8/core.h>
32#include <stdexcept>
33
34namespace drizzled
35{
36namespace utf8
37{
38 // Base for the exceptions that may be thrown from the library
39 class exception : public std::exception {
40 };
41
42 // Exceptions that may be thrown from the library functions.
43 class invalid_code_point : public exception {
44 uint32_t cp;
45 public:
46 invalid_code_point(uint32_t cp_in) : cp(cp_in) {}
47 virtual const char* what() const noexcept { return "Invalid code point"; }
48 uint32_t code_point() const {return cp;}
49 };
50
51 class invalid_utf8 : public exception {
52 uint8_t u8;
53 public:
54 invalid_utf8 (uint8_t u) : u8(u) {}
55 virtual const char* what() const noexcept { return "Invalid UTF-8"; }
56 uint8_t utf8_octet() const {return u8;}
57 };
58
59 class invalid_utf16 : public exception {
60 uint16_t u16;
61 public:
62 invalid_utf16 (uint16_t u) : u16(u) {}
63 virtual const char* what() const noexcept { return "Invalid UTF-16"; }
64 uint16_t utf16_word() const {return u16;}
65 };
66
67 class not_enough_room : public exception {
68 public:
69 virtual const char* what() const noexcept { return "Not enough space"; }
70 };
71
73
74 template <typename octet_iterator, typename output_iterator>
75 output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
76 {
77 while (start != end) {
78 octet_iterator sequence_start = start;
79 internal::utf_error err_code = internal::validate_next(start, end);
80 switch (err_code) {
81 case internal::UTF8_OK :
82 for (octet_iterator it = sequence_start; it != start; ++it)
83 *out++ = *it;
84 break;
85 case internal::NOT_ENOUGH_ROOM:
86 throw not_enough_room();
87 case internal::INVALID_LEAD:
88 append (replacement, out);
89 ++start;
90 break;
91 case internal::INCOMPLETE_SEQUENCE:
92 case internal::OVERLONG_SEQUENCE:
93 case internal::INVALID_CODE_POINT:
94 append (replacement, out);
95 ++start;
96 // just one replacement mark for the sequence
97 while (internal::is_trail(*start) && start != end)
98 ++start;
99 break;
100 }
101 }
102 return out;
103 }
104
105 template <typename octet_iterator, typename output_iterator>
106 inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
107 {
108 static const uint32_t replacement_marker = internal::mask16(0xfffd);
109 return replace_invalid(start, end, out, replacement_marker);
110 }
111
112 template <typename octet_iterator>
113 octet_iterator append(uint32_t cp, octet_iterator result)
114 {
115 if (!internal::is_code_point_valid(cp))
116 throw invalid_code_point(cp);
117
118 if (cp < 0x80) // one octet
119 *(result++) = static_cast<uint8_t>(cp);
120 else if (cp < 0x800) { // two octets
121 *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
122 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
123 }
124 else if (cp < 0x10000) { // three octets
125 *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
126 *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
127 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
128 }
129 else { // four octets
130 *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
131 *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
132 *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
133 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
134 }
135 return result;
136 }
137
138 template <typename octet_iterator>
139 uint32_t next(octet_iterator& it, octet_iterator end)
140 {
141 uint32_t cp = 0;
142 internal::utf_error err_code = internal::validate_next(it, end, &cp);
143 switch (err_code) {
144 case internal::UTF8_OK :
145 break;
146 case internal::NOT_ENOUGH_ROOM :
147 throw not_enough_room();
148 case internal::INVALID_LEAD :
149 case internal::INCOMPLETE_SEQUENCE :
150 case internal::OVERLONG_SEQUENCE :
151 throw invalid_utf8(*it);
152 case internal::INVALID_CODE_POINT :
153 throw invalid_code_point(cp);
154 }
155 return cp;
156 }
157
158 template <typename octet_iterator>
159 uint32_t peek_next(octet_iterator it, octet_iterator end)
160 {
161 return next(it, end);
162 }
163
164 template <typename octet_iterator>
165 uint32_t prior(octet_iterator& it, octet_iterator start)
166 {
167 octet_iterator end = it;
168 while (internal::is_trail(*(--it)))
169 if (it < start)
170 throw invalid_utf8(*it); // error - no lead byte in the sequence
171 octet_iterator temp = it;
172 return next(temp, end);
173 }
174
176 template <typename octet_iterator>
177 uint32_t previous(octet_iterator& it, octet_iterator pass_start)
178 {
179 octet_iterator end = it;
180 while (internal::is_trail(*(--it)))
181 if (it == pass_start)
182 throw invalid_utf8(*it); // error - no lead byte in the sequence
183 octet_iterator temp = it;
184 return next(temp, end);
185 }
186
187 template <typename octet_iterator, typename distance_type>
188 void advance (octet_iterator& it, distance_type n, octet_iterator end)
189 {
190 for (distance_type i = 0; i < n; ++i)
191 next(it, end);
192 }
193
194 template <typename octet_iterator>
195 typename std::iterator_traits<octet_iterator>::difference_type
196 distance (octet_iterator first, octet_iterator last)
197 {
198 typename std::iterator_traits<octet_iterator>::difference_type dist;
199 for (dist = 0; first < last; ++dist)
200 next(first, last);
201 return dist;
202 }
203
204 template <typename u16bit_iterator, typename octet_iterator>
205 octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
206 {
207 while (start != end) {
208 uint32_t cp = internal::mask16(*start++);
209 // Take care of surrogate pairs first
210 if (internal::is_lead_surrogate(cp)) {
211 if (start != end) {
212 uint32_t trail_surrogate = internal::mask16(*start++);
213 if (internal::is_trail_surrogate(trail_surrogate))
214 cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
215 else
216 throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
217 }
218 else
219 throw invalid_utf16(static_cast<uint16_t>(cp));
220
221 }
222 // Lone trail surrogate
223 else if (internal::is_trail_surrogate(cp))
224 throw invalid_utf16(static_cast<uint16_t>(cp));
225
226 result = append(cp, result);
227 }
228 return result;
229 }
230
231 template <typename u16bit_iterator, typename octet_iterator>
232 u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
233 {
234 while (start != end) {
235 uint32_t cp = next(start, end);
236 if (cp > 0xffff) { //make a surrogate pair
237 *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
238 *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
239 }
240 else
241 *result++ = static_cast<uint16_t>(cp);
242 }
243 return result;
244 }
245
246 template <typename octet_iterator, typename u32bit_iterator>
247 octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
248 {
249 while (start != end)
250 result = append(*(start++), result);
251
252 return result;
253 }
254
255 template <typename octet_iterator, typename u32bit_iterator>
256 u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
257 {
258 while (start != end)
259 (*result++) = next(start, end);
260
261 return result;
262 }
263
264 // The iterator class
265 template <typename octet_iterator>
266 class iterator {
267 public:
268 typedef std::bidirectional_iterator_tag iterator_category;
269 typedef uint32_t value_type;
270 typedef std::ptrdiff_t difference_type;
271 typedef uint32_t* pointer;
272 typedef uint32_t& reference;
273 private:
274 octet_iterator it;
275 octet_iterator range_start;
276 octet_iterator range_end;
277 public:
278 iterator () {};
279 explicit iterator (const octet_iterator& octet_it,
280 const octet_iterator& range_start_in,
281 const octet_iterator& range_end_in) :
282 it(octet_it), range_start(range_start_in), range_end(range_end_in)
283 {
284 if (it < range_start || it > range_end)
285 throw std::out_of_range("Invalid utf-8 iterator position");
286 }
287 // the default "big three" are OK
288 octet_iterator base () const { return it; }
289 uint32_t operator * () const
290 {
291 octet_iterator temp = it;
292 return next(temp, range_end);
293 }
294 bool operator == (const iterator& rhs) const
295 {
296 if (range_start != rhs.range_start || range_end != rhs.range_end)
297 throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
298 return (it == rhs.it);
299 }
300 bool operator != (const iterator& rhs) const
301 {
302 return !(operator == (rhs));
303 }
304 iterator& operator ++ ()
305 {
306 next(it, range_end);
307 return *this;
308 }
309 iterator operator ++ (int)
310 {
311 iterator temp = *this;
312 next(it, range_end);
313 return temp;
314 }
315 iterator& operator -- ()
316 {
317 prior(it, range_start);
318 return *this;
319 }
320 iterator operator -- (int)
321 {
322 iterator temp = *this;
323 prior(it, range_start);
324 return temp;
325 }
326 }; // class iterator
327
328} // namespace utf8
329} // namespace drizzled
330
331
332
TODO: Rename this file - func.h is stupid.