From e0826edea795ee1aac5e122c9632fa6e3556561b Mon Sep 17 00:00:00 2001 From: Mircea Trofin Date: Thu, 6 May 2021 11:12:36 -0700 Subject: [PATCH] Fix StrSplit empty string case (#1142) This also fixes #1135. Because StrSplit was returning a vector with an empty string, it was treated by PerfCounters::Create as a legitimate ask for setting up a counter with that name. The empty vector is understood by PerfCounters as "just return NoCounters()". --- src/string_util.cc | 1 + test/string_util_gtest.cc | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/string_util.cc b/src/string_util.cc index 53b1532b..cb973a7c 100644 --- a/src/string_util.cc +++ b/src/string_util.cc @@ -164,6 +164,7 @@ std::string StrFormat(const char* format, ...) { } std::vector StrSplit(const std::string& str, char delim) { + if (str.empty()) return {}; std::vector ret; size_t first = 0; size_t next = str.find(delim); diff --git a/test/string_util_gtest.cc b/test/string_util_gtest.cc index 77a719a6..1ad9fb9d 100644 --- a/test/string_util_gtest.cc +++ b/test/string_util_gtest.cc @@ -151,7 +151,7 @@ TEST(StringUtilTest, stod) { } TEST(StringUtilTest, StrSplit) { - EXPECT_EQ(benchmark::StrSplit("", ','), std::vector{""}); + EXPECT_EQ(benchmark::StrSplit("", ','), std::vector{}); EXPECT_EQ(benchmark::StrSplit("hello", ','), std::vector({"hello"})); EXPECT_EQ(benchmark::StrSplit("hello,there", ','),