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