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