Skip to content

Commit 4ae9a81

Browse files
committed
const CharT* -> const uint8_t*
1 parent ffc3ac4 commit 4ae9a81

1 file changed

Lines changed: 63 additions & 69 deletions

File tree

include/jsoncons/utility/unicode_traits.hpp

Lines changed: 63 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -299,34 +299,36 @@ namespace unicode_traits {
299299

300300
// utf8
301301

302-
inline conv_errc is_legal_utf8(const uint8_t* first, std::size_t length) noexcept
302+
template <typename CharT>
303+
typename std::enable_if<ext_traits::is_char8<CharT>::value, conv_errc>::type
304+
is_legal_utf8(const CharT* bytes, std::size_t length)
303305
{
306+
uint8_t a;
307+
const uint8_t* first = reinterpret_cast<const uint8_t*>(bytes);
304308
const uint8_t* last = first+length;
305-
uint8_t byte;
306-
307309
switch (length) {
308310
default:
309311
return conv_errc::over_long_utf8_sequence;
310312
case 4:
311-
if (((byte = (*--last))& 0xC0) != 0x80)
313+
if (((a = (*--last))& 0xC0) != 0x80)
312314
return conv_errc::expected_continuation_byte;
313315
JSONCONS_FALLTHROUGH;
314316
case 3:
315-
if (((byte = (*--last))& 0xC0) != 0x80)
317+
if (((a = (*--last))& 0xC0) != 0x80)
316318
return conv_errc::expected_continuation_byte;
317319
JSONCONS_FALLTHROUGH;
318320
case 2:
319-
if (((byte = (*--last))& 0xC0) != 0x80)
321+
if (((a = (*--last))& 0xC0) != 0x80)
320322
return conv_errc::expected_continuation_byte;
321323

322324
switch (static_cast<uint8_t>(*first))
323325
{
324326
// no fall-through in this inner switch
325-
case 0xE0: if (byte < 0xA0) return conv_errc::source_illegal; break;
326-
case 0xED: if (byte > 0x9F) return conv_errc::source_illegal; break;
327-
case 0xF0: if (byte < 0x90) return conv_errc::source_illegal; break;
328-
case 0xF4: if (byte > 0x8F) return conv_errc::source_illegal; break;
329-
default: if (byte < 0x80) return conv_errc::source_illegal;
327+
case 0xE0: if (a < 0xA0) return conv_errc::source_illegal; break;
328+
case 0xED: if (a > 0x9F) return conv_errc::source_illegal; break;
329+
case 0xF0: if (a < 0x90) return conv_errc::source_illegal; break;
330+
case 0xF4: if (a > 0x8F) return conv_errc::source_illegal; break;
331+
default: if (a < 0x80) return conv_errc::source_illegal;
330332
}
331333

332334
JSONCONS_FALLTHROUGH;
@@ -380,56 +382,56 @@ namespace unicode_traits {
380382
template <typename CharT,typename CodepointT>
381383
typename std::enable_if<ext_traits::is_char8<CharT>::value && ext_traits::is_char32<CodepointT>::value,
382384
convert_result<CharT>>::type
383-
to_codepoint(const CharT* begin, const CharT* end,
385+
to_codepoint(const CharT* first, const CharT* last,
384386
CodepointT& ch,
385387
conv_flags flags = conv_flags::strict) noexcept
386388
{
387-
const uint8_t* first = reinterpret_cast<const uint8_t*>(begin);
388-
const uint8_t* last = reinterpret_cast<const uint8_t*>(end);
389+
const uint8_t* it = reinterpret_cast<const uint8_t*>(first);
390+
const uint8_t* end = reinterpret_cast<const uint8_t*>(last);
389391

390392
ch = 0;
391-
if (first >= last)
393+
if (it >= end)
392394
{
393-
return convert_result<CharT>{begin, conv_errc::source_exhausted};
395+
return convert_result<CharT>{reinterpret_cast<const CharT*>(it), conv_errc::source_exhausted};
394396
}
395397
conv_errc result = conv_errc();
396398

397-
uint16_t extra_bytes_to_read = trailing_bytes_for_utf8[*first];
398-
if (extra_bytes_to_read >= last - first)
399+
uint16_t extra_bytes_to_read = trailing_bytes_for_utf8[static_cast<uint8_t>(*it)];
400+
if (extra_bytes_to_read >= end - it)
399401
{
400402
result = conv_errc::source_exhausted;
401-
return convert_result<CharT>{begin, result};
403+
return convert_result<CharT>{reinterpret_cast<const CharT*>(it), result};
402404
}
403405
// Do this check whether lenient or strict
404-
if ((result=is_legal_utf8(first, extra_bytes_to_read+1)) != conv_errc())
406+
if ((result=is_legal_utf8(it, extra_bytes_to_read+1)) != conv_errc())
405407
{
406-
return convert_result<CharT>{begin, result};
408+
return convert_result<CharT>{reinterpret_cast<const CharT*>(it), result};
407409
}
408410
// The cases all fall through. See "Note A" below.
409411
switch (extra_bytes_to_read)
410412
{
411413
case 5:
412-
ch += (*first++);
414+
ch += *it++;
413415
ch <<= 6;
414416
JSONCONS_FALLTHROUGH;
415417
case 4:
416-
ch += (*first++);
418+
ch += *it++;
417419
ch <<= 6;
418420
JSONCONS_FALLTHROUGH;
419421
case 3:
420-
ch += (*first++);
422+
ch += *it++;
421423
ch <<= 6;
422424
JSONCONS_FALLTHROUGH;
423425
case 2:
424-
ch += (*first++);
426+
ch += *it++;
425427
ch <<= 6;
426428
JSONCONS_FALLTHROUGH;
427429
case 1:
428-
ch += (*first++);
430+
ch += *it++;
429431
ch <<= 6;
430432
JSONCONS_FALLTHROUGH;
431433
case 0:
432-
ch += (*first++);
434+
ch += *it++;
433435
break;
434436
}
435437
ch -= offsets_from_utf8[extra_bytes_to_read];
@@ -443,9 +445,9 @@ namespace unicode_traits {
443445
{
444446
if (flags == conv_flags::strict)
445447
{
446-
first -= (extra_bytes_to_read+1); // return to the illegal value itself
448+
it -= (extra_bytes_to_read+1); // return to the illegal value itself
447449
result = conv_errc::source_illegal;
448-
return convert_result<CharT>{begin, result};
450+
return convert_result<CharT>{reinterpret_cast<const CharT*>(it), result};
449451
}
450452
else
451453
{
@@ -459,7 +461,7 @@ namespace unicode_traits {
459461
ch = replacement_char;
460462
}
461463

462-
return convert_result<CharT>{begin,result} ;
464+
return convert_result<CharT>{reinterpret_cast<const CharT*>(it),result} ;
463465
}
464466

465467
template <typename CharT,typename CodepointT>
@@ -558,17 +560,15 @@ namespace unicode_traits {
558560
&& ext_traits::is_back_insertable<Container>::value
559561
&& ext_traits::is_char8<typename Container::value_type>::value,
560562
convert_result<CharT>>::type
561-
convert(const CharT* bytes, std::size_t length, Container& target, conv_flags flags=conv_flags::strict)
563+
convert(const CharT* data, std::size_t length, Container& target, conv_flags flags=conv_flags::strict)
562564
{
563565
(void)flags;
564566

565-
const uint8_t* data = reinterpret_cast<const uint8_t*>(bytes);
566-
const uint8_t* last = data + length;
567-
568567
conv_errc result = conv_errc();
568+
const CharT* last = data + length;
569569
while (data != last)
570570
{
571-
std::size_t len = trailing_bytes_for_utf8[*data] + 1;
571+
std::size_t len = trailing_bytes_for_utf8[static_cast<uint8_t>(*data)] + 1;
572572
if (len > (std::size_t)(last - data))
573573
{
574574
return convert_result<CharT>{data, conv_errc::source_exhausted};
@@ -579,13 +579,13 @@ namespace unicode_traits {
579579
}
580580

581581
switch (len) {
582-
case 4: target.push_back(*data++);
582+
case 4: target.push_back(static_cast<uint8_t>(*data++));
583583
JSONCONS_FALLTHROUGH;
584-
case 3: target.push_back(*data++);
584+
case 3: target.push_back(static_cast<uint8_t>(*data++));
585585
JSONCONS_FALLTHROUGH;
586-
case 2: target.push_back(*data++);
586+
case 2: target.push_back(static_cast<uint8_t>(*data++));
587587
JSONCONS_FALLTHROUGH;
588-
case 1: target.push_back(*data++);
588+
case 1: target.push_back(static_cast<uint8_t>(*data++));
589589
}
590590
}
591591
return convert_result<CharT>{data,result} ;
@@ -596,18 +596,16 @@ namespace unicode_traits {
596596
&& ext_traits::is_back_insertable<Container>::value
597597
&& ext_traits::is_char16<typename Container::value_type>::value,
598598
convert_result<CharT>>::type
599-
convert(const CharT* bytes, std::size_t length,
599+
convert(const CharT* data, std::size_t length,
600600
Container& target,
601601
conv_flags flags = conv_flags::strict)
602602
{
603-
const uint8_t* data = reinterpret_cast<const uint8_t*>(bytes);
604-
const uint8_t* last = data + length;
605-
606603
conv_errc result = conv_errc();
607604

605+
const CharT* last = data + length;
608606
while (data != last)
609607
{
610-
uint16_t extra_bytes_to_read = trailing_bytes_for_utf8[*data];
608+
uint16_t extra_bytes_to_read = trailing_bytes_for_utf8[static_cast<uint8_t>(*data)];
611609
if (extra_bytes_to_read >= last - data)
612610
{
613611
result = conv_errc::source_exhausted;
@@ -623,17 +621,17 @@ namespace unicode_traits {
623621
*/
624622
uint32_t ch = 0;
625623
switch (extra_bytes_to_read) {
626-
case 5: ch += *data++; ch <<= 6; /* remember, illegal UTF-8 */
624+
case 5: ch += static_cast<uint8_t>(*data++); ch <<= 6; /* remember, illegal UTF-8 */
627625
JSONCONS_FALLTHROUGH;
628-
case 4: ch += *data++; ch <<= 6; /* remember, illegal UTF-8 */
626+
case 4: ch += static_cast<uint8_t>(*data++); ch <<= 6; /* remember, illegal UTF-8 */
629627
JSONCONS_FALLTHROUGH;
630-
case 3: ch += *data++; ch <<= 6;
628+
case 3: ch += static_cast<uint8_t>(*data++); ch <<= 6;
631629
JSONCONS_FALLTHROUGH;
632-
case 2: ch += *data++; ch <<= 6;
630+
case 2: ch += static_cast<uint8_t>(*data++); ch <<= 6;
633631
JSONCONS_FALLTHROUGH;
634-
case 1: ch += *data++; ch <<= 6;
632+
case 1: ch += static_cast<uint8_t>(*data++); ch <<= 6;
635633
JSONCONS_FALLTHROUGH;
636-
case 0: ch += *data++;
634+
case 0: ch += static_cast<uint8_t>(*data++);
637635
break;
638636
}
639637
ch -= offsets_from_utf8[extra_bytes_to_read];
@@ -675,19 +673,17 @@ namespace unicode_traits {
675673
&& ext_traits::is_back_insertable<Container>::value
676674
&& ext_traits::is_char32<typename Container::value_type>::value,
677675
convert_result<CharT>>::type
678-
convert(const CharT* bytes, std::size_t length,
676+
convert(const CharT* data, std::size_t length,
679677
Container& target,
680678
conv_flags flags = conv_flags::strict)
681679
{
682-
const uint8_t* data = reinterpret_cast<const uint8_t*>(bytes);
683-
const uint8_t* last = data + length;
684-
685680
conv_errc result = conv_errc();
686681

682+
const CharT* last = data + length;
687683
while (data < last)
688684
{
689685
uint32_t ch = 0;
690-
uint16_t extra_bytes_to_read = trailing_bytes_for_utf8[*data];
686+
uint16_t extra_bytes_to_read = trailing_bytes_for_utf8[static_cast<uint8_t>(*data)];
691687
if (extra_bytes_to_read >= last - data)
692688
{
693689
result = conv_errc::source_exhausted;
@@ -704,27 +700,27 @@ namespace unicode_traits {
704700
switch (extra_bytes_to_read)
705701
{
706702
case 5:
707-
ch += *data++;
703+
ch += static_cast<uint8_t>(*data++);
708704
ch <<= 6;
709705
JSONCONS_FALLTHROUGH;
710706
case 4:
711-
ch += *data++;
707+
ch += static_cast<uint8_t>(*data++);
712708
ch <<= 6;
713709
JSONCONS_FALLTHROUGH;
714710
case 3:
715-
ch += *data++;
711+
ch += static_cast<uint8_t>(*data++);
716712
ch <<= 6;
717713
JSONCONS_FALLTHROUGH;
718714
case 2:
719-
ch += *data++;
715+
ch += static_cast<uint8_t>(*data++);
720716
ch <<= 6;
721717
JSONCONS_FALLTHROUGH;
722718
case 1:
723-
ch += *data++;
719+
ch += static_cast<uint8_t>(*data++);
724720
ch <<= 6;
725721
JSONCONS_FALLTHROUGH;
726722
case 0:
727-
ch += *data++;
723+
ch += static_cast<uint8_t>(*data++);
728724
break;
729725
}
730726
ch -= offsets_from_utf8[extra_bytes_to_read];
@@ -1140,26 +1136,24 @@ namespace unicode_traits {
11401136
template <typename CharT>
11411137
typename std::enable_if<ext_traits::is_char8<CharT>::value,
11421138
convert_result<CharT>>::type
1143-
validate(const CharT* bytes, std::size_t length) noexcept
1139+
validate(const CharT* data, std::size_t length) noexcept
11441140
{
1145-
const uint8_t* data = reinterpret_cast<const uint8_t*>(bytes);
1146-
const uint8_t* last = data + length;
1147-
11481141
conv_errc result = conv_errc();
1142+
const CharT* last = data + length;
11491143
while (data != last)
11501144
{
1151-
std::size_t len = static_cast<std::size_t>(trailing_bytes_for_utf8[*data]) + 1;
1145+
std::size_t len = static_cast<std::size_t>(trailing_bytes_for_utf8[static_cast<uint8_t>(*data)]) + 1;
11521146
if (len > (std::size_t)(last - data))
11531147
{
1154-
return convert_result<CharT>{bytes, conv_errc::source_exhausted};
1148+
return convert_result<CharT>{data, conv_errc::source_exhausted};
11551149
}
11561150
if ((result=is_legal_utf8(data, len)) != conv_errc())
11571151
{
1158-
return convert_result<CharT>{bytes,result} ;
1152+
return convert_result<CharT>{data,result} ;
11591153
}
11601154
data += len;
11611155
}
1162-
return convert_result<CharT>{bytes,result} ;
1156+
return convert_result<CharT>{data,result} ;
11631157
}
11641158

11651159
// utf16

0 commit comments

Comments
 (0)