Ejemplo n.º 1
0
 internal 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++)
     {
         char charAtIndex = candidate[index];
         if (charAtIndex == 'x' || charAtIndex == 'X')
         {
             char 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)) != PhoneNumberUtil.MatchType.NSN_MATCH)
                 {
                     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)).Equals(
                          number.getExtension()))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Ejemplo n.º 2
0
        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()));
        }
Ejemplo n.º 3
0
 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);
 }