Disable token compress in str
There are certain cases where we use this split function, and we expect
tokens to be read out. For example:
/xyz/openbmc_project/sensors/unit/name
Should split into a "" in the first position. This use case is not
common, and a quick grep shows only two places in the code expect this
behavior. Boost::split has this behavior already, which is what this
function is emulating. While we could fix these, in the end they should
be following the rules outlined in COMMON_ERRORS.md, which disallow
this kind of parsing completely.
Tested: New unit tests passing.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: Iec3dcbf2b495b2b3b4ed419172c4133b16f7c65d
diff --git a/include/str_utility.hpp b/include/str_utility.hpp
index 39e1c82..7142583 100644
--- a/include/str_utility.hpp
+++ b/include/str_utility.hpp
@@ -14,10 +14,11 @@
{
size_t start = 0;
size_t end = 0;
- while ((start = str.find_first_not_of(delim, end)) != std::string::npos)
+ while (end <= str.size())
{
end = str.find(delim, start);
strings.emplace_back(str.substr(start, end - start));
+ start = end + 1;
}
}
} // namespace bmcweb
diff --git a/test/include/str_utility_test.cpp b/test/include/str_utility_test.cpp
index bab14c9..a32ce09 100644
--- a/test/include/str_utility_test.cpp
+++ b/test/include/str_utility_test.cpp
@@ -15,7 +15,6 @@
{
using ::testing::ElementsAre;
-using ::testing::IsEmpty;
TEST(Split, PositiveTests)
{
@@ -23,9 +22,31 @@
std::vector<std::string> vec;
split(vec, "xx-abc-xx-abb", '-');
EXPECT_THAT(vec, ElementsAre("xx", "abc", "xx", "abb"));
+
vec.clear();
split(vec, "", '-');
- EXPECT_THAT(vec, IsEmpty());
+ EXPECT_THAT(vec, ElementsAre(""));
+
+ vec.clear();
+ split(vec, "foo/", '/');
+ EXPECT_THAT(vec, ElementsAre("foo", ""));
+
+ vec.clear();
+ split(vec, "/bar", '/');
+ EXPECT_THAT(vec, ElementsAre("", "bar"));
+
+ vec.clear();
+ split(vec, "/", '/');
+ EXPECT_THAT(vec, ElementsAre("", ""));
+}
+
+TEST(Split, Sensor)
+{
+ using bmcweb::split;
+ std::vector<std::string> vec;
+ split(vec, "/xyz/openbmc_project/sensors/unit/name", '/');
+ EXPECT_THAT(vec, ElementsAre("", "xyz", "openbmc_project", "sensors",
+ "unit", "name"));
}
} // namespace