extractCountryCode method

int extractCountryCode(
  1. StringBuffer fullNumber,
  2. StringBuffer nationalNumber
)

Extracts country calling code from fullNumber, returns it and places the remaining number in nationalNumber. It assumes that the leading plus sign or IDD has already been removed. Returns 0 if fullNumber doesn't start with a valid country calling code, and leaves nationalNumber unmodified.

Implementation

int extractCountryCode(StringBuffer fullNumber, StringBuffer nationalNumber) {
  if ((fullNumber.length == 0) || (fullNumber.toString()[0] == '0')) {
    // Country codes do not begin with a '0'.
    return 0;
  }
  int potentialCountryCode;
  int numberLength = fullNumber.length;

  for (int i = 1; i <= maxLengthCountryCode && i <= numberLength; i++) {
    potentialCountryCode =
        int.parse(fullNumber.toString().substring(0, i), radix: 10);
    if (_metadataMapLoader.countryCodeToRegionCodeMap
        .containsKey('$potentialCountryCode')) {
      nationalNumber.write(fullNumber.toString().substring(i));
      return potentialCountryCode;
    }
  }
  return 0;
}