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