2 * Copyright (C) 2014 The Libphonenumber Authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "phonenumbers/regex_based_matcher.h"
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"
29 namespace phonenumbers {
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)) {}
39 RegexBasedMatcher::~RegexBasedMatcher() {}
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(),
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);
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));
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());
63 return regexp.FullMatch(national_number);
67 } // namespace phonenumbers