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 "xrange.hh"
35#include <string_view>
36
37namespace utf8::unchecked {
38
39template<typename octet_iterator>
40octet_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
64template<typename octet_iterator>
65uint32_t next(octet_iterator& it)
66{
67 uint32_t cp = *it;
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
94template<typename octet_iterator>
95[[nodiscard]] uint32_t peek_next(octet_iterator it)
96{
97 return next(it);
98}
99
100template<typename octet_iterator>
101uint32_t prior(octet_iterator& it)
102{
103 while (internal::is_trail(*(--it))) ;
104 auto temp = it;
105 return unchecked::next(temp);
106}
107
108template<typename octet_iterator, typename distance_type>
109void advance(octet_iterator& it, distance_type n)
110{
111 repeat(n, [&] { unchecked::next(it); });
112}
113
114template<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
125template<typename u16bit_iterator, typename octet_iterator>
126octet_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
141template<typename u16bit_iterator, typename octet_iterator>
142u16bit_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
158template<typename octet_iterator, typename u32bit_iterator>
159octet_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
168template<typename octet_iterator, typename u32bit_iterator>
169u32bit_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
179template<typename octet_iterator>
181{
182 octet_iterator it;
183public:
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&) const = default;
202 {
204 return *this;
205 }
207 {
208 auto temp = *this;
210 return temp;
211 }
213 {
214 prior(it);
215 return *this;
216 }
218 {
219 auto temp = *this;
220 prior(it);
221 return temp;
222 }
223};
224
225// convenience functions
226[[nodiscard]] inline size_t size(std::string_view utf8)
227{
229}
230[[nodiscard]] inline std::string_view substr(std::string_view utf8, std::string_view::size_type first = 0,
231 std::string_view::size_type len = std::string_view::npos)
232{
233 auto b = begin(utf8);
234 utf8::unchecked::advance(b, first);
235 std::string_view::const_iterator e;
236 if (len != std::string_view::npos) {
237 e = b;
238 while (len && (e != end(utf8))) {
239 unchecked::next(e); --len;
240 }
241 } else {
242 e = end(utf8);
243 }
244 return std::string_view(&*b, e - b);
245}
246
247} // namespace utf8::unchecked
248
249#endif
iterator(const octet_iterator &octet_it)
bool operator==(const iterator &) const =default
uint32_t operator*() const
std::bidirectional_iterator_tag iterator_category
octet_iterator base() const
constexpr double e
Definition: Math.hh:18
constexpr uint32_t SURROGATE_OFFSET
Definition: utf8_core.hh:52
constexpr uint16_t TRAIL_SURROGATE_MIN
Definition: utf8_core.hh:49
constexpr bool is_trail(uint8_t oc)
Definition: utf8_core.hh:57
constexpr uint16_t LEAD_OFFSET
Definition: utf8_core.hh:51
constexpr bool is_surrogate(uint16_t cp)
Definition: utf8_core.hh:62
constexpr unsigned sequence_length(uint8_t lead)
Definition: utf8_core.hh:73
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:148
constexpr auto begin(const zstring_view &x)
constexpr auto end(const zstring_view &x)