allNumberGroupsRemainGrouped static method

bool allNumberGroupsRemainGrouped(
  1. PhoneNumberUtil util,
  2. PhoneNumber number,
  3. StringBuffer normalizedCandidate,
  4. List<String> formattedNumberGroups,
)

Implementation

static bool allNumberGroupsRemainGrouped(
  PhoneNumberUtil util,
  PhoneNumber number,
  StringBuffer normalizedCandidate,
  List<String> formattedNumberGroups,
) {
  int fromIndex = 0;

  if (number.countryCodeSource !=
      PhoneNumber_CountryCodeSource.FROM_DEFAULT_COUNTRY) {
    // First skip the country code if the normalized candidate contained it.
    String countryCode = '${number.countryCode}';
    fromIndex = normalizedCandidate.toString().indexOf(countryCode) +
        countryCode.length;
  }

  /// Check each group of consecutive digits are not broken into separate groupings in the
  /// [normalizedCandidate] string.
  for (int i = 0; i < formattedNumberGroups.length; i++) {
    // Fails if the substring of [normalizedCandidate] starting from [fromIndex]
    // doesn't contain the consecutive digits in formattedNumberGroups[i].
    fromIndex = normalizedCandidate
        .toString()
        .indexOf(formattedNumberGroups[i], fromIndex);
    if (fromIndex < 0) {
      return false;
    }

    // Moves[fromIndex] forward.
    fromIndex += formattedNumberGroups[i].length;

    if (i == 0 && fromIndex < normalizedCandidate.length) {
      // We are at the position right after the NDC. We get the region used for formatting
      // information based on the country code in the phone number, rather than the number itself,
      // as we do not need to distinguish between different countries with the same country
      // calling code and this is faster.
      String region = util.getRegionCodeForCountryCode(number.countryCode);

      if (util.getNddPrefixForRegion(region, true) != null &&
          _isDigit(normalizedCandidate.toString()[fromIndex])) {
        // This means there is no formatting symbol after the NDC. In this case, we only
        // accept the number if there is no formatting symbol at all in the number, except
        // for extensions. This is only important for countries with national prefixes.
        String nationalSignificantNumber =
            util.getNationalSignificantNumber(number);
        return normalizedCandidate
            .toString()
            .substring(fromIndex - formattedNumberGroups[i].length)
            .startsWith(nationalSignificantNumber);
      } else {
        continue;
      }
    }
  }
  // The check here makes sure that we haven't mistakenly already used the extension to
  // match the last group of the subscriber number. Note the extension cannot have
  // formatting in-between digits.
  return normalizedCandidate
      .toString()
      .substring(fromIndex)
      .contains(number.extension_3);
}