Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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 if (cp < 0x80) // one octet
41 *(result++) = static_cast<uint8_t>(cp);
42 else if (cp < 0x800) { // two octets
43 *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
44 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
45 }
46 else if (cp < 0x10000) { // three octets
47 *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
48 *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
49 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
50 }
51 else { // four octets
52 *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
53 *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f)| 0x80);
54 *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
55 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
56 }
57 return result;
58 }
59
60 template <typename octet_iterator>
61 uint32_t next(octet_iterator& it)
62 {
64 typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it);
65 switch (length) {
66 case 1:
67 break;
68 case 2:
69 it++;
70 cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
71 break;
72 case 3:
73 ++it;
74 cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
75 ++it;
76 cp += (*it) & 0x3f;
77 break;
78 case 4:
79 ++it;
80 cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
81 ++it;
82 cp += (utf8::internal::mask8(*it) << 6) & 0xfff;
83 ++it;
84 cp += (*it) & 0x3f;
85 break;
86 }
87 ++it;
88 return cp;
89 }
90
91 template <typename octet_iterator>
92 uint32_t peek_next(octet_iterator it)
93 {
94 return utf8::unchecked::next(it);
95 }
96
97 template <typename octet_iterator>
98 uint32_t prior(octet_iterator& it)
99 {
100 while (utf8::internal::is_trail(*(--it))) ;
101 octet_iterator temp = it;
102 return utf8::unchecked::next(temp);
103 }
104
105 // Deprecated in versions that include prior, but only for the sake of consistency (see utf8::previous)
106 template <typename octet_iterator>
107 inline uint32_t previous(octet_iterator& it)
108 {
109 return utf8::unchecked::prior(it);
110 }
111
112 template <typename octet_iterator, typename distance_type>
113 void advance (octet_iterator& it, distance_type n)
114 {
115 for (distance_type i = 0; i < n; ++i)
117 }
118
119 template <typename octet_iterator>
120 typename std::iterator_traits<octet_iterator>::difference_type
121 distance (octet_iterator first, octet_iterator last)
122 {
123 typename std::iterator_traits<octet_iterator>::difference_type dist;
124 for (dist = 0; first < last; ++dist)
126 return dist;
127 }
128
129 template <typename u16bit_iterator, typename octet_iterator>
130 octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
131 {
132 while (start != end) {
133 uint32_t cp = utf8::internal::mask16(*start++);
134 // Take care of surrogate pairs first
136 uint32_t trail_surrogate = utf8::internal::mask16(*start++);
137 cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
138 }
139 result = utf8::unchecked::append(cp, result);
140 }
141 return result;
142 }
143
144 template <typename u16bit_iterator, typename octet_iterator>
145 u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
146 {
147 while (start < end) {
149 if (cp > 0xffff) { //make a surrogate pair
150 *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
151 *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
152 }
153 else
154 *result++ = static_cast<uint16_t>(cp);
155 }
156 return result;
157 }
158
159 template <typename octet_iterator, typename u32bit_iterator>
160 octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
161 {
162 while (start != end)
163 result = utf8::unchecked::append(*(start++), result);
164
165 return result;
166 }
167
168 template <typename octet_iterator, typename u32bit_iterator>
169 u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
170 {
171 while (start < end)
172 (*result++) = utf8::unchecked::next(start);
173
174 return result;
175 }
176
177 // The iterator class
178 template <typename octet_iterator>
179 class iterator {
180 octet_iterator it;
181 public:
182 using iterator_category = std::bidirectional_iterator_tag;
184 using difference_type = std::ptrdiff_t;
187
189 explicit iterator (const octet_iterator& octet_it): it(octet_it) {}
190 // the default "big three" are OK
191 octet_iterator base () const { return it; }
193 {
194 octet_iterator temp = it;
195 return utf8::unchecked::next(temp);
196 }
197 bool operator == (const iterator& rhs) const
198 {
199 return (it == rhs.it);
200 }
201 bool operator != (const iterator& rhs) const
202 {
203 return !(operator == (rhs));
204 }
206 {
207 ::std::advance(it, utf8::internal::sequence_length(it));
208 return *this;
209 }
211 {
212 iterator temp = *this;
213 ::std::advance(it, utf8::internal::sequence_length(it));
214 return temp;
215 }
217 {
219 return *this;
220 }
222 {
223 iterator temp = *this;
225 return temp;
226 }
227 }; // class iterator
228
229 } // namespace utf8::unchecked
230} // namespace utf8
231
232
233#endif // header guard
234
iterator(const octet_iterator &octet_it)
Definition unchecked.h:189
uint32_t operator*() const
Definition unchecked.h:192
std::bidirectional_iterator_tag iterator_category
Definition unchecked.h:182
bool operator!=(const iterator &rhs) const
Definition unchecked.h:201
octet_iterator base() const
Definition unchecked.h:191
bool operator==(const iterator &rhs) const
Definition unchecked.h:197
std::ptrdiff_t difference_type
Definition unchecked.h:184
bool is_lead_surrogate(u16 cp)
Definition core.h:75
const uint32_t SURROGATE_OFFSET
Definition core.h:53
const uint16_t LEAD_OFFSET
Definition core.h:52
const uint16_t TRAIL_SURROGATE_MIN
Definition core.h:50
uint16_t mask16(u16_type oc)
Definition core.h:64
bool is_trail(octet_type oc)
Definition core.h:69
std::iterator_traits< octet_iterator >::difference_type sequence_length(octet_iterator lead_it)
Definition core.h:100
uint8_t mask8(octet_type oc)
Definition core.h:59
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
Definition unchecked.h:121
uint32_t previous(octet_iterator &it)
Definition unchecked.h:107
void advance(octet_iterator &it, distance_type n)
Definition unchecked.h:113
u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result)
Definition unchecked.h:169
uint32_t peek_next(octet_iterator it)
Definition unchecked.h:92
uint32_t next(octet_iterator &it)
Definition unchecked.h:61
octet_iterator append(uint32_t cp, octet_iterator result)
Definition unchecked.h:38
uint32_t prior(octet_iterator &it)
Definition unchecked.h:98
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
Definition unchecked.h:130
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
Definition unchecked.h:145
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
Definition unchecked.h:160
Definition checked.h:35
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124