From: Sam McCall Date: Mon, 11 Nov 2019 17:25:01 +0000 (+0100) Subject: [Support] Add erase() to json::Object X-Git-Tag: llvmorg-11-init~4535 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4162875c3b2a2326c8f40b6cba98308d819f4d49;p=platform%2Fupstream%2Fllvm.git [Support] Add erase() to json::Object --- diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h index 0ca4109..2657ea7 100644 --- a/llvm/include/llvm/Support/JSON.h +++ b/llvm/include/llvm/Support/JSON.h @@ -122,6 +122,8 @@ public: std::pair try_emplace(ObjectKey &&K, Ts &&... Args) { return M.try_emplace(std::move(K), std::forward(Args)...); } + bool erase(StringRef K); + void erase(iterator I) { M.erase(I); } iterator find(StringRef K) { return M.find_as(K); } const_iterator find(StringRef K) const { return M.find_as(K); } @@ -555,6 +557,9 @@ inline Object::Object(std::initializer_list Properties) { inline std::pair Object::insert(KV E) { return try_emplace(std::move(E.K), std::move(E.V)); } +inline bool Object::erase(StringRef K) { + return M.erase(ObjectKey(K)); +} // Standard deserializers are provided for primitive types. // See comments on Value. diff --git a/llvm/unittests/Support/JSONTest.cpp b/llvm/unittests/Support/JSONTest.cpp index 14c11b1..93efcf2 100644 --- a/llvm/unittests/Support/JSONTest.cpp +++ b/llvm/unittests/Support/JSONTest.cpp @@ -71,7 +71,7 @@ TEST(JSONTest, CanonicalOutput) { } TEST(JSONTest, Escaping) { - std::string test = { + std::string Test = { 0, // Strings may contain nulls. '\b', '\f', // Have mnemonics, but we escape numerically. '\r', '\n', '\t', // Escaped with mnemonics. @@ -80,17 +80,17 @@ TEST(JSONTest, Escaping) { '\xce', '\x94', // Non-ASCII UTF-8 is not escaped. }; - std::string teststring = R"("\u0000\u0008\u000c\r\n\tS\"\\)" + std::string TestString = R"("\u0000\u0008\u000c\r\n\tS\"\\)" "\x7f\xCE\x94\""; - EXPECT_EQ(teststring, s(test)); + EXPECT_EQ(TestString, s(Test)); EXPECT_EQ(R"({"object keys are\nescaped":true})", s(Object{{"object keys are\nescaped", true}})); } TEST(JSONTest, PrettyPrinting) { - const char str[] = R"({ + const char Str[] = R"({ "empty_array": [], "empty_object": {}, "full_array": [ @@ -106,7 +106,7 @@ TEST(JSONTest, PrettyPrinting) { } })"; - EXPECT_EQ(str, sp(Object{ + EXPECT_EQ(Str, sp(Object{ {"empty_object", Object{}}, {"empty_array", {}}, {"full_array", {1, nullptr}}, @@ -120,6 +120,33 @@ TEST(JSONTest, PrettyPrinting) { })); } +TEST(JSONTest, Array) { + Array A{1, 2}; + A.emplace_back(3); + A.emplace(++A.begin(), 0); + A.push_back(4); + A.insert(++++A.begin(), 99); + + EXPECT_EQ(A.size(), 6u); + EXPECT_EQ(R"([1,0,99,2,3,4])", s(std::move(A))); +} + +TEST(JSONTest, Object) { + Object O{{"a", 1}, {"b", 2}, {"c", 3}}; + EXPECT_TRUE(O.try_emplace("d", 4).second); + EXPECT_FALSE(O.try_emplace("a", 4).second); + + auto D = O.find("d"); + EXPECT_FALSE(D == O.end()); + auto E = O.find("e"); + EXPECT_TRUE(E == O.end()); + + O.erase("b"); + O.erase(D); + EXPECT_EQ(O.size(), 2u); + EXPECT_EQ(R"({"a":1,"c":3})", s(std::move(O))); +} + TEST(JSONTest, Parse) { auto Compare = [](llvm::StringRef S, Value Expected) { if (auto E = parse(S)) {