Fix bug in AsYouTypeFormatter where we hit a IndexOutOfBoundsException in its Java...
authorAndy Staudacher <staudacher@google.com>
Wed, 18 Feb 2015 10:04:10 +0000 (11:04 +0100)
committerYoungjae Shin <yj99.shin@samsung.com>
Tue, 9 Jun 2015 11:43:24 +0000 (20:43 +0900)
Includes corresponding changes for C++ and JavaScript as well to keep the implementations in sync. (C++ and JS didn't exhibit buggy behavior because the corresponding substring methods don't throw errors for invalid start positions.)

Bugfix for https://github.com/googlei18n/libphonenumber/issues/592

cpp/src/phonenumbers/asyoutypeformatter.cc
java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java
javascript/i18n/phonenumbers/asyoutypeformatter.js

index 97770db..422d568 100644 (file)
@@ -466,6 +466,9 @@ void AsYouTypeFormatter::AttemptToChoosePatternWithPrefixExtracted(
   able_to_format_ = true;
   is_expecting_country_code_ = false;
   possible_formats_.clear();
+  last_match_position_ = 0;
+  formatting_template_.remove();
+  current_formatting_pattern_.clear();
   AttemptToChooseFormattingPattern(formatted_number);
 }
 
index ad63a40..456c8ed 100644 (file)
@@ -390,6 +390,9 @@ public class AsYouTypeFormatter {
     ableToFormat = true;
     isExpectingCountryCallingCode = false;
     possibleFormats.clear();
+    lastMatchPosition = 0;
+    formattingTemplate.setLength(0);
+    currentFormattingPattern = "";
     return attemptToChooseFormattingPattern();
   }
 
@@ -637,6 +640,8 @@ public class AsYouTypeFormatter {
   }
 
   private String inputDigitHelper(char nextChar) {
+    // Note that formattingTemplate is not guaranteed to have a value, it could be empty, e.g.
+    // when the next digit is entered after extracting an IDD or NDD.
     Matcher digitMatcher = DIGIT_PATTERN.matcher(formattingTemplate);
     if (digitMatcher.find(lastMatchPosition)) {
       String tempTemplate = digitMatcher.replaceFirst(Character.toString(nextChar));
index ccd109c..d4d4dd2 100644 (file)
@@ -667,6 +667,9 @@ i18n.phonenumbers.AsYouTypeFormatter.prototype.
   this.ableToFormat_ = true;
   this.isExpectingCountryCallingCode_ = false;
   this.possibleFormats_ = [];
+  this.lastMatchPosition_ = 0;
+  this.formattingTemplate_.clear();
+  this.currentFormattingPattern_ = '';
   return this.attemptToChooseFormattingPattern_();
 };