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