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>
35 
36 namespace utf8::unchecked {
37 
38 template <typename octet_iterator>
39 octet_iterator append(uint32_t cp, octet_iterator result)
40 {
41  if (cp < 0x80) {
42  // one octet
43  *result++ = cp;
44  } else if (cp < 0x800) {
45  // two octets
46  *result++ = ((cp >> 6) ) | 0xc0;
47  *result++ = ((cp >> 0) & 0x3f) | 0x80;
48  } else if (cp < 0x10000) {
49  // three octets
50  *result++ = ((cp >> 12) ) | 0xe0;
51  *result++ = ((cp >> 6) & 0x3f) | 0x80;
52  *result++ = ((cp >> 0) & 0x3f) | 0x80;
53  } else {
54  // four octets
55  *result++ = ((cp >> 18) ) | 0xf0;
56  *result++ = ((cp >> 12) & 0x3f) | 0x80;
57  *result++ = ((cp >> 6) & 0x3f) | 0x80;
58  *result++ = ((cp >> 0) & 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 = *it;
67  switch (utf8::internal::sequence_length(cp)) {
68  case 1:
69  break;
70  case 2:
71  ++it;
72  cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
73  break;
74  case 3:
75  ++it;
76  cp = ((cp << 12) & 0xffff) + ((*it << 6) & 0xfff);
77  ++it;
78  cp += (*it) & 0x3f;
79  break;
80  case 4:
81  ++it;
82  cp = ((cp << 18) & 0x1fffff) + ((*it << 12) & 0x3ffff);
83  ++it;
84  cp += (*it << 6) & 0xfff;
85  ++it;
86  cp += (*it) & 0x3f;
87  break;
88  }
89  ++it;
90  return cp;
91 }
92 
93 template <typename octet_iterator>
94 [[nodiscard]] uint32_t peek_next(octet_iterator it)
95 {
96  return next(it);
97 }
98 
99 template <typename octet_iterator>
100 uint32_t prior(octet_iterator& it)
101 {
102  while (internal::is_trail(*(--it))) ;
103  auto temp = it;
104  return next(temp);
105 }
106 
107 template <typename octet_iterator, typename distance_type>
108 void advance(octet_iterator& it, distance_type n)
109 {
110  for (distance_type i = 0; i < n; ++i) {
111  unchecked::next(it);
112  }
113 }
114 
115 template <typename octet_iterator>
116 [[nodiscard]] auto distance(octet_iterator first, octet_iterator last)
117 {
118  typename std::iterator_traits<octet_iterator>::difference_type dist;
119  for (dist = 0; first < last; ++dist) {
120  unchecked::next(first);
121  }
122  return dist;
123 }
124 
125 template <typename u16bit_iterator, typename octet_iterator>
126 octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end,
127  octet_iterator result)
128 {
129  while (start != end) {
130  uint32_t cp = *start++;
131  // Take care of surrogate pairs first
132  if (internal::is_surrogate(cp)) {
133  uint32_t trail_surrogate = *start++;
134  cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
135  }
136  result = append(cp, result);
137  }
138  return result;
139 }
140 
141 template <typename u16bit_iterator, typename octet_iterator>
142 u16bit_iterator utf8to16(octet_iterator start, octet_iterator end,
143  u16bit_iterator result)
144 {
145  while (start != end) {
146  uint32_t cp = next(start);
147  if (cp > 0xffff) {
148  // make a surrogate pair
149  *result++ = (cp >> 10) + internal::LEAD_OFFSET;
150  *result++ = (cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN;
151  } else {
152  *result++ = cp;
153  }
154  }
155  return result;
156 }
157 
158 template <typename octet_iterator, typename u32bit_iterator>
159 octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end,
160  octet_iterator result)
161 {
162  while (start != end) {
163  result = append(*start++, result);
164  }
165  return result;
166 }
167 
168 template <typename octet_iterator, typename u32bit_iterator>
169 u32bit_iterator utf8to32(octet_iterator start, octet_iterator end,
170  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 : public std::iterator<std::bidirectional_iterator_tag, uint32_t>
181 {
182  octet_iterator it;
183 public:
184  iterator() = default;
185  explicit iterator(const octet_iterator& octet_it)
186  : it(octet_it) {}
187  // the default "big three" are OK
188  [[nodiscard]] octet_iterator base() const { return it; }
189  [[nodiscard]] uint32_t operator*() const
190  {
191  octet_iterator temp = it;
192  return next(temp);
193  }
194  [[nodiscard]] bool operator==(const iterator& rhs) const
195  {
196  return it == rhs.it;
197  }
198  [[nodiscard]] bool operator!=(const iterator& rhs) const
199  {
200  return !(operator==(rhs));
201  }
203  {
205  return *this;
206  }
208  {
209  auto temp = *this;
211  return temp;
212  }
214  {
215  prior(it);
216  return *this;
217  }
219  {
220  auto temp = *this;
221  prior(it);
222  return temp;
223  }
224 };
225 
226 // convenience functions
227 [[nodiscard]] inline size_t size(std::string_view utf8)
228 {
229  return utf8::unchecked::distance(begin(utf8), end(utf8));
230 }
231 [[nodiscard]] inline std::string_view substr(std::string_view utf8, std::string_view::size_type first = 0,
232  std::string_view::size_type len = std::string_view::npos)
233 {
234  auto b = begin(utf8);
235  utf8::unchecked::advance(b, first);
236  std::string_view::const_iterator e;
237  if (len != std::string_view::npos) {
238  e = b;
239  while (len && (e != end(utf8))) {
240  unchecked::next(e); --len;
241  }
242  } else {
243  e = end(utf8);
244  }
245  return std::string_view(&*b, e - b);
246 }
247 
248 } // namespace utf8::unchecked
249 
250 #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
std::string_view substr(std::string_view utf8, std::string_view::size_type first=0, std::string_view::size_type len=std::string_view::npos)
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
size_t size(std::string_view utf8)
auto distance(octet_iterator first, octet_iterator last)
bool operator==(const iterator &rhs) const
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
void advance(octet_iterator &it, distance_type n)
unsigned sequence_length(uint8_t lead)
Definition: utf8_core.hh:72
const uint16_t LEAD_OFFSET
Definition: utf8_core.hh:50
uint32_t prior(octet_iterator &it)