1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "i18n_hilog.h"
16 #include "border_rule.h"
17 #include "phone_number_matched.h"
18
19 namespace OHOS {
20 namespace Global {
21 namespace I18n {
22 const std::string BorderRule::CONTAIN_STR = "CONTAIN";
23 const std::string BorderRule::CONTAIN_OR_INTERSECT_STR = "CONTAIN_OR_INTERSECT";
24 const std::string BorderRule::TRUE_STR = "True";
25
BorderRule(icu::UnicodeString & regex,std::string & insensitive,std::string & type)26 BorderRule::BorderRule(icu::UnicodeString& regex, std::string& insensitive, std::string& type)
27 {
28 this->regex = regex;
29 if (type.compare(CONTAIN_STR) == 0) {
30 // 9 indicates a certain execution logic of the border rule.
31 this->type = 9;
32 } else if (type.compare(CONTAIN_OR_INTERSECT_STR) == 0) {
33 // 8 indicates a certain execution logic of the border rule.
34 this->type = 8;
35 } else {
36 this->type = 0;
37 }
38 this->insensitive = insensitive;
39 }
40
GetType()41 int BorderRule::GetType()
42 {
43 return type;
44 }
45
GetPattern()46 icu::RegexPattern* BorderRule::GetPattern()
47 {
48 // Sets whether regular expression matching is case sensitive
49 UErrorCode status = U_ZERO_ERROR;
50 icu::RegexPattern* pattern;
51 if (insensitive.compare(TRUE_STR) == 0) {
52 pattern = icu::RegexPattern::compile(this->regex, URegexpFlag::UREGEX_CASE_INSENSITIVE, status);
53 } else {
54 pattern = icu::RegexPattern::compile(this->regex, 0, status);
55 }
56 if (U_FAILURE(status)) {
57 HILOG_ERROR_I18N("BorderRule::GetPattern: Compile regex pattern failed.");
58 return nullptr;
59 }
60 return pattern;
61 }
62
Handle(PhoneNumberMatch * match,icu::UnicodeString & message)63 bool BorderRule::Handle(PhoneNumberMatch* match, icu::UnicodeString& message)
64 {
65 int begin = match->start();
66 int end = match->end();
67 // chenk the 10 characters before and after the phone number
68 int beginSubTen = begin - 10 < 0 ? 0 : begin - 10;
69 int endAddTen = end + 10 > message.length() ? message.length() : end + 10;
70 icu::UnicodeString borderStr = message.tempSubString(beginSubTen, endAddTen - beginSubTen);
71
72 icu::RegexPattern* pattern = this->GetPattern();
73 if (pattern == nullptr) {
74 return false;
75 }
76 UErrorCode status = U_ZERO_ERROR;
77 icu::RegexMatcher* mat = pattern->matcher(borderStr, status);
78 if (U_FAILURE(status) || mat == nullptr) {
79 HILOG_ERROR_I18N("BorderRule::Handle: Pattern matcher failed.");
80 delete pattern;
81 return false;
82 }
83 int type = this->GetType();
84 while (mat->find()) {
85 int borderBegin = mat->start(status) + beginSubTen;
86 int borderEnd = mat->end(status) + beginSubTen;
87 bool isDel = false;
88 if (type == PhoneNumberMatched::CONTAIN && borderBegin <= begin && end <= borderEnd) {
89 isDel = true;
90 } else if (type == PhoneNumberMatched::CONTAIN_OR_INTERSECT && ((borderBegin <= begin &&
91 end <= borderEnd) || (borderBegin < begin && begin < borderEnd && borderEnd < end) ||
92 (begin < borderBegin && borderBegin < end && end < borderEnd))) {
93 isDel = true;
94 }
95 if (isDel) {
96 delete mat;
97 delete pattern;
98 return false;
99 }
100 }
101 delete mat;
102 delete pattern;
103
104 return true;
105 }
106 } // namespace I18n
107 } // namespace Global
108 } // namespace OHOS