Compare commits

..
Author SHA1 Message Date
Niels Lohmann 75e067e3df Return early from json_pointer::to_string() for an empty pointer
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-22 22:45:19 +02:00
Niels Lohmann a9ed3feca3 Copy the tokens of json_pointer::to_string() with std::copy
clang-tidy (bugprone-not-null-terminated-result) took the memcpy into
the result string for a C string missing its terminator. std::copy is
the same memmove; to_string() instructions are unchanged (Apple clang).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-22 13:32:19 +02:00
Niels Lohmann eb2a092d70 Copy the tokens of a pointer without escapes straight into to_string()
to_string() appended a slash and every escaped token with library calls.
Tokens that contain neither '~' nor '/' (nearly all) are copied into a
string of the final size instead; the others are escaped as before.

json_pointer::to_string() over the keys of citm_catalog (instructions on macOS,
Linux Ir GCC 13 / Clang 18): 47.5M -> 30.6M, 33.6M -> 25.8M / 35.9M -> 22.8M;
twitter 18.8M -> 12.9M, 13.7M -> 11.2M / 14.5M -> 9.9M.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-22 09:30:56 +02:00
Niels Lohmann 2a2c7d30f0 Split a JSON pointer in one scan and construct the tokens in place
split() looked for the end of every reference token with find_first_of('/'),
for a '~' in it with a second find_first_of, and moved the token into the
vector. One loop over the characters now finds both, and a token without an
escape is constructed in the vector. Tokens with an escape are checked and
unescaped as before, so the exceptions are the same.

json_pointer(string) over the keys of citm_catalog (instructions on macOS, Linux
Ir GCC 13 / Clang 18): 42.3M -> 36.4M, 42.9M -> 35.1M / 41.2M -> 34.7M;
twitter 17.7M -> 16.2M, 18.5M -> 16.3M / 17.6M -> 16.0M.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-22 09:30:54 +02:00
4 changed files with 110 additions and 67 deletions

No files matched your search

