16985 {
16986 const auto byte = static_cast<std::uint8_t>(s[i]);
16987
16988 switch (decode(state, codepoint, byte))
16989 {
16990 case UTF8_ACCEPT:
16991 {
16992 switch (codepoint)
16993 {
16994 case 0x08:
16995 {
16998 break;
16999 }
17000
17001 case 0x09:
17002 {
17005 break;
17006 }
17007
17008 case 0x0A:
17009 {
17012 break;
17013 }
17014
17015 case 0x0C:
17016 {
17019 break;
17020 }
17021
17022 case 0x0D:
17023 {
17026 break;
17027 }
17028
17029 case 0x22:
17030 {
17033 break;
17034 }
17035
17036 case 0x5C:
17037 {
17040 break;
17041 }
17042
17043 default:
17044 {
17045
17046
17047 if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
17048 {
17049 if (codepoint <= 0xFFFF)
17050 {
17051
17052 (std::snprintf)(
string_buffer.data() + bytes, 7,
"\\u%04x",
17053 static_cast<std::uint16_t>(codepoint));
17054 bytes += 6;
17055 }
17056
17057 else
17058 {
17059
17060 (std::snprintf)(
string_buffer.data() + bytes, 13,
"\\u%04x\\u%04x",
17061 static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
17062 static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu)));
17063 bytes += 12;
17064 }
17065 }
17066
17067 else
17068 {
17069
17070
17072 }
17073
17074 break;
17075 }
17076 }
17077
17078
17079
17080
17082 {
17084 bytes = 0;
17085 }
17086
17087
17088 bytes_after_last_accept = bytes;
17089 undumped_chars = 0;
17090 break;
17091 }
17092
17093 case UTF8_REJECT:
17094 {
17096 {
17098 {
17099 std::string sn(9, '\0');
17100
17101 (std::snprintf)(&sn[0], sn.size(), "%.2X", byte);
17102 JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType()));
17103 }
17104
17107 {
17108
17109
17110
17111
17112 if (undumped_chars > 0)
17113 {
17114 --i;
17115 }
17116
17117
17118
17119 bytes = bytes_after_last_accept;
17120
17122 {
17123
17124 if (ensure_ascii)
17125 {
17132 }
17133
17134 else
17135 {
17136 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type(
'\xEF');
17137 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type(
'\xBF');
17138 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type(
'\xBD');
17139 }
17140
17141
17142
17143
17145 {
17147 bytes = 0;
17148 }
17149
17150 bytes_after_last_accept = bytes;
17151 }
17152
17153 undumped_chars = 0;
17154
17155 state = UTF8_ACCEPT;
17156 break;
17157 }
17158
17159 default:
17160 JSON_ASSERT(false);
17161 }
17162
17163 break;
17164 }
17165
17166 default:
17167 {
17168 if (!ensure_ascii)
17169 {
17170
17172 }
17173
17174 ++undumped_chars;
17175 break;
17176 }
17177 }
17178 }
const error_handler_t error_handler
error_handler how to react on decoding errors
std::array< char, 512 > string_buffer
string buffer
@ strict
throw a type_error exception in case of invalid UTF-8
@ ignore
ignore invalid UTF-8 sequences
@ replace
replace invalid UTF-8 sequences with U+FFFD