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 {
BorderRule(icu::UnicodeString & regex,std::string & insensitive,std::string & type)22 BorderRule::BorderRule(icu::UnicodeString& regex, std::string& insensitive, std::string& type)
23 {
24 this->regex = regex;
25 if (type == "CONTAIN") {
26 // 9 indicates a certain execution logic of the border rule.
27 this->type = 9;
28 } else if (type == "CONTAIN_OR_INTERSECT") {
29 // 8 indicates a certain execution logic of the border rule.
30 this->type = 8;
31 } else {
32 this->type = 0;
33 }
34 this->status = U_ZERO_ERROR;
35 this->insensitive = insensitive;
36 if (regex.length() == 0) {
37 return;
38 }
39 if (U_FAILURE(this->status)) {
40 HILOG_ERROR_I18N("member pattern construct failed.");
41 }
42 }
43
GetType()44 int BorderRule::GetType()
45 {
46 return type;
47 }
48
GetPattern()49 icu::RegexPattern* BorderRule::GetPattern()
50 {
51 // Sets whether regular expression matching is case sensitive
52 if (insensitive == "True") {
53 return icu::RegexPattern::compile(this->regex, URegexpFlag::UREGEX_CASE_INSENSITIVE, this->status);
54 } else {
55 return icu::RegexPattern::compile(this->regex, 0, this->status);
56 }
57 }
58
Handle(PhoneNumberMatch * match,icu::UnicodeString & message)59 bool BorderRule::Handle(PhoneNumberMatch* match, icu::UnicodeString& message)
60 {
61 int begin = match->start();
62 int end = match->end();
63 // chenk the 10 characters before and after the phone number
64 int beginSubTen = begin - 10 < 0 ? 0 : begin - 10;
65 int endAddTen = end + 10 > message.length() ? message.length() : end + 10;
66 icu::UnicodeString borderStr = message.tempSubString(beginSubTen, endAddTen - beginSubTen);
67
68 icu::RegexPattern* pattern = this->GetPattern();
69 UErrorCode status;
70 icu::RegexMatcher* mat = pattern->matcher(borderStr, status);
71 int type = this->GetType();
72 while (mat->find()) {
73 int borderBegin = mat->start(status) + beginSubTen;
74 int borderEnd = mat->end(status) + beginSubTen;
75 bool isDel = false;
76 if (type == PhoneNumberMatched::CONTAIN && borderBegin <= begin && end <= borderEnd) {
77 isDel = true;
78 } else if (type == PhoneNumberMatched::CONTAIN_OR_INTERSECT && ((borderBegin <= begin &&
79 end <= borderEnd) || (borderBegin < begin && begin < borderEnd && borderEnd < end) ||
80 (begin < borderBegin && borderBegin < end && end < borderEnd))) {
81 isDel = true;
82 }
83 if (isDel) {
84 delete mat;
85 delete pattern;
86 return false;
87 }
88 }
89 delete mat;
90 delete pattern;
91
92 return true;
93 }
94 } // namespace I18n
95 } // namespace Global
96 } // namespace OHOS