Improve loops & fix cpp check warning
- This commit improves certain while loops to range based for loops.
- This commit also fixes the cppcheck warning that mentions about
performance issues when using postfix operators on non-primitive
types.
Tested By:
- A function is unittested.
- GET on both EthernetInterfaces & certificate service
looks good without any issues.
Signed-off-by: Manojkiran Eda <manojkiran.eda@gmail.com>
Change-Id: I85420f7bf9af45a97e1a93b916f292c2516f5802
diff --git a/include/dbus_utility.hpp b/include/dbus_utility.hpp
index e1360f7..8ba9a57 100644
--- a/include/dbus_utility.hpp
+++ b/include/dbus_utility.hpp
@@ -17,6 +17,7 @@
#include <sdbusplus/message.hpp>
+#include <filesystem>
#include <regex>
namespace dbus
@@ -54,27 +55,21 @@
inline bool getNthStringFromPath(const std::string& path, int index,
std::string& result)
{
- int count = 0;
- std::string::const_iterator first = path.begin();
- std::string::const_iterator last = path.end();
- for (std::string::const_iterator it = path.begin(); it < path.end(); it++)
+ if (index < 0)
{
- // skip first character as it's either a leading slash or the first
- // character in the word
- if (it == path.begin())
+ return false;
+ }
+
+ std::filesystem::path p1(path);
+ int count = -1;
+ for (auto const& element : p1)
+ {
+ if (element.has_filename())
{
- continue;
- }
- if (*it == '/')
- {
- count++;
+ ++count;
if (count == index)
{
- first = it;
- }
- if (count == index + 1)
- {
- last = it;
+ result = element.stem().string();
break;
}
}
@@ -83,12 +78,7 @@
{
return false;
}
- if (first != path.begin())
- {
- first++;
- }
- result = path.substr(static_cast<size_t>(first - path.begin()),
- static_cast<size_t>(last - first));
+
return true;
}
diff --git a/include/ut/dbus_utility_test.cpp b/include/ut/dbus_utility_test.cpp
new file mode 100644
index 0000000..d4342a5
--- /dev/null
+++ b/include/ut/dbus_utility_test.cpp
@@ -0,0 +1,36 @@
+#include <boost/algorithm/string.hpp>
+#include <boost/container/flat_set.hpp>
+#include <dbus_singleton.hpp>
+#include <dbus_utility.hpp>
+
+#include "gmock/gmock.h"
+
+TEST(DbusUtility, getNthStringFromPathGoodTest)
+{
+ std::string path("/0th/1st/2nd/3rd");
+ std::string result;
+ EXPECT_TRUE(dbus::utility::getNthStringFromPath(path, 0, result));
+ EXPECT_EQ(result, "0th");
+ EXPECT_TRUE(dbus::utility::getNthStringFromPath(path, 1, result));
+ EXPECT_EQ(result, "1st");
+ EXPECT_TRUE(dbus::utility::getNthStringFromPath(path, 2, result));
+ EXPECT_EQ(result, "2nd");
+ EXPECT_TRUE(dbus::utility::getNthStringFromPath(path, 3, result));
+ EXPECT_EQ(result, "3rd");
+ EXPECT_FALSE(dbus::utility::getNthStringFromPath(path, 4, result));
+}
+
+TEST(DbusUtility, getNthStringFromPathBadTest)
+{
+ std::string path("////0th///1st//\2nd///3rd?/");
+ std::string result;
+ EXPECT_TRUE(dbus::utility::getNthStringFromPath(path, 0, result));
+ EXPECT_EQ(result, "0th");
+ EXPECT_TRUE(dbus::utility::getNthStringFromPath(path, 1, result));
+ EXPECT_EQ(result, "1st");
+ EXPECT_TRUE(dbus::utility::getNthStringFromPath(path, 2, result));
+ EXPECT_EQ(result, "\2nd");
+ EXPECT_TRUE(dbus::utility::getNthStringFromPath(path, 3, result));
+ EXPECT_EQ(result, "3rd?");
+ EXPECT_FALSE(dbus::utility::getNthStringFromPath(path, -1, result));
+}