maybeStripInternationalPrefixAndNormalize method

PhoneNumber_CountryCodeSource maybeStripInternationalPrefixAndNormalize(
  1. StringBuffer number,
  2. String possibleIddPrefix
)

Strips any international prefix (such as +, 00, 011) present in the number provided, normalizes the resulting number, and indicates if an international prefix was present.

number the non-normalized telephone number that we wish to strip any international dialing prefix from. possibleIddPrefix the international direct dialing prefix from the region we think this number may be dialed in. returns PhoneNumber_CountryCodeSource of the corresponding CountryCodeSource if an international dialing prefix could be removed from the number, otherwise PhoneNumber_CountryCodeSource.fromDefaultCountry if the number did not seem to be in international format.

Implementation

PhoneNumber_CountryCodeSource maybeStripInternationalPrefixAndNormalize(
  StringBuffer number,
  String possibleIddPrefix,
) {
  if (number.toString().isEmpty) {
    return PhoneNumber_CountryCodeSource.FROM_DEFAULT_COUNTRY;
  }

  // Check to see if the number begins with one or more plus signs.
  Match? m = _leadingPlusCharsPattern.matchAsPrefix(number.toString());
  if (m != null) {
    final remainingNumber = number.toString().replaceRange(0, m.end, '');
    number.clear();
    number.write(remainingNumber);
    // Can now normalize the rest of the number since we've consumed the '+' sign at
    // the start.
    normalize(number);
    return PhoneNumber_CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN;
  }

  // Attempt to parse the first digits as an international prefix.
  RegExp iddPattern = RegExp(possibleIddPrefix);
  normalize(number);

  return _parsePrefixAsIdd(iddPattern, number)
      ? PhoneNumber_CountryCodeSource.FROM_NUMBER_WITH_IDD
      : PhoneNumber_CountryCodeSource.FROM_DEFAULT_COUNTRY;
}