util: Remove temporary strings in getIgnoredInterfaces
Change-Id: I75be67b88a3f7b4dff6de9af25d3f496be09da14
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/test_util.cpp b/test/test_util.cpp
index a63e205..d335adc 100644
--- a/test/test_util.cpp
+++ b/test/test_util.cpp
@@ -342,7 +342,7 @@
TEST(IgnoredInterfaces, NotEmpty)
{
using ::testing::ContainerEq;
- std::set<std::string> expected = {"eth0"};
+ std::set<std::string_view> expected = {"eth0"};
auto ret = internal::parseInterfaces("eth0");
EXPECT_THAT(ret, ContainerEq(expected));
diff --git a/util.cpp b/util.cpp
index 6cc42d1..5e1c92b 100644
--- a/util.cpp
+++ b/util.cpp
@@ -18,7 +18,6 @@
#include <nlohmann/json.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/log.hpp>
-#include <sstream>
#include <stdexcept>
#include <stdplus/raw.hpp>
#include <string>
@@ -94,43 +93,47 @@
}
/** @brief Get ignored interfaces from environment */
-std::string getIgnoredInterfacesEnv()
+std::string_view getIgnoredInterfacesEnv()
{
auto r = std::getenv("IGNORED_INTERFACES");
if (r == nullptr)
{
- return {};
+ return "";
}
return r;
}
/** @brief Parse the comma separated interface names */
-std::set<std::string> parseInterfaces(const std::string& interfaces)
+std::set<std::string_view> parseInterfaces(std::string_view interfaces)
{
- std::set<std::string> result;
- std::stringstream ss(interfaces);
- while (ss.good())
+ std::set<std::string_view> result;
+ while (true)
{
- std::string str;
- std::getline(ss, str, ',');
- // Trim str
- if (!str.empty())
+ auto sep = interfaces.find(',');
+ auto interface = interfaces.substr(0, sep);
+ while (!interface.empty() && std::isspace(interface.front()))
{
- str.erase(
- std::remove_if(str.begin(), str.end(),
- [](unsigned char c) { return std::isspace(c); }),
- str.end());
+ interface.remove_prefix(1);
}
- if (!str.empty())
+ while (!interface.empty() && std::isspace(interface.back()))
{
- result.insert(str);
+ interface.remove_suffix(1);
}
+ if (!interface.empty())
+ {
+ result.insert(interface);
+ }
+ if (sep == interfaces.npos)
+ {
+ break;
+ }
+ interfaces = interfaces.substr(sep + 1);
}
return result;
}
/** @brief Get the ignored interfaces */
-const std::set<std::string>& getIgnoredInterfaces()
+const std::set<std::string_view>& getIgnoredInterfaces()
{
static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv());
return ignoredInterfaces;
diff --git a/util.hpp b/util.hpp
index 9c180ee..804d492 100644
--- a/util.hpp
+++ b/util.hpp
@@ -169,13 +169,13 @@
void executeCommandinChildProcess(const char* path, char** args);
/** @brief Get ignored interfaces from environment */
-std::string getIgnoredInterfacesEnv();
+std::string_view getIgnoredInterfacesEnv();
/** @brief Parse the comma separated interface names */
-std::set<std::string> parseInterfaces(const std::string& interfaces);
+std::set<std::string_view> parseInterfaces(std::string_view interfaces);
/** @brief Get the ignored interfaces */
-const std::set<std::string>& getIgnoredInterfaces();
+const std::set<std::string_view>& getIgnoredInterfaces();
} // namespace internal