1 // Copyright 2006 Nemanja Trifunovic
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
11 The copyright notices in the Software and this entire statement, including
12 the above license grant, this restriction and the following disclaimer,
13 must be included in all copies of the Software, in whole or in part, and
14 all derivative works of the Software, unless such copies or derivative
15 works are solely in the form of machine-executable object code generated by
16 a source language processor.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
28 #ifndef UTF8_FOR_CPP_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
29 #define UTF8_FOR_CPP_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
37 template <typename octet_iterator>
38 octet_iterator append(uint32_t cp, octet_iterator result)
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);
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);
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);
60 template <typename octet_iterator>
61 uint32_t next(octet_iterator& it)
63 uint32_t cp = utf8::internal::mask8(*it);
64 typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it);
70 cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
74 cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
80 cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
82 cp += (utf8::internal::mask8(*it) << 6) & 0xfff;
91 template <typename octet_iterator>
92 uint32_t peek_next(octet_iterator it)
94 return utf8::unchecked::next(it);
97 template <typename octet_iterator>
98 uint32_t prior(octet_iterator& it)
100 while (utf8::internal::is_trail(*(--it))) ;
101 octet_iterator temp = it;
102 return utf8::unchecked::next(temp);
105 template <typename octet_iterator, typename distance_type>
106 void advance (octet_iterator& it, distance_type n)
108 const distance_type zero(0);
111 for (distance_type i = n; i < zero; ++i)
112 utf8::unchecked::prior(it);
115 for (distance_type i = zero; i < n; ++i)
116 utf8::unchecked::next(it);
120 template <typename octet_iterator>
121 typename std::iterator_traits<octet_iterator>::difference_type
122 distance (octet_iterator first, octet_iterator last)
124 typename std::iterator_traits<octet_iterator>::difference_type dist;
125 for (dist = 0; first < last; ++dist)
126 utf8::unchecked::next(first);
130 template <typename u16bit_iterator, typename octet_iterator>
131 octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
133 while (start != end) {
134 uint32_t cp = utf8::internal::mask16(*start++);
135 // Take care of surrogate pairs first
136 if (utf8::internal::is_lead_surrogate(cp)) {
137 uint32_t trail_surrogate = utf8::internal::mask16(*start++);
138 cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
140 result = utf8::unchecked::append(cp, result);
145 template <typename u16bit_iterator, typename octet_iterator>
146 u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
148 while (start < end) {
149 uint32_t cp = utf8::unchecked::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);
155 *result++ = static_cast<uint16_t>(cp);
160 template <typename octet_iterator, typename u32bit_iterator>
161 octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
164 result = utf8::unchecked::append(*(start++), result);
169 template <typename octet_iterator, typename u32bit_iterator>
170 u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
173 (*result++) = utf8::unchecked::next(start);
178 // The iterator class
179 template <typename octet_iterator>
180 class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
184 explicit iterator (const octet_iterator& octet_it): it(octet_it) {}
185 // the default "big three" are OK
186 octet_iterator base () const { return it; }
187 uint32_t operator * () const
189 octet_iterator temp = it;
190 return utf8::unchecked::next(temp);
192 bool operator == (const iterator& rhs) const
194 return (it == rhs.it);
196 bool operator != (const iterator& rhs) const
198 return !(operator == (rhs));
200 iterator& operator ++ ()
202 ::std::advance(it, utf8::internal::sequence_length(it));
205 iterator operator ++ (int)
207 iterator temp = *this;
208 ::std::advance(it, utf8::internal::sequence_length(it));
211 iterator& operator -- ()
213 utf8::unchecked::prior(it);
216 iterator operator -- (int)
218 iterator temp = *this;
219 utf8::unchecked::prior(it);
224 } // namespace utf8::unchecked
228 #endif // header guard