From b7b86baec9ce47569affc5db54a20a6cc520e0f0 Mon Sep 17 00:00:00 2001 From: Chris Mumford Date: Wed, 8 May 2019 17:28:28 -0700 Subject: [PATCH] Using std::ostringstream in key DebugString. Switching from snprintf to std::ostringstream eliminates cast warning for (unsigned long long). PiperOrigin-RevId: 247326681 --- db/dbformat.cc | 23 ++++++++++------------- db/dbformat_test.cc | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/db/dbformat.cc b/db/dbformat.cc index 69e8dc6..459eddf 100644 --- a/db/dbformat.cc +++ b/db/dbformat.cc @@ -6,6 +6,8 @@ #include +#include + #include "port/port.h" #include "util/coding.h" @@ -23,25 +25,20 @@ void AppendInternalKey(std::string* result, const ParsedInternalKey& key) { } std::string ParsedInternalKey::DebugString() const { - char buf[50]; - snprintf(buf, sizeof(buf), "' @ %llu : %d", (unsigned long long)sequence, - int(type)); - std::string result = "'"; - result += EscapeString(user_key.ToString()); - result += buf; - return result; + std::ostringstream ss; + ss << '\'' << EscapeString(user_key.ToString()) << "' @ " << sequence << " : " + << static_cast(type); + return ss.str(); } std::string InternalKey::DebugString() const { - std::string result; ParsedInternalKey parsed; if (ParseInternalKey(rep_, &parsed)) { - result = parsed.DebugString(); - } else { - result = "(bad)"; - result.append(EscapeString(rep_)); + return parsed.DebugString(); } - return result; + std::ostringstream ss; + ss << "(bad)" << EscapeString(rep_); + return ss.str(); } const char* InternalKeyComparator::Name() const { diff --git a/db/dbformat_test.cc b/db/dbformat_test.cc index 87e6aae..f75d850 100644 --- a/db/dbformat_test.cc +++ b/db/dbformat_test.cc @@ -106,6 +106,20 @@ TEST(FormatTest, InternalKeyShortestSuccessor) { ShortSuccessor(IKey("\xff\xff", 100, kTypeValue))); } +TEST(FormatTest, ParsedInternalKeyDebugString) { + ParsedInternalKey key("The \"key\" in 'single quotes'", 42, kTypeValue); + + ASSERT_EQ("'The \"key\" in 'single quotes'' @ 42 : 1", key.DebugString()); +} + +TEST(FormatTest, InternalKeyDebugString) { + InternalKey key("The \"key\" in 'single quotes'", 42, kTypeValue); + ASSERT_EQ("'The \"key\" in 'single quotes'' @ 42 : 1", key.DebugString()); + + InternalKey invalid_key; + ASSERT_EQ("(bad)", invalid_key.DebugString()); +} + } // namespace leveldb int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }