From cbbfa9672aed51df7bd2f2d7e08e9f0eb0fdb7ec Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Tue, 13 Mar 2018 16:46:28 -0700 Subject: Make getUrl not copy Make get url return a const std::string* instead of a copy. Also, make the getSubroutes function modify strings in place rather than construct new strings where possible Change-Id: I68d8053a62abd8d907ecb6717e0f266eee4d09a8 Signed-off-by: Ed Tanous --- redfish-core/include/node.hpp | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/redfish-core/include/node.hpp b/redfish-core/include/node.hpp index a33c8e0..0a1564a 100644 --- a/redfish-core/include/node.hpp +++ b/redfish-core/include/node.hpp @@ -41,12 +41,13 @@ class Node { virtual ~Node() = default; - std::string getUrl() const { + const std::string* getUrl() const { auto odataId = json.find("@odata.id"); - if (odataId != json.end() && odataId->is_string()) { - return odataId->get(); + if (odataId == json.end()) { + return nullptr; } - return std::string(); + + return odataId->get_ptr(); } /** @@ -58,27 +59,35 @@ class Node { * @return None */ void getSubRoutes(const std::vector>& allNodes) { - std::string url = getUrl(); + const std::string* url = getUrl(); + if (url == nullptr) { + CROW_LOG_CRITICAL << "Unable to get url for route"; + return; + } for (const auto& node : allNodes) { - auto route = node->getUrl(); - - if (boost::starts_with(route, url)) { - auto subRoute = route.substr(url.size()); + const std::string* route = node->getUrl(); + if (route == nullptr) { + CROW_LOG_CRITICAL << "Unable to get url for route"; + return; + } + if (boost::starts_with(*route, *url)) { + std::string subRoute = route->substr(url->size()); if (subRoute.empty()) { continue; } - if (subRoute.at(0) == '/') { - subRoute = subRoute.substr(1); + if (boost::starts_with(subRoute, "/")) { + subRoute.erase(0, 1); } - if (subRoute.at(subRoute.size() - 1) == '/') { - subRoute = subRoute.substr(0, subRoute.size() - 1); + if (boost::ends_with(subRoute, "/")) { + subRoute.pop_back(); } - if (subRoute[0] != '$' && subRoute.find('/') == std::string::npos) { - json[subRoute] = nlohmann::json{{"@odata.id", route}}; + if (!boost::starts_with(subRoute, "$") && + subRoute.find('/') == std::string::npos) { + json[subRoute] = nlohmann::json{{"@odata.id", *route}}; } } } -- cgit v1.2.3