From a536ce746e5971b5613dded990b6a5a98d912913 Mon Sep 17 00:00:00 2001 From: Shaopeng Jia Date: Wed, 19 May 2010 22:28:24 +0000 Subject: [PATCH] Modify AsYouTypeFormatter to do no formatting for vanity numbers. Also improve the formatter to handle numbers with variable lengths in subscriber number. --- .../i18n/phonenumbers/AsYouTypeFormatter.java | 13 +++++---- .../phonenumbers/AsYouTypeFormatterTest.java | 27 ++++++++++++++----- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java b/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java index 30fe98efd..37e4262a9 100644 --- a/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java +++ b/java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java @@ -58,6 +58,7 @@ public class AsYouTypeFormatter { private Pattern internationalPrefix; private StringBuffer prefixBeforeNationalNumber; private StringBuffer nationalNumber; + private static Pattern UNSUPPORTED_SYNTAX_PATTERN = Pattern.compile("\\|"); /** * Constructs a light-weight formatter which does no formatting, but outputs exactly what is @@ -123,9 +124,9 @@ public class AsYouTypeFormatter { String numberFormat = format.getFormat(); String numberPattern = format.getPattern(); - // The formatter doesn't format numbers when numberPattern contains "|" or ",", e.g. + // The formatter doesn't format numbers when numberPattern contains "|", e.g. // (20|3)\d{4,5}. In those cases we quickly return. - Matcher unsupportedSyntax = Pattern.compile("\\||,").matcher(numberPattern); + Matcher unsupportedSyntax = UNSUPPORTED_SYNTAX_PATTERN.matcher(numberPattern); if (unsupportedSyntax.find()) { return false; } @@ -134,7 +135,7 @@ public class AsYouTypeFormatter { numberPattern = numberPattern.replaceAll("\\[([^\\[\\]])*\\]","\\\\d"); // Replace any standalone digit (not the one in d{}) with \d - numberPattern = numberPattern.replaceAll("\\d(?=[^}])", "\\\\d"); + numberPattern = numberPattern.replaceAll("\\d(?=[^,}])", "\\\\d"); formattingTemplate = getFormattingTemplate(numberPattern, numberFormat); return true; @@ -179,14 +180,12 @@ public class AsYouTypeFormatter { * @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. - * @return the partially formatted phone number, with the remaining digits each denoted by - * \u2008. Clients could display the result as it is, as \u2008 will be displayed as a normal - * white space. + * @return the partially formatted phone number. */ public String inputDigit(char nextChar) { accruedInput.append(nextChar); // * and # are normally used in mobile codes, which we do not format. - if (nextChar == '*' || nextChar == '#') { + if (nextChar == '*' || nextChar == '#' || Character.isLetter(nextChar)) { ableToFormat = false; } if (!ableToFormat) { diff --git a/java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java b/java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java index 3683f0161..01e14d869 100644 --- a/java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java +++ b/java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java @@ -177,6 +177,21 @@ public class AsYouTypeFormatterTest extends TestCase { assertEquals("*12", formatter.inputDigit('2')); assertEquals("*121", formatter.inputDigit('1')); assertEquals("*121#", formatter.inputDigit('#')); + + // Test vanity numbers. + formatter.clear(); + assertEquals("8", formatter.inputDigit('8')); + assertEquals("80", formatter.inputDigit('0')); + assertEquals("800", formatter.inputDigit('0')); + assertEquals("800 ", formatter.inputDigit(' ')); + assertEquals("800 M", formatter.inputDigit('M')); + assertEquals("800 MY", formatter.inputDigit('Y')); + assertEquals("800 MY ", formatter.inputDigit(' ')); + assertEquals("800 MY A", formatter.inputDigit('A')); + assertEquals("800 MY AP", formatter.inputDigit('P')); + assertEquals("800 MY APP", formatter.inputDigit('P')); + assertEquals("800 MY APPL", formatter.inputDigit('L')); + assertEquals("800 MY APPLE", formatter.inputDigit('E')); } public void testAsYouTypeFormatterGBFixedLine() { @@ -231,10 +246,10 @@ public class AsYouTypeFormatterTest extends TestCase { assertEquals("021", formatter.inputDigit('1')); assertEquals("0211", formatter.inputDigit('1')); assertEquals("02112", formatter.inputDigit('2')); - assertEquals("021123", formatter.inputDigit('3')); - assertEquals("0211234", formatter.inputDigit('4')); - assertEquals("02112345", formatter.inputDigit('5')); - assertEquals("021123456", formatter.inputDigit('6')); + assertEquals("02-112 3", formatter.inputDigit('3')); + assertEquals("02-112 34", formatter.inputDigit('4')); + assertEquals("02-112 345", formatter.inputDigit('5')); + assertEquals("02-112 3456", formatter.inputDigit('6')); } public void testAsYouTypeFormatterDE() { @@ -244,8 +259,8 @@ public class AsYouTypeFormatterTest extends TestCase { assertEquals("030", formatter.inputDigit('0')); assertEquals("0301", formatter.inputDigit('1')); assertEquals("03012", formatter.inputDigit('2')); - assertEquals("030123", formatter.inputDigit('3')); - assertEquals("0301234", formatter.inputDigit('4')); + assertEquals("030 123", formatter.inputDigit('3')); + assertEquals("030 1234", formatter.inputDigit('4')); } public void testAsYouTypeFormatterAR() {