use string_view to avoid temporary string allocation.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / builder / tree-node.cpp
index 302c7a2..ef0071d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 // EXTERNAL INCLUDES
-#include <cstring>
+#include <algorithm>
+#include <cctype>
+#include <string>
+#include <string_view>
 
 // INTERNAL INCLUDES
 #include "dali-toolkit/devel-api/builder/tree-node.h"
 namespace Dali
 {
 
-namespace Toolkit
+/**
+ * return true if the lower cased ASCII strings are equal.
+ */
+bool CaseInsensitiveStringCompare(std::string_view a, std::string_view b)
 {
+  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
+{
 TreeNode::TreeNode()
-  : mName(NULL),
-    mParent(NULL),
-    mNextSibling(NULL),
-    mFirstChild(NULL),
-    mLastChild(NULL),
-    mStringValue(NULL),
-    mType(TreeNode::IS_NULL),
-    mSubstituion(false)
+: mName(NULL),
+  mParent(NULL),
+  mNextSibling(NULL),
+  mFirstChild(NULL),
+  mLastChild(NULL),
+  mStringValue(NULL),
+  mType(TreeNode::IS_NULL),
+  mSubstituion(false)
 {
 }
 
 TreeNode::~TreeNode()
 {
-
 }
 
 const char* TreeNode::GetName() const
@@ -80,10 +91,9 @@ bool TreeNode::GetBoolean() const
   return mIntValue == 1 ? true : false;
 }
 
-
 size_t TreeNode::Size() const
 {
-  size_t c = 0;
+  size_t    c = 0;
   TreeNode* p = mFirstChild;
   while(p)
   {
@@ -111,7 +121,21 @@ const TreeNode* TreeNode::GetChild(const std::string& childName) const
   const TreeNode* p = mFirstChild;
   while(p)
   {
-    if(p->mName && (std::string(p->mName) == childName) )
+    if(p->mName && (std::string(p->mName) == childName))
+    {
+      return p;
+    }
+    p = p->mNextSibling;
+  }
+  return NULL;
+}
+
+const TreeNode* TreeNode::GetChildIgnoreCase(const std::string& childName) const
+{
+  const TreeNode* p = mFirstChild;
+  while(p)
+  {
+    if(p->mName && CaseInsensitiveStringCompare(p->mName, childName))
     {
       return p;
     }
@@ -137,19 +161,17 @@ TreeNode::ConstIterator TreeNode::CBegin() const
   return ConstIterator(mFirstChild);
 }
 
-
 TreeNode::ConstIterator TreeNode::CEnd() const
 {
   return ConstIterator(NULL);
 }
 
-
-TreeNode::ConstIterator::ConstIterator(TreeNode* v) : mNode(v)
+TreeNode::ConstIterator::ConstIterator(TreeNode* v)
+: mNode(v)
 {
-
 }
 
-TreeNode::ConstIterator& TreeNode::ConstIterator::operator ++()
+TreeNode::ConstIterator& TreeNode::ConstIterator::operator++()
 {
   if(mNode)
   {
@@ -162,7 +184,7 @@ TreeNode::ConstIterator& TreeNode::ConstIterator::operator ++()
   return *this;
 }
 
-TreeNode::ConstIterator TreeNode::ConstIterator::operator ++(int)
+TreeNode::ConstIterator TreeNode::ConstIterator::operator++(int)
 {
   TreeNode::ConstIterator ret(mNode);
 
@@ -177,12 +199,12 @@ TreeNode::ConstIterator TreeNode::ConstIterator::operator ++(int)
   return ret;
 }
 
-TreeNode::KeyNodePair TreeNode::ConstIterator::operator *()
+TreeNode::KeyNodePair TreeNode::ConstIterator::operator*()
 {
   return KeyNodePair(mNode->mName, *mNode);
 }
 
-bool TreeNode::ConstIterator::operator!=( const TreeNode::ConstIterator& rhs ) const
+bool TreeNode::ConstIterator::operator!=(const TreeNode::ConstIterator& rhs) const
 {
   return mNode != rhs.mNode;
 }