cpp/src/phonenumbers/phonenumberutil.cc
// Tries to extract a country calling code from a number. Country calling codes
// are extracted in the following ways:
//   - by stripping the international dialing prefix of the region the person
//   is dialing from, if this is present in the number, and looking at the next
//   digits
//   - by stripping the '+' sign if present and then looking at the next digits
//   - by comparing the start of the number and the country calling code of the
//   default region. If the number is not considered possible for the numbering
//   plan of the default region initially, but starts with the country calling
//   code of this region, validation will be reattempted after stripping this
//   country calling code. If this number is considered a possible number, then
//   the first digits will be considered the country calling code and removed as
//   such.
//
//   Returns NO_PARSING_ERROR if a country calling code was successfully
//   extracted or none was present, or the appropriate error otherwise, such as
//   if a + was present but it was not followed by a valid country calling code.
//   If NO_PARSING_ERROR is returned, the national_number without the country
//   calling code is populated, and the country_code of the phone_number passed
//   in is set to the country calling code if found, otherwise to 0.
Tries to extract a country calling code from a number. Country calling codes
are extracted in the following ways:
by stripping the international dialing prefix of the region the person
is dialing from, if this is present in the number, and looking at the next
digits
by stripping the '+' sign if present and then looking at the next digits
by comparing the start of the number and the country calling code of the
default region. If the number is not considered possible for the numbering
plan of the default region initially, but starts with the country calling
code of this region, validation will be reattempted after stripping this
country calling code. If this number is considered a possible number, then
the first digits will be considered the country calling code and removed as
such.