@@ -37,14 +37,6 @@ jobs:
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr"
})[0];
// a run that failed before its jobs started (e.g. a stale queued
// run cancelled long after its push) has no artifact to report on
if (!matchArtifact) {
core.notice('Run ${{ github.event.workflow_run.id }} has no "pr" artifact; nothing to comment on.');
core.setOutput('found', 'false');
return;
}
core.setOutput('found', 'true');
var download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
@@ -59,11 +51,9 @@ jobs:
# Extract the untrusted PR artifact into a dedicated empty directory and
# read only the two expected files by fixed path afterwards. This avoids a
# malicious archive overwriting workspace files or escaping via ../ paths.
- if: steps.download.outputs.found == 'true'
run: unzip -o pr.zip -d ./pr_artifact
- run: unzip -o pr.zip -d ./pr_artifact
- name: 'Comment on PR'
if: steps.download.outputs.found == 'true'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
+54 -27
View File
@@ -8,7 +8,7 @@
#pragma once
#include <algorithm> // all_of
#include <algorithm> // all_of, copy
#include <array> // array
#include <cctype> // isdigit
#include <cerrno> // errno, ERANGE
@@ -80,14 +80,38 @@ class json_pointer
/// @sa https://json.nlohmann.me/api/json_pointer/to_string/
string_t to_string() const
{
if (reference_tokens.empty())
{
return string_t();
}
// one buffer: every token adds its escaped form (at least its length) and a slash
std::size_t length = 0;
bool plain = true;
for (const auto& reference_token : reference_tokens)
{
length += reference_token.size() + 1;
const char* const data = reference_token.data();
for (std::size_t i = 0, size = reference_token.size(); plain && i < size; ++i)
{
plain = data[i] != '~' && data[i] != '/';
}
}
string_t result;
if (JSON_HEDLEY_LIKELY(plain))
{
// nothing to escape: the tokens are copied into the string
result.resize(length);
char* out = &result[0];
for (const auto& reference_token : reference_tokens)
{
*out++ = '/';
out = std::copy(reference_token.data(), reference_token.data() + reference_token.size(), out);
}
return result;
}
result.reserve(length);
for (const auto& reference_token : reference_tokens)
{
@@ -1198,33 +1222,32 @@ class json_pointer
}
result.reserve(token_count);
// extract the reference tokens:
// - slash: position of the last read slash (or end of string)
// - start: position after the previous slash
for (
// search for the first slash after the first character
std::size_t slash = reference_string.find_first_of('/', 1),
// set the beginning of the first reference token
start = 1;
// we can stop if start == 0 (if slash == string_t::npos)
start != 0;
// set the beginning of the next reference token
// (will eventually be 0 if slash == string_t::npos)
start = (slash == string_t::npos) ? 0 : slash + 1,
// find next slash
slash = reference_string.find_first_of('/', start))
// extract the reference tokens: every one runs from after a slash to
// the next slash or the end of the string; a token without a '~' (the
// common case) is stored as it is, others are checked and unescaped
const char* const data = reference_string.data();
const char* const end = data + size;
for (const char* start = data + 1;; ++start)
{
// use the text between the beginning of the reference token
// (start) and the last slash (slash).
const auto count = (slash == string_t::npos ? reference_string.size() : slash) - start;
auto reference_token = string_t(reference_string.data() + start, count);
const char* slash = start;
bool escaped = false;
while (slash != end && *slash != '/')
{
escaped = escaped || *slash == '~';
++slash;
}
// check reference tokens are properly escaped; a token without
// a '~' (the common case) needs neither check nor unescaping
std::size_t pos = reference_token.find_first_of('~');
if (pos != string_t::npos)
const auto count = static_cast<std::size_t>(slash - start);
if (JSON_HEDLEY_LIKELY(!escaped))
{
for (; pos != string_t::npos; pos = reference_token.find_first_of('~', pos + 1))
result.emplace_back(start, count);
}
else
{
auto reference_token = string_t(start, count);
// check reference tokens are properly escaped
for (std::size_t pos = reference_token.find_first_of('~'); pos != string_t::npos; pos = reference_token.find_first_of('~', pos + 1))
{
JSON_ASSERT(reference_token[pos] == '~');
@@ -1238,10 +1261,14 @@ class json_pointer
}
detail::unescape(reference_token);
result.push_back(std::move(reference_token));
}
// finally, store the reference token
result.push_back(std::move(reference_token));
if (slash == end)
{
break;
}
start = slash;
}
return result;
+54 -27
View File
@@ -22350,7 +22350,7 @@ NLOHMANN_JSON_NAMESPACE_END
#include <algorithm> // all_of
#include <algorithm> // all_of, copy
#include <array> // array
#include <cctype> // isdigit
#include <cerrno> // errno, ERANGE
@@ -22430,14 +22430,38 @@ class json_pointer
/// @sa https://json.nlohmann.me/api/json_pointer/to_string/
string_t to_string() const
{
if (reference_tokens.empty())
{
return string_t();
}
// one buffer: every token adds its escaped form (at least its length) and a slash
std::size_t length = 0;
bool plain = true;
for (const auto& reference_token : reference_tokens)
{
length += reference_token.size() + 1;
const char* const data = reference_token.data();
for (std::size_t i = 0, size = reference_token.size(); plain && i < size; ++i)
{
plain = data[i] != '~' && data[i] != '/';
}
}
string_t result;
if (JSON_HEDLEY_LIKELY(plain))
{
// nothing to escape: the tokens are copied into the string
result.resize(length);
char* out = &result[0];
for (const auto& reference_token : reference_tokens)
{
*out++ = '/';
out = std::copy(reference_token.data(), reference_token.data() + reference_token.size(), out);
}
return result;
}
result.reserve(length);
for (const auto& reference_token : reference_tokens)
{
@@ -23548,33 +23572,32 @@ class json_pointer
}
result.reserve(token_count);
// extract the reference tokens:
// - slash: position of the last read slash (or end of string)
// - start: position after the previous slash
for (
// search for the first slash after the first character
std::size_t slash = reference_string.find_first_of('/', 1),
// set the beginning of the first reference token
start = 1;
// we can stop if start == 0 (if slash == string_t::npos)
start != 0;
// set the beginning of the next reference token
// (will eventually be 0 if slash == string_t::npos)
start = (slash == string_t::npos) ? 0 : slash + 1,
// find next slash
slash = reference_string.find_first_of('/', start))
// extract the reference tokens: every one runs from after a slash to
// the next slash or the end of the string; a token without a '~' (the
// common case) is stored as it is, others are checked and unescaped
const char* const data = reference_string.data();
const char* const end = data + size;
for (const char* start = data + 1;; ++start)
{
// use the text between the beginning of the reference token
// (start) and the last slash (slash).
const auto count = (slash == string_t::npos ? reference_string.size() : slash) - start;
auto reference_token = string_t(reference_string.data() + start, count);
const char* slash = start;
bool escaped = false;
while (slash != end && *slash != '/')
{
escaped = escaped || *slash == '~';
++slash;
}
// check reference tokens are properly escaped; a token without
// a '~' (the common case) needs neither check nor unescaping
std::size_t pos = reference_token.find_first_of('~');
if (pos != string_t::npos)
const auto count = static_cast<std::size_t>(slash - start);
if (JSON_HEDLEY_LIKELY(!escaped))
{
for (; pos != string_t::npos; pos = reference_token.find_first_of('~', pos + 1))
result.emplace_back(start, count);
}
else
{
auto reference_token = string_t(start, count);
// check reference tokens are properly escaped
for (std::size_t pos = reference_token.find_first_of('~'); pos != string_t::npos; pos = reference_token.find_first_of('~', pos + 1))
{
JSON_ASSERT(reference_token[pos] == '~');
@@ -23588,10 +23611,14 @@ class json_pointer
}
detail::unescape(reference_token);
result.push_back(std::move(reference_token));
}
// finally, store the reference token
result.push_back(std::move(reference_token));
if (slash == end)
{
break;
}
start = slash;
}
return result;
+1 -2
View File
@@ -78,8 +78,7 @@ TEST_CASE("detail::bit_cast")
// to and from byte arrays
const auto bytes = bit_cast<std::array<std::uint8_t, 4>>(std::uint32_t{0x01020304u});
// qualified: with a std::array argument, ADL would also find std::bit_cast in C++20
CHECK(nlohmann::detail::bit_cast<std::uint32_t>(bytes) == 0x01020304u);
CHECK(bit_cast<std::uint32_t>(bytes) == 0x01020304u);
static_assert(noexcept(bit_cast<std::uint64_t>(1.0)), "bit_cast must be noexcept");
}