use string_view to avoid temporary string allocation.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / builder / tree-node.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <algorithm>
20 #include <cctype>
21 #include <string>
22 #include <string_view>
23
24 // INTERNAL INCLUDES
25 #include "dali-toolkit/devel-api/builder/tree-node.h"
26 #include "dali-toolkit/internal/builder/tree-node-manipulator.h"
27
28 namespace Dali
29 {
30
31 /**
32  * return true if the lower cased ASCII strings are equal.
33  */
34 bool CaseInsensitiveStringCompare(std::string_view a, std::string_view b)
35 {
36   return (a.length() == b.length()) && std::equal(a.begin(), a.end(), b.begin(), [](auto x, auto y) {
37            return std::tolower(x) == std::tolower(y);
38          });
39 }
40
41 namespace Toolkit
42 {
43 TreeNode::TreeNode()
44 : mName(NULL),
45   mParent(NULL),
46   mNextSibling(NULL),
47   mFirstChild(NULL),
48   mLastChild(NULL),
49   mStringValue(NULL),
50   mType(TreeNode::IS_NULL),
51   mSubstituion(false)
52 {
53 }
54
55 TreeNode::~TreeNode()
56 {
57 }
58
59 const char* TreeNode::GetName() const
60 {
61   return mName;
62 }
63
64 TreeNode::NodeType TreeNode::GetType() const
65 {
66   return mType;
67 }
68
69 const char* TreeNode::GetString() const
70 {
71   return mStringValue;
72 }
73
74 bool TreeNode::HasSubstitution() const
75 {
76   return mSubstituion;
77 }
78
79 float TreeNode::GetFloat() const
80 {
81   return mFloatValue;
82 }
83
84 int TreeNode::GetInteger() const
85 {
86   return mIntValue;
87 }
88
89 bool TreeNode::GetBoolean() const
90 {
91   return mIntValue == 1 ? true : false;
92 }
93
94 size_t TreeNode::Size() const
95 {
96   size_t    c = 0;
97   TreeNode* p = mFirstChild;
98   while(p)
99   {
100     c++;
101     p = p->mNextSibling;
102   }
103   return c;
104 }
105
106 size_t TreeNode::Count(const std::string& childName) const
107 {
108   const TreeNode* c = GetChild(childName);
109   if(c)
110   {
111     return c->Size();
112   }
113   else
114   {
115     return 0;
116   }
117 }
118
119 const TreeNode* TreeNode::GetChild(const std::string& childName) const
120 {
121   const TreeNode* p = mFirstChild;
122   while(p)
123   {
124     if(p->mName && (std::string(p->mName) == childName))
125     {
126       return p;
127     }
128     p = p->mNextSibling;
129   }
130   return NULL;
131 }
132
133 const TreeNode* TreeNode::GetChildIgnoreCase(const std::string& childName) const
134 {
135   const TreeNode* p = mFirstChild;
136   while(p)
137   {
138     if(p->mName && CaseInsensitiveStringCompare(p->mName, childName))
139     {
140       return p;
141     }
142     p = p->mNextSibling;
143   }
144   return NULL;
145 }
146
147 const TreeNode* TreeNode::Find(const std::string& childName) const
148 {
149   if(mName && std::string(mName) == childName)
150   {
151     return this;
152   }
153   else
154   {
155     return Internal::FindIt(childName, this);
156   }
157 }
158
159 TreeNode::ConstIterator TreeNode::CBegin() const
160 {
161   return ConstIterator(mFirstChild);
162 }
163
164 TreeNode::ConstIterator TreeNode::CEnd() const
165 {
166   return ConstIterator(NULL);
167 }
168
169 TreeNode::ConstIterator::ConstIterator(TreeNode* v)
170 : mNode(v)
171 {
172 }
173
174 TreeNode::ConstIterator& TreeNode::ConstIterator::operator++()
175 {
176   if(mNode)
177   {
178     mNode = mNode->mNextSibling;
179   }
180   else
181   {
182     mNode = NULL;
183   }
184   return *this;
185 }
186
187 TreeNode::ConstIterator TreeNode::ConstIterator::operator++(int)
188 {
189   TreeNode::ConstIterator ret(mNode);
190
191   if(mNode)
192   {
193     mNode = mNode->mNextSibling;
194   }
195   else
196   {
197     mNode = NULL;
198   }
199   return ret;
200 }
201
202 TreeNode::KeyNodePair TreeNode::ConstIterator::operator*()
203 {
204   return KeyNodePair(mNode->mName, *mNode);
205 }
206
207 bool TreeNode::ConstIterator::operator!=(const TreeNode::ConstIterator& rhs) const
208 {
209   return mNode != rhs.mNode;
210 }
211
212 } // namespace Toolkit
213
214 } // namespace Dali