Datasets:
Download pool/_quarantine/untokenizable.jsonl from AtomicChat/calib-corpora: direct link, hf CLI and curl.
- Browser
- Download file 88.8 kB
-
https://huggingface.co/datasets/AtomicChat/calib-corpora/resolve/main/pool/_quarantine/untokenizable.jsonl
- Command line
-
hf download hf://datasets/AtomicChat/calib-corpora/pool/_quarantine/untokenizable.jsonl
-
curl -L -o untokenizable.jsonl https://huggingface.co/datasets/AtomicChat/calib-corpora/resolve/main/pool/_quarantine/untokenizable.jsonl
88.8 kB
| {"id": "5d30d52d5766c762", "category": "longctx", "domain": "longctx", "source": "json-cpp", "license": "MIT", "license_url": "", "path": "tests/src/ [bundle of 6 files]", "lang": "mixed", "origin": "module_bundle", "synthetic": false, "render": "text", "reasoning_strength": "", "provenance": {"imported_from": "calib_train.txt", "import_commit": "unknown", "legacy_domain": "longctx", "legacy_content_domain": "longctx", "legacy_id": "922b37646c49eb7e", "legacy_tokens_deepseek_v4": 15328, "legacy_split": "calib_train", "quarantined": "llama-tokenize aborts on this document (invalid codepoint in src/unicode.cpp); it would kill an llama-imatrix run", "quarantined_from": "pool/longctx/legacy-dsv4.jsonl"}, "chars": 55381, "sha256": "42f0e0b5a1e0bb9786605483173022ce1be27ede4fde017276d6a49963ff35a7", "text": "// ==== tests/src/unit-serialization.cpp ====\n// __ _____ _____ _____\n// __| | __| | | | JSON for Modern C++ (supporting code)\n// | | |__ | | | | | | version 3.12.0\n// |_____|_____|_____|_|___| https://github.com/nlohmann/json\n//\n// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>\n// SPDX-License-Identifier: MIT\n\n#include \"doctest_compatibility.h\"\n\n#include <nlohmann/json.hpp>\nusing nlohmann::json;\n\n#include <array>\n#include <sstream>\n#include <iomanip>\n\nTEST_CASE(\"serialization\")\n{\n SECTION(\"operator<<\")\n {\n SECTION(\"no given width\")\n {\n std::stringstream ss;\n const json j = {\"foo\", 1, 2, 3, false, {{\"one\", 1}}};\n ss << j;\n CHECK(ss.str() == \"[\\\"foo\\\",1,2,3,false,{\\\"one\\\":1}]\");\n }\n\n SECTION(\"given width\")\n {\n std::stringstream ss;\n const json j = {\"foo\", 1, 2, 3, false, {{\"one\", 1}}};\n ss << std::setw(4) << j;\n CHECK(ss.str() ==\n \"[\\n \\\"foo\\\",\\n 1,\\n 2,\\n 3,\\n false,\\n {\\n \\\"one\\\": 1\\n }\\n]\");\n }\n\n SECTION(\"given fill\")\n {\n std::stringstream ss;\n const json j = {\"foo\", 1, 2, 3, false, {{\"one\", 1}}};\n ss << std::setw(1) << std::setfill('\\t') << j;\n CHECK(ss.str() ==\n \"[\\n\\t\\\"foo\\\",\\n\\t1,\\n\\t2,\\n\\t3,\\n\\tfalse,\\n\\t{\\n\\t\\t\\\"one\\\": 1\\n\\t}\\n]\");\n }\n }\n\n SECTION(\"operator>>\")\n {\n SECTION(\"no given width\")\n {\n std::stringstream ss;\n const json j = {\"foo\", 1, 2, 3, false, {{\"one\", 1}}};\n j >> ss;\n CHECK(ss.str() == \"[\\\"foo\\\",1,2,3,false,{\\\"one\\\":1}]\");\n }\n\n SECTION(\"given width\")\n {\n std::stringstream ss;\n const json j = {\"foo\", 1, 2, 3, false, {{\"one\", 1}}};\n ss.width(4);\n j >> ss;\n CHECK(ss.str() ==\n \"[\\n \\\"foo\\\",\\n 1,\\n 2,\\n 3,\\n false,\\n {\\n \\\"one\\\": 1\\n }\\n]\");\n }\n\n SECTION(\"given fill\")\n {\n std::stringstream ss;\n const json j = {\"foo\", 1, 2, 3, false, {{\"one\", 1}}};\n ss.width(1);\n ss.fill('\\t');\n j >> ss;\n CHECK(ss.str() ==\n \"[\\n\\t\\\"foo\\\",\\n\\t1,\\n\\t2,\\n\\t3,\\n\\tfalse,\\n\\t{\\n\\t\\t\\\"one\\\": 1\\n\\t}\\n]\");\n }\n }\n\n SECTION(\"dump\")\n {\n SECTION(\"invalid character\")\n {\n const json j = \"ä\\xA9ü\";\n\n CHECK_THROWS_WITH_AS(j.dump(), \"[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9\", json::type_error&);\n CHECK_THROWS_WITH_AS(j.dump(1, ' ', false, json::error_handler_t::strict), \"[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9\", json::type_error&);\n CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == \"\\\"äü\\\"\");\n CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == \"\\\"ä\\xEF\\xBF\\xBDü\\\"\");\n CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == \"\\\"\\\\u00e4\\\\ufffd\\\\u00fc\\\"\");\n }\n\n SECTION(\"ending with incomplete character\")\n {\n const json j = \"123\\xC2\";\n\n CHECK_THROWS_WITH_AS(j.dump(), \"[json.exception.type_error.316] incomplete UTF-8 string; last byte: 0xC2\", json::type_error&);\n CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&);\n CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == \"\\\"123\\\"\");\n CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == \"\\\"123\\xEF\\xBF\\xBD\\\"\");\n CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == \"\\\"123\\\\ufffd\\\"\");\n }\n\n SECTION(\"unexpected character\")\n {\n const json j = \"123\\xF1\\xB0\\x34\\x35\\x36\";\n\n CHECK_THROWS_WITH_AS(j.dump(), \"[json.exception.type_error.316] invalid UTF-8 byte at index 5: 0x34\", json::type_error&);\n CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&);\n CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == \"\\\"123456\\\"\");\n CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == \"\\\"123\\xEF\\xBF\\xBD\\x34\\x35\\x36\\\"\");\n CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == \"\\\"123\\\\ufffd456\\\"\");\n }\n\n SECTION(\"U+FFFD Substitution of Maximal Subparts\")\n {\n // Some tests (mostly) from\n // https://www.unicode.org/versions/Unicode11.0.0/ch03.pdf\n // Section 3.9 -- U+FFFD Substitution of Maximal Subparts\n\n auto test = [&](std::string const & input, std::string const & expected)\n {\n const json j = input;\n CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == \"\\\"\" + expected + \"\\\"\");\n };\n\n test(\"\\xC2\", \"\\\\ufffd\");\n test(\"\\xC2\\x41\\x42\", \"\\\\ufffd\" \"\\x41\" \"\\x42\");\n test(\"\\xC2\\xF4\", \"\\\\ufffd\" \"\\\\ufffd\");\n\n test(\"\\xF0\\x80\\x80\\x41\", \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF1\\x80\\x80\\x41\", \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF2\\x80\\x80\\x41\", \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF3\\x80\\x80\\x41\", \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF4\\x80\\x80\\x41\", \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF5\\x80\\x80\\x41\", \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\x41\");\n\n test(\"\\xF0\\x90\\x80\\x41\", \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF1\\x90\\x80\\x41\", \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF2\\x90\\x80\\x41\", \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF3\\x90\\x80\\x41\", \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF4\\x90\\x80\\x41\", \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF5\\x90\\x80\\x41\", \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\x41\");\n\n test(\"\\xC0\\xAF\\xE0\\x80\\xBF\\xF0\\x81\\x82\\x41\", \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\x41\");\n test(\"\\xED\\xA0\\x80\\xED\\xBF\\xBF\\xED\\xAF\\x41\", \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\x41\");\n test(\"\\xF4\\x91\\x92\\x93\\xFF\\x41\\x80\\xBF\\x42\", \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\x41\" \"\\\\ufffd\"\"\\\\ufffd\" \"\\x42\");\n test(\"\\xE1\\x80\\xE2\\xF0\\x91\\x92\\xF1\\xBF\\x41\", \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\\\ufffd\" \"\\x41\");\n }\n }\n\n SECTION(\"to_string\")\n {\n auto test = [&](std::string const & input, std::string const & expected)\n {\n using std::to_string;\n const json j = input;\n CHECK(to_string(j) == \"\\\"\" + expected + \"\\\"\");\n };\n\n test(R\"({\"x\":5,\"y\":6})\", R\"({\\\"x\\\":5,\\\"y\\\":6})\");\n test(\"{\\\"x\\\":[10,null,null,null]}\", R\"({\\\"x\\\":[10,null,null,null]})\");\n test(\"test\", \"test\");\n test(\"[3,\\\"false\\\",false]\", R\"([3,\\\"false\\\",false])\");\n }\n}\n\nTEST_CASE_TEMPLATE(\"serialization for extreme integer values\", T, int32_t, uint32_t, int64_t, uint64_t) // NOLINT(readability-math-missing-parentheses, bugprone-throwing-static-initialization)\n{\n SECTION(\"minimum\")\n {\n constexpr auto minimum = (std::numeric_limits<T>::min)();\n const json j = minimum;\n CHECK(j.dump() == std::to_string(minimum));\n }\n\n SECTION(\"maximum\")\n {\n constexpr auto maximum = (std::numeric_limits<T>::max)();\n const json j = maximum;\n CHECK(j.dump() == std::to_string(maximum));\n }\n}\n\nTEST_CASE(\"dump with binary values\")\n{\n auto binary = json::binary({1, 2, 3, 4});\n auto binary_empty = json::binary({});\n auto binary_with_subtype = json::binary({1, 2, 3, 4}, 128);\n auto binary_empty_with_subtype = json::binary({}, 128);\n\n const json object = {{\"key\", binary}};\n const json object_empty = {{\"key\", binary_empty}};\n const json object_with_subtype = {{\"key\", binary_with_subtype}};\n const json object_empty_with_subtype = {{\"key\", binary_empty_with_subtype}};\n\n const json array = {\"value\", 1, binary};\n const json array_empty = {\"value\", 1, binary_empty};\n const json array_with_subtype = {\"value\", 1, binary_with_subtype};\n const json array_empty_with_subtype = {\"value\", 1, binary_empty_with_subtype};\n\n SECTION(\"normal\")\n {\n CHECK(binary.dump() == \"{\\\"bytes\\\":[1,2,3,4],\\\"subtype\\\":null}\");\n CHECK(binary_empty.dump() == \"{\\\"bytes\\\":[],\\\"subtype\\\":null}\");\n CHECK(binary_with_subtype.dump() == \"{\\\"bytes\\\":[1,2,3,4],\\\"subtype\\\":128}\");\n CHECK(binary_empty_with_subtype.dump() == \"{\\\"bytes\\\":[],\\\"subtype\\\":128}\");\n\n CHECK(object.dump() == \"{\\\"key\\\":{\\\"bytes\\\":[1,2,3,4],\\\"subtype\\\":null}}\");\n CHECK(object_empty.dump() == \"{\\\"key\\\":{\\\"bytes\\\":[],\\\"subtype\\\":null}}\");\n CHECK(object_with_subtype.dump() == \"{\\\"key\\\":{\\\"bytes\\\":[1,2,3,4],\\\"subtype\\\":128}}\");\n CHECK(object_empty_with_subtype.dump() == \"{\\\"key\\\":{\\\"bytes\\\":[],\\\"subtype\\\":128}}\");\n\n CHECK(array.dump() == \"[\\\"value\\\",1,{\\\"bytes\\\":[1,2,3,4],\\\"subtype\\\":null}]\");\n CHECK(array_empty.dump() == \"[\\\"value\\\",1,{\\\"bytes\\\":[],\\\"subtype\\\":null}]\");\n CHECK(array_with_subtype.dump() == \"[\\\"value\\\",1,{\\\"bytes\\\":[1,2,3,4],\\\"subtype\\\":128}]\");\n CHECK(array_empty_with_subtype.dump() == \"[\\\"value\\\",1,{\\\"bytes\\\":[],\\\"subtype\\\":128}]\");\n }\n\n SECTION(\"pretty-printed\")\n {\n CHECK(binary.dump(4) == \"{\\n\"\n \" \\\"bytes\\\": [1, 2, 3, 4],\\n\"\n \" \\\"subtype\\\": null\\n\"\n \"}\");\n CHECK(binary_empty.dump(4) == \"{\\n\"\n \" \\\"bytes\\\": [],\\n\"\n \" \\\"subtype\\\": null\\n\"\n \"}\");\n CHECK(binary_with_subtype.dump(4) == \"{\\n\"\n \" \\\"bytes\\\": [1, 2, 3, 4],\\n\"\n \" \\\"subtype\\\": 128\\n\"\n \"}\");\n CHECK(binary_empty_with_subtype.dump(4) == \"{\\n\"\n \" \\\"bytes\\\": [],\\n\"\n \" \\\"subtype\\\": 128\\n\"\n \"}\");\n\n CHECK(object.dump(4) == \"{\\n\"\n \" \\\"key\\\": {\\n\"\n \" \\\"bytes\\\": [1, 2, 3, 4],\\n\"\n \" \\\"subtype\\\": null\\n\"\n \" }\\n\"\n \"}\");\n CHECK(object_empty.dump(4) == \"{\\n\"\n \" \\\"key\\\": {\\n\"\n \" \\\"bytes\\\": [],\\n\"\n \" \\\"subtype\\\": null\\n\"\n \" }\\n\"\n \"}\");\n CHECK(object_with_subtype.dump(4) == \"{\\n\"\n \" \\\"key\\\": {\\n\"\n \" \\\"bytes\\\": [1, 2, 3, 4],\\n\"\n \" \\\"subtype\\\": 128\\n\"\n \" }\\n\"\n \"}\");\n CHECK(object_empty_with_subtype.dump(4) == \"{\\n\"\n \" \\\"key\\\": {\\n\"\n \" \\\"bytes\\\": [],\\n\"\n \" \\\"subtype\\\": 128\\n\"\n \" }\\n\"\n \"}\");\n\n CHECK(array.dump(4) == \"[\\n\"\n \" \\\"value\\\",\\n\"\n \" 1,\\n\"\n \" {\\n\"\n \" \\\"bytes\\\": [1, 2, 3, 4],\\n\"\n \" \\\"subtype\\\": null\\n\"\n \" }\\n\"\n \"]\");\n CHECK(array_empty.dump(4) == \"[\\n\"\n \" \\\"value\\\",\\n\"\n \" 1,\\n\"\n \" {\\n\"\n \" \\\"bytes\\\": [],\\n\"\n \" \\\"subtype\\\": null\\n\"\n \" }\\n\"\n \"]\");\n CHECK(array_with_subtype.dump(4) == \"[\\n\"\n \" \\\"value\\\",\\n\"\n \" 1,\\n\"\n \" {\\n\"\n \" \\\"bytes\\\": [1, 2, 3, 4],\\n\"\n \" \\\"subtype\\\": 128\\n\"\n \" }\\n\"\n \"]\");\n CHECK(array_empty_with_subtype.dump(4) == \"[\\n\"\n \" \\\"value\\\",\\n\"\n \" 1,\\n\"\n \" {\\n\"\n \" \\\"bytes\\\": [],\\n\"\n \" \\\"subtype\\\": 128\\n\"\n \" }\\n\"\n \"]\");\n }\n}\n\nTEST_CASE(\"dump for basic_json with long double number_float_t\")\n{\n // Custom basic_json instantiation with long double as NumberFloatType.\n // On platforms where long double is wider than double (e.g. GCC/Clang on\n // Linux/macOS x86_64), dump() goes through the snprintf path in\n // serializer::dump_float(x, std::false_type). That branch must use the\n // \"%.*Lg\" format specifier; using \"%.*g\" with a long double argument is\n // undefined behavior and corrupts the output.\n using long_double_json = nlohmann::basic_json<std::map, std::vector, std::string,\n bool, std::int64_t, std::uint64_t, long double>;\n\n SECTION(\"round-trip dump/parse\")\n {\n constexpr std::array<long double, 13> values =\n {\n {\n 0.0L, -0.0L, 1.0L, -1.0L,\n 0.5L, -0.5L, 1.5L, -2.25L,\n 1.23e45L, 1.23e-45L,\n (std::numeric_limits<long double>::min)(),\n std::numeric_limits<long double>::lowest(),\n (std::numeric_limits<long double>::max)()\n }\n };\n\n for (long double v : values)\n {\n const long_double_json j = v;\n const auto s = j.dump();\n const auto j2 = long_double_json::parse(s);\n CHECK(j2.template get<long double>() == v);\n }\n }\n\n SECTION(\"exact dump string for simple values\")\n {\n CHECK(long_double_json(0.5L).dump() == \"0.5\");\n CHECK(long_double_json(-0.5L).dump() == \"-0.5\");\n CHECK(long_double_json(1.5L).dump() == \"1.5\");\n CHECK(long_double_json(-2.25L).dump() == \"-2.25\");\n CHECK(long_double_json(0.0L).dump() == \"0.0\");\n CHECK(long_double_json(1.0L).dump() == \"1.0\");\n CHECK(long_double_json(-1.0L).dump() == \"-1.0\");\n CHECK(long_double_json(100.0L).dump() == \"100.0\");\n }\n\n SECTION(\"NaN and infinity dump as null\")\n {\n CHECK(long_double_json(std::numeric_limits<long double>::quiet_NaN()).dump() == \"null\");\n\n // Probe the platform's runtime behavior — `volatile` forces a runtime\n // call rather than constexpr-folding to a known answer at compile time.\n // Skip the infinity assertions if std::isfinite() doesn't actually\n // recognize long double infinity on this platform (notably, Valgrind\n // 3.22's x87 80-bit emulation reports +/-inf as a large finite value).\n // TODO(rusloker): remove this guard once Valgrind's 80-bit long double\n // support ships (Valgrind bug https://bugs.kde.org/show_bug.cgi?id=197915,\n // ASSIGNED since 2009 — the Valgrind project tracks its bugs on\n // bugs.kde.org) and the minimum supported Valgrind version contains it.\n const volatile long double inf_probe = std::numeric_limits<long double>::infinity();\n if (!std::isfinite(inf_probe))\n {\n CHECK(long_double_json(std::numeric_limits<long double>::infinity()).dump() == \"null\");\n CHECK(long_double_json(-std::numeric_limits<long double>::infinity()).dump() == \"null\");\n }\n }\n\n SECTION(\"dump output matches double for exactly-representable values\")\n {\n auto check_same = [](long double v_ld, double v_d)\n {\n const long_double_json j_ld = v_ld;\n const json j_d = v_d;\n CHECK(j_ld.dump() == j_d.dump());\n };\n\n check_same(0.0L, 0.0);\n check_same(0.5L, 0.5);\n check_same(-0.5L, -0.5);\n check_same(1.5L, 1.5);\n check_same(-2.25L, -2.25);\n check_same(1.0L, 1.0);\n check_same(100.0L, 100.0);\n }\n}\n\n// ==== tests/src/unit-std-format.cpp ====\n// __ _____ _____ _____\n// __| | __| | | | JSON for Modern C++ (supporting code)\n// | | |__ | | | | | | version 3.12.0\n// |_____|_____|_____|_|___| https://github.com/nlohmann/json\n//\n// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>\n// SPDX-License-Identifier: MIT\n\n// cmake/test.cmake selects the C++ standard versions with which to build a\n// unit test based on the presence of JSON_HAS_CPP_<VERSION> macros.\n// When using macros that are only defined for particular versions of the standard\n// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding\n// version macro in a comment close by, like this:\n// JSON_HAS_CPP_<VERSION> (do not remove; see note at top of file)\n\n#include \"doctest_compatibility.h\"\n\n#include <nlohmann/json.hpp>\nusing json = nlohmann::json;\n\n// JSON_HAS_CPP_20 (do not remove; see note at top of file)\n#if JSON_HAS_STD_FORMAT\n\n#include <iterator>\n#include <string>\n\nTEST_CASE(\"std::formatter<nlohmann::json>\")\n{\n SECTION(\"compact formatting matches dump()\")\n {\n CHECK(std::format(\"{}\", json(nullptr)) == json(nullptr).dump());\n CHECK(std::format(\"{}\", json(true)) == json(true).dump());\n CHECK(std::format(\"{}\", json(42)) == json(42).dump());\n CHECK(std::format(\"{}\", json(42.23)) == json(42.23).dump());\n CHECK(std::format(\"{}\", json(\"foo\")) == json(\"foo\").dump());\n CHECK(std::format(\"{}\", json::array({1, 2, 3})) == json::array({1, 2, 3}).dump());\n\n const json j = {{\"foo\", 1}, {\"bar\", {1, 2, 3}}};\n CHECK(std::format(\"{}\", j) == j.dump());\n }\n\n SECTION(\"'#' triggers pretty-printing with an indent of 4, like dump(4)\")\n {\n const json j = {{\"foo\", 1}, {\"bar\", {1, 2, 3}}};\n CHECK(std::format(\"{:#}\", j) == j.dump(4));\n CHECK(std::format(\"{:#}\", json::array()) == json::array().dump(4));\n }\n\n SECTION(\"a width sets the indent, like dump(width), with or without '#'\")\n {\n const json j = {{\"foo\", 1}, {\"bar\", {1, 2, 3}}};\n CHECK(std::format(\"{:2}\", j) == j.dump(2));\n CHECK(std::format(\"{:#2}\", j) == j.dump(2));\n CHECK(std::format(\"{:8}\", j) == j.dump(8));\n }\n\n SECTION(\"fill-and-align sets the indent character, like dump(indent, indent_char)\")\n {\n const json j = {{\"foo\", 1}, {\"bar\", {1, 2, 3}}};\n CHECK(std::format(\"{:.>#}\", j) == j.dump(4, '.'));\n CHECK(std::format(\"{:.>#3}\", j) == j.dump(3, '.'));\n CHECK(std::format(\"{:.>3}\", j) == j.dump(3, '.'));\n // the alignment direction itself ('<', '>', '^') has no separate meaning for\n // JSON values -- only the fill character before it is used as the indent character\n CHECK(std::format(\"{:.<3}\", j) == j.dump(3, '.'));\n CHECK(std::format(\"{:.^3}\", j) == j.dump(3, '.'));\n }\n\n SECTION(\"format args with no meaning for JSON values are rejected\")\n {\n // std::vformat parses the format string at runtime (unlike std::format, whose\n // format_string type is checked at compile time), so it lets us verify that an\n // invalid spec throws std::format_error without needing a compile-time-illegal\n // format string.\n const json j = 42;\n CHECK_THROWS_AS(std::vformat(\"{:x}\", std::make_format_args(j)), std::format_error);\n CHECK_THROWS_AS(std::vformat(\"{:+}\", std::make_format_args(j)), std::format_error); // sign\n CHECK_THROWS_AS(std::vformat(\"{:-}\", std::make_format_args(j)), std::format_error); // sign\n CHECK_THROWS_AS(std::vformat(\"{: }\", std::make_format_args(j)), std::format_error); // sign\n CHECK_THROWS_AS(std::vformat(\"{:04}\", std::make_format_args(j)), std::format_error); // '0' flag\n CHECK_THROWS_AS(std::vformat(\"{:.2}\", std::make_format_args(j)), std::format_error); // precision\n CHECK_THROWS_AS(std::vformat(\"{:L}\", std::make_format_args(j)), std::format_error); // locale\n const int dynamic_width = 4;\n CHECK_THROWS_AS(std::vformat(\"{:{}}\", std::make_format_args(j, dynamic_width)), std::format_error); // dynamic width\n }\n\n SECTION(\"std::format_to writes through an arbitrary output iterator\")\n {\n const json j = {{\"foo\", 1}, {\"bar\", {1, 2, 3}}};\n std::string out;\n std::format_to(std::back_inserter(out), \"{}\", j);\n CHECK(out == j.dump());\n }\n}\n\n#endif\n\n// ==== tests/src/unit-type_traits.cpp ====\n// __ _____ _____ _____\n// __| | __| | | | JSON for Modern C++ (supporting code)\n// | | |__ | | | | | | version 3.12.0\n// |_____|_____|_____|_|___| https://github.com/nlohmann/json\n//\n// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>\n// SPDX-License-Identifier: MIT\n\n#include \"doctest_compatibility.h\"\n\n#if JSON_TEST_USING_MULTIPLE_HEADERS\n #include <nlohmann/detail/meta/type_traits.hpp>\n#else\n #include <nlohmann/json.hpp>\n#endif\n\nTEST_CASE(\"type traits\")\n{\n SECTION(\"is_c_string\")\n {\n using nlohmann::detail::is_c_string;\n using nlohmann::detail::is_c_string_uncvref;\n\n SECTION(\"char *\")\n {\n CHECK(is_c_string<char*>::value);\n CHECK(is_c_string<const char*>::value);\n CHECK(is_c_string<char* const>::value);\n CHECK(is_c_string<const char* const>::value);\n\n CHECK_FALSE(is_c_string<char*&>::value);\n CHECK_FALSE(is_c_string<const char*&>::value);\n CHECK_FALSE(is_c_string<char* const&>::value);\n CHECK_FALSE(is_c_string<const char* const&>::value);\n\n CHECK(is_c_string_uncvref<char*&>::value);\n CHECK(is_c_string_uncvref<const char*&>::value);\n CHECK(is_c_string_uncvref<char* const&>::value);\n CHECK(is_c_string_uncvref<const char* const&>::value);\n }\n\n SECTION(\"char[]\")\n {\n // NOLINTBEGIN(hicpp-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)\n CHECK(is_c_string<char[]>::value);\n CHECK(is_c_string<const char[]>::value);\n\n CHECK_FALSE(is_c_string<char(&)[]>::value);\n CHECK_FALSE(is_c_string<const char(&)[]>::value);\n\n CHECK(is_c_string_uncvref<char(&)[]>::value);\n CHECK(is_c_string_uncvref<const char(&)[]>::value);\n // NOLINTEND(hicpp-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)\n }\n }\n}\n\n// ==== tests/src/unit-udl.cpp ====\n// __ _____ _____ _____\n// __| | __| | | | JSON for Modern C++ (supporting code)\n// | | |__ | | | | | | version 3.12.0\n// |_____|_____|_____|_|___| https://github.com/nlohmann/json\n//\n// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>\n// SPDX-License-Identifier: MIT\n\n#include \"doctest_compatibility.h\"\n\n#include <nlohmann/json.hpp>\n\nTEST_CASE(\"user-defined string literals\")\n{\n auto j_expected = nlohmann::json::parse(R\"({\"foo\": \"bar\", \"baz\": 42})\");\n auto ptr_expected = nlohmann::json::json_pointer(\"/foo/bar\");\n\n SECTION(\"using namespace nlohmann::literals::json_literals\")\n {\n using namespace nlohmann::literals::json_literals; // NOLINT(google-build-using-namespace)\n\n CHECK(R\"({\"foo\": \"bar\", \"baz\": 42})\"_json == j_expected);\n CHECK(\"/foo/bar\"_json_pointer == ptr_expected);\n }\n\n SECTION(\"using namespace nlohmann::json_literals\")\n {\n using namespace nlohmann::json_literals; // NOLINT(google-build-using-namespace)\n\n CHECK(R\"({\"foo\": \"bar\", \"baz\": 42})\"_json == j_expected);\n CHECK(\"/foo/bar\"_json_pointer == ptr_expected);\n }\n\n SECTION(\"using namespace nlohmann::literals\")\n {\n using namespace nlohmann::literals; // NOLINT(google-build-using-namespace)\n\n CHECK(R\"({\"foo\": \"bar\", \"baz\": 42})\"_json == j_expected);\n CHECK(\"/foo/bar\"_json_pointer == ptr_expected);\n }\n\n SECTION(\"using namespace nlohmann\")\n {\n using namespace nlohmann; // NOLINT(google-build-using-namespace)\n\n CHECK(R\"({\"foo\": \"bar\", \"baz\": 42})\"_json == j_expected);\n CHECK(\"/foo/bar\"_json_pointer == ptr_expected);\n }\n\n#ifndef JSON_TEST_NO_GLOBAL_UDLS\n SECTION(\"global namespace\")\n {\n CHECK(R\"({\"foo\": \"bar\", \"baz\": 42})\"_json == j_expected);\n CHECK(\"/foo/bar\"_json_pointer == ptr_expected);\n }\n#endif\n}\n\n// ==== tests/src/unit-unicode2.cpp ====\n// __ _____ _____ _____\n// __| | __| | | | JSON for Modern C++ (supporting code)\n// | | |__ | | | | | | version 3.12.0\n// |_____|_____|_____|_|___| https://github.com/nlohmann/json\n//\n// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>\n// SPDX-License-Identifier: MIT\n\n#include \"doctest_compatibility.h\"\n\n// for some reason including this after the json header leads to linker errors with VS 2017...\n#include <locale>\n\n#include <nlohmann/json.hpp>\nusing nlohmann::json;\n\n#include <fstream>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include \"make_test_data_available.hpp\"\n\n// this test suite uses static variables with non-trivial destructors\nDOCTEST_CLANG_SUPPRESS_WARNING_PUSH\nDOCTEST_CLANG_SUPPRESS_WARNING(\"-Wexit-time-destructors\")\n\nnamespace\n{\nextern size_t calls;\nsize_t calls = 0;\n\nvoid check_utf8dump(bool success_expected, int byte1, int byte2, int byte3, int byte4);\n\nvoid check_utf8dump(bool success_expected, int byte1, int byte2 = -1, int byte3 = -1, int byte4 = -1)\n{\n static std::string json_string;\n json_string.clear();\n\n CAPTURE(byte1)\n CAPTURE(byte2)\n CAPTURE(byte3)\n CAPTURE(byte4)\n\n json_string += std::string(1, static_cast<char>(byte1));\n\n if (byte2 != -1)\n {\n json_string += std::string(1, static_cast<char>(byte2));\n }\n\n if (byte3 != -1)\n {\n json_string += std::string(1, static_cast<char>(byte3));\n }\n\n if (byte4 != -1)\n {\n json_string += std::string(1, static_cast<char>(byte4));\n }\n\n CAPTURE(json_string)\n\n // store the string in a JSON value\n static json j;\n static json j2;\n j = json_string;\n j2 = \"abc\" + json_string + \"xyz\";\n\n static std::string s_ignored;\n static std::string s_ignored2;\n static std::string s_ignored_ascii;\n static std::string s_ignored2_ascii;\n static std::string s_replaced;\n static std::string s_replaced2;\n static std::string s_replaced_ascii;\n static std::string s_replaced2_ascii;\n\n // dumping with ignore/replace must not throw in any case\n s_ignored = j.dump(-1, ' ', false, json::error_handler_t::ignore);\n s_ignored2 = j2.dump(-1, ' ', false, json::error_handler_t::ignore);\n s_ignored_ascii = j.dump(-1, ' ', true, json::error_handler_t::ignore);\n s_ignored2_ascii = j2.dump(-1, ' ', true, json::error_handler_t::ignore);\n s_replaced = j.dump(-1, ' ', false, json::error_handler_t::replace);\n s_replaced2 = j2.dump(-1, ' ', false, json::error_handler_t::replace);\n s_replaced_ascii = j.dump(-1, ' ', true, json::error_handler_t::replace);\n s_replaced2_ascii = j2.dump(-1, ' ', true, json::error_handler_t::replace);\n\n if (success_expected)\n {\n static std::string s_strict;\n // strict mode must not throw if success is expected\n s_strict = j.dump();\n // all dumps should agree on the string\n CHECK(s_strict == s_ignored);\n CHECK(s_strict == s_replaced);\n }\n else\n {\n // strict mode must throw if success is not expected\n CHECK_THROWS_AS(j.dump(), json::type_error&);\n // ignore and replace must create different dumps\n CHECK(s_ignored != s_replaced);\n\n // check that replace string contains a replacement character\n CHECK(s_replaced.find(\"\\xEF\\xBF\\xBD\") != std::string::npos);\n }\n\n // check that prefix and suffix are preserved\n CHECK(s_ignored2.substr(1, 3) == \"abc\");\n CHECK(s_ignored2.substr(s_ignored2.size() - 4, 3) == \"xyz\");\n CHECK(s_ignored2_ascii.substr(1, 3) == \"abc\");\n CHECK(s_ignored2_ascii.substr(s_ignored2_ascii.size() - 4, 3) == \"xyz\");\n CHECK(s_replaced2.substr(1, 3) == \"abc\");\n CHECK(s_replaced2.substr(s_replaced2.size() - 4, 3) == \"xyz\");\n CHECK(s_replaced2_ascii.substr(1, 3) == \"abc\");\n CHECK(s_replaced2_ascii.substr(s_replaced2_ascii.size() - 4, 3) == \"xyz\");\n}\n\nvoid check_utf8string(bool success_expected, int byte1, int byte2, int byte3, int byte4);\n\n// create and check a JSON string with up to four UTF-8 bytes\nvoid check_utf8string(bool success_expected, int byte1, int byte2 = -1, int byte3 = -1, int byte4 = -1)\n{\n if (++calls % 100000 == 0)\n {\n std::cout << calls << \" of 455355 UTF-8 strings checked\" << std::endl; // NOLINT(performance-avoid-endl)\n }\n\n static std::string json_string;\n json_string = \"\\\"\";\n\n CAPTURE(byte1)\n json_string += std::string(1, static_cast<char>(byte1));\n\n if (byte2 != -1)\n {\n CAPTURE(byte2)\n json_string += std::string(1, static_cast<char>(byte2));\n }\n\n if (byte3 != -1)\n {\n CAPTURE(byte3)\n json_string += std::string(1, static_cast<char>(byte3));\n }\n\n if (byte4 != -1)\n {\n CAPTURE(byte4)\n json_string += std::string(1, static_cast<char>(byte4));\n }\n\n json_string += \"\\\"\";\n\n CAPTURE(json_string)\n\n json _;\n if (success_expected)\n {\n CHECK_NOTHROW(_ = json::parse(json_string));\n }\n else\n {\n CHECK_THROWS_AS(_ = json::parse(json_string), json::parse_error&);\n }\n}\n} // namespace\n\nTEST_CASE(\"Unicode (2/5)\" * doctest::skip())\n{\n SECTION(\"RFC 3629\")\n {\n /*\n RFC 3629 describes in Sect. 4 the syntax of UTF-8 byte sequences as\n follows:\n\n A UTF-8 string is a sequence of octets representing a sequence of UCS\n characters. An octet sequence is valid UTF-8 only if it matches the\n following syntax, which is derived from the rules for encoding UTF-8\n and is expressed in the ABNF of [RFC2234].\n\n UTF8-octets = *( UTF8-char )\n UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4\n UTF8-1 = %x00-7F\n UTF8-2 = %xC2-DF UTF8-tail\n UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /\n %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )\n UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /\n %xF4 %x80-8F 2( UTF8-tail )\n UTF8-tail = %x80-BF\n */\n\n SECTION(\"ill-formed first byte\")\n {\n for (int byte1 = 0x80; byte1 <= 0xC1; ++byte1)\n {\n check_utf8string(false, byte1);\n check_utf8dump(false, byte1);\n }\n\n for (int byte1 = 0xF5; byte1 <= 0xFF; ++byte1)\n {\n check_utf8string(false, byte1);\n check_utf8dump(false, byte1);\n }\n }\n\n SECTION(\"UTF8-1 (x00-x7F)\")\n {\n SECTION(\"well-formed\")\n {\n for (int byte1 = 0x00; byte1 <= 0x7F; ++byte1)\n {\n // unescaped control characters are parse errors in JSON\n if (0x00 <= byte1 && byte1 <= 0x1F)\n {\n check_utf8string(false, byte1);\n continue;\n }\n\n // a single quote is a parse error in JSON\n if (byte1 == 0x22)\n {\n check_utf8string(false, byte1);\n continue;\n }\n\n // a single backslash is a parse error in JSON\n if (byte1 == 0x5C)\n {\n check_utf8string(false, byte1);\n continue;\n }\n\n // all other characters are OK\n check_utf8string(true, byte1);\n check_utf8dump(true, byte1);\n }\n }\n }\n\n SECTION(\"UTF8-2 (xC2-xDF UTF8-tail)\")\n {\n SECTION(\"well-formed\")\n {\n for (int byte1 = 0xC2; byte1 <= 0xDF; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n check_utf8string(true, byte1, byte2);\n check_utf8dump(true, byte1, byte2);\n }\n }\n }\n\n SECTION(\"ill-formed: missing second byte\")\n {\n for (int byte1 = 0xC2; byte1 <= 0xDF; ++byte1)\n {\n check_utf8string(false, byte1);\n check_utf8dump(false, byte1);\n }\n }\n\n SECTION(\"ill-formed: wrong second byte\")\n {\n for (int byte1 = 0xC2; byte1 <= 0xDF; ++byte1)\n {\n for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)\n {\n // skip correct second byte\n if (0x80 <= byte2 && byte2 <= 0xBF)\n {\n continue;\n }\n\n check_utf8string(false, byte1, byte2);\n check_utf8dump(false, byte1, byte2);\n }\n }\n }\n }\n\n SECTION(\"UTF8-3 (xE0 xA0-BF UTF8-tail)\")\n {\n SECTION(\"well-formed\")\n {\n for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)\n {\n for (int byte2 = 0xA0; byte2 <= 0xBF; ++byte2)\n {\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n check_utf8string(true, byte1, byte2, byte3);\n check_utf8dump(true, byte1, byte2, byte3);\n }\n }\n }\n }\n\n SECTION(\"ill-formed: missing second byte\")\n {\n for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)\n {\n check_utf8string(false, byte1);\n check_utf8dump(false, byte1);\n }\n }\n\n SECTION(\"ill-formed: missing third byte\")\n {\n for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)\n {\n for (int byte2 = 0xA0; byte2 <= 0xBF; ++byte2)\n {\n check_utf8string(false, byte1, byte2);\n check_utf8dump(false, byte1, byte2);\n }\n }\n }\n\n SECTION(\"ill-formed: wrong second byte\")\n {\n for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)\n {\n for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)\n {\n // skip correct second byte\n if (0xA0 <= byte2 && byte2 <= 0xBF)\n {\n continue;\n }\n\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n check_utf8string(false, byte1, byte2, byte3);\n check_utf8dump(false, byte1, byte2, byte3);\n }\n }\n }\n }\n\n SECTION(\"ill-formed: wrong third byte\")\n {\n for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)\n {\n for (int byte2 = 0xA0; byte2 <= 0xBF; ++byte2)\n {\n for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)\n {\n // skip correct third byte\n if (0x80 <= byte3 && byte3 <= 0xBF)\n {\n continue;\n }\n\n check_utf8string(false, byte1, byte2, byte3);\n check_utf8dump(false, byte1, byte2, byte3);\n }\n }\n }\n }\n }\n\n SECTION(\"UTF8-3 (xE1-xEC UTF8-tail UTF8-tail)\")\n {\n SECTION(\"well-formed\")\n {\n for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n check_utf8string(true, byte1, byte2, byte3);\n check_utf8dump(true, byte1, byte2, byte3);\n }\n }\n }\n }\n\n SECTION(\"ill-formed: missing second byte\")\n {\n for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)\n {\n check_utf8string(false, byte1);\n check_utf8dump(false, byte1);\n }\n }\n\n SECTION(\"ill-formed: missing third byte\")\n {\n for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n check_utf8string(false, byte1, byte2);\n check_utf8dump(false, byte1, byte2);\n }\n }\n }\n\n SECTION(\"ill-formed: wrong second byte\")\n {\n for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)\n {\n for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)\n {\n // skip correct second byte\n if (0x80 <= byte2 && byte2 <= 0xBF)\n {\n continue;\n }\n\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n check_utf8string(false, byte1, byte2, byte3);\n check_utf8dump(false, byte1, byte2, byte3);\n }\n }\n }\n }\n\n SECTION(\"ill-formed: wrong third byte\")\n {\n for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)\n {\n // skip correct third byte\n if (0x80 <= byte3 && byte3 <= 0xBF)\n {\n continue;\n }\n\n check_utf8string(false, byte1, byte2, byte3);\n check_utf8dump(false, byte1, byte2, byte3);\n }\n }\n }\n }\n }\n\n SECTION(\"UTF8-3 (xED x80-9F UTF8-tail)\")\n {\n SECTION(\"well-formed\")\n {\n for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0x9F; ++byte2)\n {\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n check_utf8string(true, byte1, byte2, byte3);\n check_utf8dump(true, byte1, byte2, byte3);\n }\n }\n }\n }\n\n SECTION(\"ill-formed: missing second byte\")\n {\n for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)\n {\n check_utf8string(false, byte1);\n check_utf8dump(false, byte1);\n }\n }\n\n SECTION(\"ill-formed: missing third byte\")\n {\n for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0x9F; ++byte2)\n {\n check_utf8string(false, byte1, byte2);\n check_utf8dump(false, byte1, byte2);\n }\n }\n }\n\n SECTION(\"ill-formed: wrong second byte\")\n {\n for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)\n {\n for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)\n {\n // skip correct second byte\n if (0x80 <= byte2 && byte2 <= 0x9F)\n {\n continue;\n }\n\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n check_utf8string(false, byte1, byte2, byte3);\n check_utf8dump(false, byte1, byte2, byte3);\n }\n }\n }\n }\n\n SECTION(\"ill-formed: wrong third byte\")\n {\n for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0x9F; ++byte2)\n {\n for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)\n {\n // skip correct third byte\n if (0x80 <= byte3 && byte3 <= 0xBF)\n {\n continue;\n }\n\n check_utf8string(false, byte1, byte2, byte3);\n check_utf8dump(false, byte1, byte2, byte3);\n }\n }\n }\n }\n }\n\n SECTION(\"UTF8-3 (xEE-xEF UTF8-tail UTF8-tail)\")\n {\n SECTION(\"well-formed\")\n {\n for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n check_utf8string(true, byte1, byte2, byte3);\n check_utf8dump(true, byte1, byte2, byte3);\n }\n }\n }\n }\n\n SECTION(\"ill-formed: missing second byte\")\n {\n for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)\n {\n check_utf8string(false, byte1);\n check_utf8dump(false, byte1);\n }\n }\n\n SECTION(\"ill-formed: missing third byte\")\n {\n for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n check_utf8string(false, byte1, byte2);\n check_utf8dump(false, byte1, byte2);\n }\n }\n }\n\n SECTION(\"ill-formed: wrong second byte\")\n {\n for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)\n {\n for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)\n {\n // skip correct second byte\n if (0x80 <= byte2 && byte2 <= 0xBF)\n {\n continue;\n }\n\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n check_utf8string(false, byte1, byte2, byte3);\n check_utf8dump(false, byte1, byte2, byte3);\n }\n }\n }\n }\n\n SECTION(\"ill-formed: wrong third byte\")\n {\n for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)\n {\n // skip correct third byte\n if (0x80 <= byte3 && byte3 <= 0xBF)\n {\n continue;\n }\n\n check_utf8string(false, byte1, byte2, byte3);\n check_utf8dump(false, byte1, byte2, byte3);\n }\n }\n }\n }\n }\n }\n}\n\nDOCTEST_CLANG_SUPPRESS_WARNING_POP\n\n// ==== tests/src/unit-unicode4.cpp ====\n// __ _____ _____ _____\n// __| | __| | | | JSON for Modern C++ (supporting code)\n// | | |__ | | | | | | version 3.12.0\n// |_____|_____|_____|_|___| https://github.com/nlohmann/json\n//\n// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>\n// SPDX-License-Identifier: MIT\n\n#include \"doctest_compatibility.h\"\n\n// for some reason including this after the json header leads to linker errors with VS 2017...\n#include <locale>\n\n#include <nlohmann/json.hpp>\nusing nlohmann::json;\n\n#include <fstream>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include \"make_test_data_available.hpp\"\n\n// this test suite uses static variables with non-trivial destructors\nDOCTEST_CLANG_SUPPRESS_WARNING_PUSH\nDOCTEST_CLANG_SUPPRESS_WARNING(\"-Wexit-time-destructors\")\n\nnamespace\n{\nextern size_t calls;\nsize_t calls = 0;\n\nvoid check_utf8dump(bool success_expected, int byte1, int byte2, int byte3, int byte4);\n\nvoid check_utf8dump(bool success_expected, int byte1, int byte2 = -1, int byte3 = -1, int byte4 = -1)\n{\n static std::string json_string;\n json_string.clear();\n\n CAPTURE(byte1)\n CAPTURE(byte2)\n CAPTURE(byte3)\n CAPTURE(byte4)\n\n json_string += std::string(1, static_cast<char>(byte1));\n\n if (byte2 != -1)\n {\n json_string += std::string(1, static_cast<char>(byte2));\n }\n\n if (byte3 != -1)\n {\n json_string += std::string(1, static_cast<char>(byte3));\n }\n\n if (byte4 != -1)\n {\n json_string += std::string(1, static_cast<char>(byte4));\n }\n\n CAPTURE(json_string)\n\n // store the string in a JSON value\n static json j;\n static json j2;\n j = json_string;\n j2 = \"abc\" + json_string + \"xyz\";\n\n static std::string s_ignored;\n static std::string s_ignored2;\n static std::string s_ignored_ascii;\n static std::string s_ignored2_ascii;\n static std::string s_replaced;\n static std::string s_replaced2;\n static std::string s_replaced_ascii;\n static std::string s_replaced2_ascii;\n\n // dumping with ignore/replace must not throw in any case\n s_ignored = j.dump(-1, ' ', false, json::error_handler_t::ignore);\n s_ignored2 = j2.dump(-1, ' ', false, json::error_handler_t::ignore);\n s_ignored_ascii = j.dump(-1, ' ', true, json::error_handler_t::ignore);\n s_ignored2_ascii = j2.dump(-1, ' ', true, json::error_handler_t::ignore);\n s_replaced = j.dump(-1, ' ', false, json::error_handler_t::replace);\n s_replaced2 = j2.dump(-1, ' ', false, json::error_handler_t::replace);\n s_replaced_ascii = j.dump(-1, ' ', true, json::error_handler_t::replace);\n s_replaced2_ascii = j2.dump(-1, ' ', true, json::error_handler_t::replace);\n\n if (success_expected)\n {\n static std::string s_strict;\n // strict mode must not throw if success is expected\n s_strict = j.dump();\n // all dumps should agree on the string\n CHECK(s_strict == s_ignored);\n CHECK(s_strict == s_replaced);\n }\n else\n {\n // strict mode must throw if success is not expected\n CHECK_THROWS_AS(j.dump(), json::type_error&);\n // ignore and replace must create different dumps\n CHECK(s_ignored != s_replaced);\n\n // check that replace string contains a replacement character\n CHECK(s_replaced.find(\"\\xEF\\xBF\\xBD\") != std::string::npos);\n }\n\n // check that prefix and suffix are preserved\n CHECK(s_ignored2.substr(1, 3) == \"abc\");\n CHECK(s_ignored2.substr(s_ignored2.size() - 4, 3) == \"xyz\");\n CHECK(s_ignored2_ascii.substr(1, 3) == \"abc\");\n CHECK(s_ignored2_ascii.substr(s_ignored2_ascii.size() - 4, 3) == \"xyz\");\n CHECK(s_replaced2.substr(1, 3) == \"abc\");\n CHECK(s_replaced2.substr(s_replaced2.size() - 4, 3) == \"xyz\");\n CHECK(s_replaced2_ascii.substr(1, 3) == \"abc\");\n CHECK(s_replaced2_ascii.substr(s_replaced2_ascii.size() - 4, 3) == \"xyz\");\n}\n\nvoid check_utf8string(bool success_expected, int byte1, int byte2, int byte3, int byte4);\n\n// create and check a JSON string with up to four UTF-8 bytes\nvoid check_utf8string(bool success_expected, int byte1, int byte2 = -1, int byte3 = -1, int byte4 = -1)\n{\n if (++calls % 100000 == 0)\n {\n std::cout << calls << \" of 5517507 UTF-8 strings checked\" << std::endl; // NOLINT(performance-avoid-endl)\n }\n\n static std::string json_string;\n json_string = \"\\\"\";\n\n CAPTURE(byte1)\n json_string += std::string(1, static_cast<char>(byte1));\n\n if (byte2 != -1)\n {\n CAPTURE(byte2)\n json_string += std::string(1, static_cast<char>(byte2));\n }\n\n if (byte3 != -1)\n {\n CAPTURE(byte3)\n json_string += std::string(1, static_cast<char>(byte3));\n }\n\n if (byte4 != -1)\n {\n CAPTURE(byte4)\n json_string += std::string(1, static_cast<char>(byte4));\n }\n\n json_string += \"\\\"\";\n\n CAPTURE(json_string)\n\n json _;\n if (success_expected)\n {\n CHECK_NOTHROW(_ = json::parse(json_string));\n }\n else\n {\n CHECK_THROWS_AS(_ = json::parse(json_string), json::parse_error&);\n }\n}\n} // namespace\n\nTEST_CASE(\"Unicode (4/5)\" * doctest::skip())\n{\n SECTION(\"RFC 3629\")\n {\n /*\n RFC 3629 describes in Sect. 4 the syntax of UTF-8 byte sequences as\n follows:\n\n A UTF-8 string is a sequence of octets representing a sequence of UCS\n characters. An octet sequence is valid UTF-8 only if it matches the\n following syntax, which is derived from the rules for encoding UTF-8\n and is expressed in the ABNF of [RFC2234].\n\n UTF8-octets = *( UTF8-char )\n UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4\n UTF8-1 = %x00-7F\n UTF8-2 = %xC2-DF UTF8-tail\n UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /\n %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )\n UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /\n %xF4 %x80-8F 2( UTF8-tail )\n UTF8-tail = %x80-BF\n */\n\n SECTION(\"UTF8-4 (xF1-F3 UTF8-tail UTF8-tail UTF8-tail)\")\n {\n SECTION(\"well-formed\")\n {\n for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n for (int byte4 = 0x80; byte4 <= 0xBF; ++byte4)\n {\n check_utf8string(true, byte1, byte2, byte3, byte4);\n check_utf8dump(true, byte1, byte2, byte3, byte4);\n }\n }\n }\n }\n }\n\n SECTION(\"ill-formed: missing second byte\")\n {\n for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)\n {\n check_utf8string(false, byte1);\n check_utf8dump(false, byte1);\n }\n }\n\n SECTION(\"ill-formed: missing third byte\")\n {\n for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n check_utf8string(false, byte1, byte2);\n check_utf8dump(false, byte1, byte2);\n }\n }\n }\n\n SECTION(\"ill-formed: missing fourth byte\")\n {\n for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n check_utf8string(false, byte1, byte2, byte3);\n check_utf8dump(false, byte1, byte2, byte3);\n }\n }\n }\n }\n\n SECTION(\"ill-formed: wrong second byte\")\n {\n for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)\n {\n for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)\n {\n // skip correct second byte\n if (0x80 <= byte2 && byte2 <= 0xBF)\n {\n continue;\n }\n\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n for (int byte4 = 0x80; byte4 <= 0xBF; ++byte4)\n {\n check_utf8string(false, byte1, byte2, byte3, byte4);\n check_utf8dump(false, byte1, byte2, byte3, byte4);\n }\n }\n }\n }\n }\n\n SECTION(\"ill-formed: wrong third byte\")\n {\n for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)\n {\n // skip correct third byte\n if (0x80 <= byte3 && byte3 <= 0xBF)\n {\n continue;\n }\n\n for (int byte4 = 0x80; byte4 <= 0xBF; ++byte4)\n {\n check_utf8string(false, byte1, byte2, byte3, byte4);\n check_utf8dump(false, byte1, byte2, byte3, byte4);\n }\n }\n }\n }\n }\n\n SECTION(\"ill-formed: wrong fourth byte\")\n {\n for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)\n {\n for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)\n {\n for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)\n {\n for (int byte4 = 0x00; byte4 <= 0xFF; ++byte4)\n {\n // skip correct fourth byte\n if (0x80 <= byte3 && byte3 <= 0xBF)\n {\n continue;\n }\n\n check_utf8string(false, byte1, byte2, byte3, byte4);\n check_utf8dump(false, byte1, byte2, byte3, byte4);\n }\n }\n }\n }\n }\n }\n }\n}\n\nDOCTEST_CLANG_SUPPRESS_WARNING_POP", "messages": null, "tools": null} | |
| {"id": "e6f6152bebfcaa82", "category": "code", "domain": "code", "source": "json-cpp", "license": "MIT", "license_url": "https://spdx.org/licenses/MIT.html", "path": "tests/src/unit-unicode1.cpp", "lang": "cpp", "origin": "repo_file", "synthetic": false, "render": "text", "reasoning_strength": "", "provenance": {"upstream": "https://github.com/nlohmann/json", "commit": "21af527e756435701f23e01aa8ea8dab6e050c90", "collector": "tools/harvest.py", "quarantined": "llama-tokenize aborts on this document (invalid codepoint in src/unicode.cpp); it would kill an llama-imatrix run", "quarantined_from": "pool/code/repos.jsonl"}, "chars": 26981, "sha256": "66195909daa63ead2eb972000b06f4fcc04fe3d5db3119c4f3ba8123ce8b44ae", "text": "// __ _____ _____ _____\n// __| | __| | | | JSON for Modern C++ (supporting code)\n// | | |__ | | | | | | version 3.12.0\n// |_____|_____|_____|_|___| https://github.com/nlohmann/json\n//\n// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>\n// SPDX-License-Identifier: MIT\n\n#include \"doctest_compatibility.h\"\n\n// for some reason including this after the json header leads to linker errors with VS 2017...\n#include <locale>\n#include <nlohmann/json.hpp>\nusing nlohmann::json;\n\n#include <fstream>\n#include <sstream>\n#include <iomanip>\n#include \"make_test_data_available.hpp\"\n\nTEST_CASE(\"Unicode (1/5)\" * doctest::skip())\n{\n SECTION(\"\\\\uxxxx sequences\")\n {\n // create an escaped string from a code point\n const auto codepoint_to_unicode = [](std::size_t cp)\n {\n // code points are represented as a six-character sequence: a\n // reverse solidus, followed by the lowercase letter u, followed\n // by four hexadecimal digits that encode the character's code\n // point\n std::stringstream ss;\n ss << \"\\\\u\" << std::setw(4) << std::setfill('0') << std::hex << cp;\n return ss.str();\n };\n\n SECTION(\"correct sequences\")\n {\n // generate all UTF-8 code points; in total, 1112064 code points are\n // generated: 0x1FFFFF code points - 2048 invalid values between\n // 0xD800 and 0xDFFF.\n for (std::size_t cp = 0; cp <= 0x10FFFFu; ++cp)\n {\n // string to store the code point as in \\uxxxx format\n std::string json_text = \"\\\"\";\n\n // decide whether to use one or two \\uxxxx sequences\n if (cp < 0x10000u)\n {\n // The Unicode standard permanently reserves these code point\n // values for UTF-16 encoding of the high and low surrogates, and\n // they will never be assigned a character, so there should be no\n // reason to encode them. The official Unicode standard says that\n // no UTF forms, including UTF-16, can encode these code points.\n if (cp >= 0xD800u && cp <= 0xDFFFu)\n {\n // if we would not skip these code points, we would get a\n // \"missing low surrogate\" exception\n continue;\n }\n\n // code points in the Basic Multilingual Plane can be\n // represented with one \\uxxxx sequence\n json_text += codepoint_to_unicode(cp);\n }\n else\n {\n // To escape an extended character that is not in the Basic\n // Multilingual Plane, the character is represented as a\n // 12-character sequence, encoding the UTF-16 surrogate pair\n const auto codepoint1 = 0xd800u + (((cp - 0x10000u) >> 10) & 0x3ffu);\n const auto codepoint2 = 0xdc00u + ((cp - 0x10000u) & 0x3ffu);\n json_text += codepoint_to_unicode(codepoint1) + codepoint_to_unicode(codepoint2);\n }\n\n json_text += \"\\\"\";\n CAPTURE(json_text)\n json _;\n CHECK_NOTHROW(_ = json::parse(json_text));\n }\n }\n\n SECTION(\"incorrect sequences\")\n {\n SECTION(\"incorrect surrogate values\")\n {\n json _;\n\n CHECK_THROWS_WITH_AS(_ = json::parse(\"\\\"\\\\uDC00\\\\uDC00\\\"\"), \"[json.exception.parse_error.101] parse error at line 1, column 7: syntax error while parsing value - invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF; last read: '\\\"\\\\uDC00'\", json::parse_error&);\n\n CHECK_THROWS_WITH_AS(_ = json::parse(\"\\\"\\\\uD7FF\\\\uDC00\\\"\"), \"[json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF; last read: '\\\"\\\\uD7FF\\\\uDC00'\", json::parse_error&);\n\n CHECK_THROWS_WITH_AS(_ = json::parse(\"\\\"\\\\uD800]\\\"\"), \"[json.exception.parse_error.101] parse error at line 1, column 8: syntax error while parsing value - invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF; last read: '\\\"\\\\uD800]'\", json::parse_error&);\n\n CHECK_THROWS_WITH_AS(_ = json::parse(\"\\\"\\\\uD800\\\\v\\\"\"), \"[json.exception.parse_error.101] parse error at line 1, column 9: syntax error while parsing value - invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF; last read: '\\\"\\\\uD800\\\\v'\", json::parse_error&);\n\n CHECK_THROWS_WITH_AS(_ = json::parse(\"\\\"\\\\uD800\\\\u123\\\"\"), \"[json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - invalid string: '\\\\u' must be followed by 4 hex digits; last read: '\\\"\\\\uD800\\\\u123\\\"'\", json::parse_error&);\n\n CHECK_THROWS_WITH_AS(_ = json::parse(\"\\\"\\\\uD800\\\\uDBFF\\\"\"), \"[json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF; last read: '\\\"\\\\uD800\\\\uDBFF'\", json::parse_error&);\n\n CHECK_THROWS_WITH_AS(_ = json::parse(\"\\\"\\\\uD800\\\\uE000\\\"\"), \"[json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF; last read: '\\\"\\\\uD800\\\\uE000'\", json::parse_error&);\n }\n }\n\n#if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)\n SECTION(\"incorrect sequences\")\n {\n SECTION(\"high surrogate without low surrogate\")\n {\n // D800..DBFF are high surrogates and must be followed by low\n // surrogates DC00..DFFF; here, nothing follows\n for (std::size_t cp = 0xD800u; cp <= 0xDBFFu; ++cp)\n {\n std::string json_text = \"\\\"\" + codepoint_to_unicode(cp) + \"\\\"\";\n CAPTURE(json_text)\n CHECK_THROWS_AS(json::parse(json_text), json::parse_error&);\n }\n }\n\n SECTION(\"high surrogate with wrong low surrogate\")\n {\n // D800..DBFF are high surrogates and must be followed by low\n // surrogates DC00..DFFF; here a different sequence follows\n for (std::size_t cp1 = 0xD800u; cp1 <= 0xDBFFu; ++cp1)\n {\n for (std::size_t cp2 = 0x0000u; cp2 <= 0xFFFFu; ++cp2)\n {\n if (0xDC00u <= cp2 && cp2 <= 0xDFFFu)\n {\n continue;\n }\n\n std::string json_text = \"\\\"\" + codepoint_to_unicode(cp1) + codepoint_to_unicode(cp2) + \"\\\"\";\n CAPTURE(json_text)\n CHECK_THROWS_AS(json::parse(json_text), json::parse_error&);\n }\n }\n }\n\n SECTION(\"low surrogate without high surrogate\")\n {\n // low surrogates DC00..DFFF must follow high surrogates; here,\n // they occur alone\n for (std::size_t cp = 0xDC00u; cp <= 0xDFFFu; ++cp)\n {\n std::string json_text = \"\\\"\" + codepoint_to_unicode(cp) + \"\\\"\";\n CAPTURE(json_text)\n CHECK_THROWS_AS(json::parse(json_text), json::parse_error&);\n }\n }\n\n }\n#endif\n }\n\n SECTION(\"read all unicode characters\")\n {\n // read a file with all Unicode characters stored as single-character\n // strings in a JSON array\n std::ifstream f(TEST_DATA_DIRECTORY \"/json_nlohmann_tests/all_unicode.json\");\n json j;\n CHECK_NOTHROW(f >> j);\n\n // the array has 1112064 + 1 elements (a terminating \"null\" value)\n // Note: 1112064 = 0x1FFFFF code points - 2048 invalid values between\n // 0xD800 and 0xDFFF.\n CHECK(j.size() == 1112065);\n\n SECTION(\"check JSON Pointers\")\n {\n for (const auto& s : j)\n {\n // skip non-string JSON values\n if (!s.is_string())\n {\n continue;\n }\n\n auto ptr = s.get<std::string>();\n\n // tilde must be followed by 0 or 1\n if (ptr == \"~\")\n {\n ptr += \"0\";\n }\n\n // JSON Pointers must begin with \"/\"\n ptr.insert(0, \"/\");\n\n CHECK_NOTHROW(json::json_pointer(\"/\" + ptr));\n\n // check escape/unescape roundtrip\n auto escaped = nlohmann::detail::escape(ptr);\n nlohmann::detail::unescape(escaped);\n CHECK(escaped == ptr);\n }\n }\n }\n\n SECTION(\"ignore byte-order-mark\")\n {\n SECTION(\"in a stream\")\n {\n // read a file with a UTF-8 BOM\n std::ifstream f(TEST_DATA_DIRECTORY \"/json_nlohmann_tests/bom.json\");\n json j;\n CHECK_NOTHROW(f >> j);\n }\n\n SECTION(\"with an iterator\")\n {\n std::string i = \"\\xef\\xbb\\xbf{\\n \\\"foo\\\": true\\n}\";\n json _;\n CHECK_NOTHROW(_ = json::parse(i.begin(), i.end()));\n }\n }\n\n SECTION(\"error for incomplete/wrong BOM\")\n {\n json _;\n CHECK_THROWS_AS(_ = json::parse(\"\\xef\\xbb\"), json::parse_error&);\n CHECK_THROWS_AS(_ = json::parse(\"\\xef\\xbb\\xbb\"), json::parse_error&);\n }\n}\n\nnamespace\n{\nvoid roundtrip(bool success_expected, const std::string& s);\n\nvoid roundtrip(bool success_expected, const std::string& s)\n{\n CAPTURE(s)\n json _;\n\n // create JSON string value\n const json j = s;\n // create JSON text\n const std::string ps = std::string(\"\\\"\") + s + \"\\\"\";\n\n if (success_expected)\n {\n // serialization succeeds\n CHECK_NOTHROW(j.dump());\n\n // exclude parse test for U+0000\n if (s[0] != '\\0')\n {\n // parsing JSON text succeeds\n CHECK_NOTHROW(_ = json::parse(ps));\n }\n\n // roundtrip succeeds\n CHECK_NOTHROW(_ = json::parse(j.dump()));\n\n // after roundtrip, the same string is stored\n const json jr = json::parse(j.dump());\n CHECK(jr.get<std::string>() == s);\n }\n else\n {\n // serialization fails\n CHECK_THROWS_AS(j.dump(), json::type_error&);\n\n // parsing JSON text fails\n CHECK_THROWS_AS(_ = json::parse(ps), json::parse_error&);\n }\n}\n} // namespace\n\nTEST_CASE(\"Markus Kuhn's UTF-8 decoder capability and stress test\")\n{\n // Markus Kuhn <http://www.cl.cam.ac.uk/~mgk25/> - 2015-08-28 - CC BY 4.0\n // http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n\n SECTION(\"1 Some correct UTF-8 text\")\n {\n roundtrip(true, \"κόσμε\");\n }\n\n SECTION(\"2 Boundary condition test cases\")\n {\n SECTION(\"2.1 First possible sequence of a certain length\")\n {\n // 2.1.1 1 byte (U-00000000)\n roundtrip(true, std::string(\"\\0\", 1));\n // 2.1.2 2 bytes (U-00000080)\n roundtrip(true, \"\\xc2\\x80\");\n // 2.1.3 3 bytes (U-00000800)\n roundtrip(true, \"\\xe0\\xa0\\x80\");\n // 2.1.4 4 bytes (U-00010000)\n roundtrip(true, \"\\xf0\\x90\\x80\\x80\");\n\n // 2.1.5 5 bytes (U-00200000)\n roundtrip(false, \"\\xF8\\x88\\x80\\x80\\x80\");\n // 2.1.6 6 bytes (U-04000000)\n roundtrip(false, \"\\xFC\\x84\\x80\\x80\\x80\\x80\");\n }\n\n SECTION(\"2.2 Last possible sequence of a certain length\")\n {\n // 2.2.1 1 byte (U-0000007F)\n roundtrip(true, \"\\x7f\");\n // 2.2.2 2 bytes (U-000007FF)\n roundtrip(true, \"\\xdf\\xbf\");\n // 2.2.3 3 bytes (U-0000FFFF)\n roundtrip(true, \"\\xef\\xbf\\xbf\");\n\n // 2.2.4 4 bytes (U-001FFFFF)\n roundtrip(false, \"\\xF7\\xBF\\xBF\\xBF\");\n // 2.2.5 5 bytes (U-03FFFFFF)\n roundtrip(false, \"\\xFB\\xBF\\xBF\\xBF\\xBF\");\n // 2.2.6 6 bytes (U-7FFFFFFF)\n roundtrip(false, \"\\xFD\\xBF\\xBF\\xBF\\xBF\\xBF\");\n }\n\n SECTION(\"2.3 Other boundary conditions\")\n {\n // 2.3.1 U-0000D7FF = ed 9f bf\n roundtrip(true, \"\\xed\\x9f\\xbf\");\n // 2.3.2 U-0000E000 = ee 80 80\n roundtrip(true, \"\\xee\\x80\\x80\");\n // 2.3.3 U-0000FFFD = ef bf bd\n roundtrip(true, \"\\xef\\xbf\\xbd\");\n // 2.3.4 U-0010FFFF = f4 8f bf bf\n roundtrip(true, \"\\xf4\\x8f\\xbf\\xbf\");\n\n // 2.3.5 U-00110000 = f4 90 80 80\n roundtrip(false, \"\\xf4\\x90\\x80\\x80\");\n }\n }\n\n SECTION(\"3 Malformed sequences\")\n {\n SECTION(\"3.1 Unexpected continuation bytes\")\n {\n // Each unexpected continuation byte should be separately signalled as a\n // malformed sequence of its own.\n\n // 3.1.1 First continuation byte 0x80\n roundtrip(false, \"\\x80\");\n // 3.1.2 Last continuation byte 0xbf\n roundtrip(false, \"\\xbf\");\n\n // 3.1.3 2 continuation bytes\n roundtrip(false, \"\\x80\\xbf\");\n // 3.1.4 3 continuation bytes\n roundtrip(false, \"\\x80\\xbf\\x80\");\n // 3.1.5 4 continuation bytes\n roundtrip(false, \"\\x80\\xbf\\x80\\xbf\");\n // 3.1.6 5 continuation bytes\n roundtrip(false, \"\\x80\\xbf\\x80\\xbf\\x80\");\n // 3.1.7 6 continuation bytes\n roundtrip(false, \"\\x80\\xbf\\x80\\xbf\\x80\\xbf\");\n // 3.1.8 7 continuation bytes\n roundtrip(false, \"\\x80\\xbf\\x80\\xbf\\x80\\xbf\\x80\");\n\n // 3.1.9 Sequence of all 64 possible continuation bytes (0x80-0xbf)\n roundtrip(false, \"\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\");\n }\n\n SECTION(\"3.2 Lonely start characters\")\n {\n // 3.2.1 All 32 first bytes of 2-byte sequences (0xc0-0xdf)\n roundtrip(false, \"\\xc0 \\xc1 \\xc2 \\xc3 \\xc4 \\xc5 \\xc6 \\xc7 \\xc8 \\xc9 \\xca \\xcb \\xcc \\xcd \\xce \\xcf \\xd0 \\xd1 \\xd2 \\xd3 \\xd4 \\xd5 \\xd6 \\xd7 \\xd8 \\xd9 \\xda \\xdb \\xdc \\xdd \\xde \\xdf\");\n // 3.2.2 All 16 first bytes of 3-byte sequences (0xe0-0xef)\n roundtrip(false, \"\\xe0 \\xe1 \\xe2 \\xe3 \\xe4 \\xe5 \\xe6 \\xe7 \\xe8 \\xe9 \\xea \\xeb \\xec \\xed \\xee \\xef\");\n // 3.2.3 All 8 first bytes of 4-byte sequences (0xf0-0xf7)\n roundtrip(false, \"\\xf0 \\xf1 \\xf2 \\xf3 \\xf4 \\xf5 \\xf6 \\xf7\");\n // 3.2.4 All 4 first bytes of 5-byte sequences (0xf8-0xfb)\n roundtrip(false, \"\\xf8 \\xf9 \\xfa \\xfb\");\n // 3.2.5 All 2 first bytes of 6-byte sequences (0xfc-0xfd)\n roundtrip(false, \"\\xfc \\xfd\");\n }\n\n SECTION(\"3.3 Sequences with last continuation byte missing\")\n {\n // All bytes of an incomplete sequence should be signalled as a single\n // malformed sequence, i.e., you should see only a single replacement\n // character in each of the next 10 tests. (Characters as in section 2)\n\n // 3.3.1 2-byte sequence with last byte missing (U+0000)\n roundtrip(false, \"\\xc0\");\n // 3.3.2 3-byte sequence with last byte missing (U+0000)\n roundtrip(false, \"\\xe0\\x80\");\n // 3.3.3 4-byte sequence with last byte missing (U+0000)\n roundtrip(false, \"\\xf0\\x80\\x80\");\n // 3.3.4 5-byte sequence with last byte missing (U+0000)\n roundtrip(false, \"\\xf8\\x80\\x80\\x80\");\n // 3.3.5 6-byte sequence with last byte missing (U+0000)\n roundtrip(false, \"\\xfc\\x80\\x80\\x80\\x80\");\n // 3.3.6 2-byte sequence with last byte missing (U-000007FF)\n roundtrip(false, \"\\xdf\");\n // 3.3.7 3-byte sequence with last byte missing (U-0000FFFF)\n roundtrip(false, \"\\xef\\xbf\");\n // 3.3.8 4-byte sequence with last byte missing (U-001FFFFF)\n roundtrip(false, \"\\xf7\\xbf\\xbf\");\n // 3.3.9 5-byte sequence with last byte missing (U-03FFFFFF)\n roundtrip(false, \"\\xfb\\xbf\\xbf\\xbf\");\n // 3.3.10 6-byte sequence with last byte missing (U-7FFFFFFF)\n roundtrip(false, \"\\xfd\\xbf\\xbf\\xbf\\xbf\");\n }\n\n SECTION(\"3.4 Concatenation of incomplete sequences\")\n {\n // All the 10 sequences of 3.3 concatenated, you should see 10 malformed\n // sequences being signalled:\n roundtrip(false, \"\\xc0\\xe0\\x80\\xf0\\x80\\x80\\xf8\\x80\\x80\\x80\\xfc\\x80\\x80\\x80\\x80\\xdf\\xef\\xbf\\xf7\\xbf\\xbf\\xfb\\xbf\\xbf\\xbf\\xfd\\xbf\\xbf\\xbf\\xbf\");\n }\n\n SECTION(\"3.5 Impossible bytes\")\n {\n // The following two bytes cannot appear in a correct UTF-8 string\n\n // 3.5.1 fe\n roundtrip(false, \"\\xfe\");\n // 3.5.2 ff\n roundtrip(false, \"\\xff\");\n // 3.5.3 fe fe ff ff\n roundtrip(false, \"\\xfe\\xfe\\xff\\xff\");\n }\n }\n\n SECTION(\"4 Overlong sequences\")\n {\n // The following sequences are not malformed according to the letter of\n // the Unicode 2.0 standard. However, they are longer then necessary and\n // a correct UTF-8 encoder is not allowed to produce them. A \"safe UTF-8\n // decoder\" should reject them just like malformed sequences for two\n // reasons: (1) It helps to debug applications if overlong sequences are\n // not treated as valid representations of characters, because this helps\n // to spot problems more quickly. (2) Overlong sequences provide\n // alternative representations of characters, that could maliciously be\n // used to bypass filters that check only for ASCII characters. For\n // instance, a 2-byte encoded line feed (LF) would not be caught by a\n // line counter that counts only 0x0a bytes, but it would still be\n // processed as a line feed by an unsafe UTF-8 decoder later in the\n // pipeline. From a security point of view, ASCII compatibility of UTF-8\n // sequences means also, that ASCII characters are *only* allowed to be\n // represented by ASCII bytes in the range 0x00-0x7f. To ensure this\n // aspect of ASCII compatibility, use only \"safe UTF-8 decoders\" that\n // reject overlong UTF-8 sequences for which a shorter encoding exists.\n\n SECTION(\"4.1 Examples of an overlong ASCII character\")\n {\n // With a safe UTF-8 decoder, all the following five overlong\n // representations of the ASCII character slash (\"/\") should be rejected\n // like a malformed UTF-8 sequence, for instance by substituting it with\n // a replacement character. If you see a slash below, you do not have a\n // safe UTF-8 decoder!\n\n // 4.1.1 U+002F = c0 af\n roundtrip(false, \"\\xc0\\xaf\");\n // 4.1.2 U+002F = e0 80 af\n roundtrip(false, \"\\xe0\\x80\\xaf\");\n // 4.1.3 U+002F = f0 80 80 af\n roundtrip(false, \"\\xf0\\x80\\x80\\xaf\");\n // 4.1.4 U+002F = f8 80 80 80 af\n roundtrip(false, \"\\xf8\\x80\\x80\\x80\\xaf\");\n // 4.1.5 U+002F = fc 80 80 80 80 af\n roundtrip(false, \"\\xfc\\x80\\x80\\x80\\x80\\xaf\");\n }\n\n SECTION(\"4.2 Maximum overlong sequences\")\n {\n // Below you see the highest Unicode value that is still resulting in an\n // overlong sequence if represented with the given number of bytes. This\n // is a boundary test for safe UTF-8 decoders. All five characters should\n // be rejected like malformed UTF-8 sequences.\n\n // 4.2.1 U-0000007F = c1 bf\n roundtrip(false, \"\\xc1\\xbf\");\n // 4.2.2 U-000007FF = e0 9f bf\n roundtrip(false, \"\\xe0\\x9f\\xbf\");\n // 4.2.3 U-0000FFFF = f0 8f bf bf\n roundtrip(false, \"\\xf0\\x8f\\xbf\\xbf\");\n // 4.2.4 U-001FFFFF = f8 87 bf bf bf\n roundtrip(false, \"\\xf8\\x87\\xbf\\xbf\\xbf\");\n // 4.2.5 U-03FFFFFF = fc 83 bf bf bf bf\n roundtrip(false, \"\\xfc\\x83\\xbf\\xbf\\xbf\\xbf\");\n }\n\n SECTION(\"4.3 Overlong representation of the NUL character\")\n {\n // The following five sequences should also be rejected like malformed\n // UTF-8 sequences and should not be treated like the ASCII NUL\n // character.\n\n // 4.3.1 U+0000 = c0 80\n roundtrip(false, \"\\xc0\\x80\");\n // 4.3.2 U+0000 = e0 80 80\n roundtrip(false, \"\\xe0\\x80\\x80\");\n // 4.3.3 U+0000 = f0 80 80 80\n roundtrip(false, \"\\xf0\\x80\\x80\\x80\");\n // 4.3.4 U+0000 = f8 80 80 80 80\n roundtrip(false, \"\\xf8\\x80\\x80\\x80\\x80\");\n // 4.3.5 U+0000 = fc 80 80 80 80 80\n roundtrip(false, \"\\xfc\\x80\\x80\\x80\\x80\\x80\");\n }\n }\n\n SECTION(\"5 Illegal code positions\")\n {\n // The following UTF-8 sequences should be rejected like malformed\n // sequences, because they never represent valid ISO 10646 characters and\n // a UTF-8 decoder that accepts them might introduce security problems\n // comparable to overlong UTF-8 sequences.\n\n SECTION(\"5.1 Single UTF-16 surrogates\")\n {\n // 5.1.1 U+D800 = ed a0 80\n roundtrip(false, \"\\xed\\xa0\\x80\");\n // 5.1.2 U+DB7F = ed ad bf\n roundtrip(false, \"\\xed\\xad\\xbf\");\n // 5.1.3 U+DB80 = ed ae 80\n roundtrip(false, \"\\xed\\xae\\x80\");\n // 5.1.4 U+DBFF = ed af bf\n roundtrip(false, \"\\xed\\xaf\\xbf\");\n // 5.1.5 U+DC00 = ed b0 80\n roundtrip(false, \"\\xed\\xb0\\x80\");\n // 5.1.6 U+DF80 = ed be 80\n roundtrip(false, \"\\xed\\xbe\\x80\");\n // 5.1.7 U+DFFF = ed bf bf\n roundtrip(false, \"\\xed\\xbf\\xbf\");\n }\n\n SECTION(\"5.2 Paired UTF-16 surrogates\")\n {\n // 5.2.1 U+D800 U+DC00 = ed a0 80 ed b0 80\n roundtrip(false, \"\\xed\\xa0\\x80\\xed\\xb0\\x80\");\n // 5.2.2 U+D800 U+DFFF = ed a0 80 ed bf bf\n roundtrip(false, \"\\xed\\xa0\\x80\\xed\\xbf\\xbf\");\n // 5.2.3 U+DB7F U+DC00 = ed ad bf ed b0 80\n roundtrip(false, \"\\xed\\xad\\xbf\\xed\\xb0\\x80\");\n // 5.2.4 U+DB7F U+DFFF = ed ad bf ed bf bf\n roundtrip(false, \"\\xed\\xad\\xbf\\xed\\xbf\\xbf\");\n // 5.2.5 U+DB80 U+DC00 = ed ae 80 ed b0 80\n roundtrip(false, \"\\xed\\xae\\x80\\xed\\xb0\\x80\");\n // 5.2.6 U+DB80 U+DFFF = ed ae 80 ed bf bf\n roundtrip(false, \"\\xed\\xae\\x80\\xed\\xbf\\xbf\");\n // 5.2.7 U+DBFF U+DC00 = ed af bf ed b0 80\n roundtrip(false, \"\\xed\\xaf\\xbf\\xed\\xb0\\x80\");\n // 5.2.8 U+DBFF U+DFFF = ed af bf ed bf bf\n roundtrip(false, \"\\xed\\xaf\\xbf\\xed\\xbf\\xbf\");\n }\n\n SECTION(\"5.3 Noncharacter code positions\")\n {\n // The following \"noncharacters\" are \"reserved for internal use\" by\n // applications, and according to older versions of the Unicode Standard\n // \"should never be interchanged\". Unicode Corrigendum #9 dropped the\n // latter restriction. Nevertheless, their presence in incoming UTF-8 data\n // can remain a potential security risk, depending on what use is made of\n // these codes subsequently. Examples of such internal use:\n //\n // - Some file APIs with 16-bit characters may use the integer value -1\n // = U+FFFF to signal an end-of-file (EOF) or error condition.\n //\n // - In some UTF-16 receivers, code point U+FFFE might trigger a\n // byte-swap operation (to convert between UTF-16LE and UTF-16BE).\n //\n // With such internal use of noncharacters, it may be desirable and safer\n // to block those code points in UTF-8 decoders, as they should never\n // occur legitimately in incoming UTF-8 data, and could trigger unsafe\n // behaviour in subsequent processing.\n\n // Particularly problematic noncharacters in 16-bit applications:\n\n // 5.3.1 U+FFFE = ef bf be\n roundtrip(true, \"\\xef\\xbf\\xbe\");\n // 5.3.2 U+FFFF = ef bf bf\n roundtrip(true, \"\\xef\\xbf\\xbf\");\n\n // 5.3.3 U+FDD0 .. U+FDEF\n roundtrip(true, \"\\xEF\\xB7\\x90\");\n roundtrip(true, \"\\xEF\\xB7\\x91\");\n roundtrip(true, \"\\xEF\\xB7\\x92\");\n roundtrip(true, \"\\xEF\\xB7\\x93\");\n roundtrip(true, \"\\xEF\\xB7\\x94\");\n roundtrip(true, \"\\xEF\\xB7\\x95\");\n roundtrip(true, \"\\xEF\\xB7\\x96\");\n roundtrip(true, \"\\xEF\\xB7\\x97\");\n roundtrip(true, \"\\xEF\\xB7\\x98\");\n roundtrip(true, \"\\xEF\\xB7\\x99\");\n roundtrip(true, \"\\xEF\\xB7\\x9A\");\n roundtrip(true, \"\\xEF\\xB7\\x9B\");\n roundtrip(true, \"\\xEF\\xB7\\x9C\");\n roundtrip(true, \"\\xEF\\xB7\\x9D\");\n roundtrip(true, \"\\xEF\\xB7\\x9E\");\n roundtrip(true, \"\\xEF\\xB7\\x9F\");\n roundtrip(true, \"\\xEF\\xB7\\xA0\");\n roundtrip(true, \"\\xEF\\xB7\\xA1\");\n roundtrip(true, \"\\xEF\\xB7\\xA2\");\n roundtrip(true, \"\\xEF\\xB7\\xA3\");\n roundtrip(true, \"\\xEF\\xB7\\xA4\");\n roundtrip(true, \"\\xEF\\xB7\\xA5\");\n roundtrip(true, \"\\xEF\\xB7\\xA6\");\n roundtrip(true, \"\\xEF\\xB7\\xA7\");\n roundtrip(true, \"\\xEF\\xB7\\xA8\");\n roundtrip(true, \"\\xEF\\xB7\\xA9\");\n roundtrip(true, \"\\xEF\\xB7\\xAA\");\n roundtrip(true, \"\\xEF\\xB7\\xAB\");\n roundtrip(true, \"\\xEF\\xB7\\xAC\");\n roundtrip(true, \"\\xEF\\xB7\\xAD\");\n roundtrip(true, \"\\xEF\\xB7\\xAE\");\n roundtrip(true, \"\\xEF\\xB7\\xAF\");\n\n // 5.3.4 U+nFFFE U+nFFFF (for n = 1..10)\n roundtrip(true, \"\\xF0\\x9F\\xBF\\xBF\");\n roundtrip(true, \"\\xF0\\xAF\\xBF\\xBF\");\n roundtrip(true, \"\\xF0\\xBF\\xBF\\xBF\");\n roundtrip(true, \"\\xF1\\x8F\\xBF\\xBF\");\n roundtrip(true, \"\\xF1\\x9F\\xBF\\xBF\");\n roundtrip(true, \"\\xF1\\xAF\\xBF\\xBF\");\n roundtrip(true, \"\\xF1\\xBF\\xBF\\xBF\");\n roundtrip(true, \"\\xF2\\x8F\\xBF\\xBF\");\n roundtrip(true, \"\\xF2\\x9F\\xBF\\xBF\");\n roundtrip(true, \"\\xF2\\xAF\\xBF\\xBF\");\n }\n }\n}", "messages": null, "tools": null} | |