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/*
7Permission is hereby granted, free of charge, to any person or organization
8obtaining a copy of the software and accompanying documentation covered by
9this license (the "Software") to use, reproduce, display, distribute,
10execute, and transmit the Software, and to prepare derivative works of the
11Software, and to permit third-parties to whom the Software is furnished to
12do so, all subject to the following:
13
14The copyright notices in the Software and this entire statement, including
15the above license grant, this restriction and the following disclaimer,
16must be included in all copies of the Software, in whole or in part, and
17all derivative works of the Software, unless such copies or derivative
18works are solely in the form of machine-executable object code generated by
19a source language processor.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27DEALINGS IN THE SOFTWARE.
28*/
29
30#ifndef UTF8_UNCHECKED_HH
31#define UTF8_UNCHECKED_HH
32
33#include "utf8_core.hh"
34#include "narrow.hh"
35#include "xrange.hh"
36#include <string_view>
37
38namespace utf8::unchecked {
39
40template<typename octet_iterator>
41octet_iterator append(uint32_t cp, octet_iterator result)
42{
43 if (cp < 0x80) {
44 // one octet
45 *result++ = narrow_cast<uint8_t>(cp);
46 } else if (cp < 0x800) {
47 // two octets
48 *result++ = narrow_cast<uint8_t>(((cp >> 6) ) | 0xc0);
49 *result++ = narrow_cast<uint8_t>(((cp >> 0) & 0x3f) | 0x80);
50 } else if (cp < 0x10000) {
51 // three octets
52 *result++ = narrow_cast<uint8_t>(((cp >> 12) ) | 0xe0);
53 *result++ = narrow_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
54 *result++ = narrow_cast<uint8_t>(((cp >> 0) & 0x3f) | 0x80);
55 } else {
56 // four octets
57 *result++ = narrow_cast<uint8_t>(((cp >> 18) ) | 0xf0);
58 *result++ = narrow_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
59 *result++ = narrow_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
60 *result++ = narrow_cast<uint8_t>(((cp >> 0) & 0x3f) | 0x80);
61 }
62 return result;
63}
64
65template<typename octet_iterator>
66uint32_t next(octet_iterator& it)
67{
68 uint32_t cp = narrow_cast<uint8_t>(*it);
69 switch (utf8::internal::sequence_length(narrow_cast<uint8_t>(cp))) {
70 case 1:
71 break;
72 case 2:
73 ++it;
74 cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
75 break;
76 case 3:
77 ++it;
78 cp = ((cp << 12) & 0xffff) + ((*it << 6) & 0xfff);
79 ++it;
80 cp += (*it) & 0x3f;
81 break;
82 case 4:
83 ++it;
84 cp = ((cp << 18) & 0x1fffff) + ((*it << 12) & 0x3ffff);
85 ++it;
86 cp += (*it << 6) & 0xfff;
87 ++it;
88 cp += (*it) & 0x3f;
89 break;
90 }
91 ++it;
92 return cp;
93}
94
95template<typename octet_iterator>
96[[nodiscard]] uint32_t peek_next(octet_iterator it)
97{
98 return next(it);
99}
100
101template<typename octet_iterator>
102uint32_t prior(octet_iterator& it)
103{
104 while (internal::is_trail(*(--it))) ;
105 auto temp = it;
106 return unchecked::next(temp);
107}
108
109template<typename octet_iterator, typename distance_type>
110void advance(octet_iterator& it, distance_type n)
111{
112 repeat(n, [&] { unchecked::next(it); });
113}
114
115template<typename octet_iterator>
116[[nodiscard]] auto distance(octet_iterator first, octet_iterator last)
117{
118 typename std::iterator_traits<octet_iterator>::difference_type dist = 0;
119 while (first < last) {
120 ++dist;
121 unchecked::next(first);
122 }
123 return dist;
124}
125
126template<typename u16bit_iterator, typename octet_iterator>
127octet_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
142template<typename u16bit_iterator, typename octet_iterator>
143u16bit_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
159template<typename octet_iterator, typename u32bit_iterator>
160octet_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
169template<typename octet_iterator, typename u32bit_iterator>
170u32bit_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
180template<typename octet_iterator>
182{
183 octet_iterator it;
184public:
185 using iterator_category = std::bidirectional_iterator_tag;
186 using difference_type = ptrdiff_t;
187 using value_type = uint32_t;
188 using pointer = uint32_t*;
189 using reference = uint32_t&;
190
191 iterator() = default;
192 explicit iterator(const octet_iterator& octet_it)
193 : it(octet_it) {}
194 // the default "big three" are OK
195 [[nodiscard]] octet_iterator base() const { return it; }
196 [[nodiscard]] uint32_t operator*() const
197 {
198 octet_iterator temp = it;
199 return next(temp);
200 }
201 [[nodiscard]] bool operator==(const iterator&) const = default;
203 {
204 std::advance(it, internal::sequence_length(*it));
205 return *this;
206 }
208 {
209 auto temp = *this;
210 std::advance(it, internal::sequence_length(*it));
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{
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 {&*b, narrow<std::string_view::size_type>(e - b)};
246}
247
248} // namespace utf8::unchecked
249
250#endif
iterator(const octet_iterator &octet_it)
bool operator==(const iterator &) const =default
std::bidirectional_iterator_tag iterator_category
octet_iterator base() const
constexpr uint32_t SURROGATE_OFFSET
Definition utf8_core.hh:54
constexpr uint16_t TRAIL_SURROGATE_MIN
Definition utf8_core.hh:51
constexpr bool is_trail(uint8_t oc)
Definition utf8_core.hh:59
constexpr uint16_t LEAD_OFFSET
Definition utf8_core.hh:53
constexpr bool is_surrogate(uint32_t cp)
Definition utf8_core.hh:64
constexpr unsigned sequence_length(uint8_t lead)
Definition utf8_core.hh:75
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)
void advance(octet_iterator &it, distance_type n)
size_t size(std::string_view utf8)
auto distance(octet_iterator first, octet_iterator last)
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 append(uint32_t cp, octet_iterator result)
uint32_t prior(octet_iterator &it)
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.
Definition xrange.hh:147
constexpr auto begin(const zstring_view &x)
constexpr auto end(const zstring_view &x)