Adding missing license information for the new matcher API files.
[platform/upstream/libphonenumber.git] / cpp / src / phonenumbers / regex_based_matcher.cc
1 /*
2  * Copyright (C) 2014 The Libphonenumber Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "phonenumbers/regex_based_matcher.h"
18
19 #include <memory>
20 #include <string>
21
22 #include "phonenumbers/base/memory/scoped_ptr.h"
23 #include "phonenumbers/phonemetadata.pb.h"
24 #include "phonenumbers/regexp_adapter.h"
25 #include "phonenumbers/regexp_cache.h"
26 #include "phonenumbers/regexp_factory.h"
27
28 namespace i18n {
29 namespace phonenumbers {
30
31 using std::string;
32
33 // Same implementations of AbstractRegExpFactory and RegExpCache in
34 // PhoneNumberUtil (copy from phonenumberutil.cc).
35 RegexBasedMatcher::RegexBasedMatcher()
36     : regexp_factory_(new RegExpFactory()),
37       regexp_cache_(new RegExpCache(*regexp_factory_, 128)) {}
38
39 RegexBasedMatcher::~RegexBasedMatcher() {}
40
41 bool RegexBasedMatcher::MatchesNationalNumber(
42     const string& national_number, const PhoneNumberDesc& number_desc,
43     bool allow_prefix_match) const {
44   return Match(national_number, number_desc.national_number_pattern(),
45                allow_prefix_match);
46 }
47
48 bool RegexBasedMatcher::MatchesPossibleNumber(
49     const string& national_number, const PhoneNumberDesc& number_desc) const {
50   return Match(national_number, number_desc.possible_number_pattern(), false);
51 }
52
53 bool RegexBasedMatcher::Match(const string& national_number,
54                               const string& number_pattern,
55                               bool allow_prefix_match) const {
56   const RegExp& regexp(regexp_cache_->GetRegExp(number_pattern));
57
58   if (allow_prefix_match) {
59     const scoped_ptr<RegExpInput> normalized_number_input(
60         regexp_factory_->CreateInput(national_number));
61     return regexp.Consume(normalized_number_input.get());
62   } else {
63     return regexp.FullMatch(national_number);
64   }
65 }
66
67 }  // namespace phonenumbers
68 }  // namespace i18n