From fabcf6d367ef22a992f8f8ed03a81945e28bfabf Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 16 Mar 2025 17:03:47 +0100 Subject: [PATCH] refactor: prefer raw strings for strings with lots of escapes --- src/writer/internal/file_scanner.cpp | 27 ++++++++++++++------------- src/writer/scanner.cpp | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/writer/internal/file_scanner.cpp b/src/writer/internal/file_scanner.cpp index fd49f6f7..5716bdfa 100644 --- a/src/writer/internal/file_scanner.cpp +++ b/src/writer/internal/file_scanner.cpp @@ -563,17 +563,18 @@ void file_scanner_::dump_value(std::ostream& os, auto ino_num = p->inode_num(); os << "{\n" - << " \"ptr\": \"" + << R"( "ptr": ")" << fmt::format("{}", reinterpret_cast(p)) << "\",\n" - << " \"path\": " << nlohmann::json{p->path_as_string()}.dump() + << R"( "path": )" << nlohmann::json{p->path_as_string()}.dump() << ",\n" - << " \"size\": " << fmt::format("{}", p->size()) << ",\n" - << " \"refcnt\": " << fmt::format("{}", p->refcount()) << ",\n" - << " \"hash\": \"" << folly::hexlify(p->hash()) << "\",\n" - << " \"invalid\": " << (p->is_invalid() ? "true" : "false") << ",\n" - << " \"inode_num\": " + << R"( "size": )" << fmt::format("{}", p->size()) << ",\n" + << R"( "refcnt": )" << fmt::format("{}", p->refcount()) << ",\n" + << R"( "hash": ")" << folly::hexlify(p->hash()) << "\",\n" + << R"( "invalid": )" << (p->is_invalid() ? "true" : "false") + << ",\n" + << R"( "inode_num": )" << (ino_num ? fmt::format("{}", *ino_num) : "null") << ",\n" - << " \"inode\": \"" + << R"( "inode": ")" << fmt::format("{}", reinterpret_cast(ino.get())) << "\"\n" << " }"; } @@ -606,9 +607,9 @@ void file_scanner_::dump_inodes(std::ostream& os) const { } first = false; os << " {\n" - << " \"ptr\": \"" + << R"( "ptr": ")" << fmt::format("{}", reinterpret_cast(ino.get())) << "\",\n" - << " \"files\": "; + << R"( "files": )"; dump_value(os, ino->all()); os << "\n }"; } @@ -626,12 +627,12 @@ void file_scanner_::dump_inode_create_info( } first = false; os << " {\n" - << " \"inode\": \"" + << R"( "inode": ")" << fmt::format("{}", reinterpret_cast(ici.i)) << "\",\n" - << " \"file\": "; + << R"( "file": )"; dump_value(os, ici.f); os << ",\n" - << " \"line\": " << fmt::format("{}", ici.line) << "\n" + << R"( "line": )" << fmt::format("{}", ici.line) << "\n" << " }"; } os << "\n ]"; diff --git a/src/writer/scanner.cpp b/src/writer/scanner.cpp index f661cadc..e5495e74 100644 --- a/src/writer/scanner.cpp +++ b/src/writer/scanner.cpp @@ -377,7 +377,7 @@ scanner_::add_entry(std::filesystem::path const& name, auto tmp [[maybe_unused]] = name.filename().u8string(); } catch (std::system_error const& e) { LOG_ERROR << fmt::format( - "invalid file name in \"{}\", storing as \"{}\": {}", + R"(invalid file name in "{}", storing as "{}": {})", path_to_utf8_string_sanitized(name.parent_path()), pe->name(), error_cp_to_utf8(e.what()));