openMSX
utf8_unchecked.hh
Go to the documentation of this file.
1 // UTF8-CPP http://utfcpp.sourceforge.net/
2 // Slightly simplified (and reformatted) to fit openMSX coding style.
3 
4 // Copyright 2006 Nemanja Trifunovic
5 
6 /*
7 Permission is hereby granted, free of charge, to any person or organization
8 obtaining a copy of the software and accompanying documentation covered by
9 this license (the "Software") to use, reproduce, display, distribute,
10 execute, and transmit the Software, and to prepare derivative works of the
11 Software, and to permit third-parties to whom the Software is furnished to
12 do so, all subject to the following:
13 
14 The copyright notices in the Software and this entire statement, including
15 the above license grant, this restriction and the following disclaimer,
16 must be included in all copies of the Software, in whole or in part, and
17 all derivative works of the Software, unless such copies or derivative
18 works are solely in the form of machine-executable object code generated by
19 a source language processor.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 */
29 
30 #ifndef UTF8_UNCHECKED_HH
31 #define UTF8_UNCHECKED_HH
32 
33 #include "utf8_core.hh"
34 #include "string_view.hh"
35 
36 namespace utf8 {
37 namespace unchecked {
38 
39 template <typename octet_iterator>
40 octet_iterator append(uint32_t cp, octet_iterator result)
41 {
42  if (cp < 0x80) {
43  // one octet
44  *result++ = cp;
45  } else if (cp < 0x800) {
46  // two octets
47  *result++ = ((cp >> 6) ) | 0xc0;
48  *result++ = ((cp >> 0) & 0x3f) | 0x80;
49  } else if (cp < 0x10000) {
50  // three octets
51  *result++ = ((cp >> 12) ) | 0xe0;
52  *result++ = ((cp >> 6) & 0x3f) | 0x80;
53  *result++ = ((cp >> 0) & 0x3f) | 0x80;
54  } else {
55  // four octets
56  *result++ = ((cp >> 18) ) | 0xf0;
57  *result++ = ((cp >> 12) & 0x3f) | 0x80;
58  *result++ = ((cp >> 6) & 0x3f) | 0x80;
59  *result++ = ((cp >> 0) & 0x3f) | 0x80;
60  }
61  return result;
62 }
63 
64 template <typename octet_iterator>
65 uint32_t next(octet_iterator& it)
66 {
67  uint32_t cp = *it;
68  switch (utf8::internal::sequence_length(cp)) {
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) + ((*it << 6) & 0xfff);
78  ++it;
79  cp += (*it) & 0x3f;
80  break;
81  case 4:
82  ++it;
83  cp = ((cp << 18) & 0x1fffff) + ((*it << 12) & 0x3ffff);
84  ++it;
85  cp += (*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  auto temp = it;
105  return next(temp);
106 }
107 
108 template <typename octet_iterator, typename distance_type>
109 void advance(octet_iterator& it, distance_type n)
110 {
111  for (distance_type i = 0; i < n; ++i) {
112  unchecked::next(it);
113  }
114 }
115 
116 template <typename octet_iterator>
117 auto distance(octet_iterator first, octet_iterator last)
118 {
119  typename std::iterator_traits<octet_iterator>::difference_type dist;
120  for (dist = 0; first < last; ++dist) {
121  unchecked::next(first);
122  }
123  return dist;
124 }
125 
126 template <typename u16bit_iterator, typename octet_iterator>
127 octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end,
128  octet_iterator result)
129 {
130  while (start != end) {
131  uint32_t cp = *start++;
132  // Take care of surrogate pairs first
133  if (internal::is_surrogate(cp)) {
134  uint32_t trail_surrogate = *start++;
135  cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
136  }
137  result = 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,
144  u16bit_iterator result)
145 {
146  while (start != end) {
147  uint32_t cp = next(start);
148  if (cp > 0xffff) {
149  // make a surrogate pair
150  *result++ = (cp >> 10) + internal::LEAD_OFFSET;
151  *result++ = (cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN;
152  } else {
153  *result++ = cp;
154  }
155  }
156  return result;
157 }
158 
159 template <typename octet_iterator, typename u32bit_iterator>
160 octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end,
161  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,
171  u32bit_iterator result)
172 {
173  while (start < end) {
174  *result++ = next(start);
175  }
176  return result;
177 }
178 
179 // The iterator class
180 template <typename octet_iterator>
181 class iterator : public std::iterator<std::bidirectional_iterator_tag, uint32_t>
182 {
183  octet_iterator it;
184 public:
185  iterator() = default;
186  explicit iterator(const octet_iterator& octet_it)
187  : it(octet_it) {}
188  // the default "big three" are OK
189  octet_iterator base() const { return it; }
190  uint32_t operator*() const
191  {
192  octet_iterator temp = it;
193  return next(temp);
194  }
195  bool operator==(const iterator& rhs) const
196  {
197  return it == rhs.it;
198  }
199  bool operator!=(const iterator& rhs) const
200  {
201  return !(operator==(rhs));
202  }
204  {
206  return *this;
207  }
209  {
210  auto temp = *this;
212  return temp;
213  }
215  {
216  prior(it);
217  return *this;
218  }
220  {
221  auto temp = *this;
222  prior(it);
223  return temp;
224  }
225 };
226 
227 // convenience functions
228 inline size_t size(string_view utf8)
229 {
230  return utf8::unchecked::distance(begin(utf8), end(utf8));
231 }
234 {
235  auto b = begin(utf8);
236  utf8::unchecked::advance(b, first);
238  if (len != string_view::npos) {
239  e = b;
240  while (len && (e != end(utf8))) {
241  unchecked::next(e); --len;
242  }
243  } else {
244  e = end(utf8);
245  }
246  return string_view(b, e);
247 }
248 
249 } // namespace unchecked
250 } // namespace utf8
251 
252 #endif
bool is_trail(uint8_t oc)
Definition: utf8_core.hh:56
octet_iterator append(uint32_t cp, octet_iterator result)
const uint16_t TRAIL_SURROGATE_MIN
Definition: utf8_core.hh:48
u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result)
uint32_t peek_next(octet_iterator it)
uint32_t next(octet_iterator &it)
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
bool is_surrogate(uint16_t cp)
Definition: utf8_core.hh:61
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
octet_iterator base() const
auto distance(octet_iterator first, octet_iterator last)
bool operator==(const iterator &rhs) const
static const size_type npos
Definition: string_view.hh:24
auto begin(const string_view &x)
Definition: string_view.hh:151
size_t size(string_view utf8)
iterator(const octet_iterator &octet_it)
bool operator!=(const iterator &rhs) const
const uint32_t SURROGATE_OFFSET
Definition: utf8_core.hh:51
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
uint32_t operator*() const
size_t size_type
Definition: string_view.hh:19
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
void advance(octet_iterator &it, distance_type n)
unsigned sequence_length(uint8_t lead)
Definition: utf8_core.hh:72
const char * const_iterator
Definition: string_view.hh:21
string_view substr(string_view utf8, string_view::size_type first=0, string_view::size_type len=string_view::npos)
const uint16_t LEAD_OFFSET
Definition: utf8_core.hh:50
auto end(const string_view &x)
Definition: string_view.hh:152
uint32_t prior(octet_iterator &it)