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->status = U_ZERO_ERROR;
39 this->insensitive = insensitive;
40 if (regex.length() == 0) {
41 return;
42 }
43 if (U_FAILURE(this->status)) {
44 HILOG_ERROR_I18N("member pattern construct failed.");
45 }
46 }
47
GetType()48 int BorderRule::GetType()
49 {
50 return type;
51 }
52
GetPattern()53 icu::RegexPattern* BorderRule::GetPattern()
54 {
55 // Sets whether regular expression matching is case sensitive
56 if (insensitive.compare(TRUE_STR) == 0) {
57 return icu::RegexPattern::compile(this->regex, URegexpFlag::UREGEX_CASE_INSENSITIVE, this->status);
58 } else {
59 return icu::RegexPattern::compile(this->regex, 0, this->status);
60 }
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;
77 icu::RegexMatcher* mat = pattern->matcher(borderStr, status);
78 int type = this->GetType();
79 while (mat->find()) {
80 int borderBegin = mat->start(status) + beginSubTen;
81 int borderEnd = mat->end(status) + beginSubTen;
82 bool isDel = false;
83 if (type == PhoneNumberMatched::CONTAIN && borderBegin <= begin && end <= borderEnd) {
84 isDel = true;
85 } else if (type == PhoneNumberMatched::CONTAIN_OR_INTERSECT && ((borderBegin <= begin &&
86 end <= borderEnd) || (borderBegin < begin && begin < borderEnd && borderEnd < end) ||
87 (begin < borderBegin && borderBegin < end && end < borderEnd))) {
88 isDel = true;
89 }
90 if (isDel) {
91 delete mat;
92 delete pattern;
93 return false;
94 }
95 }
96 delete mat;
97 delete pattern;
98
99 return true;
100 }
101 } // namespace I18n
102 } // namespace Global
103 } // namespace OHOS