@@ -39,28 +39,28 @@ namespace utf8
39
39
40
40
// Exceptions that may be thrown from the library functions.
41
41
class invalid_code_point : public exception {
42
- uint32_t cp;
42
+ utfchar32_t cp;
43
43
public:
44
- invalid_code_point (uint32_t codepoint) : cp(codepoint) {}
44
+ invalid_code_point (utfchar32_t codepoint) : cp(codepoint) {}
45
45
virtual const char * what () const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return " Invalid code point" ; }
46
- uint32_t code_point () const {return cp;}
46
+ utfchar32_t code_point () const {return cp;}
47
47
};
48
48
49
49
class invalid_utf8 : public exception {
50
- uint8_t u8;
50
+ utfchar8_t u8;
51
51
public:
52
- invalid_utf8 (uint8_t u) : u8(u) {}
53
- invalid_utf8 (char c) : u8(static_cast <uint8_t >(c)) {}
52
+ invalid_utf8 (utfchar8_t u) : u8(u) {}
53
+ invalid_utf8 (char c) : u8(static_cast <utfchar8_t >(c)) {}
54
54
virtual const char * what () const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return " Invalid UTF-8" ; }
55
- uint8_t utf8_octet () const {return u8;}
55
+ utfchar8_t utf8_octet () const {return u8;}
56
56
};
57
57
58
58
class invalid_utf16 : public exception {
59
- uint16_t u16;
59
+ utfchar16_t u16;
60
60
public:
61
- invalid_utf16 (uint16_t u) : u16(u) {}
61
+ invalid_utf16 (utfchar16_t u) : u16(u) {}
62
62
virtual const char * what () const UTF_CPP_NOEXCEPT UTF_CPP_OVERRIDE { return " Invalid UTF-16" ; }
63
- uint16_t utf16_word () const {return u16;}
63
+ utfchar16_t utf16_word () const {return u16;}
64
64
};
65
65
66
66
class not_enough_room : public exception {
@@ -71,16 +71,30 @@ namespace utf8
71
71
// / The library API - functions intended to be called by the users
72
72
73
73
template <typename octet_iterator>
74
- octet_iterator append (uint32_t cp, octet_iterator result)
74
+ octet_iterator append (utfchar32_t cp, octet_iterator result)
75
75
{
76
76
if (!utf8::internal::is_code_point_valid (cp))
77
77
throw invalid_code_point (cp);
78
78
79
79
return internal::append (cp, result);
80
80
}
81
81
82
+ inline void append (utfchar32_t cp, std::string& s)
83
+ {
84
+ append (cp, std::back_inserter (s));
85
+ }
86
+
87
+ template <typename word_iterator>
88
+ word_iterator append16 (utfchar32_t cp, word_iterator result)
89
+ {
90
+ if (!utf8::internal::is_code_point_valid (cp))
91
+ throw invalid_code_point (cp);
92
+
93
+ return internal::append16 (cp, result);
94
+ }
95
+
82
96
template <typename octet_iterator, typename output_iterator>
83
- output_iterator replace_invalid (octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
97
+ output_iterator replace_invalid (octet_iterator start, octet_iterator end, output_iterator out, utfchar32_t replacement)
84
98
{
85
99
while (start != end) {
86
100
octet_iterator sequence_start = start;
@@ -115,14 +129,28 @@ namespace utf8
115
129
template <typename octet_iterator, typename output_iterator>
116
130
inline output_iterator replace_invalid (octet_iterator start, octet_iterator end, output_iterator out)
117
131
{
118
- static const uint32_t replacement_marker = utf8::internal::mask16 (0xfffd );
132
+ static const utfchar32_t replacement_marker = utf8::internal::mask16 (0xfffd );
119
133
return utf8::replace_invalid (start, end, out, replacement_marker);
120
134
}
121
135
136
+ inline std::string replace_invalid (const std::string& s, utfchar32_t replacement)
137
+ {
138
+ std::string result;
139
+ replace_invalid (s.begin (), s.end (), std::back_inserter (result), replacement);
140
+ return result;
141
+ }
142
+
143
+ inline std::string replace_invalid (const std::string& s)
144
+ {
145
+ std::string result;
146
+ replace_invalid (s.begin (), s.end (), std::back_inserter (result));
147
+ return result;
148
+ }
149
+
122
150
template <typename octet_iterator>
123
- uint32_t next (octet_iterator& it, octet_iterator end)
151
+ utfchar32_t next (octet_iterator& it, octet_iterator end)
124
152
{
125
- uint32_t cp = 0 ;
153
+ utfchar32_t cp = 0 ;
126
154
internal::utf_error err_code = utf8::internal::validate_next (it, end, cp);
127
155
switch (err_code) {
128
156
case internal::UTF8_OK :
@@ -132,21 +160,31 @@ namespace utf8
132
160
case internal::INVALID_LEAD :
133
161
case internal::INCOMPLETE_SEQUENCE :
134
162
case internal::OVERLONG_SEQUENCE :
135
- throw invalid_utf8 (static_cast <uint8_t >(*it));
163
+ throw invalid_utf8 (static_cast <utfchar8_t >(*it));
136
164
case internal::INVALID_CODE_POINT :
137
165
throw invalid_code_point (cp);
138
166
}
139
167
return cp;
140
168
}
141
169
170
+ template <typename word_iterator>
171
+ utfchar32_t next16 (word_iterator& it, word_iterator end)
172
+ {
173
+ utfchar32_t cp = 0 ;
174
+ internal::utf_error err_code = utf8::internal::validate_next16 (it, end, cp);
175
+ if (err_code == internal::NOT_ENOUGH_ROOM)
176
+ throw not_enough_room ();
177
+ return cp;
178
+ }
179
+
142
180
template <typename octet_iterator>
143
- uint32_t peek_next (octet_iterator it, octet_iterator end)
181
+ utfchar32_t peek_next (octet_iterator it, octet_iterator end)
144
182
{
145
183
return utf8::next (it, end);
146
184
}
147
185
148
186
template <typename octet_iterator>
149
- uint32_t prior (octet_iterator& it, octet_iterator start)
187
+ utfchar32_t prior (octet_iterator& it, octet_iterator start)
150
188
{
151
189
// can't do much if it == start
152
190
if (it == start)
@@ -189,23 +227,23 @@ namespace utf8
189
227
octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
190
228
{
191
229
while (start != end) {
192
- uint32_t cp = utf8::internal::mask16 (*start++);
230
+ utfchar32_t cp = utf8::internal::mask16 (*start++);
193
231
// Take care of surrogate pairs first
194
232
if (utf8::internal::is_lead_surrogate (cp)) {
195
233
if (start != end) {
196
- uint32_t trail_surrogate = utf8::internal::mask16 (*start++);
234
+ const utfchar32_t trail_surrogate = utf8::internal::mask16 (*start++);
197
235
if (utf8::internal::is_trail_surrogate (trail_surrogate))
198
236
cp = (cp << 10 ) + trail_surrogate + internal::SURROGATE_OFFSET;
199
237
else
200
- throw invalid_utf16 (static_cast <uint16_t >(trail_surrogate));
238
+ throw invalid_utf16 (static_cast <utfchar16_t >(trail_surrogate));
201
239
}
202
240
else
203
- throw invalid_utf16 (static_cast <uint16_t >(cp));
241
+ throw invalid_utf16 (static_cast <utfchar16_t >(cp));
204
242
205
243
}
206
244
// Lone trail surrogate
207
245
else if (utf8::internal::is_trail_surrogate (cp))
208
- throw invalid_utf16 (static_cast <uint16_t >(cp));
246
+ throw invalid_utf16 (static_cast <utfchar16_t >(cp));
209
247
210
248
result = utf8::append (cp, result);
211
249
}
@@ -216,13 +254,13 @@ namespace utf8
216
254
u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
217
255
{
218
256
while (start < end) {
219
- uint32_t cp = utf8::next (start, end);
257
+ const utfchar32_t cp = utf8::next (start, end);
220
258
if (cp > 0xffff ) { // make a surrogate pair
221
- *result++ = static_cast <uint16_t >((cp >> 10 ) + internal::LEAD_OFFSET);
222
- *result++ = static_cast <uint16_t >((cp & 0x3ff ) + internal::TRAIL_SURROGATE_MIN);
259
+ *result++ = static_cast <utfchar16_t >((cp >> 10 ) + internal::LEAD_OFFSET);
260
+ *result++ = static_cast <utfchar16_t >((cp & 0x3ff ) + internal::TRAIL_SURROGATE_MIN);
223
261
}
224
262
else
225
- *result++ = static_cast <uint16_t >(cp);
263
+ *result++ = static_cast <utfchar16_t >(cp);
226
264
}
227
265
return result;
228
266
}
@@ -252,9 +290,9 @@ namespace utf8
252
290
octet_iterator range_start;
253
291
octet_iterator range_end;
254
292
public:
255
- typedef uint32_t value_type;
256
- typedef uint32_t * pointer;
257
- typedef uint32_t & reference;
293
+ typedef utfchar32_t value_type;
294
+ typedef utfchar32_t * pointer;
295
+ typedef utfchar32_t & reference;
258
296
typedef std::ptrdiff_t difference_type;
259
297
typedef std::bidirectional_iterator_tag iterator_category;
260
298
iterator () {}
@@ -268,7 +306,7 @@ namespace utf8
268
306
}
269
307
// the default "big three" are OK
270
308
octet_iterator base () const { return it; }
271
- uint32_t operator * () const
309
+ utfchar32_t operator * () const
272
310
{
273
311
octet_iterator temp = it;
274
312
return utf8::next (temp, range_end);
@@ -309,7 +347,9 @@ namespace utf8
309
347
310
348
} // namespace utf8
311
349
312
- #if UTF_CPP_CPLUSPLUS >= 201703L // C++ 17 or later
350
+ #if UTF_CPP_CPLUSPLUS >= 202002L // C++ 20 or later
351
+ #include " cpp20.h"
352
+ #elif UTF_CPP_CPLUSPLUS >= 201703L // C++ 17 or later
313
353
#include " cpp17.h"
314
354
#elif UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later
315
355
#include " cpp11.h"
0 commit comments