Home | History | Annotate | Download | only in phonenumbers
      1 // Copyright (C) 2012 The Libphonenumber Authors
      2 //
      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 // Author: David Yonge-Mallo
     16 
     17 #include "phonenumbers/shortnumberutil.h"
     18 
     19 #include "phonenumbers/base/memory/scoped_ptr.h"
     20 #include "phonenumbers/phonemetadata.pb.h"
     21 #include "phonenumbers/phonenumberutil.h"
     22 #include "phonenumbers/regexp_adapter.h"
     23 #include "phonenumbers/regexp_factory.h"
     24 
     25 namespace i18n {
     26 namespace phonenumbers {
     27 
     28 using std::string;
     29 
     30 ShortNumberUtil::ShortNumberUtil()
     31     : phone_util_(*PhoneNumberUtil::GetInstance()) {
     32 }
     33 
     34 bool ShortNumberUtil::ConnectsToEmergencyNumber(const string& number,
     35     const string& region_code) const {
     36   return MatchesEmergencyNumberHelper(number, region_code,
     37       true /* allows prefix match */);
     38 }
     39 
     40 bool ShortNumberUtil::IsEmergencyNumber(const string& number,
     41     const string& region_code) const {
     42   return MatchesEmergencyNumberHelper(number, region_code,
     43       false /* doesn't allow prefix match */);
     44 }
     45 
     46 bool ShortNumberUtil::MatchesEmergencyNumberHelper(const string& number,
     47     const string& region_code, bool allow_prefix_match) const {
     48   string extracted_number;
     49   phone_util_.ExtractPossibleNumber(number, &extracted_number);
     50   if (phone_util_.StartsWithPlusCharsPattern(extracted_number)) {
     51     // Returns false if the number starts with a plus sign. We don't believe
     52     // dialing the country code before emergency numbers (e.g. +1911) works,
     53     // but later, if that proves to work, we can add additional logic here to
     54     // handle it.
     55     return false;
     56   }
     57   const PhoneMetadata* metadata = phone_util_.GetMetadataForRegion(region_code);
     58   if (!metadata || !metadata->has_emergency()) {
     59     return false;
     60   }
     61   const scoped_ptr<const AbstractRegExpFactory> regexp_factory(
     62       new RegExpFactory());
     63   const scoped_ptr<const RegExp> emergency_number_pattern(
     64       regexp_factory->CreateRegExp(
     65           metadata->emergency().national_number_pattern()));
     66   phone_util_.NormalizeDigitsOnly(&extracted_number);
     67   const scoped_ptr<RegExpInput> normalized_number_input(
     68       regexp_factory->CreateInput(extracted_number));
     69 
     70   // In Brazil, emergency numbers don't work when additional digits are
     71   // appended.
     72   return (!allow_prefix_match || region_code == "BR")
     73       ? emergency_number_pattern->FullMatch(extracted_number)
     74       : emergency_number_pattern->Consume(normalized_number_input.get());
     75 }
     76 
     77 }  // namespace phonenumbers
     78 }  // namespace i18n
     79