internal static bool containsMoreThanOneSlashInNationalNumber(PhoneNumber number, String candidate) { int firstSlashInBodyIndex = candidate.IndexOf('/'); if (firstSlashInBodyIndex < 0) { // No slashes, this is okay. return(false); } // Now look for a second one. int secondSlashInBodyIndex = candidate.IndexOf('/', firstSlashInBodyIndex + 1); if (secondSlashInBodyIndex < 0) { // Only one slash, this is okay. return(false); } // If the first slash is after the country calling code, this is permitted. bool candidateHasCountryCode = (number.getCountryCodeSource() == PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN || number.getCountryCodeSource() == PhoneNumber.CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN); if (candidateHasCountryCode && PhoneNumberUtil.normalizeDigitsOnly(candidate.Substring(0, firstSlashInBodyIndex)) .Equals(number.getCountryCode().ToString())) { // Any more slashes and this is illegal. return(candidate.Substring(secondSlashInBodyIndex + 1).Contains("/")); } return(true); }
internal static bool allNumberGroupsRemainGrouped(PhoneNumberUtil util, PhoneNumber number, StringBuilder normalizedCandidate, String[] formattedNumberGroups) { int fromIndex = 0; if (number.getCountryCodeSource() != PhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY) { // First skip the country code if the normalized candidate contained it. String countryCode = number.getCountryCode().ToString(); fromIndex = normalizedCandidate.ToString().IndexOf(countryCode) + countryCode.Length; } // 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.ToString().IndexOf(formattedNumberGroups[i], fromIndex, StringComparison.Ordinal); 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 && char.IsDigit(normalizedCandidate[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())); }
internal static bool isNationalPrefixPresentIfRequired(PhoneNumber number, PhoneNumberUtil util) { // First, check how we deduced the country code. If it was written in international format, then // the national prefix is not required. if (number.getCountryCodeSource() != PhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY) { return(true); } String phoneNumberRegion = util.getRegionCodeForCountryCode(number.getCountryCode()); PhoneMetadata metadata = util.getMetadataForRegion(phoneNumberRegion); if (metadata == null) { return(true); } // Check if a national prefix should be present when formatting this number. String nationalNumber = util.getNationalSignificantNumber(number); NumberFormat formatRule = util.chooseFormattingRegexForNumber(metadata.numberFormats(), nationalNumber); // To do this, we check that a national prefix formatting rule was present and that it wasn't // just the first-group symbol ($1) with punctuation. if ((formatRule != null) && formatRule.getNationalPrefixFormattingRule().Length > 0) { if (formatRule.isNationalPrefixOptionalWhenFormatting()) { // The national-prefix is optional in these cases, so we don't need to check if it was // present. return(true); } if (PhoneNumberUtil.formattingRuleHasFirstGroupOnly( formatRule.getNationalPrefixFormattingRule())) { // National Prefix not needed for this number. return(true); } // Normalize the remainder. String rawInputCopy = PhoneNumberUtil.normalizeDigitsOnly(number.getRawInput()); StringBuilder rawInput = new StringBuilder(rawInputCopy); // Check if we found a national prefix and/or carrier code at the start of the raw input, and // return the result. return(util.maybeStripNationalPrefixAndCarrierCode(rawInput, metadata, null)); } return(true); }
public PhoneNumber mergeFrom(PhoneNumber other) { if (other.HasCountryCode()) { setCountryCode(other.getCountryCode()); } if (other.HasNationalNumber()) { setNationalNumber(other.getNationalNumber()); } if (other.HasExtension()) { setExtension(other.getExtension()); } if (other.HasItalianLeadingZero()) { setItalianLeadingZero(other.isItalianLeadingZero()); } if (other.HasNumberOfLeadingZeros()) { setNumberOfLeadingZeros(other.getNumberOfLeadingZeros()); } if (other.HasRawInput()) { setRawInput(other.getRawInput()); } if (other.HasCountryCodeSource()) { setCountryCodeSource(other.getCountryCodeSource()); } if (other.HasPreferredDomesticCarrierCode()) { setPreferredDomesticCarrierCode(other.getPreferredDomesticCarrierCode()); } return(this); }