librime: unchecked.h Source File

| 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;
48 internal::utf_error err_code = utf8::internal::validate_next(start, end);
49 switch (err_code) {
50 case internal::UTF8_OK :
51 for (octet_iterator it = sequence_start; it != start; ++it)
52 *out++ = *it;
53 break;
54 case internal::NOT_ENOUGH_ROOM:
55 out = utf8::unchecked::append (replacement, out);
56 start = end;
57 break;
58 case internal::INVALID_LEAD:
59 out = utf8::unchecked::append (replacement, out);
60 ++start;
61 break;
62 case internal::INCOMPLETE_SEQUENCE:
63 case internal::OVERLONG_SEQUENCE:
64 case internal::INVALID_CODE_POINT:
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 {
86 uint32_t cp = utf8::internal::mask8(*it);
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)
135 utf8::unchecked::prior(it);
136 } else {
137 // forward
138 for (distance_type i = zero; i < n; ++i)
139 utf8::unchecked::next(it);
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)
149 utf8::unchecked::next(first);
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
161 if (utf8::internal::is_lead_surrogate(cp)) {
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) {
174 uint32_t cp = utf8::unchecked::next(start);
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:
208 typedef uint32_t value_type;
209 typedef uint32_t* pointer;
210 typedef uint32_t& reference;
211 typedef std::ptrdiff_t difference_type;
212 typedef std::bidirectional_iterator_tag iterator_category;
213 iterator () {}
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; }
217 uint32_t operator * () const
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 }
230 iterator& operator ++ ()
231 {
232 ::std::advance(it, utf8::internal::sequence_length(it));
233 return *this;
234 }
235 iterator operator ++ (int)
236 {
237 iterator temp = *this;
238 ::std::advance(it, utf8::internal::sequence_length(it));
239 return temp;
240 }
241 iterator& operator -- ()
242 {
243 utf8::unchecked::prior(it);
244 return *this;
245 }
246 iterator operator -- (int)
247 {
248 iterator temp = *this;
249 utf8::unchecked::prior(it);
250 return temp;
251 }
252 }; // class iterator
253
254 } // namespace utf8::unchecked
255} // namespace utf8
256
257
258#endif // header guard
259
core.h
utf8
Definition checked.h:35
utf8::uint32_t
unsigned int uint32_t
Definition core.h:57
utf8::uint16_t
unsigned short uint16_t
Definition core.h:56
utf8::internal::is_lead_surrogate
bool is_lead_surrogate(u16 cp)
Definition core.h:92
utf8::internal::SURROGATE_OFFSET
const uint32_t SURROGATE_OFFSET
Definition core.h:70
utf8::internal::LEAD_OFFSET
const uint16_t LEAD_OFFSET
Definition core.h:69
utf8::internal::TRAIL_SURROGATE_MIN
const uint16_t TRAIL_SURROGATE_MIN
Definition core.h:67
utf8::internal::utf_error
utf_error
Definition core.h:151
utf8::internal::INCOMPLETE_SEQUENCE
@ INCOMPLETE_SEQUENCE
Definition core.h:151
utf8::internal::INVALID_LEAD
@ INVALID_LEAD
Definition core.h:151
utf8::internal::OVERLONG_SEQUENCE
@ OVERLONG_SEQUENCE
Definition core.h:151
utf8::internal::INVALID_CODE_POINT
@ INVALID_CODE_POINT
Definition core.h:151
utf8::internal::NOT_ENOUGH_ROOM
@ NOT_ENOUGH_ROOM
Definition core.h:151
utf8::internal::UTF8_OK
@ UTF8_OK
Definition core.h:151
utf8::internal::mask16
uint16_t mask16(u16_type oc)
Definition core.h:81
utf8::internal::is_trail
bool is_trail(octet_type oc)
Definition core.h:86
utf8::internal::validate_next
utf_error validate_next(octet_iterator &it, octet_iterator end, uint32_t &code_point)
Definition core.h:240
utf8::internal::sequence_length
std::iterator_traits< octet_iterator >::difference_type sequence_length(octet_iterator lead_it)
Definition core.h:117
utf8::internal::mask8
uint8_t mask8(octet_type oc)
Definition core.h:76
utf8::internal::append
octet_iterator append(uint32_t cp, octet_iterator result)
Definition core.h:304
utf8::unchecked
Definition unchecked.h:36
utf8::unchecked::distance
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
Definition unchecked.h:145
utf8::unchecked::replace_invalid
output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
Definition unchecked.h:44
utf8::unchecked::advance
void advance(octet_iterator &it, distance_type n)
Definition unchecked.h:129
utf8::unchecked::utf8to32
u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result)
Definition unchecked.h:195
utf8::unchecked::peek_next
uint32_t peek_next(octet_iterator it)
Definition unchecked.h:115
utf8::unchecked::next
uint32_t next(octet_iterator &it)
Definition unchecked.h:84
utf8::unchecked::append
octet_iterator append(uint32_t cp, octet_iterator result)
Definition unchecked.h:38
utf8::unchecked::prior
uint32_t prior(octet_iterator &it)
Definition unchecked.h:121
utf8::unchecked::utf16to8
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
Definition unchecked.h:154
utf8::unchecked::utf8to16
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
Definition unchecked.h:171
utf8::unchecked::utf32to8
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
Definition unchecked.h:186
utf8::unchecked::iterator::pointer
uint32_t * pointer
Definition unchecked.h:209
utf8::unchecked::iterator::iterator
iterator(const octet_iterator &octet_it)
Definition unchecked.h:214
utf8::unchecked::iterator::operator*
uint32_t operator*() const
Definition unchecked.h:217
utf8::unchecked::iterator::operator!=
bool operator!=(const iterator &rhs) const
Definition unchecked.h:226
utf8::unchecked::iterator::base
octet_iterator base() const
Definition unchecked.h:216
utf8::unchecked::iterator::operator--
iterator & operator--()
Definition unchecked.h:241
utf8::unchecked::iterator::iterator
iterator()
Definition unchecked.h:213
utf8::unchecked::iterator::iterator_category
std::bidirectional_iterator_tag iterator_category
Definition unchecked.h:212
utf8::unchecked::iterator::operator++
iterator & operator++()
Definition unchecked.h:230
utf8::unchecked::iterator::value_type
uint32_t value_type
Definition unchecked.h:208
utf8::unchecked::iterator::operator==
bool operator==(const iterator &rhs) const
Definition unchecked.h:222
utf8::unchecked::iterator::difference_type
std::ptrdiff_t difference_type
Definition unchecked.h:211
utf8::unchecked::iterator::reference
uint32_t & reference
Definition unchecked.h:210