1 /* 2 * Copyright (c) 2023 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 #ifndef OHOS_GLOBAL_REGEX_RULE_H 16 #define OHOS_GLOBAL_REGEX_RULE_H 17 18 #include <unicode/regex.h> 19 #include <vector> 20 #include <string> 21 #include "matched_number_info.h" 22 #include "phonenumbers/phonenumbermatch.h" 23 24 namespace OHOS { 25 namespace Global { 26 namespace I18n { 27 using i18n::phonenumbers::PhoneNumberMatch; 28 class RegexRule { 29 public: 30 RegexRule(icu::UnicodeString& regex, std::string& IsValidType, std::string& HandleType, 31 std::string& insensitive, std::string& type); 32 ~RegexRule(); 33 34 int GetType(); // border rules 35 icu::RegexPattern* GetPattern(); // positive Rules, negative rules, border rules, find rules 36 37 std::vector<MatchedNumberInfo> Handle(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); 38 PhoneNumberMatch* IsValid(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); // codes rules 39 static int CountDigits(icu::UnicodeString& str); 40 41 private: 42 void StartWithBrackets(icu::UnicodeString& phone); // codes rules 43 static std::vector<MatchedNumberInfo> GetNumbersWithSlant(icu::UnicodeString& testStr); // positive Rules 44 std::vector<MatchedNumberInfo> HandlePossibleNumberWithPattern(PhoneNumberMatch* possibleNumber, 45 icu::UnicodeString& message, bool isStartsWithNumber); // positive Rules 46 bool NumberValid(icu::UnicodeString& number); // codes rules 47 bool PrefixValid(icu::UnicodeString& number, int length); // codes rules 48 49 PhoneNumberMatch* IsValidEnd(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); // codes rules 50 PhoneNumberMatch* IsValidStart(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); // codes rules 51 PhoneNumberMatch* IsValidDefault(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); // codes rules 52 PhoneNumberMatch* IsValidPreSuf(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); // codes rules 53 PhoneNumberMatch* IsValidCode(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); // codes rules 54 PhoneNumberMatch* IsValidRawstr(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); // codes rules 55 56 std::vector<MatchedNumberInfo> HandleDefault(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); 57 std::vector<MatchedNumberInfo> HandleOperator(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); 58 std::vector<MatchedNumberInfo> HandleBlank(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); 59 std::vector<MatchedNumberInfo> HandleSlant(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); 60 std::vector<MatchedNumberInfo> HandleStartWithMobile(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); 61 std::vector<MatchedNumberInfo> HandleEndWithMobile(PhoneNumberMatch* possibleNumber, icu::UnicodeString& message); 62 63 int type; // border rules 64 icu::UnicodeString regex; // positive Rules, negative rules, border rules, find rules 65 66 UErrorCode status; 67 // Decide which IsValid function to use 68 std::string isValidType; // codes rules 69 // Decide which Handle function to use 70 std::string handleType; // positive Rules 71 std::string insensitive; // positive Rules, negative rules, border rules, find rules 72 }; 73 } // namespace I18n 74 } // namespace Global 75 } // namespace OHOS 76 #endif