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;
}