Commits

Anonymous committed 078ba4a

Fix a bug where NANPA numbers starting with 1 were incorrectly formatted.

Comments (0)

Files changed (2)

java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java

 
   private void removeNationalPrefixFromNationalNumber() {
     int startOfNationalNumber = 0;
-    if (currentMetaData.hasNationalPrefix()) {
+    if (currentMetaData.getCountryCode() == 1 && nationalNumber.charAt(0) == '1') {
+      startOfNationalNumber = 1;
+      prefixBeforeNationalNumber.append("1 ");
+    } else if (currentMetaData.hasNationalPrefix()) {
       Matcher m = nationalPrefixForParsing.matcher(nationalNumber);
       if (m.lookingAt()) {
         startOfNationalNumber = m.end();

java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java

     assertEquals("650 253 2222", formatter.inputDigit('2'));
 
     formatter.clear();
+    assertEquals("1", formatter.inputDigit('1'));
+    assertEquals("16", formatter.inputDigit('6'));
+    assertEquals("165", formatter.inputDigit('5'));
+    assertEquals("1650", formatter.inputDigit('0'));
+    assertEquals("16502", formatter.inputDigit('2'));
+    assertEquals("1 650 25", formatter.inputDigit('5'));
+    assertEquals("1 650 253", formatter.inputDigit('3'));
+    assertEquals("1 650 253 2", formatter.inputDigit('2'));
+    assertEquals("1 650 253 22", formatter.inputDigit('2'));
+    assertEquals("1 650 253 222", formatter.inputDigit('2'));
+    assertEquals("1 650 253 2222", formatter.inputDigit('2'));    
+    
+    formatter.clear();
     assertEquals("6", formatter.inputDigit('6'));
     assertEquals("65", formatter.inputDigit('5'));
     assertEquals("650", formatter.inputDigit('0'));
     assertEquals("0301", formatter.inputDigit('1'));
     assertEquals("03012", formatter.inputDigit('2'));
     assertEquals("030 123", formatter.inputDigit('3'));
-    assertEquals("030 1234", formatter.inputDigit('4'));    
+    assertEquals("030 1234", formatter.inputDigit('4'));
   }
 
   public void testAsYouTypeFormatterAR() {