/** * Invokes inputDigitHelper on each digit of the national number accrued, and returns a formatted * string in the end. */ private String inputAccruedNationalNumber() { int lengthOfNationalNumber = nationalNumber.length(); if (lengthOfNationalNumber > 0) { String tempNationalNumber = ""; for (int i = 0; i < lengthOfNationalNumber; i++) { tempNationalNumber = inputDigitHelper(nationalNumber.charAt(i)); } return(ableToFormat ? appendNationalNumber(tempNationalNumber) : accruedInput.toString()); } else { return(prefixBeforeNationalNumber.toString()); } }
/** * Returns the current position in the partially formatted phone number of the character which was * previously passed in as the parameter of {@link #inputDigitAndRememberPosition}. */ public int getRememberedPosition() { if (!ableToFormat) { return(originalPosition); } int accruedInputIndex = 0, currentOutputIndex = 0; while (accruedInputIndex < positionToRemember && currentOutputIndex < currentOutput.length()) { if (accruedInputWithoutFormatting.charAt(accruedInputIndex) == currentOutput.charAt(currentOutputIndex)) { accruedInputIndex++; } currentOutputIndex++; } return(currentOutputIndex); }
internal static boolean allNumberGroupsRemainGrouped(PhoneNumberUtil util, PhoneNumber number, StringBuilder normalizedCandidate, String[] formattedNumberGroups) { int fromIndex = 0; // Check each group of consecutive digits are not broken into separate groupings in the // {@code normalizedCandidate} string. for (int i = 0; i < formattedNumberGroups.length(); i++) { // Fails if the substring of {@code normalizedCandidate} starting from {@code fromIndex} // doesn't contain the consecutive digits in formattedNumberGroups[i]. fromIndex = normalizedCandidate.indexOf(formattedNumberGroups[i], fromIndex); if (fromIndex < 0) { return(false); } // Moves {@code 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.getCountryCode()); if (util.getNddPrefixForRegion(region, true) != null && Character.isDigit(normalizedCandidate.charAt(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.substring(fromIndex - formattedNumberGroups[i].length()) .startsWith(nationalSignificantNumber)); } } } // 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.substring(fromIndex).contains(number.getExtension())); }
/** * Combines the national number with any prefix (IDD/+ and country code or national prefix) that * was collected. A space will be inserted between them if the current formatting template * indicates this to be suitable. */ private String appendNationalNumber(String nationalNumber) { int prefixBeforeNationalNumberLength = prefixBeforeNationalNumber.length(); if (shouldAddSpaceAfterNationalPrefix && prefixBeforeNationalNumberLength > 0 && prefixBeforeNationalNumber.charAt(prefixBeforeNationalNumberLength - 1) != SEPARATOR_BEFORE_NATIONAL_NUMBER) { // We want to add a space after the national prefix if the national prefix formatting rule // indicates that this would normally be done, with the exception of the case where we already // appended a space because the NDD was surprisingly long. return(new String(prefixBeforeNationalNumber) + SEPARATOR_BEFORE_NATIONAL_NUMBER + nationalNumber); } else { return(prefixBeforeNationalNumber + nationalNumber); } }