containsOnlyValidXChars static method

bool containsOnlyValidXChars(
  1. PhoneNumber number,
  2. String candidate,
  3. PhoneNumberUtil util
)

Implementation

static bool containsOnlyValidXChars(
    PhoneNumber number, String candidate, PhoneNumberUtil util) {
  // The characters 'x' and 'X' can be (1) a carrier code, in which case they always precede the
  // national significant number or (2) an extension sign, in which case they always precede the
  // extension number. We assume a carrier code is more than 1 digit, so the first case has to
  // have more than 1 consecutive 'x' or 'X', whereas the second case can only have exactly 1 'x'
  // or 'X'. We ignore the character if it appears as the last character of the string.
  for (int index = 0; index < candidate.length - 1; index++) {
    String charAtIndex = candidate[index];
    if (charAtIndex == 'x' || charAtIndex == 'X') {
      String charAtNextIndex = candidate[index + 1];
      if (charAtNextIndex == 'x' || charAtNextIndex == 'X') {
        // This is the carrier code case, in which the 'X's always precede the national
        // significant number.
        index++;
        if (util.isNumberMatch(number, candidate.substring(index)) !=
            MatchType.nsnMatch) {
          return false;
        }
        // This is the extension sign case, in which the 'x' or 'X' should always precede the
        // extension number.
      } else if (PhoneNumberUtil.normalizeDigitsOnly(
              candidate.substring(index)) !=
          (number.extension_3)) {
        return false;
      }
    }
  }
  return true;
}