TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ for()

template<typename BasicJsonType >
nlohmann::detail::serializer< BasicJsonType >::for ( )
inline

Definition at line 16984 of file json.hpp.

16985 {
16986 const auto byte = static_cast<std::uint8_t>(s[i]);
16987
16988 switch (decode(state, codepoint, byte))
16989 {
16990 case UTF8_ACCEPT: // decode found a new code point
16991 {
16992 switch (codepoint)
16993 {
16994 case 0x08: // backspace
16995 {
16996 string_buffer[bytes++] = '\\';
16997 string_buffer[bytes++] = 'b';
16998 break;
16999 }
17000
17001 case 0x09: // horizontal tab
17002 {
17003 string_buffer[bytes++] = '\\';
17004 string_buffer[bytes++] = 't';
17005 break;
17006 }
17007
17008 case 0x0A: // newline
17009 {
17010 string_buffer[bytes++] = '\\';
17011 string_buffer[bytes++] = 'n';
17012 break;
17013 }
17014
17015 case 0x0C: // formfeed
17016 {
17017 string_buffer[bytes++] = '\\';
17018 string_buffer[bytes++] = 'f';
17019 break;
17020 }
17021
17022 case 0x0D: // carriage return
17023 {
17024 string_buffer[bytes++] = '\\';
17025 string_buffer[bytes++] = 'r';
17026 break;
17027 }
17028
17029 case 0x22: // quotation mark
17030 {
17031 string_buffer[bytes++] = '\\';
17032 string_buffer[bytes++] = '\"';
17033 break;
17034 }
17035
17036 case 0x5C: // reverse solidus
17037 {
17038 string_buffer[bytes++] = '\\';
17039 string_buffer[bytes++] = '\\';
17040 break;
17041 }
17042
17043 default:
17044 {
17045 // escape control characters (0x00..0x1F) or, if
17046 // ensure_ascii parameter is used, non-ASCII characters
17047 if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
17048 {
17049 if (codepoint <= 0xFFFF)
17050 {
17051 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
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 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
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 // copy byte to buffer (all previous bytes
17070 // been copied have in default case above)
17071 string_buffer[bytes++] = s[i];
17072 }
17073
17074 break;
17075 }
17076 }
17077
17078 // write buffer and reset index; there must be 13 bytes
17079 // left, as this is the maximal number of bytes to be
17080 // written ("\uxxxx\uxxxx\0") for one code point
17081 if (string_buffer.size() - bytes < 13)
17082 {
17083 o->write_characters(string_buffer.data(), bytes);
17084 bytes = 0;
17085 }
17086
17087 // remember the byte position of this accept
17088 bytes_after_last_accept = bytes;
17089 undumped_chars = 0;
17090 break;
17091 }
17092
17093 case UTF8_REJECT: // decode found invalid UTF-8 byte
17094 {
17095 switch (error_handler)
17096 {
17098 {
17099 std::string sn(9, '\0');
17100 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
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 // in case we saw this character the first time, we
17109 // would like to read it again, because the byte
17110 // may be OK for itself, but just not OK for the
17111 // previous sequence
17112 if (undumped_chars > 0)
17113 {
17114 --i;
17115 }
17116
17117 // reset length buffer to the last accepted index;
17118 // thus removing/ignoring the invalid characters
17119 bytes = bytes_after_last_accept;
17120
17122 {
17123 // add a replacement character
17124 if (ensure_ascii)
17125 {
17126 string_buffer[bytes++] = '\\';
17127 string_buffer[bytes++] = 'u';
17128 string_buffer[bytes++] = 'f';
17129 string_buffer[bytes++] = 'f';
17130 string_buffer[bytes++] = 'f';
17131 string_buffer[bytes++] = 'd';
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 // write buffer and reset index; there must be 13 bytes
17142 // left, as this is the maximal number of bytes to be
17143 // written ("\uxxxx\uxxxx\0") for one code point
17144 if (string_buffer.size() - bytes < 13)
17145 {
17146 o->write_characters(string_buffer.data(), bytes);
17147 bytes = 0;
17148 }
17149
17150 bytes_after_last_accept = bytes;
17151 }
17152
17153 undumped_chars = 0;
17154 // continue processing the string
17155 state = UTF8_ACCEPT;
17156 break;
17157 }
17158
17159 default: // LCOV_EXCL_LINE
17160 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
17161 }
17162
17163 break;
17164 }
17165
17166 default: // decode found yet incomplete multi-byte code point
17167 {
17168 if (!ensure_ascii)
17169 {
17170 // code point will not be escaped - copy byte to buffer
17171 string_buffer[bytes++] = s[i];
17172 }
17173
17174 ++undumped_chars;
17175 break;
17176 }
17177 }
17178 }
const error_handler_t error_handler
error_handler how to react on decoding errors
Definition: json.hpp:17551
std::array< char, 512 > string_buffer
string buffer
Definition: json.hpp:17543
@ 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