From 965754b3f8eae3bf4e9a7c73bf13b2db526101af Mon Sep 17 00:00:00 2001 From: Andy Staudacher Date: Wed, 18 Feb 2015 11:04:10 +0100 Subject: [PATCH] Fix bug in AsYouTypeFormatter where we hit a IndexOutOfBoundsException in its Java implementation when extracting the Chinese national prefix 17951 and didn't reset the format template / lastMatchPosition. 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 | 3 +++ .../src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java | 5 +++++ javascript/i18n/phonenumbers/asyoutypeformatter.js | 3 +++ 3 files changed, 11 insertions(+) diff --git a/cpp/src/phonenumbers/asyoutypeformatter.cc b/cpp/src/phonenumbers/asyoutypeformatter.cc index 97770db..422d568 100644 --- a/cpp/src/phonenumbers/asyoutypeformatter.cc +++ b/cpp/src/phonenumbers/asyoutypeformatter.cc @@ -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); } diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java index ad63a40..456c8ed 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java @@ -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)); diff --git a/javascript/i18n/phonenumbers/asyoutypeformatter.js b/javascript/i18n/phonenumbers/asyoutypeformatter.js index ccd109c..d4d4dd2 100644 --- a/javascript/i18n/phonenumbers/asyoutypeformatter.js +++ b/javascript/i18n/phonenumbers/asyoutypeformatter.js @@ -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_(); }; -- 2.7.4