librime 1.2
Rime Input Method Engine, the core library
unchecked.h
Go to the documentation of this file.
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#ifndef UTF8_FOR_CPP_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
29#define UTF8_FOR_CPP_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
30
31#include "core.h"
32
33namespace utf8
34{
35 namespace unchecked
36 {
37 template <typename octet_iterator>
38 octet_iterator append(uint32_t cp, octet_iterator result)
39 {
40 return internal::append(cp, result);
41 }
42
43 template <typename octet_iterator, typename output_iterator>
44 output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
45 {
46 while (start != end) {
47 octet_iterator sequence_start = start;
49 switch (err_code) {
51 for (octet_iterator it = sequence_start; it != start; ++it)
52 *out++ = *it;
53 break;
55 out = utf8::unchecked::append (replacement, out);
56 start = end;
57 break;
59 out = utf8::unchecked::append (replacement, out);
60 ++start;
61 break;
65 out = utf8::unchecked::append (replacement, out);
66 ++start;
67 // just one replacement mark for the sequence
68 while (start != end && utf8::internal::is_trail(*start))
69 ++start;
70 break;
71 }
72 }
73 return out;
74 }
75
76 template <typename octet_iterator, typename output_iterator>
77 inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
78 {
79 static const uint32_t replacement_marker = utf8::internal::mask16(0xfffd);
80 return utf8::unchecked::replace_invalid(start, end, out, replacement_marker);
81 }
82
83 template <typename octet_iterator>
84 uint32_t next(octet_iterator& it)
85 {
87 typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it);
88 switch (length) {
89 case 1:
90 break;
91 case 2:
92 it++;
93 cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
94 break;
95 case 3:
96 ++it;
97 cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
98 ++it;
99 cp += (*it) & 0x3f;
100 break;
101 case 4:
102 ++it;
103 cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
104 ++it;
105 cp += (utf8::internal::mask8(*it) << 6) & 0xfff;
106 ++it;
107 cp += (*it) & 0x3f;
108 break;
109 }
110 ++it;
111 return cp;
112 }
113
114 template <typename octet_iterator>
115 uint32_t peek_next(octet_iterator it)
116 {
117 return utf8::unchecked::next(it);
118 }
119
120 template <typename octet_iterator>
121 uint32_t prior(octet_iterator& it)
122 {
123 while (utf8::internal::is_trail(*(--it))) ;
124 octet_iterator temp = it;
125 return utf8::unchecked::next(temp);
126 }
127
128 template <typename octet_iterator, typename distance_type>
129 void advance (octet_iterator& it, distance_type n)
130 {
131 const distance_type zero(0);
132 if (n < zero) {
133 // backward
134 for (distance_type i = n; i < zero; ++i)
136 } else {
137 // forward
138 for (distance_type i = zero; i < n; ++i)
140 }
141 }
142
143 template <typename octet_iterator>
144 typename std::iterator_traits<octet_iterator>::difference_type
145 distance (octet_iterator first, octet_iterator last)
146 {
147 typename std::iterator_traits<octet_iterator>::difference_type dist;
148 for (dist = 0; first < last; ++dist)
150 return dist;
151 }
152
153 template <typename u16bit_iterator, typename octet_iterator>
154 octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
155 {
156 while (start != end) {
157 uint32_t cp = utf8::internal::mask16(*start++);
158 if (start == end)
159 return result;
160 // Take care of surrogate pairs first
162 uint32_t trail_surrogate = utf8::internal::mask16(*start++);
163 cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
164 }
165 result = utf8::unchecked::append(cp, result);
166 }
167 return result;
168 }
169
170 template <typename u16bit_iterator, typename octet_iterator>
171 u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
172 {
173 while (start < end) {
175 if (cp > 0xffff) { //make a surrogate pair
176 *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
177 *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
178 }
179 else
180 *result++ = static_cast<uint16_t>(cp);
181 }
182 return result;
183 }
184
185 template <typename octet_iterator, typename u32bit_iterator>
186 octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
187 {
188 while (start != end)
189 result = utf8::unchecked::append(*(start++), result);
190
191 return result;
192 }
193
194 template <typename octet_iterator, typename u32bit_iterator>
195 u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
196 {
197 while (start < end)
198 (*result++) = utf8::unchecked::next(start);
199
200 return result;
201 }
202
203 // The iterator class
204 template <typename octet_iterator>
205 class iterator {
206 octet_iterator it;
207 public:
211 typedef std::ptrdiff_t difference_type;
212 typedef std::bidirectional_iterator_tag iterator_category;
214 explicit iterator (const octet_iterator& octet_it): it(octet_it) {}
215 // the default "big three" are OK
216 octet_iterator base () const { return it; }
218 {
219 octet_iterator temp = it;
220 return utf8::unchecked::next(temp);
221 }
222 bool operator == (const iterator& rhs) const
223 {
224 return (it == rhs.it);
225 }
226 bool operator != (const iterator& rhs) const
227 {
228 return !(operator == (rhs));
229 }
231 {
232 ::std::advance(it, utf8::internal::sequence_length(it));
233 return *this;
234 }
236 {
237 iterator temp = *this;
238 ::std::advance(it, utf8::internal::sequence_length(it));
239 return temp;
240 }
242 {
244 return *this;
245 }
247 {
248 iterator temp = *this;
250 return temp;
251 }
252 }; // class iterator
253
254 } // namespace utf8::unchecked
255} // namespace utf8
256
257
258#endif // header guard
259
Definition checked.h:35
unsigned int uint32_t
Definition core.h:57
unsigned short uint16_t
Definition core.h:56
bool is_lead_surrogate(u16 cp)
Definition core.h:92
const uint32_t SURROGATE_OFFSET
Definition core.h:70
const uint16_t LEAD_OFFSET
Definition core.h:69
const uint16_t TRAIL_SURROGATE_MIN
Definition core.h:67
utf_error
Definition core.h:151
@ INCOMPLETE_SEQUENCE
Definition core.h:151
@ INVALID_LEAD
Definition core.h:151
@ OVERLONG_SEQUENCE
Definition core.h:151
@ INVALID_CODE_POINT
Definition core.h:151
@ NOT_ENOUGH_ROOM
Definition core.h:151
@ UTF8_OK
Definition core.h:151
uint16_t mask16(u16_type oc)
Definition core.h:81
bool is_trail(octet_type oc)
Definition core.h:86
utf_error validate_next(octet_iterator &it, octet_iterator end, uint32_t &code_point)
Definition core.h:240
std::iterator_traits< octet_iterator >::difference_type sequence_length(octet_iterator lead_it)
Definition core.h:117
uint8_t mask8(octet_type oc)
Definition core.h:76
octet_iterator append(uint32_t cp, octet_iterator result)
Definition core.h:304
Definition unchecked.h:36
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
Definition unchecked.h:145
output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
Definition unchecked.h:44
void advance(octet_iterator &it, distance_type n)
Definition unchecked.h:129
u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result)
Definition unchecked.h:195
uint32_t peek_next(octet_iterator it)
Definition unchecked.h:115
uint32_t next(octet_iterator &it)
Definition unchecked.h:84
octet_iterator append(uint32_t cp, octet_iterator result)
Definition unchecked.h:38
uint32_t prior(octet_iterator &it)
Definition unchecked.h:121
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
Definition unchecked.h:154
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
Definition unchecked.h:171
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
Definition unchecked.h:186
uint32_t * pointer
Definition unchecked.h:209
iterator(const octet_iterator &octet_it)
Definition unchecked.h:214
uint32_t operator*() const
Definition unchecked.h:217
bool operator!=(const iterator &rhs) const
Definition unchecked.h:226
octet_iterator base() const
Definition unchecked.h:216
iterator & operator--()
Definition unchecked.h:241
iterator()
Definition unchecked.h:213
std::bidirectional_iterator_tag iterator_category
Definition unchecked.h:212
iterator & operator++()
Definition unchecked.h:230
uint32_t value_type
Definition unchecked.h:208
bool operator==(const iterator &rhs) const
Definition unchecked.h:222
std::ptrdiff_t difference_type
Definition unchecked.h:211
uint32_t & reference
Definition unchecked.h:210