cpp/src/phonenumbers/phonenumberutil.cc
// Strips any national prefix (such as 0, 1) present in the number provided.
// The number passed in should be the normalized telephone number that we wish
// to strip any national dialing prefix from. The metadata should be for the
// region that we think this number is from. Returns true if a national prefix
// and/or carrier code was stripped.
Strips any national prefix (such as 0, 1) present in the number provided.
The number passed in should be the normalized telephone number that we wish
to strip any national dialing prefix from. The metadata should be for the
region that we think this number is from. Returns true if a national prefix
and/or carrier code was stripped.
Strips any national prefix ( such as 0 1 ) present in the number provided . The number passed in should be the normalized telephone number that we wish to strip any national dialing prefix from . The metadata should be for the region that we think this number is from . Returns true if a national prefix and / or carrier code was stripped .
bool PhoneNumberUtil::MaybeStripNationalPrefixAndCarrierCode(
    const PhoneMetadata& metadata,
    string* number,
    string* carrier_code) const {
  DCHECK(number);
  string carrier_code_temp;
  const string& possible_national_prefix =
      metadata.national_prefix_for_parsing();
  if (number->empty() || possible_national_prefix.empty()) {
    return false;
  }
  const scoped_ptr<RegExpInput> number_copy(
      reg_exps_->regexp_factory_->CreateInput(*number));
  const scoped_ptr<RegExpInput> number_copy_without_transform(
      reg_exps_->regexp_factory_->CreateInput(*number));
  string number_string_copy(*number);
  string captured_part_of_prefix;
  const PhoneNumberDesc& general_desc = metadata.general_desc();
  bool is_viable_original_number =
      IsMatch(*matcher_api_, *number, general_desc);
  const string& transform_rule = metadata.national_prefix_transform_rule();
  const RegExp& possible_national_prefix_pattern =
      reg_exps_->regexp_cache_->GetRegExp(possible_national_prefix);
  if (!transform_rule.empty() &&
      (possible_national_prefix_pattern.Consume(
          number_copy.get(), &carrier_code_temp, &captured_part_of_prefix) ||
       possible_national_prefix_pattern.Consume(
           number_copy.get(), &captured_part_of_prefix)) &&
      !captured_part_of_prefix.empty()) {
    possible_national_prefix_pattern.Replace(&number_string_copy,
                                             transform_rule);
    if (is_viable_original_number &&
        !IsMatch(*matcher_api_, number_string_copy, general_desc)) {
      return false;
    }
    number->assign(number_string_copy);
    if (carrier_code) {
      carrier_code->assign(carrier_code_temp);
    }
  } else if (possible_national_prefix_pattern.Consume(
                 number_copy_without_transform.get(), &carrier_code_temp) ||
             possible_national_prefix_pattern.Consume(
                 number_copy_without_transform.get())) {
    VLOG(4) << "Parsed the first digits as a national prefix.";
    const string number_copy_as_string =
        number_copy_without_transform->ToString();
    if (is_viable_original_number &&
        !IsMatch(*matcher_api_, number_copy_as_string, general_desc)) {
      return false;
    }
    number->assign(number_copy_as_string);
    if (carrier_code) {
      carrier_code->assign(carrier_code_temp);
    }
  } else {
    return false;
    VLOG(4) << "The first digits did not match the national prefix.";
  }
  return true;
}
bool PhoneNumberUtil :: MaybeStripNationalPrefixAndCarrierCode ( const PhoneMetadata & metadata , string * number , string * carrier_code ) const { DCHECK ( number ) ; string carrier_code_temp ; const string & possible_national_prefix = metadata . national_prefix_for_parsing ( ) ; if ( number -> empty ( ) || possible_national_prefix . empty ( ) ) { return false ; } const scoped_ptr < RegExpInput > number_copy ( reg_exps_ -> regexp_factory_ -> CreateInput ( * number ) ) ; const scoped_ptr < RegExpInput > number_copy_without_transform ( reg_exps_ -> regexp_factory_ -> CreateInput ( * number ) ) ; string number_string_copy ( * number ) ; string captured_part_of_prefix ; const PhoneNumberDesc & general_desc = metadata . general_desc ( ) ; bool is_viable_original_number = IsMatch ( * matcher_api_ , * number , general_desc ) ; const string & transform_rule = metadata . national_prefix_transform_rule ( ) ; const RegExp & possible_national_prefix_pattern = reg_exps_ -> regexp_cache_ -> GetRegExp ( possible_national_prefix ) ; if ( ! transform_rule . empty ( ) && ( possible_national_prefix_pattern . Consume ( number_copy . get ( ) , & carrier_code_temp , & captured_part_of_prefix ) || possible_national_prefix_pattern . Consume ( number_copy . get ( ) , & captured_part_of_prefix ) ) && ! captured_part_of_prefix . empty ( ) ) { possible_national_prefix_pattern . Replace ( & number_string_copy , transform_rule ) ; if ( is_viable_original_number && ! IsMatch ( * matcher_api_ , number_string_copy , general_desc ) ) { return false ; } number -> assign ( number_string_copy ) ; if ( carrier_code ) { carrier_code -> assign ( carrier_code_temp ) ; } } else if ( possible_national_prefix_pattern . Consume ( number_copy_without_transform . get ( ) , & carrier_code_temp ) || possible_national_prefix_pattern . Consume ( number_copy_without_transform . get ( ) ) ) { VLOG ( 4 ) << " " ; const string number_copy_as_string = number_copy_without_transform -> ToString ( ) ; if ( is_viable_original_number && ! IsMatch ( * matcher_api_ , number_copy_as_string , general_desc ) ) { return false ; } number -> assign ( number_copy_as_string ) ; if ( carrier_code ) { carrier_code -> assign ( carrier_code_temp ) ; } } else { return false ; VLOG ( 4 ) << " " ; } return true ; }
Strips any national prefix (such as 0, 1) present in the number provided.
Strips any national prefix ( such as 0 1 ) present in the number provided .
// Early return for numbers of zero length or with no national prefix // possible. // We use two copies here since Consume modifies the phone number, and if the // first if-clause fails the number will already be changed. // Check if the original number is viable. // Attempt to parse the first digits as a national prefix. We make a // copy so that we can revert to the original string if necessary. // If this succeeded, then we must have had a transform rule and there must // have been some part of the prefix that we captured. // We make the transformation and check that the resultant number is still // viable. If so, replace the number and return. // If captured_part_of_prefix is empty, this implies nothing was captured by // the capturing groups in possible_national_prefix; therefore, no // transformation is necessary, and we just remove the national prefix.