From 66e34be8b6c5d6e9c567e2eea8f4ed0bc9085b4b Mon Sep 17 00:00:00 2001 From: Subhransu Mohanty Date: Thu, 27 Aug 2020 09:38:27 +0900 Subject: [PATCH] use string_view to avoid temporary string allocation. Change-Id: I234742fc0ad1337e54b56fcb916f435d487da2c5 --- dali-toolkit/devel-api/builder/tree-node.cpp | 26 +++++++------------------- dali-toolkit/internal/builder/dictionary.h | 4 +++- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/dali-toolkit/devel-api/builder/tree-node.cpp b/dali-toolkit/devel-api/builder/tree-node.cpp index 0825905..ef0071d 100644 --- a/dali-toolkit/devel-api/builder/tree-node.cpp +++ b/dali-toolkit/devel-api/builder/tree-node.cpp @@ -18,8 +18,8 @@ // EXTERNAL INCLUDES #include #include -#include #include +#include // INTERNAL INCLUDES #include "dali-toolkit/devel-api/builder/tree-node.h" @@ -27,23 +27,15 @@ namespace Dali { -bool CaseInsensitiveCharacterCompare(unsigned char a, unsigned char b) -{ - // Converts to lower case in the current locale. - return std::tolower(a) == std::tolower(b); -} /** * return true if the lower cased ASCII strings are equal. */ -bool CaseInsensitiveStringCompare(const std::string& a, const std::string& b) +bool CaseInsensitiveStringCompare(std::string_view a, std::string_view b) { - bool result = false; - if(a.length() == b.length()) - { - result = std::equal(a.begin(), a.end(), b.begin(), CaseInsensitiveCharacterCompare); - } - return result; + return (a.length() == b.length()) && std::equal(a.begin(), a.end(), b.begin(), [](auto x, auto y) { + return std::tolower(x) == std::tolower(y); + }); } namespace Toolkit @@ -143,13 +135,9 @@ const TreeNode* TreeNode::GetChildIgnoreCase(const std::string& childName) const const TreeNode* p = mFirstChild; while(p) { - if(p->mName) + if(p->mName && CaseInsensitiveStringCompare(p->mName, childName)) { - std::string nodeName(p->mName); - if(CaseInsensitiveStringCompare(nodeName, childName)) - { - return p; - } + return p; } p = p->mNextSibling; } diff --git a/dali-toolkit/internal/builder/dictionary.h b/dali-toolkit/internal/builder/dictionary.h index 88095eb..65c7e18 100644 --- a/dali-toolkit/internal/builder/dictionary.h +++ b/dali-toolkit/internal/builder/dictionary.h @@ -18,11 +18,13 @@ */ #include + #include +#include namespace Dali { -extern bool CaseInsensitiveStringCompare( const std::string& a, const std::string& b ); +extern bool CaseInsensitiveStringCompare(std::string_view a, std::string_view b); namespace Toolkit { -- 2.7.4