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