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 "xrange.hh"
35 #include <string_view>
36 
37 namespace utf8::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 [[nodiscard]] 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 unchecked::next(temp);
106 }
107 
108 template<typename octet_iterator, typename distance_type>
109 void advance(octet_iterator& it, distance_type n)
110 {
111  repeat(n, [&] { unchecked::next(it); });
112 }
113 
114 template<typename octet_iterator>
115 [[nodiscard]] auto distance(octet_iterator first, octet_iterator last)
116 {
117  typename std::iterator_traits<octet_iterator>::difference_type dist = 0;
118  while (first < last) {
119  ++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
181 {
182  octet_iterator it;
183 public:
184  using iterator_category = std::bidirectional_iterator_tag;
185  using difference_type = ptrdiff_t;
186  using value_type = uint32_t;
187  using pointer = uint32_t*;
188  using reference = uint32_t&;
189 
190  iterator() = default;
191  explicit iterator(const octet_iterator& octet_it)
192  : it(octet_it) {}
193  // the default "big three" are OK
194  [[nodiscard]] octet_iterator base() const { return it; }
195  [[nodiscard]] uint32_t operator*() const
196  {
197  octet_iterator temp = it;
198  return next(temp);
199  }
200  [[nodiscard]] bool operator==(const iterator& rhs) const
201  {
202  return it == rhs.it;
203  }
204  [[nodiscard]] bool operator!=(const iterator& rhs) const
205  {
206  return !(operator==(rhs));
207  }
209  {
211  return *this;
212  }
214  {
215  auto temp = *this;
217  return temp;
218  }
220  {
221  prior(it);
222  return *this;
223  }
225  {
226  auto temp = *this;
227  prior(it);
228  return temp;
229  }
230 };
231 
232 // convenience functions
233 [[nodiscard]] inline size_t size(std::string_view utf8)
234 {
236 }
237 [[nodiscard]] inline std::string_view substr(std::string_view utf8, std::string_view::size_type first = 0,
238  std::string_view::size_type len = std::string_view::npos)
239 {
240  auto b = begin(utf8);
241  utf8::unchecked::advance(b, first);
242  std::string_view::const_iterator e;
243  if (len != std::string_view::npos) {
244  e = b;
245  while (len && (e != end(utf8))) {
246  unchecked::next(e); --len;
247  }
248  } else {
249  e = end(utf8);
250  }
251  return std::string_view(&*b, e - b);
252 }
253 
254 } // namespace utf8::unchecked
255 
256 #endif
utf8
Definition: utf8_checked.hh:37
utf8::internal::LEAD_OFFSET
const uint16_t LEAD_OFFSET
Definition: utf8_core.hh:51
utf8::unchecked::next
uint32_t next(octet_iterator &it)
Definition: utf8_unchecked.hh:65
utf8::unchecked::iterator::pointer
uint32_t * pointer
Definition: utf8_unchecked.hh:187
utf8::unchecked::size
size_t size(std::string_view utf8)
Definition: utf8_unchecked.hh:233
utf8::unchecked::iterator::operator++
iterator operator++(int)
Definition: utf8_unchecked.hh:213
utf8::internal::sequence_length
unsigned sequence_length(uint8_t lead)
Definition: utf8_core.hh:73
utf8::unchecked::substr
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)
Definition: utf8_unchecked.hh:237
utf8::internal::is_trail
bool is_trail(uint8_t oc)
Definition: utf8_core.hh:57
utf8::unchecked
Definition: utf8_unchecked.hh:37
utf8::unchecked::iterator::base
octet_iterator base() const
Definition: utf8_unchecked.hh:194
end
auto end(const zstring_view &x)
Definition: zstring_view.hh:83
utf8::unchecked::distance
auto distance(octet_iterator first, octet_iterator last)
Definition: utf8_unchecked.hh:115
utf8::unchecked::advance
void advance(octet_iterator &it, distance_type n)
Definition: utf8_unchecked.hh:109
utf8::unchecked::iterator::iterator
iterator(const octet_iterator &octet_it)
Definition: utf8_unchecked.hh:191
utf8::unchecked::utf16to8
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
Definition: utf8_unchecked.hh:126
utf8::unchecked::iterator::operator--
iterator operator--(int)
Definition: utf8_unchecked.hh:224
utf8::unchecked::iterator::operator++
iterator & operator++()
Definition: utf8_unchecked.hh:208
repeat
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.
Definition: xrange.hh:170
utf8::unchecked::utf8to32
u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result)
Definition: utf8_unchecked.hh:169
utf8::unchecked::iterator::reference
uint32_t & reference
Definition: utf8_unchecked.hh:188
utf8::unchecked::peek_next
uint32_t peek_next(octet_iterator it)
Definition: utf8_unchecked.hh:95
utf8::unchecked::iterator::operator*
uint32_t operator*() const
Definition: utf8_unchecked.hh:195
utf8::unchecked::iterator::difference_type
ptrdiff_t difference_type
Definition: utf8_unchecked.hh:185
utf8::internal::SURROGATE_OFFSET
const uint32_t SURROGATE_OFFSET
Definition: utf8_core.hh:52
utf8::unchecked::iterator::operator--
iterator & operator--()
Definition: utf8_unchecked.hh:219
begin
auto begin(const zstring_view &x)
Definition: zstring_view.hh:82
utf8::unchecked::iterator::operator==
bool operator==(const iterator &rhs) const
Definition: utf8_unchecked.hh:200
utf8::unchecked::iterator::operator!=
bool operator!=(const iterator &rhs) const
Definition: utf8_unchecked.hh:204
utf8::unchecked::utf32to8
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
Definition: utf8_unchecked.hh:159
utf8::unchecked::iterator::value_type
uint32_t value_type
Definition: utf8_unchecked.hh:186
utf8::internal::TRAIL_SURROGATE_MIN
const uint16_t TRAIL_SURROGATE_MIN
Definition: utf8_core.hh:49
utf8::unchecked::append
octet_iterator append(uint32_t cp, octet_iterator result)
Definition: utf8_unchecked.hh:40
utf8_core.hh
utf8::internal::is_surrogate
bool is_surrogate(uint16_t cp)
Definition: utf8_core.hh:62
utf8::unchecked::iterator::iterator_category
std::bidirectional_iterator_tag iterator_category
Definition: utf8_unchecked.hh:184
utf8::unchecked::iterator
Definition: utf8_unchecked.hh:181
utf8::unchecked::utf8to16
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
Definition: utf8_unchecked.hh:142
utf8::unchecked::iterator::iterator
iterator()=default
xrange.hh
utf8::unchecked::prior
uint32_t prior(octet_iterator &it)
Definition: utf8_unchecked.hh:101