Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2
diff options
context:
space:
mode:
authorMartin Hořeňovský2026-07-23 14:34:39 +0200
committerMartin Hořeňovský2026-07-23 14:34:43 +0200
commit46bdf1a04c92e7d34ee24f95d8d8a862498eede7 (patch)
treefe1b3f91802331c62ee8cb2c9ea8b08e96659410 /src/catch2
parentc267251e709c52cda421e9490c5cbaf62914fa52 (diff)
downloadcatch3-46bdf1a04c92e7d34ee24f95d8d8a862498eede7.tar.gz
Optimize writing of non-string values to JSON
In practice, the majority of values the JSON writer handles are strings, but this makes the code more obvious and can still provide a small speed-up.
Diffstat (limited to 'src/catch2')
-rw-r--r--src/catch2/internal/catch_jsonwriter.cpp33
1 files changed, 21 insertions, 12 deletions
diff --git a/src/catch2/internal/catch_jsonwriter.cpp b/src/catch2/internal/catch_jsonwriter.cpp
index d5122dbc..a33590f0 100644
--- a/src/catch2/internal/catch_jsonwriter.cpp
+++ b/src/catch2/internal/catch_jsonwriter.cpp
@@ -172,21 +172,30 @@ namespace Catch {
}
void JsonValueWriter::writeImpl( Catch::StringRef value, bool quote ) {
- if ( quote ) { m_os << '"'; }
- size_t current_start = 0;
- for ( size_t i = 0; i < value.size(); ++i ) {
- if ( needsEscape( value[i] ) ) {
- if ( current_start < i ) {
- m_os << value.substr( current_start, i - current_start );
+ // Escaping only makes sense for actual strings, which are passed
+ // with `quote == true`. Non-quoted values are things like bools
+ // and numbers, which cannot create inputs that need escaping.
+ if ( !quote ) {
+ m_os << value;
+ } else {
+ m_os << '"';
+ size_t current_start = 0;
+ for ( size_t i = 0; i < value.size(); ++i ) {
+ if ( needsEscape( value[i] ) ) {
+ if ( current_start < i ) {
+ m_os
+ << value.substr( current_start, i - current_start );
+ }
+ m_os << makeEscapeStringRef( value[i] );
+ current_start = i + 1;
}
- m_os << makeEscapeStringRef( value[i] );
- current_start = i + 1;
}
+ if ( current_start < value.size() ) {
+ m_os << value.substr( current_start,
+ value.size() - current_start );
+ }
+ m_os << '"';
}
- if ( current_start < value.size() ) {
- m_os << value.substr( current_start, value.size() - current_start );
- }
- if ( quote ) { m_os << '"'; }
}
} // namespace Catch