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_DATE_TIME_FILTER_H
16 #define OHOS_GLOBAL_DATE_TIME_FILTER_H
17 
18 #include <string>
19 #include <unicode/regex.h>
20 #include <vector>
21 #include "date_time_rule.h"
22 #include "matched_date_time_info.h"
23 #include "rules_engine.h"
24 
25 namespace OHOS {
26 namespace Global {
27 namespace I18n {
28 class DateTimeFilter {
29 public:
30     DateTimeFilter(std::string& locale, DateTimeRule* dateTimeRule);
31     ~DateTimeFilter();
32     std::vector<MatchedDateTimeInfo> Filter(icu::UnicodeString& content, std::vector<MatchedDateTimeInfo>& matches,
33         std::vector<MatchedDateTimeInfo>& clearMatches, std::vector<MatchedDateTimeInfo>& pastMatches);
34     enum FilterType {
35         TYPE_NULL = -1,
36         TYPE_DATETIME = 0,
37         TYPE_DATE = 1,
38         TYPE_TIME = 2,
39         TYPE_TIME_PERIOD = 3,
40         TYPE_PERIOD = 4,
41         TYPE_TODAY = 5,
42         TYPE_WEEK = 6
43     };
44     enum DateCombine {
45         NOT_COMBINE = 0,
46         TWO_COMBINE = 1,
47         ALL_COMBINE = 2,
48     };
49 private:
50     int GetType(std::string& name);
51     std::vector<MatchedDateTimeInfo> FilterOverlay(std::vector<MatchedDateTimeInfo>& matches);
52     std::vector<MatchedDateTimeInfo> FilterDatePeriod(icu::UnicodeString& content,
53         std::vector<MatchedDateTimeInfo>& matches);
54     std::vector<MatchedDateTimeInfo> FilterByRules(icu::UnicodeString& content,
55         std::vector<MatchedDateTimeInfo>& matches, std::vector<MatchedDateTimeInfo>& clears);
56     std::vector<MatchedDateTimeInfo> FilterByPast(icu::UnicodeString& content,
57         std::vector<MatchedDateTimeInfo>& matches, std::vector<MatchedDateTimeInfo>& pasts);
58     std::vector<MatchedDateTimeInfo> FilterOverlayFirst(std::vector<MatchedDateTimeInfo>& matches);
59     std::vector<MatchedDateTimeInfo> FilterOverlaySecond(std::vector<MatchedDateTimeInfo>& matches);
60     std::vector<MatchedDateTimeInfo> FilterDate(icu::UnicodeString& content,
61         std::vector<MatchedDateTimeInfo>& matches);
62     std::vector<MatchedDateTimeInfo> FilterDateTime(icu::UnicodeString& content,
63         std::vector<MatchedDateTimeInfo>& matches);
64     std::vector<MatchedDateTimeInfo> FilterPeriod(icu::UnicodeString& content,
65         std::vector<MatchedDateTimeInfo>& matches);
66     std::vector<MatchedDateTimeInfo> FilterDateTimePunc(icu::UnicodeString& content,
67         std::vector<MatchedDateTimeInfo>& matches);
68     bool DealBrackets(icu::UnicodeString& content, std::vector<MatchedDateTimeInfo>& matches,
69         int matchIndex, MatchedDateTimeInfo& lastMatch,
70         MatchedDateTimeInfo& currentMatch);
71     int NestDealDate(icu::UnicodeString& content, MatchedDateTimeInfo& current,
72         std::vector<MatchedDateTimeInfo>& matches, int preType);
73     void DealMatchE(icu::UnicodeString& content, MatchedDateTimeInfo& nextMatch, MatchedDateTimeInfo& match);
74     int GetResult(icu::UnicodeString& content, MatchedDateTimeInfo& current, MatchedDateTimeInfo& nextMatch,
75         std::vector<MatchedDateTimeInfo>& matches);
76 
77     DateTimeRule* dateTimeRule;
78     std::string locale;
79 };
80 } // namespace I18n
81 } // namespace Global
82 } // namespace OHOS
83 #endif