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 19:01:53 +0200
committerMartin Hořeňovský2026-07-23 19:10:14 +0200
commitbb8873ccd710d55a4fa7cc02d710fdb4da69cfb0 (patch)
tree930aac9ae23deb544cc5981599e7003e342c3e41 /src/catch2
parent46bdf1a04c92e7d34ee24f95d8d8a862498eede7 (diff)
downloadcatch3-bb8873ccd710d55a4fa7cc02d710fdb4da69cfb0.tar.gz
JSONWriter uses LUT for faster checking if a char needs escaping
This provides nice speedup when writing strings that don't need escaping, at about 6% in Debug build and ~40% in Release build. If the strings do need escaping the speedup is much smaller, at ~1% in Debug and ~6% in Release build, as the cost of actually escaping the strings dwarves the cost of checking.
Diffstat (limited to 'src/catch2')
-rw-r--r--src/catch2/internal/catch_jsonwriter.cpp19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/catch2/internal/catch_jsonwriter.cpp b/src/catch2/internal/catch_jsonwriter.cpp
index a33590f0..190a978c 100644
--- a/src/catch2/internal/catch_jsonwriter.cpp
+++ b/src/catch2/internal/catch_jsonwriter.cpp
@@ -16,9 +16,22 @@
namespace Catch {
namespace {
- static bool needsEscape( char c ) {
- return c == '"' || c == '\\' || c == '\b' || c == '\f' ||
- c == '\n' || c == '\r' || c == '\t';
+ struct EscapeLUT {
+ bool escape[256] = {};
+ constexpr EscapeLUT() {
+ escape[static_cast<unsigned char>( '"' )] = true;
+ escape[static_cast<unsigned char>( '\\' )] = true;
+ escape[static_cast<unsigned char>( '\b' )] = true;
+ escape[static_cast<unsigned char>( '\f' )] = true;
+ escape[static_cast<unsigned char>( '\n' )] = true;
+ escape[static_cast<unsigned char>( '\r' )] = true;
+ escape[static_cast<unsigned char>( '\t' )] = true;
+ }
+ };
+ static constexpr EscapeLUT escapeLUT{};
+
+ static constexpr bool needsEscape( char c ) {
+ return escapeLUT.escape[static_cast<unsigned char>( c )];
}
static Catch::StringRef makeEscapeStringRef( char c ) {