[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / input-filter.cpp
1 /*
2  * Copyright (c) 2021 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 // CLASS HEADER
19 #include <dali-toolkit/internal/text/hidden-text.h>
20 #include <dali-toolkit/internal/text/input-filter.h>
21
22 // INTERNAL INCLUDES
23
24 using namespace Dali::Toolkit;
25
26 namespace Dali
27 {
28 namespace Toolkit
29 {
30 namespace Text
31 {
32 const char* const PROPERTY_ACCEPTED = "accepted";
33 const char* const PROPERTY_REJECTED = "rejected";
34
35 InputFilter::InputFilter()
36 : mAccepted(""),
37   mRejected("")
38 {
39 }
40
41 void InputFilter::SetProperties(const Property::Map& map)
42 {
43   const Property::Map::SizeType count = map.Count();
44
45   for(Property::Map::SizeType position = 0; position < count; ++position)
46   {
47     KeyValuePair     keyValue = map.GetKeyValue(position);
48     Property::Key&   key      = keyValue.first;
49     Property::Value& value    = keyValue.second;
50
51     if(key == Toolkit::InputFilter::Property::ACCEPTED || key == PROPERTY_ACCEPTED)
52     {
53       value.Get(mAccepted);
54     }
55     else if(key == Toolkit::InputFilter::Property::REJECTED || key == PROPERTY_REJECTED)
56     {
57       value.Get(mRejected);
58     }
59   }
60 }
61
62 void InputFilter::GetProperties(Property::Map& map)
63 {
64   map[Toolkit::InputFilter::Property::ACCEPTED] = mAccepted.c_str();
65   map[Toolkit::InputFilter::Property::REJECTED] = mRejected.c_str();
66 }
67
68 bool InputFilter::Contains(Toolkit::InputFilter::Property::Type type, std::string source)
69 {
70   bool       match = false;
71   std::regex pattern;
72
73   if(type == Toolkit::InputFilter::Property::ACCEPTED)
74   {
75     if(mAccepted.empty())
76     {
77       return true;
78     }
79     pattern = mAccepted;
80   }
81   else if(type == Toolkit::InputFilter::Property::REJECTED)
82   {
83     if(mRejected.empty())
84     {
85       return false;
86     }
87     pattern = mRejected;
88   }
89
90   match = std::regex_match(source, pattern);
91
92   return match;
93 }
94
95 bool InputFilter::Filter(Toolkit::InputFilter::Property::Type type, std::string& text)
96 {
97   std::regex pattern;
98   std::string result;
99
100   if(type == Toolkit::InputFilter::Property::ACCEPTED)
101   {
102     if(mAccepted.empty())
103     {
104       return false;
105     }
106     pattern = mAccepted;
107
108     auto start = std::sregex_iterator(text.begin(), text.end(), pattern);
109     auto end   = std::sregex_iterator();
110
111     while (start != end) {
112       result += start->str();
113       ++start;
114     }
115   }
116   else if(type == Toolkit::InputFilter::Property::REJECTED)
117   {
118     if(mRejected.empty())
119     {
120       return false;
121     }
122     pattern = mRejected;
123
124     result = std::regex_replace(text, pattern, "");
125   }
126
127   if(result.compare(text) == 0)
128   {
129     return false;
130   }
131
132   text = result;
133   return true;
134 }
135
136 } // namespace Text
137
138 } // namespace Toolkit
139
140 } // namespace Dali