Returns NO_PARSING_ERROR if a country calling code was successfully
extracted or none was present, or the appropriate error otherwise, such as
if a + was present but it was not followed by a valid country calling code.
If NO_PARSING_ERROR is returned, the national_number without the country
calling code is populated, and the country_code of the phone_number passed
in is set to the country calling code if found, otherwise to 0.
Tries to extract a country calling code from a number . Country calling codes are extracted in the following ways : by stripping the international dialing prefix of the region the person is dialing from if this is present in the number and looking at the next digits by stripping the ' + ' sign if present and then looking at the next digits by comparing the start of the number and the country calling code of the default region . If the number is not considered possible for the numbering plan of the default region initially but starts with the country calling code of this region validation will be reattempted after stripping this country calling code . If this number is considered a possible number then the first digits will be considered the country calling code and removed as such . Returns NO_PARSING_ERROR if a country calling code was successfully extracted or none was present or the appropriate error otherwise such as if a + was present but it was not followed by a valid country calling code . If NO_PARSING_ERROR is returned the national_number without the country calling code is populated and the country_code of the phone_number passed in is set to the country calling code if found otherwise to 0 .
PhoneNumberUtil::ErrorType PhoneNumberUtil::MaybeExtractCountryCode(
    const PhoneMetadata* default_region_metadata,
    bool keep_raw_input,
    string* national_number,
    PhoneNumber* phone_number) const {
  DCHECK(national_number);
  DCHECK(phone_number);
  string possible_country_idd_prefix = default_region_metadata
      ?  default_region_metadata->international_prefix()
      : "NonMatch";
  PhoneNumber::CountryCodeSource country_code_source =
      MaybeStripInternationalPrefixAndNormalize(possible_country_idd_prefix,
                                                national_number);
  if (keep_raw_input) {
    phone_number->set_country_code_source(country_code_source);
  }
  if (country_code_source != PhoneNumber::FROM_DEFAULT_COUNTRY) {
    if (national_number->length() <= kMinLengthForNsn) {
      VLOG(2) << "Phone number had an IDD, but after this was not "
              << "long enough to be a viable phone number.";
      return TOO_SHORT_AFTER_IDD;
    }
    int potential_country_code = ExtractCountryCode(national_number);
    if (potential_country_code != 0) {
      phone_number->set_country_code(potential_country_code);
      return NO_PARSING_ERROR;
    }
    return INVALID_COUNTRY_CODE_ERROR;
  } else if (default_region_metadata) {
     If so, we remove the country calling code, and do some
    int default_country_code = default_region_metadata->country_code();
    string default_country_code_string(SimpleItoa(default_country_code));
    VLOG(4) << "Possible country calling code: " << default_country_code_string;
    string potential_national_number;
    if (TryStripPrefixString(*national_number,
                             default_country_code_string,
                             &potential_national_number)) {
      const PhoneNumberDesc& general_num_desc =
          default_region_metadata->general_desc();
      MaybeStripNationalPrefixAndCarrierCode(*default_region_metadata,
                                             &potential_national_number,
                                             NULL);
      VLOG(4) << "Number without country calling code prefix";
      if ((!IsMatch(*matcher_api_, *national_number, general_num_desc) &&
          IsMatch(
              *matcher_api_, potential_national_number, general_num_desc)) ||
          TestNumberLength(*national_number, *default_region_metadata) ==
              TOO_LONG) {
        national_number->assign(potential_national_number);
        if (keep_raw_input) {
          phone_number->set_country_code_source(
              PhoneNumber::FROM_NUMBER_WITHOUT_PLUS_SIGN);
        }
        phone_number->set_country_code(default_country_code);
        return NO_PARSING_ERROR;
      }
    }
  }
  phone_number->set_country_code(0);
  return NO_PARSING_ERROR;
}
PhoneNumberUtil :: ErrorType PhoneNumberUtil :: MaybeExtractCountryCode ( const PhoneMetadata * default_region_metadata , bool keep_raw_input , string * national_number , PhoneNumber * phone_number ) const { DCHECK ( national_number ) ; DCHECK ( phone_number ) ; string possible_country_idd_prefix = default_region_metadata ? default_region_metadata -> international_prefix ( ) : " " ; PhoneNumber :: CountryCodeSource country_code_source = MaybeStripInternationalPrefixAndNormalize ( possible_country_idd_prefix , national_number ) ; if ( keep_raw_input ) { phone_number -> set_country_code_source ( country_code_source ) ; } if ( country_code_source != PhoneNumber :: FROM_DEFAULT_COUNTRY ) { if ( national_number -> length ( ) <= kMinLengthForNsn ) { VLOG ( 2 ) << " " << " " ; return TOO_SHORT_AFTER_IDD ; } int potential_country_code = ExtractCountryCode ( national_number ) ; if ( potential_country_code != 0 ) { phone_number -> set_country_code ( potential_country_code ) ; return NO_PARSING_ERROR ; } return INVALID_COUNTRY_CODE_ERROR ; } else if ( default_region_metadata ) { int default_country_code = default_region_metadata -> country_code ( ) ; string default_country_code_string ( SimpleItoa ( default_country_code ) ) ; VLOG ( 4 ) << " " << default_country_code_string ; string potential_national_number ; if ( TryStripPrefixString ( * national_number , default_country_code_string , & potential_national_number ) ) { const PhoneNumberDesc & general_num_desc = default_region_metadata -> general_desc ( ) ; MaybeStripNationalPrefixAndCarrierCode ( * default_region_metadata , & potential_national_number , NULL ) ; VLOG ( 4 ) << " " ; if ( ( ! IsMatch ( * matcher_api_ , * national_number , general_num_desc ) && IsMatch ( * matcher_api_ , potential_national_number , general_num_desc ) ) || TestNumberLength ( * national_number , * default_region_metadata ) == TOO_LONG ) { national_number -> assign ( potential_national_number ) ; if ( keep_raw_input ) { phone_number -> set_country_code_source ( PhoneNumber :: FROM_NUMBER_WITHOUT_PLUS_SIGN ) ; } phone_number -> set_country_code ( default_country_code ) ; return NO_PARSING_ERROR ; } } } phone_number -> set_country_code ( 0 ) ; return NO_PARSING_ERROR ; }
Tries to extract a country calling code from a number.
Tries to extract a country calling code from a number .
// Set the default prefix to be something that will never match if there is no // default region. // If this fails, they must be using a strange country calling code that we // don't recognize, or that doesn't exist. // Check to see if the number starts with the country calling code for the // default region. If so, we remove the country calling code, and do some // checks on the validity of the number before and after. // If the number was not valid before but is valid now, or if it was too // long before, we consider the number with the country code stripped to // be a better result and keep that instead. // No country calling code present. Set the country_code to 0.