From 6dda1bb87fd459d5ee9b7bab4b13c72c7b9843f9 Mon Sep 17 00:00:00 2001 From: Shaopeng Jia Date: Mon, 11 Apr 2011 07:02:57 +0000 Subject: [PATCH] JAVA: libphonenumber 2011/04/11 release. --- java/release_notes.txt | 11 + .../i18n/phonenumbers/AsYouTypeFormatter.java | 132 ++-- .../CountryCodeToRegionCodeMap.java | 3 +- .../i18n/phonenumbers/PhoneNumberMatcher.java | 11 +- .../i18n/phonenumbers/PhoneNumberUtil.java | 88 ++- .../data/PhoneNumberMetadataProto_AG | Bin 476 -> 491 bytes .../data/PhoneNumberMetadataProto_AI | Bin 410 -> 425 bytes .../data/PhoneNumberMetadataProto_AS | Bin 392 -> 407 bytes .../data/PhoneNumberMetadataProto_BB | Bin 388 -> 403 bytes .../data/PhoneNumberMetadataProto_BM | Bin 426 -> 441 bytes .../data/PhoneNumberMetadataProto_BS | Bin 568 -> 583 bytes .../data/PhoneNumberMetadataProto_CA | Bin 661 -> 695 bytes .../data/PhoneNumberMetadataProto_CK | Bin 155 -> 282 bytes .../data/PhoneNumberMetadataProto_DM | Bin 435 -> 450 bytes .../data/PhoneNumberMetadataProto_DO | Bin 376 -> 398 bytes .../data/PhoneNumberMetadataProto_ET | Bin 1074 -> 936 bytes .../data/PhoneNumberMetadataProto_FK | Bin 153 -> 218 bytes .../data/PhoneNumberMetadataProto_GD | Bin 485 -> 500 bytes .../data/PhoneNumberMetadataProto_GE | Bin 696 -> 500 bytes .../data/PhoneNumberMetadataProto_GT | Bin 147 -> 387 bytes .../data/PhoneNumberMetadataProto_GU | Bin 445 -> 785 bytes .../data/PhoneNumberMetadataProto_IN | Bin 3101 -> 3161 bytes .../data/PhoneNumberMetadataProto_IO | Bin 147 -> 245 bytes .../data/PhoneNumberMetadataProto_JM | Bin 530 -> 545 bytes .../data/PhoneNumberMetadataProto_KI | Bin 153 -> 270 bytes .../data/PhoneNumberMetadataProto_KN | Bin 419 -> 434 bytes .../data/PhoneNumberMetadataProto_KY | Bin 518 -> 533 bytes .../data/PhoneNumberMetadataProto_LC | Bin 463 -> 478 bytes .../data/PhoneNumberMetadataProto_MN | Bin 691 -> 691 bytes .../data/PhoneNumberMetadataProto_MP | Bin 463 -> 485 bytes .../data/PhoneNumberMetadataProto_MR | Bin 511 -> 385 bytes .../data/PhoneNumberMetadataProto_MS | Bin 359 -> 374 bytes .../data/PhoneNumberMetadataProto_NO | Bin 566 -> 576 bytes .../data/PhoneNumberMetadataProto_PR | Bin 385 -> 407 bytes .../data/PhoneNumberMetadataProto_SJ | Bin 0 -> 411 bytes .../data/PhoneNumberMetadataProto_SV | Bin 147 -> 455 bytes .../data/PhoneNumberMetadataProto_TC | Bin 452 -> 467 bytes .../data/PhoneNumberMetadataProto_TR | Bin 662 -> 684 bytes .../data/PhoneNumberMetadataProto_TT | Bin 514 -> 529 bytes .../data/PhoneNumberMetadataProto_US | Bin 1578 -> 1601 bytes .../data/PhoneNumberMetadataProto_VC | Bin 505 -> 492 bytes .../data/PhoneNumberMetadataProto_VG | Bin 490 -> 477 bytes .../data/PhoneNumberMetadataProto_VI | Bin 512 -> 413 bytes .../phonenumbers/PhoneNumberMatcherTest.java | 13 +- .../phonenumbers/PhoneNumberUtilTest.java | 11 +- .../PhoneNumberMetadataProtoForTesting_SG | Bin 393 -> 400 bytes .../PhoneNumberMetadataProtoForTesting_US | Bin 512 -> 533 bytes resources/PhoneNumberMetaData.xml | 740 ++++++++++++------ resources/PhoneNumberMetaDataForTesting.xml | 8 +- 49 files changed, 662 insertions(+), 355 deletions(-) create mode 100644 java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SJ diff --git a/java/release_notes.txt b/java/release_notes.txt index 3460a14f1..5784e0ed8 100644 --- a/java/release_notes.txt +++ b/java/release_notes.txt @@ -1,3 +1,14 @@ +Apr 11th, 2011 +* Bug fixes: + - Adding date exclusion pattern in PhoneNumberMatcher. + - Fixing bug with selecting a suitable region for numbers with leading zeros where mutiple regions + for that country code are possible. +* Metadata changes: + - Updates: AG, AI, AS, BB, BM, BS, CA, DM, DO, ET, FO, GD, GE, GU, IN, JM, KN, KY, LC, MN, MP, MR, + MS, NO, PR, TC, TR, TT, US, VC, VG, VI + - New countries: CK, FK, GT, IO, KI, SJ, SV +* Documentation updates and some small refactoring to AsYouTypeFormatter + Mar 31st, 2011 * New functionality: - Can parse and format numbers in RFC-3966 format. diff --git a/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java b/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java index 15d9313d2..1dac89a1a 100644 --- a/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java +++ b/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java @@ -28,13 +28,13 @@ import java.util.regex.Pattern; /** * A formatter which formats phone numbers as they are entered. * - * An AsYouTypeFormatter could be created by invoking the getAsYouTypeFormatter method of the - * PhoneNumberUtil. After that digits could be added by invoking the inputDigit method on the - * formatter instance, and the partially formatted phone number will be returned each time a digit - * is added. The clear method could be invoked before a new number needs to be formatted. + *

An AsYouTypeFormatter could be created by invoking + * {@link PhoneNumberUtil#getAsYouTypeFormatter}. After that digits could be added by invoking + * {@link #inputDigit} on the formatter instance, and the partially formatted phone number will be + * returned each time a digit is added. {@link #clear} could be invoked before a new number needs to + * be formatted. * - * See testAsYouTypeFormatterUS(), testAsYouTestFormatterGB() and testAsYouTypeFormatterDE() in - * PhoneNumberUtilTest.java for more details on how the formatter is to be used. + *

See the unittests for more details on how the formatter is to be used. * * @author Shaopeng Jia */ @@ -47,9 +47,12 @@ public class AsYouTypeFormatter { private StringBuffer accruedInputWithoutFormatting = new StringBuffer(); private boolean ableToFormat = true; private boolean isInternationalFormatting = false; - private boolean isExpectingCountryCode = false; + private boolean isExpectingCountryCallingCode = false; private final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); private String defaultCountry; + + private static final PhoneMetadata EMPTY_METADATA = + new PhoneMetadata().setInternationalPrefix("NA"); private PhoneMetadata defaultMetaData; private PhoneMetadata currentMetaData; @@ -79,8 +82,6 @@ public class AsYouTypeFormatter { // The position of a digit upon which inputDigitAndRememberPosition is most recently invoked, as // found in accruedInputWithoutFormatting. private int positionToRemember = 0; - private Pattern nationalPrefixForParsing; - private Pattern internationalPrefix; private StringBuffer prefixBeforeNationalNumber = new StringBuffer(); private StringBuffer nationalNumber = new StringBuffer(); private List possibleFormats = new ArrayList(); @@ -96,21 +97,18 @@ public class AsYouTypeFormatter { */ AsYouTypeFormatter(String regionCode) { defaultCountry = regionCode; - initializeCountrySpecificInfo(defaultCountry); + currentMetaData = getMetadataForRegion(defaultCountry); defaultMetaData = currentMetaData; } - private void initializeCountrySpecificInfo(String regionCode) { - currentMetaData = phoneUtil.getMetadataForRegion(regionCode); - if (currentMetaData == null) { - // Set to a default instance of the metadata. This allows us to function with an incorrect - // region code, even if formatting only works for numbers specified with "+". - currentMetaData = new PhoneMetadata().setInternationalPrefix("NA"); + private PhoneMetadata getMetadataForRegion(String regionCode) { + PhoneMetadata metadata = phoneUtil.getMetadataForRegion(regionCode); + if (metadata != null) { + return metadata; } - nationalPrefixForParsing = - regexCache.getPatternForRegex(currentMetaData.getNationalPrefixForParsing()); - internationalPrefix = - regexCache.getPatternForRegex("\\+|" + currentMetaData.getInternationalPrefix()); + // Set to a default instance of the metadata. This allows us to function with an incorrect + // region code, even if formatting only works for numbers specified with "+". + return EMPTY_METADATA; } // Returns true if a new template is created as opposed to reusing the existing template. @@ -141,8 +139,7 @@ public class AsYouTypeFormatter { } private void narrowDownPossibleFormats(String leadingDigits) { - int lengthOfLeadingDigits = leadingDigits.length(); - int indexOfLeadingDigitsPattern = lengthOfLeadingDigits - MIN_LEADING_DIGITS_LENGTH; + int indexOfLeadingDigitsPattern = leadingDigits.length() - MIN_LEADING_DIGITS_LENGTH; Iterator it = possibleFormats.iterator(); while (it.hasNext()) { NumberFormat format = it.next(); @@ -160,7 +157,6 @@ public class AsYouTypeFormatter { } private boolean createFormattingTemplate(NumberFormat format) { - String numberFormat = format.getFormat(); String numberPattern = format.getPattern(); // The formatter doesn't format numbers when numberPattern contains "|", e.g. @@ -175,7 +171,7 @@ public class AsYouTypeFormatter { // Replace any standalone digit (not the one in d{}) with \d numberPattern = STANDALONE_DIGIT_PATTERN.matcher(numberPattern).replaceAll("\\\\d"); formattingTemplate.setLength(0); - String tempTemplate = getFormattingTemplate(numberPattern, numberFormat); + String tempTemplate = getFormattingTemplate(numberPattern, format.getFormat()); if (tempTemplate.length() > nationalNumber.length()) { formattingTemplate.append(tempTemplate); return true; @@ -215,10 +211,10 @@ public class AsYouTypeFormatter { positionToRemember = 0; originalPosition = 0; isInternationalFormatting = false; - isExpectingCountryCode = false; + isExpectingCountryCallingCode = false; possibleFormats.clear(); if (!currentMetaData.equals(defaultMetaData)) { - initializeCountrySpecificInfo(defaultCountry); + currentMetaData = getMetadataForRegion(defaultCountry); } } @@ -226,8 +222,9 @@ public class AsYouTypeFormatter { * Formats a phone number on-the-fly as each digit is entered. * * @param nextChar the most recently entered digit of a phone number. Formatting characters are - * allowed, but they are removed from the result. Full width digits and Arabic-indic digits - * are allowed, and will be shown as they are. + * allowed, but as soon as they are encountered this method formats the number as entered and + * not "as you type" anymore. Full width digits and Arabic-indic digits are allowed, and will + * be shown as they are. * @return the partially formatted phone number. */ public String inputDigit(char nextChar) { @@ -236,9 +233,10 @@ public class AsYouTypeFormatter { } /** - * Same as inputDigit, but remembers the position where nextChar is inserted, so that it could be - * retrieved later by using getRememberedPosition(). The remembered position will be automatically - * adjusted if additional formatting characters are later inserted/removed in front of nextChar. + * Same as {@link #inputDigit}, but remembers the position where {@code nextChar} is inserted, so + * that it could be retrieved later by using {@link #getRememberedPosition}. The remembered + * position will be automatically adjusted if additional formatting characters are later + * inserted/removed in front of {@code nextChar}. */ public String inputDigitAndRememberPosition(char nextChar) { currentOutput = inputDigitWithOptionToRememberPosition(nextChar, true); @@ -265,29 +263,29 @@ public class AsYouTypeFormatter { // We start to attempt to format only when at least MIN_LEADING_DIGITS_LENGTH digits (the plus // sign is counted as a digit as well for this purpose) have been entered. switch (accruedInputWithoutFormatting.length()) { - case 0: // this is the case where the first few inputs are neither digits nor the plus sign. + case 0: case 1: case 2: return accruedInput.toString(); case 3: if (attemptToExtractIdd()) { - isExpectingCountryCode = true; + isExpectingCountryCallingCode = true; } else { // No IDD or plus sign is found, must be entering in national format. removeNationalPrefixFromNationalNumber(); return attemptToChooseFormattingPattern(); } case 4: case 5: - if (isExpectingCountryCode) { - if (attemptToExtractCountryCode()) { - isExpectingCountryCode = false; + if (isExpectingCountryCallingCode) { + if (attemptToExtractCountryCallingCode()) { + isExpectingCountryCallingCode = false; } return prefixBeforeNationalNumber + nationalNumber.toString(); } - // We make a last attempt to extract a country code at the 6th digit because the maximum - // length of IDD and country code are both 3. + // We make a last attempt to extract a country calling code at the 6th digit because the + // maximum length of IDD and country calling code are both 3. case 6: - if (isExpectingCountryCode && !attemptToExtractCountryCode()) { + if (isExpectingCountryCallingCode && !attemptToExtractCountryCallingCode()) { ableToFormat = false; return accruedInput.toString(); } @@ -326,22 +324,19 @@ public class AsYouTypeFormatter { /** * Returns the current position in the partially formatted phone number of the character which was - * previously passed in as the parameter of inputDigitAndRememberPosition(). + * previously passed in as the parameter of {@link #inputDigitAndRememberPosition}. */ public int getRememberedPosition() { if (!ableToFormat) { return originalPosition; } int accruedInputIndex = 0, currentOutputIndex = 0; - int currentOutputLength = currentOutput.length(); - while (accruedInputIndex < positionToRemember && currentOutputIndex < currentOutputLength) { + while (accruedInputIndex < positionToRemember && currentOutputIndex < currentOutput.length()) { if (accruedInputWithoutFormatting.charAt(accruedInputIndex) == currentOutput.charAt(currentOutputIndex)) { accruedInputIndex++; - currentOutputIndex++; - } else { - currentOutputIndex++; } + currentOutputIndex++; } return currentOutputIndex; } @@ -384,6 +379,8 @@ public class AsYouTypeFormatter { prefixBeforeNationalNumber.append("1 "); isInternationalFormatting = true; } else if (currentMetaData.hasNationalPrefix()) { + Pattern nationalPrefixForParsing = + regexCache.getPatternForRegex(currentMetaData.getNationalPrefixForParsing()); Matcher m = nationalPrefixForParsing.matcher(nationalNumber); if (m.lookingAt()) { // When the national prefix is detected, we use international formatting rules instead of @@ -405,14 +402,17 @@ public class AsYouTypeFormatter { * defaultCountry. */ private boolean attemptToExtractIdd() { + Pattern internationalPrefix = + regexCache.getPatternForRegex("\\" + PhoneNumberUtil.PLUS_SIGN + "|" + + currentMetaData.getInternationalPrefix()); Matcher iddMatcher = internationalPrefix.matcher(accruedInputWithoutFormatting); if (iddMatcher.lookingAt()) { isInternationalFormatting = true; - int startOfCountryCode = iddMatcher.end(); + int startOfCountryCallingCode = iddMatcher.end(); nationalNumber.setLength(0); - nationalNumber.append(accruedInputWithoutFormatting.substring(startOfCountryCode)); + nationalNumber.append(accruedInputWithoutFormatting.substring(startOfCountryCallingCode)); prefixBeforeNationalNumber.append( - accruedInputWithoutFormatting.substring(0, startOfCountryCode)); + accruedInputWithoutFormatting.substring(0, startOfCountryCallingCode)); if (accruedInputWithoutFormatting.charAt(0) != PhoneNumberUtil.PLUS_SIGN) { prefixBeforeNationalNumber.append(" "); } @@ -422,41 +422,41 @@ public class AsYouTypeFormatter { } /** - * Extracts country code from the beginning of nationalNumber to prefixBeforeNationalNumber when - * they are available, and places the remaining input into nationalNumber. + * Extracts the country calling code from the beginning of nationalNumber to + * prefixBeforeNationalNumber when they are available, and places the remaining input into + * nationalNumber. * - * @return true when a valid country code can be found. + * @return true when a valid country calling code can be found. */ - private boolean attemptToExtractCountryCode() { + private boolean attemptToExtractCountryCallingCode() { if (nationalNumber.length() == 0) { return false; } - StringBuffer numberWithoutCountryCode = new StringBuffer(); - int countryCode = phoneUtil.extractCountryCode(nationalNumber, numberWithoutCountryCode); + StringBuffer numberWithoutCountryCallingCode = new StringBuffer(); + int countryCode = phoneUtil.extractCountryCode(nationalNumber, numberWithoutCountryCallingCode); if (countryCode == 0) { return false; - } else { - nationalNumber.setLength(0); - nationalNumber.append(numberWithoutCountryCode); - String newRegionCode = phoneUtil.getRegionCodeForCountryCode(countryCode); - if (!newRegionCode.equals(defaultCountry)) { - initializeCountrySpecificInfo(newRegionCode); - } - String countryCodeString = Integer.toString(countryCode); - prefixBeforeNationalNumber.append(countryCodeString).append(" "); } + nationalNumber.setLength(0); + nationalNumber.append(numberWithoutCountryCallingCode); + String newRegionCode = phoneUtil.getRegionCodeForCountryCode(countryCode); + if (newRegionCode != defaultCountry) { + currentMetaData = getMetadataForRegion(newRegionCode); + } + String countryCodeString = Integer.toString(countryCode); + prefixBeforeNationalNumber.append(countryCodeString).append(" "); return true; } // Accrues digits and the plus sign to accruedInputWithoutFormatting for later use. If nextChar // contains a digit in non-ASCII format (e.g. the full-width version of digits), it is first // normalized to the ASCII version. The return value is nextChar itself, or its normalized - // version, if nextChar is a digit in non-ASCII format. + // version, if nextChar is a digit in non-ASCII format. This method assumes its input is either a + // digit or the plus sign. private char normalizeAndAccrueDigitsAndPlusSign(char nextChar, boolean rememberPosition) { if (nextChar == PhoneNumberUtil.PLUS_SIGN) { accruedInputWithoutFormatting.append(nextChar); - } - if (PhoneNumberUtil.DIGIT_MAPPINGS.containsKey(nextChar)) { + } else { nextChar = PhoneNumberUtil.DIGIT_MAPPINGS.get(nextChar); accruedInputWithoutFormatting.append(nextChar); nationalNumber.append(nextChar); diff --git a/java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java b/java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java index 8d4b1f9ce..eebb724b8 100644 --- a/java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java +++ b/java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java @@ -132,8 +132,9 @@ public class CountryCodeToRegionCodeMap { listWithRegionCode.add("SE"); countryCodeToRegionCodeMap.put(46, listWithRegionCode); - listWithRegionCode = new ArrayList(1); + listWithRegionCode = new ArrayList(2); listWithRegionCode.add("NO"); + listWithRegionCode.add("SJ"); countryCodeToRegionCodeMap.put(47, listWithRegionCode); listWithRegionCode = new ArrayList(1); diff --git a/java/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java b/java/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java index 1905b5e44..09bc51e57 100644 --- a/java/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java +++ b/java/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java @@ -68,6 +68,13 @@ final class PhoneNumberMatcher implements Iterator { */ private static final Pattern PUB_PAGES = Pattern.compile("\\d{1,5}-+\\d{1,5}\\s{0,4}\\(\\d{1,4}"); + /** + * Matches strings that look like dates using "/" as a separator. Examples: 3/10/2011, 31/10/96 or + * 08/31/95. + */ + private static final Pattern SLASH_SEPARATED_DATES = + Pattern.compile("(?:(?:[0-3]?\\d/[01]?\\d)|(?:[01]?\\d/[0-3]?\\d))/(?:[12]\\d)?\\d{2}"); + static { /* Builds the PATTERN and INNER regular expression patterns. The building blocks below * exist to make the patterns more easily understood. */ @@ -268,8 +275,8 @@ final class PhoneNumberMatcher implements Iterator { * @return the match found, null if none can be found */ private PhoneNumberMatch extractMatch(CharSequence candidate, int offset) { - // Skip a match that is more likely a publication page reference. - if (PUB_PAGES.matcher(candidate).find()) { + // Skip a match that is more likely a publication page reference or a date. + if (PUB_PAGES.matcher(candidate).find() || SLASH_SEPARATED_DATES.matcher(candidate).find()) { return null; } diff --git a/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index 13a5bc2e1..23c3446f8 100644 --- a/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -1527,7 +1527,7 @@ public class PhoneNumberUtil { private String getRegionCodeForNumberFromRegionList(PhoneNumber number, List regionCodes) { - String nationalNumber = String.valueOf(number.getNationalNumber()); + String nationalNumber = getNationalSignificantNumber(number); for (String regionCode : regionCodes) { // If leadingDigits is present, use this. Otherwise, do full validation. PhoneMetadata metadata = getMetadataForRegion(regionCode); @@ -1663,6 +1663,24 @@ public class PhoneNumberUtil { return isPossibleNumberWithReason(number) == ValidationResult.IS_POSSIBLE; } + /** + * Helper method to check a number against a particular pattern and determine whether it matches, + * or is too short or too long. Currently, if a number pattern suggests that numbers of length 7 + * and 10 are possible, and a number in between these possible lengths is entered, such as of + * length 8, this will return TOO_LONG. + */ + private ValidationResult testNumberLengthAgainstPattern(Pattern numberPattern, String number) { + Matcher numberMatcher = numberPattern.matcher(number); + if (numberMatcher.matches()) { + return ValidationResult.IS_POSSIBLE; + } + if (numberMatcher.lookingAt()) { + return ValidationResult.TOO_LONG; + } else { + return ValidationResult.TOO_SHORT; + } + } + /** * Check whether a phone number is a possible number. It provides a more lenient check than * {@link #isValidNumber} in the following sense: @@ -1708,14 +1726,9 @@ public class PhoneNumberUtil { return ValidationResult.IS_POSSIBLE; } } - String possibleNumberPattern = generalNumDesc.getPossibleNumberPattern(); - Matcher m = regexCache.getPatternForRegex(possibleNumberPattern).matcher(nationalNumber); - if (m.lookingAt()) { - return (m.end() == nationalNumber.length()) ? ValidationResult.IS_POSSIBLE - : ValidationResult.TOO_LONG; - } else { - return ValidationResult.TOO_SHORT; - } + Pattern possibleNumberPattern = + regexCache.getPatternForRegex(generalNumDesc.getPossibleNumberPattern()); + return testNumberLengthAgainstPattern(possibleNumberPattern, nationalNumber); } /** @@ -1869,37 +1882,34 @@ public class PhoneNumberUtil { throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE, "Country calling code supplied was not recognised."); } else if (defaultRegionMetadata != null) { - // Check to see if the number is valid for the default region already. If not, we check to - // see if the country calling code for the default region is present at the start of the - // number. - PhoneNumberDesc generalDesc = defaultRegionMetadata.getGeneralDesc(); - Pattern validNumberPattern = - regexCache.getPatternForRegex(generalDesc.getNationalNumberPattern()); - if (!validNumberPattern.matcher(fullNumber).matches()) { - int defaultCountryCode = defaultRegionMetadata.getCountryCode(); - String defaultCountryCodeString = String.valueOf(defaultCountryCode); - String normalizedNumber = fullNumber.toString(); - if (normalizedNumber.startsWith(defaultCountryCodeString)) { - // If so, strip this, and see if the resultant number is valid. - StringBuffer potentialNationalNumber = - new StringBuffer(normalizedNumber.substring(defaultCountryCodeString.length())); - maybeStripNationalPrefixAndCarrierCode(potentialNationalNumber, defaultRegionMetadata); - Matcher possibleNumberMatcher = - regexCache.getPatternForRegex(generalDesc.getPossibleNumberPattern()).matcher( - potentialNationalNumber); - // If the resultant number is either valid, or still too long even with the country - // calling code stripped, we consider this a better result and keep the potential national - // number. - if (validNumberPattern.matcher(potentialNationalNumber).matches() || - (possibleNumberMatcher.lookingAt() && - possibleNumberMatcher.end() != potentialNationalNumber.length())) { - nationalNumber.append(potentialNationalNumber); - if (keepRawInput) { - phoneNumber.setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN); - } - phoneNumber.setCountryCode(defaultCountryCode); - return defaultCountryCode; + // Check to see if the number starts with the country calling code for the default region. If + // so, we remove the country calling code, and do some checks on the validity of the number + // before and after. + int defaultCountryCode = defaultRegionMetadata.getCountryCode(); + String defaultCountryCodeString = String.valueOf(defaultCountryCode); + String normalizedNumber = fullNumber.toString(); + if (normalizedNumber.startsWith(defaultCountryCodeString)) { + StringBuffer potentialNationalNumber = + new StringBuffer(normalizedNumber.substring(defaultCountryCodeString.length())); + PhoneNumberDesc generalDesc = defaultRegionMetadata.getGeneralDesc(); + Pattern validNumberPattern = + regexCache.getPatternForRegex(generalDesc.getNationalNumberPattern()); + maybeStripNationalPrefixAndCarrierCode(potentialNationalNumber, defaultRegionMetadata); + Pattern possibleNumberPattern = + regexCache.getPatternForRegex(generalDesc.getPossibleNumberPattern()); + // If the number was not valid before but is valid now, or if it was too long before, we + // consider the number with the country code stripped to be a better result and keep that + // instead. + if ((!validNumberPattern.matcher(fullNumber).matches() && + validNumberPattern.matcher(potentialNationalNumber).matches()) || + testNumberLengthAgainstPattern(possibleNumberPattern, fullNumber.toString()) + == ValidationResult.TOO_LONG) { + nationalNumber.append(potentialNationalNumber); + if (keepRawInput) { + phoneNumber.setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN); } + phoneNumber.setCountryCode(defaultCountryCode); + return defaultCountryCode; } } } diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AG b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AG index 8679e74bb71076d9d1d878069f7867b6747f977b..ce1d50894c8c520814cdc441b2b8ce8e2bd51971 100644 GIT binary patch delta 75 zcmcb^{F+&B&07Z6Dh39|M+`uMk&%Hr+Q`(xGBzfq+On3Bfgebl*J{{X0cqn}P5X&z MS_0@|8zY+;0s7Yz>Hq)$ delta 60 zcmaFOe1}@Mn*;k?r0-Z3(MG;lxoXbMh1Q$ZCwMn*;ku4p3*%h;HdYRg(i1`Z%?u48CWJ5jHc6VBPV Hc?u%{v7--< diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CA b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CA index d58714a64d577dac61509250cdfa9939bc1e8536..5d9bfb2ad3e22c5cf60fc8ca72df568a1b7d8258 100644 GIT binary patch delta 167 zcmbQrx}BAC&07Z6Dh39obrU(IwfJLFs?BRP?5%*bajm9310y5DA|TfYhz(3?jLd4H zO)O$9YfKF${%@JQhmqS1T?Zormyv;qk+F%XnYjf}_e>nR+XT>cPVQs0#1y>1coYCn Cp)hR# delta 131 zcmdnaI+c}k&07Z6Dh39oo{5~&3LG&h)#f^e2DJ=~j12QN?5&J|*ubR5$gIZHpvJ;- tqF=ubTn!@wmyv;qk+F%XnYjf}{WNUqTeuk+;5sI+WVGZ&@IEmf1psg!A|C(% diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CK b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CK index 4f01ce284f07851709243ae4b5844bec7b7c8b3b..ce2a8ec3504f07eb4fa4b2aa1366723b4de545fc 100644 GIT binary patch literal 282 zcmZ4UmVvd3fq_ww0Z1@1GH^#5>6)6y#-vo6)G{)#0%_A)pag@ohP{VWMtrqHnp&fjY+AttYu{22h!%X8unH|+PGHJ9;kvr f&?4Hv$RyS>+DI3y&J2sXiEb(a=z2F6voZnzZf_m< delta 72 zcmeBU{=uZW<}Cwj6$1lfJ_C?oWMtrswy=ziNvXE1Wn|z0(&jpb2DKA)B{&hBP!$Ac IV<#&k0Dh1X9smFU diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_ET b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_ET index 443dcb9cd9efe2c394b93b4efc75513a550b8780..c6d0e64704ded3b31d42c8b40a6d53a91080a982 100644 GIT binary patch delta 68 zcmdnQv4Wj*&07Z6Dh3AT`5QS;F)8v`8b%xFTExbrRGZZ@GOz+^OCZf@X=rFqLUOh7n4A^T&yfU={ki3PQ=WMQK$lR7*uKq zF{4BZgC{#vZ7MOC!iz&<1DX-?;_H^IuW3>t*oD~v_|PZkGv~v2hRcGq*dulpl~US( Tb$8lNl5VD+issl&H`@IH5cW43 diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_FK b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_FK index 9ef7a8cfa8a4a45c02396a098152cb3759742610..47e774d24a7e9c5798df5660c8ebb2f6139cbeb4 100644 GIT binary patch literal 218 zcmZ4UmVveW3IhWJBO@aNSG19?d2CEdwMi`_11pd=tp$oRa07)+(8PiA#)d}5CO~P< oXj3y(O+ZOgs3eo0BO?Ql0GUH|HUpEJH_+u@7?=zUfL=fa0O?jAxc~qF literal 153 zcmZ4UmVvc=G6MqxBO{1_VMYcfKSwYDlAt=9fyvDqXxkSCCIbVYrHpU_Xg?GH0Ko7L ABme*a diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GD b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GD index b54ca62709591a486bcc9d7d6c6bfbb263268bee..ef111ddd119b6410756d6f8f8f61f8a4f040e5cc 100644 GIT binary patch delta 75 zcmaFL{DoO>&07Z6Dh39|*9<^{k&%Hr+QihtGBzfq+On3Bfgebl*J{{X0cqn}P5X&z NxdP~78|Qd30ssid6;l8J delta 60 zcmeyu{FGU3&07Z6Dh39|I}AXAk&%Hb+Qh;#HYTOovX+s714x_e7#h@0)XU|Bb2hH_ GWCQ@@q!2m) diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GE b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GE index f1941072690816e619b8d16446b21394283bd123..b385664aefb87694f98f6f067359ee8d867f84cf 100644 GIT binary patch delta 125 zcmdnN`i0qS&07Z6Dh39|*9<^{k&!_-+R#|n+%h&MrP{o<#sWxL)G{)#18Gwo%UYlk zh9C`ltBEz~DtbWL(4@vR+Q87%+#4oA)(3Lu z17sjgnt7v{H=p~%CHnfgLG%f*7y$qg;n^F_u5J$P(RHB`R2{;jkhgUw3Y^t`XRHlY zD4;U1&xxBX(A4p{Ce~FGNH!6dvMUsaHn^!Fc|Di4`}6y{OO diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GT b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GT index 5d68cc6885082d8393e77c7fe920b0fc5d844466..2f9f995adbd8ed6932b92100914c66c81fc97d51 100644 GIT binary patch literal 387 zcmbV|Jr2S!42A7V1q2dI-5EO20Vz&@lCU5qHYT>f#wmc@h^w(O^O6>+GnNuR*`FQ9 zuQ%m)BE}(Rro!v3xn6D$6Q1I&i_!(A6}%0b{SLkk)gCadyza~u1&QiF<%wdNY2CD) zu@2PS*G=1Z909FpggXOZA=WF2ghh)9ya)usN{NVuftrd#CXO;9+?dEt2PRx`w7)fF z$1^tZp-d^Pq_>Qg8$8~GYH*a7vMx2U_)^*!M@dwSIIQW1&l0{a>r}xq(0%tW(osY= delta 45 lcmZo>p3Eq^<}Cwjc`pM410y4dfZ&Pu4JSTuY36+J0da{dX;ii(bIE0c~>fcHimhih;L#t#W77I6TclsIqf z@&EOmPM(R@XGG+jqjM>>kNNoYG7gR|A=}|*cMF*geg_kJt1SRYILw+Pjj35uhA2g= z#5%oZXiL>5VGF!VOtp33#qh;yTYz`3)qF|Z7UYziBFCWP^b4OEgvb>(FTPk<`7fAr);qMf(EdvFXgwdQ~dAbupNan?dnywtBd8gv<8o%67vxDrwHY i#3&$Jgw{d$N&fRqtNXpJIITGZ2={dXT|THVaQXm2#J}Aok~>7J1z@iVq;?}YEsdNA_k>L{geD>M7 zKWTg=QV#+sMYAe$c|KikWvyt6JFShbK~adwthO76n61U9(?voA#L5)`3P~L+T;{@- zb0qp<5fn!WXRPxf{-fZM;XrE%L1L7YggLKl(T)@0WCBMR20mD8+xwW8T7OOh@lMMQ tJ4JwI7mLt!a!$Pe(j!3JOqv*iq4?*Uvi-674jO4=aH$>;Z4W4{l3q8JS$_Zk diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN index f8fafa942792fbc6f46c85c52254820d7681b226..5134699a062623eb9afe2c69aacc95a3ca5b34c6 100644 GIT binary patch delta 230 zcmbO$aZ|!*&07Z6Dh37?1|VQ$WMmLGj7h0B*D*A#t%)|$wTuPwEOiVGY8e?gfJ`$T zL!*rv*O~djT8vGB3Tlj_jm$RlvHWLLmN3+?w=y#@04c1ku>dnnbS!E$A(}0KnkVbB z&0yr)e2mSSkrQl&k*-{TCM$A?P5#9#$pP{Z2mkS8@VJWTk(i` diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JM b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JM index eef5197ee5ff80f26cf68681434e14c50899f470..bff2a7b67a2f167eaed5a340eb62f0ad2885721b 100644 GIT binary patch delta 74 zcmbQlvXDi0&07Z6Dh3872?hoRMn*;ku4q#W%h;HdYRg(i27VxIUaMhm1*DB@HSH&= N773t>ZCp^q2mqKE6W9O% delta 59 zcmZ3;GKocX&07Z6Dh387P6h@BMn*;k&S(qE*qD@R%UVVT4j^r=V`xx2QMZT_&e^!W Gh!Fsd6b`=t diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KI b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KI index 2cf4365629b9ee88cc72f55e1375604ed1244d4e..1b754cc33355d1a02eaa4bece34868793ffcc73c 100644 GIT binary patch literal 270 zcmZ4UmVvd3fq{{g0Z1@1GVnwj>6%$s#>S*no76HgumWk*TA&Prl!m=kw2`q1P^QMz zpvEHFP}ek86DVyAR|ZsWYzR~WRLvP}hGqs((hMre delta 43 icmeBUn#m}(<}Cwj`D6wL21Z5@0mTyw%XvU-7ytmO(*<(? diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KN b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KN index 746a95aa0bc1b6ce6ab7b88935e9f2bdf99ebfb6..c575305a6d16786cf7877f4dfa1cb343cc306e46 100644 GIT binary patch delta 74 zcmZ3?yop(N&07Z6Dh39|l?*_Fk&%Hb+SI}_HYTOovX+s7A4r?mYS>!=Y2#W=`-!SL L0_b8JqdXY_ykQg5 delta 59 zcmdnQyqH;a&07Z6Dh39|Sqwmek&%Hj+QKq6CZ*c4mXU!2NSo^z8q`kI)!~G5HfDM< F0syG@4!QsU diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KY b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KY index 2e285c1b70159fd3666780c0dfa9d7512bf0e74b..5bfc93e27002c8d26cbce2178f1fd71bc15b615a 100644 GIT binary patch delta 75 zcmZo;naZNK<}Cwj6$1kk4+8@OBO@aNceJsog=K6^O0{JzBLhE>Hm}vNw*u0}wVL)5 O)iMRp#Wv2!V*~)4O%v4s delta 60 zcmbQr(#E2;<}Cwj6$1m~Uj`t-$jHDIZERr~8Sc1mIUASf GF#-U;IuE-5 diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_LC b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_LC index 3b3ab6ba8391d2c9a20227fd411ec6b921cd588b..3239dd667d534fcb298a403bc0cfde4c20de78e6 100644 GIT binary patch delta 75 zcmX@le2-ag&07Z6Dh39|s|-Mbk&%Hr+SJ^_GBzfq+On3Bfgebl*J{{X0cqn}P5X&z M76RyE8`Cow0rFQA$p8QV delta 60 zcmcb|e4bft&07Z6Dh39|V+=rok&%Hb+T6l2HYTOovX+s714x_e7#h@0)U)7(b2gS` GFaiM6X%Daf diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MN b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MN index ebbcc6cd5a68de02fc54272c4eb7ae71139dc97d..b96b7f9e3e2c8f0b6dad66d400fefb196d3f0ff7 100644 GIT binary patch delta 17 YcmdnYx|wx?9;4Yr{bEM5jf(}C05U%XHUIzs delta 17 YcmdnYx|wx?9;3xX{bELojf(}C05V+#IsgCw diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MP b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MP index 7b51d36ac8fa97ce93367ed76cadc406113dd953..ca8b834a9a8a8b24ebda6645eed5e3ed7586b0ea 100644 GIT binary patch delta 96 zcmX@l{FGUL&07Z6Dh39|I}AXAk&%Hr+SJU#GBzfq+On3Bfgebl*J{{X0cqn}O?!rk TjV=P{f)h&uF~l~WV`KyX^}QRG delta 74 zcmaFLe4bf-&07Z6Dh39|V+=rok&%Hb+RVZ-HYTOovX+s714x_e7#h?vOq}4t3Fl30 M4n%M^K4D}80MW`69smFU diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MR b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MR index dd9fa9e822b0f0c373d11b99b15112f617c79ab4..d4e34bc550cb2147f41783fb8eb709b785b4815d 100644 GIT binary patch literal 385 zcmb_YI}XAy40S41s?*PD;YH9nsP@*?WW)pPBIwcP3D!czTGCp5J#=#em2(m0FON^*w_TT6RMqH z77j;{G@28TFBwIK4O}c+F%$PSc}6zohulT2t6EWu7}0`U)jW)wr+r=Iq5ra{=J6R* zexpWf#Pw_FW|S#JjCnhv4O7knT@7K#HG*JGz@@D YlZgf9Y^(-`M#d(Sr!dZEW@KOl0J`K8FaQ7m delta 60 zcmX@WvW)j zE&#5c>(OM@r2m zIiZ}2q)x{^`T2^|K@qmp>F~V%jz@G#%Za{f`=F5)9E2vOR#WQ#&@ia19~!ARA2IkX J70jrkrQX>=Q~>}0 literal 0 HcmV?d00001 diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SV b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SV index 0fc9f706a8a1625822216a6b48281301b98f1a10..695ef3f14d5babe62cafab3726cf375ad9904d10 100644 GIT binary patch literal 455 zcmbVIy$ZrW3{I-mf>1$l6gqThLE-LNd#y|H1zZG|>gH2u?~Qy0-^kh3q*twigLp#{ z?)ypjE;n#b0C)u|h%l>^KJ1TYZR#p7c*;yvuBfLbE)jgj=DDe+sI%y$|k-O{FP)6t%$USk@BN$cAQ G>=_TiU{!Gd literal 147 zcmZ4UmVvdrmw|zSkr70|Fe3w#pCgz6Nl=~5z!V$?wCy_slYs%yN{Gc^29N*%XJ-x7 diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TC b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TC index 056739d39f5f6c79dee1e36ac7ca10b392aaadf0..0db939538c88c84a81d91de85e52549e0c130504 100644 GIT binary patch delta 75 zcmX@Ye3@Bq&07Z6Dh39|Qw%_Yk&%Hr+SJU#GBzfq+On3Bfgebl*J{{X0cqn}P5X&z MN&@I&8v~0O0p|`Bh5!Hn delta 60 zcmcc2e1utT&07Z6Dh39|-3&m2k&%Hb+RVZ-HYTOovX+s714x_e7#h@0)KlVwb2cUv GGXen0w-0Rq diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TR b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TR index 32b88e1b39247be573cd28c32601fa8cdb3183f5..df54d356e09b4ec1cd2ce4524249de87383e197d 100644 GIT binary patch delta 55 zcmbQnx`vf=&07Z6Dh39o#Tz-R809%k4UJ+_s?BQ|8Q6fdp#hM=1r#$jF*P%{u-x3k H$i)Z%mi7&P delta 27 jcmZ3(I*pZc&07Z6Dh39o-i@49jFYn&c{VR)WM>2bb5;kQ diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TT b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TT index ddbdfc6af36a3476d8e5277bef391e6a098a1f9f..233023376138778c245cffeb48dd3daed57a6099 100644 GIT binary patch delta 74 zcmZo-naHBM<}Cwj6$1kk2Ll5GBO@aNSG1{xWo%4JwPh_M13!>9uhp=(0@B8{n)VY_ NLj};qHa6EY0sx3@6MFyv delta 59 zcmbQp(!`>=<}Cwj6$1m~PX-{t$jHDMZDAQ3lTvM2%gDe1q|J2<4QeOqhH}C=8z F0syYn4`BcR diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_US b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_US index 03d97c8fc21b44eaa67847a8a9e8825b95e1c64d..2ee936c9039889d4c143c5849207a69b30a5ef69 100644 GIT binary patch delta 113 zcmZ3*bC73(tSEm>O0{{dhP@S#Hm=pQXJBMxGMuRWn$d8x7^6F*BDy>y1DBD3p^>qP hshPP2Tg>o7Y%aXx3O77-&Wt g=~~9dq*R;LGBR)gX>%PzgW8FD5u9+&#%W=U0Q|2Pg8%>k diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_VG b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_VG index 6d1fbe29a3badb76167f10d220b9f42cee774998..17db04b69ba8e1ad64bc7d12cafd7db65afd995e 100644 GIT binary patch delta 75 zcmaFGe3w~o&07Z6Dh39|D-1w_k&%Hr+Q`(xGBzfq+On3Bfgebl*J{{X0cqn}P5X&X Nt^(*{8z)3D0s#ED6(Ils delta 88 zcmcc1{EFFW&07Z6Dh39|hYUc1k&!`7!`{ls!lcFmNE;Z`n3>g>o7Y%aXx3O77-&Wt g=~~9dq*R;LGBR)gX>%PzgW8FDuAFer#^xwS0PBwzF8}}l diff --git a/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_VI b/java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_VI index fd9447d9080d0090ce736df2bf8da051211d8a5f..2af1716ee2a5bc460e406d7307a693aeef6a5267 100644 GIT binary patch delta 121 zcmZo*nagav<}Cwj6$1m~6b2x{$jHDQZER{`85@&QZCT66zz?L&Yc=exfV6R~rae#z jgVaPpDP>G4Mg}fp69Y37BSRx&6R0vFOl2GYGcp1If#@6! literal 512 zcmcIhJqyAx5WQ3s5uBaeEht>RQ$fVh#nGkh)?Z=Mf8<|tcJ)=oR?tlYxeIrfm-k-I z7aE?3#FY?)pp7=X*sfh}vQkB#OJ5dMT2!@ZBI*H~IXV};k0GQnV@`9m$}m;CM$j19 zFC{lX`yH5|33xf9q|)Y}!1Jd-Ydej7c_YY!IPeL~;9{K*;4_5{pU2SX#*)KM5JKzC n%s#BtIvVq4VfTjpcDWDq@l45a-~K2KSV|=E3aH)}TCkLE_#bLF diff --git a/java/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java b/java/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java index d0485f8d3..8153694d3 100644 --- a/java/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java +++ b/java/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java @@ -453,7 +453,18 @@ public class PhoneNumberMatcherTest extends TestCase { "As quoted by Alfonso 12-15 (2009), you may call me at ", "")); contextPairs.add(new NumberContext( "As quoted by Alfonso et al. 12-15 (2009), you may call me at ", "")); - // with a postfix stripped off as it looks like the start of another number + // With dates, written in the American style. + contextPairs.add(new NumberContext( + "As I said on 03/10/2011, you may call me at ", "")); + contextPairs.add(new NumberContext( + "As I said on 03/27/2011, you may call me at ", "")); + contextPairs.add(new NumberContext( + "As I said on 31/8/2011, you may call me at ", "")); + contextPairs.add(new NumberContext( + "As I said on 1/12/2011, you may call me at ", "")); + contextPairs.add(new NumberContext( + "I was born on 10/12/82. Please call me at ", "")); + // With a postfix stripped off as it looks like the start of another number contextPairs.add(new NumberContext("Call ", "/x12 more")); doTestInContext(number, defaultCountry, contextPairs, Leniency.POSSIBLE); diff --git a/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 432e6c819..82aeb218e 100644 --- a/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -140,7 +140,7 @@ public class PhoneNumberUtilTest extends TestCase { assertEquals("$1 $2 $3", metadata.getNumberFormat(0).getFormat()); assertEquals("[13-9]\\d{9}|2[0-35-9]\\d{8}", metadata.getGeneralDesc().getNationalNumberPattern()); - assertEquals("\\d{7,10}", metadata.getGeneralDesc().getPossibleNumberPattern()); + assertEquals("\\d{7}(?:\\d{3})?", metadata.getGeneralDesc().getPossibleNumberPattern()); assertTrue(metadata.getGeneralDesc().exactlySameAs(metadata.getFixedLine())); assertEquals("\\d{10}", metadata.getTollFree().getPossibleNumberPattern()); assertEquals("900\\d{7}", metadata.getPremiumRate().getNationalNumberPattern()); @@ -913,6 +913,11 @@ public class PhoneNumberUtilTest extends TestCase { assertEquals(PhoneNumberUtil.ValidationResult.TOO_SHORT, phoneUtil.isPossibleNumberWithReason(number)); + number.setCountryCode(65); + number.setNationalNumber(1234567890L); + assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberWithReason(number)); + // Try with number that we don't have metadata for. PhoneNumber adNumber = new PhoneNumber(); adNumber.setCountryCode(376).setNationalNumber(12345L); @@ -1242,9 +1247,9 @@ public class PhoneNumberUtilTest extends TestCase { } number.clear(); try { - String phoneNumber = "(1 610) 619 43"; + String phoneNumber = "(1 610) 619"; StringBuffer numberToFill = new StringBuffer(); - assertEquals("Should not have extracted a country calling code - invalid number both " + + assertEquals("Should not have extracted a country calling code - too short number both " + "before and after extraction of uncertain country calling code.", 0, phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, diff --git a/java/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG b/java/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG index 1dbb03bcdb7846ee0fef0a3ca6c453aadde8c4e2..0838f554cfd0316c5bc07f1d831e9594cdb26149 100644 GIT binary patch delta 55 zcmeBVp1`cP<}Cwj6$1lfI|GnlWMtrvHZ(S~u#AmKsW#U!G^hoN0GSrGH9*qPV552x FBLHAv4Uzx= delta 48 zcmbQh+{vuI<}Cwj6$1lf0|Ss?WMtrvHZ(S~u#AmKsW#U!G^k}{-~ci$HtHoY0ssTY B3hw{_ diff --git a/java/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US b/java/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US index 6aa74142ddb8893de8902e6f5da784d890657d1d..34d2f220fb9f4f07292e5291531baec887da6d26 100644 GIT binary patch delta 78 xcmZo*naaYs<}Cwj6$1kk&qPiY0sfejYV%qRdn+JqT&rn6F-Mmuv5kx37y(@?7Z(5k delta 57 scmbQr(!j#G<}Cwj6$1m~_lcY;oE$MJ)#f^e2DKAyb#ZVuZjWOG0M1 - [289]\d{9} - \d{7,10} + [2589]\d{9} + \d{7}(?:\d{3})? - [289]\d{9} - \d{7,10} + [2589]\d{9} + \d{7}(?:\d{3})? @@ -933,8 +933,8 @@ nationalPrefix="1" internationalPrefix="011"> - [689]\d{9} - \d{7,10} + [5689]\d{9} + \d{7}(?:\d{3})? @@ -1474,8 +1474,8 @@ nationalPrefix="1" internationalPrefix="011"> - [289]\d{9} - \d{7,10} + [2589]\d{9} + \d{7}(?:\d{3})? 246[2-9]\d{6} @@ -2182,8 +2182,8 @@ nationalPrefix="1" internationalPrefix="011"> - [489]\d{9} - \d{7,10} + [4589]\d{9} + \d{7}(?:\d{3})? @@ -2411,8 +2411,8 @@ nationalPrefix="1" internationalPrefix="011"> - [289]\d{9} - \d{7,10} + [2589]\d{9} + \d{7}(?:\d{3})? @@ -2773,7 +2773,7 @@ [2-9]\d{9}| 3\d{6} - \d{7,10} + \d{7}(?:\d{3})? @@ -2781,8 +2781,8 @@ 2(?: 04| 26| - 50| - 89 + [48]9| + 50 )| 3(?: 06| @@ -2826,8 +2826,8 @@ 2(?: 04| 26| - 50| - 89 + [48]9| + 50 )| 3(?: 06| @@ -3102,8 +3102,36 @@ - + + + + + $1 $2 + + + + [2-57]\d{4} + \d{5} + + + + (?: + 2\d| + 3[13-7]| + 4[1-5] + )\d{3} + + 21234 + + + + (?: + 5[0-68]| + 7\d + )\d{3} + + 71234 + @@ -4413,8 +4441,8 @@ nationalPrefix="1" internationalPrefix="011"> - [7-9]\d{9} - \d{7,10} + [57-9]\d{9} + \d{7}(?:\d{3})? @@ -4484,8 +4512,8 @@ nationalPrefix="1" internationalPrefix="011"> - [89]\d{9} - \d{7,10} + [589]\d{9} + \d{7}(?:\d{3})? - - 91(?: - 1(?: - [146]\d| - 2[0-5]| - 3[4-6]| - 50| - 7[2-6]| - 8[46-9] - )| - 31\d| - 4(?: - 3[0-2489]| - 7[0-3] - )| - 5(?: - 3[23]| - 7[3-5] - )| - 6(?: - 58| - 8[23] - )| - 7(?: - 5[57]| - 8[01] - )| - 8(?: - 3[45]| - 7[67] - ) - )\d{4} - + + 91[0-8]\d{6} \d{9} - 911123456 + 911234567 @@ -5240,8 +5237,29 @@ - + + + + + [2-7]\d{4} + \d{5} + + + [2-47]\d{4} + 31234 + + + [56]\d{4} + 51234 + + + + + 1\d{2}| + 999 + + 123 + @@ -5254,9 +5272,11 @@ nationalPrefixForParsing="(10(?:01|[12]0|88))" carrierCodeFormattingRule="$CC $FG"> - - $1 - + + + $1 + + [2-9]\d{5} \d{6} @@ -6075,8 +6095,8 @@ nationalPrefix="1" internationalPrefix="011"> - [489]\d{9} - \d{7,10} + [4589]\d{9} + \d{7}(?:\d{3})? @@ -6155,28 +6175,15 @@ - + - - 32 - $1 $2 $3 $4 - - - - [24]| - 3[13-79] - - $1 $2 $3 $4 - + the communications commission uses 2 3 3. Wikipedia says 3 2 3. Some use 2 6. --> - [5679] + [13-79] $1 $2 $3 $4 @@ -6186,30 +6193,14 @@ - [1-579]\d{7}| + [13-79]\d{7}| 8\d{8} \d{5,9} - (?: - 122| - 2(?: - 22| - 36| - 5[035] - )| - 3(?: - 1[0-35-8]| - 3[1-35679]| - 4\d| - 7[0-39]| - 9[1-35-7] - )| 3(?: [256]\d| 4[124-9]| @@ -6231,10 +6222,11 @@ (?: - 5[1578]| + 14| + 5[01578]| 6[28]| 7[0147-9]| - 9[0135-9] + 9[0-35-9] )\d{6} \d{8} @@ -6862,50 +6854,168 @@ + + + + [2-7] + $1 $2 + + + 1 + $1 $2 $3 + + + + + [2-7]\d{7}| + 1[89]\d{9} + + \d{8}(?:\d{3})? + + + [267][2-9]\d{6} + \d{8} + 22456789 + + + + [345]\d{7} + \d{8} + 51234567 + + + 18[01]\d{8} + \d{11} + 18001112222 + + + 19\d{9} + \d{11} + 19001112222 + + + + 1(?: + 2| + [57]\d + )\d + + \d{3,4} + 123 + - + - [689]\d{9} - \d{7,10} + [5689]\d{9} + \d{7}(?:\d{3})? + 671(?: - 3\d{2}| - 47\d| - 56\d| - 6[3-5]\d| + 3(?: + 00| + 3[39]| + 4[349]| + 55| + 6[26] + )| + 4(?: + 56| + 7[1-9]| + 8[23678] + )| + 5(?: + 55| + 6[2-5]| + 88 + )| + 6(?: + 3[2-578]| + 4[24-9]| + 5[34]| + 78| + 8[5-9] + )| 7(?: - 3\d| - 89 + [079]7| + 2[0167]| + 3[45]| + 8[789] + )| + 8(?: + [2-5789]8| + 6[48] )| - 828 + 9(?: + 2[29]| + 6[79]| + 7[179]| + 8[789]| + 9[78] + ) )\d{4} - 6713123456 + 6713001234 671(?: - 3\d{2}| - 47\d| - 56\d| - 6[3-5]\d| + 3(?: + 00| + 3[39]| + 4[349]| + 55| + 6[26] + )| + 4(?: + 56| + 7[1-9]| + 8[23678] + )| + 5(?: + 55| + 6[2-5]| + 88 + )| + 6(?: + 3[2-578]| + 4[24-9]| + 5[34]| + 78| + 8[5-9] + )| 7(?: - 3\d| - 89 + [079]7| + 2[0167]| + 3[45]| + 8[789] )| - 828 + 8(?: + [2-5789]8| + 6[48] + )| + 9(?: + 2[29]| + 6[79]| + 7[179]| + 8[789]| + 9[78] + ) )\d{4} - 6713123456 + 6713001234 @@ -8078,15 +8188,24 @@ $1 $2 $3 + pattern="(1800)(\d{4,5})"> + 180 + 1800 + $1 $2 + + 18[06] 18[06]0 $1 $2 $3 - [1-9]\d{9,10} - \d{6,11} + + 1\d{7,11}| + [2-9]\d{9,10} + + \d{6,12} 1(?: - 800\d? | - 600 - )\d{6} + 600\d{6}| + 800\d{4,8} + ) - \d{10,11} + \d{8,12} 1800123456 + + + + $1 $2 + + + + 3\d{6} + \d{7} + + + 37\d{5} + 3709100 + + + 38\d{5} + 3801234 + @@ -8971,8 +9108,8 @@ nationalPrefix="1" internationalPrefix="011"> - [89]\d{9} - \d{7,10} + [589]\d{9} + \d{7}(?:\d{3})? @@ -10241,8 +10378,43 @@ + + nationalPrefix="0" nationalPrefixFormattingRule="$NP$FG"> + + + $1 + + + + [2-689]\d{4} + \d{5} + + + + (?: + [234]\d| + 50| + 8[1-5] + )\d{3} + + 31234 + + + [69]\d{4} + 61234 + + + + 10(?: + [0-8]| + 5[01259] + )| + 99[234] + + \d{3,4} + 992 + @@ -10280,8 +10452,8 @@ nationalPrefix="1" internationalPrefix="011"> - [89]\d{9} - \d{7,10} + [589]\d{9} + \d{7}(?:\d{3})? @@ -10668,8 +10840,8 @@ nationalPrefix="1" internationalPrefix="011"> - [389]\d{9} - \d{7,10} + [3589]\d{9} + \d{7}(?:\d{3})? @@ -11026,8 +11198,8 @@ nationalPrefix="1" internationalPrefix="011"> - [789]\d{9} - \d{7,10} + [5789]\d{9} + \d{7}(?:\d{3})? @@ -12393,7 +12565,7 @@ [12]\d{7,9}| [57-9]\d{7} - \d{8,10} + \d{6,10} - [689]\d{9} - \d{7,10} + [5689]\d{9} + \d{7}(?:\d{3})? @@ -12559,61 +12731,47 @@ - - [2-48] $1 $2 $3 $4 - - [2-7] - $1 $2 $3 - - - [2-4]\d{6,7}| - [5-7]\d{6}| - 8\d{7} - - \d{7,8} + [2-48]\d{7} + \d{8} - 5(?: - 1[035]| - 2[0-69]| - 3[0348]| - 4[468]| - 5[02-467]| - 6[39]| - 7[4-69] - )\d{4}| - [2-4]5\d{6} + 25[08]\d{5}| + 35\d{6}| + 45[1-7]\d{5} 35123456 (?: - [23][0-4]| - 4[3-5]| - 6\d| - 7[0-7] - )\d{5}| - (?: - 2[27]| - 3[367]| - 4[467] - )\d{6} + 2(?: + 2\d| + 70 + )| + 3(?: + 3\d| + 6[1-36]| + 7[1-3] + )| + 4(?: + 4\d| + 6[0457-9]| + 7[4-9] + ) + )\d{5} 22123456 800\d{5} - \d{8} 80012345 @@ -12624,8 +12782,8 @@ nationalPrefix="1" internationalPrefix="011"> - [689]\d{9} - \d{7,10} + [5689]\d{9} + \d{7}(?:\d{3})? 664491\d{4} @@ -13744,9 +13902,10 @@ - - + + + [489] @@ -13765,25 +13924,17 @@ \d{5}(?:\d{3})? - + - 0\d{4}| (?: 2[1-4]| 3[1-3578]| 5[1-35-7]| 6[1-4679]| - 7\d - )\d{6}| - 81(?: - 0(?: - 0[7-9]| - 1\d - )| - 5\d{2} - )\d{3} + 7[0-8] + )\d{6} + \d{8} 21234567 @@ -13821,6 +13972,20 @@ \d{8} 88012345 + + + + 0\d{4}| + 81(?: + 0(?: + 0[7-9]| + 1\d + )| + 5\d{2} + )\d{3} + + 01234 + @@ -14736,8 +14901,8 @@ nationalPrefix="1" internationalPrefix="011"> - [789]\d{9} - \d{7,10} + [5789]\d{9} + \d{7}(?:\d{3})? @@ -15964,6 +16129,73 @@ + + + + + + + 0\d{4}| + [4789]\d{7} + + \d{5}(?:\d{3})? + + + 79\d{6} + \d{8} + 79123456 + + + + + (?: + 4[015-8]| + 9\d + )\d{6} + + \d{8} + 41234567 + + + 80[01]\d{5} + \d{8} + 80012345 + + + 82[09]\d{5} + \d{8} + 82012345 + + + + 810(?: + 0[0-6]| + [2-8]\d + )\d{3} + + \d{8} + 81021234 + + + 880\d{5} + \d{8} + 88012345 + + + + 0\d{4}| + 81(?: + 0(?: + 0[7-9]| + 1\d + )| + 5\d{2} + )\d{3} + + 01234 + + + + + + + [27] + $1 $2 + + + [89] + $1 $2 + + + [89] + $1 $2 $3 + + + + + [27]\d{7}| + [89]\d{6}(?:\d{4})? + + + \d{7,8}| + \d{11} + + + + + 2[1-6]\d{6} + \d{8} + 21234567 + + + 7\d{7} + \d{8} + 70123456 + + + + 800\d{4}(?:\d{4})? + \d{7}(?:\d{4})? + 8001234 + + + + 900\d{4}(?:\d{4})? + \d{7}(?:\d{4})? + 9001234 + @@ -16417,8 +16699,8 @@ nationalPrefix="1" internationalPrefix="011"> - [689]\d{9} - \d{7,10} + [5689]\d{9} + \d{7}(?:\d{3})? @@ -16910,11 +17192,11 @@ \d{10} 5012345678 - + 512\d{7} \d{10} 5123456789 - + 800\d{7} \d{10} @@ -16942,8 +17224,8 @@ nationalPrefix="1" internationalPrefix="011"> - [89]\d{9} - \d{7,10} + [589]\d{9} + \d{7}(?:\d{3})? @@ -17418,7 +17700,7 @@ [2-9]\d{9} - \d{7,10} + \d{7}(?:\d{3})? @@ -17459,7 +17741,7 @@ 0[1-57-9]| 1[0235-8]| 20| - 3[04]| + 3[014]| 4[01]| 5[19]| 6[1-37]| @@ -17551,7 +17833,7 @@ 0[1-57-9]| 1[0235-8]| 20| - 3[04]| + 3[014]| 4[01]| 5[19]| 6[1-37]| @@ -17707,19 +17989,8 @@ nationalPrefix="1" internationalPrefix="011"> - - (?: - 784| - 8(?: - 00| - 66| - 77| - 88 - )| - 900 - )[2-9]\d{6} - - \d{7,10} + [5789]\d{9} + \d{7}(?:\d{3})? @@ -17857,19 +18128,8 @@ nationalPrefix="1" internationalPrefix="011"> - - (?: - 284| - 8(?: - 00| - 66| - 77| - 88 - )| - 900 - )[2-9]\d{6} - - \d{7,10} + [2589]\d{9} + \d{7}(?:\d{3})? @@ -17946,27 +18206,25 @@ nationalPrefix="1" internationalPrefix="011"> + [3589]\d{9} + \d{7}(?:\d{3})? + + 340(?: 6[49]2| 7[17]\d - )\d{4}| - (?: - 8(?: - 00| - 66| - 77| - 88 - )| - 900 - )[2-9]\d{6} + )\d{4} - \d{7,10} - - 3406421234 + + 340(?: + 6[49]2| + 7[17]\d + )\d{4} + 3406421234 diff --git a/resources/PhoneNumberMetaDataForTesting.xml b/resources/PhoneNumberMetaDataForTesting.xml index 81969118f..0293daeac 100644 --- a/resources/PhoneNumberMetaDataForTesting.xml +++ b/resources/PhoneNumberMetaDataForTesting.xml @@ -670,7 +670,11 @@ [13689]\d{7,10} - \d{8,11} + + + \d{8}| + \d{10,11} + [36]\d{7} @@ -710,7 +714,7 @@ [13-9]\d{9}|2[0-35-9]\d{8} - \d{7,10} + \d{7}(?:\d{3})? 1234567890