Commits

jia....@ee073f10-1060-11df-b6a4-87a95322a99c  committed dd10a3f

Fix another bug in AsYouTypeFormatter where the rememberedPosition is incorrect when a number is entered with more digits than correct.

  • Participants
  • Parent commits 52d0100
  • Branches default

Comments (0)

Files changed (2)

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

   private String inputAccruedNationalNumber() {
     int lengthOfNationalNumber = nationalNumber.length();
     if (lengthOfNationalNumber > 0) {
+      // The positionRemembered should be only adjusted once in the loop that follows.
+      Boolean positionAlreadyAdjusted = false;
       for (int i = 0; i < lengthOfNationalNumber - 1; i++) {
         String temp = inputDigitHelper(nationalNumber.charAt(i));
-        if (positionRemembered == i + 1) {
-          positionRemembered = temp.length();
+        if (!positionAlreadyAdjusted &&
+            positionRemembered - prefixBeforeNationalNumber.length() == i + 1) {
+          positionRemembered = prefixBeforeNationalNumber.length() + temp.length();
+          positionAlreadyAdjusted = true;
         }
       }
       String temp = inputDigitHelper(nationalNumber.charAt(lengthOfNationalNumber - 1));
     int startOfNationalNumber = 0;
     if (currentMetaData.getCountryCode() == 1 && nationalNumber.charAt(0) == '1') {
       startOfNationalNumber = 1;
-      prefixBeforeNationalNumber.append("1 ");
+      prefixBeforeNationalNumber.append("1");
+      // Since a space will be inserted after the national prefix in this case, we increase the
+      // remembered position by 1 for anything that is after the national prefix.
+      if (positionRemembered > prefixBeforeNationalNumber.length()) {
+        positionRemembered++;
+      }
+      prefixBeforeNationalNumber.append(" ");
     } else if (currentMetaData.hasNationalPrefix()) {
       Matcher m = nationalPrefixForParsing.matcher(nationalNumber);
       if (m.lookingAt()) {

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

     assertEquals(10, formatter.getRememberedPosition());
 
     formatter.clear();
+    assertEquals("1", formatter.inputDigit('1'));
+    assertEquals("16", formatter.inputDigit('6'));
+    assertEquals("165", formatter.inputDigit('5'));
+    assertEquals("1650", formatter.inputDigitAndRememberPosition('0'));
+    assertEquals(4, formatter.getRememberedPosition());
+    assertEquals("16502", formatter.inputDigit('2'));
+    assertEquals("1 650 25", formatter.inputDigit('5'));
+    assertEquals(5, formatter.getRememberedPosition());
+    assertEquals("1 650 253", formatter.inputDigit('3'));
+    assertEquals("1 650 253 2", formatter.inputDigit('2'));
+    assertEquals("1 650 253 22", formatter.inputDigit('2'));
+    assertEquals(5, formatter.getRememberedPosition());
+    assertEquals("1 650 253 222", formatter.inputDigit('2'));
+    assertEquals("1 650 253 2222", formatter.inputDigit('2'));
+    assertEquals("165025322222", formatter.inputDigit('2'));
+    assertEquals(4, formatter.getRememberedPosition());
+    assertEquals("1650253222222", formatter.inputDigit('2'));
+    assertEquals(4, formatter.getRememberedPosition());    
+
+    formatter.clear();
+    assertEquals("1", formatter.inputDigit('1'));
+    assertEquals("16", formatter.inputDigit('6'));
+    assertEquals("165", formatter.inputDigitAndRememberPosition('5'));
+    assertEquals("1650", formatter.inputDigit('0'));
+    assertEquals(3, formatter.getRememberedPosition());
+    assertEquals("16502", formatter.inputDigit('2'));
+    assertEquals("1 650 25", formatter.inputDigit('5'));
+    assertEquals(4, formatter.getRememberedPosition());
+    assertEquals("1 650 253", formatter.inputDigit('3'));
+    assertEquals("1 650 253 2", formatter.inputDigit('2'));
+    assertEquals("1 650 253 22", formatter.inputDigit('2'));
+    assertEquals(4, formatter.getRememberedPosition());
+    assertEquals("1 650 253 222", formatter.inputDigit('2'));
+    assertEquals("1 650 253 2222", formatter.inputDigit('2'));
+    assertEquals("165025322222", formatter.inputDigit('2'));
+    assertEquals(3, formatter.getRememberedPosition());
+    assertEquals("1650253222222", formatter.inputDigit('2'));
+    assertEquals(3, formatter.getRememberedPosition());
+
+    formatter.clear();
     assertEquals("6", formatter.inputDigit('6'));
     assertEquals("65", formatter.inputDigit('5'));
     assertEquals("650", formatter.inputDigit('0'));