@@ -39,28 +39,28 @@ namespace utf8
3939
4040 // Exceptions that may be thrown from the library functions.
4141 class invalid_code_point : public exception {
42- uint32_t cp;
42+ utfchar32_t cp;
4343 public:
44- invalid_code_point (uint32_t codepoint) : cp(codepoint) {}
44+ invalid_code_point (utfchar32_t codepoint) : cp(codepoint) {}
4545 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;}
4747 };
4848
4949 class invalid_utf8 : public exception {
50- uint8_t u8 ;
50+ utfchar8_t u8 ;
5151 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)) {}
5454 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 ;}
5656 };
5757
5858 class invalid_utf16 : public exception {
59- uint16_t u16 ;
59+ utfchar16_t u16 ;
6060 public:
61- invalid_utf16 (uint16_t u) : u16 (u) {}
61+ invalid_utf16 (utfchar16_t u) : u16 (u) {}
6262 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 ;}
6464 };
6565
6666 class not_enough_room : public exception {
@@ -71,16 +71,30 @@ namespace utf8
7171 // / The library API - functions intended to be called by the users
7272
7373 template <typename octet_iterator>
74- octet_iterator append (uint32_t cp, octet_iterator result)
74+ octet_iterator append (utfchar32_t cp, octet_iterator result)
7575 {
7676 if (!utf8::internal::is_code_point_valid (cp))
7777 throw invalid_code_point (cp);
7878
7979 return internal::append (cp, result);
8080 }
8181
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+
8296 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)
8498 {
8599 while (start != end) {
86100 octet_iterator sequence_start = start;
@@ -115,14 +129,28 @@ namespace utf8
115129 template <typename octet_iterator, typename output_iterator>
116130 inline output_iterator replace_invalid (octet_iterator start, octet_iterator end, output_iterator out)
117131 {
118- static const uint32_t replacement_marker = utf8::internal::mask16 (0xfffd );
132+ static const utfchar32_t replacement_marker = static_cast < utfchar32_t >( utf8::internal::mask16 (0xfffd ) );
119133 return utf8::replace_invalid (start, end, out, replacement_marker);
120134 }
121135
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+
122150 template <typename octet_iterator>
123- uint32_t next (octet_iterator& it, octet_iterator end)
151+ utfchar32_t next (octet_iterator& it, octet_iterator end)
124152 {
125- uint32_t cp = 0 ;
153+ utfchar32_t cp = 0 ;
126154 internal::utf_error err_code = utf8::internal::validate_next (it, end, cp);
127155 switch (err_code) {
128156 case internal::UTF8_OK :
@@ -132,21 +160,31 @@ namespace utf8
132160 case internal::INVALID_LEAD :
133161 case internal::INCOMPLETE_SEQUENCE :
134162 case internal::OVERLONG_SEQUENCE :
135- throw invalid_utf8 (static_cast <uint8_t >(*it));
163+ throw invalid_utf8 (static_cast <utfchar8_t >(*it));
136164 case internal::INVALID_CODE_POINT :
137165 throw invalid_code_point (cp);
138166 }
139167 return cp;
140168 }
141169
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+
142180 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)
144182 {
145183 return utf8::next (it, end);
146184 }
147185
148186 template <typename octet_iterator>
149- uint32_t prior (octet_iterator& it, octet_iterator start)
187+ utfchar32_t prior (octet_iterator& it, octet_iterator start)
150188 {
151189 // can't do much if it == start
152190 if (it == start)
@@ -189,23 +227,23 @@ namespace utf8
189227 octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
190228 {
191229 while (start != end) {
192- uint32_t cp = utf8::internal::mask16 (*start++);
230+ utfchar32_t cp = static_cast < utfchar32_t >( utf8::internal::mask16 (*start++) );
193231 // Take care of surrogate pairs first
194232 if (utf8::internal::is_lead_surrogate (cp)) {
195233 if (start != end) {
196- uint32_t trail_surrogate = utf8::internal::mask16 (*start++);
234+ const utfchar32_t trail_surrogate = static_cast < utfchar32_t >( utf8::internal::mask16 (*start++) );
197235 if (utf8::internal::is_trail_surrogate (trail_surrogate))
198236 cp = (cp << 10 ) + trail_surrogate + internal::SURROGATE_OFFSET;
199237 else
200- throw invalid_utf16 (static_cast <uint16_t >(trail_surrogate));
238+ throw invalid_utf16 (static_cast <utfchar16_t >(trail_surrogate));
201239 }
202240 else
203- throw invalid_utf16 (static_cast <uint16_t >(cp));
241+ throw invalid_utf16 (static_cast <utfchar16_t >(cp));
204242
205243 }
206244 // Lone trail surrogate
207245 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));
209247
210248 result = utf8::append (cp, result);
211249 }
@@ -216,13 +254,13 @@ namespace utf8
216254 u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
217255 {
218256 while (start < end) {
219- uint32_t cp = utf8::next (start, end);
257+ const utfchar32_t cp = utf8::next (start, end);
220258 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);
223261 }
224262 else
225- *result++ = static_cast <uint16_t >(cp);
263+ *result++ = static_cast <utfchar16_t >(cp);
226264 }
227265 return result;
228266 }
@@ -252,9 +290,9 @@ namespace utf8
252290 octet_iterator range_start;
253291 octet_iterator range_end;
254292 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;
258296 typedef std::ptrdiff_t difference_type;
259297 typedef std::bidirectional_iterator_tag iterator_category;
260298 iterator () {}
@@ -268,7 +306,7 @@ namespace utf8
268306 }
269307 // the default "big three" are OK
270308 octet_iterator base () const { return it; }
271- uint32_t operator * () const
309+ utfchar32_t operator * () const
272310 {
273311 octet_iterator temp = it;
274312 return utf8::next (temp, range_end);
@@ -309,7 +347,9 @@ namespace utf8
309347
310348} // namespace utf8
311349
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
313353#include " cpp17.h"
314354#elif UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later
315355#include " cpp11.h"
0 commit comments