Browse Source

Modify AsYouTypeFormatter to do no formatting for vanity numbers. Also improve the formatter to handle numbers with variable lengths in subscriber number.

pull/567/head
Shaopeng Jia 16 years ago
committed by Mihaela Rosca
parent
commit
a536ce746e
2 changed files with 27 additions and 13 deletions
  1. +6
    -7
      java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java
  2. +21
    -6
      java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java

+ 6
- 7
java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java View File

@ -58,6 +58,7 @@ public class AsYouTypeFormatter {
private Pattern internationalPrefix; private Pattern internationalPrefix;
private StringBuffer prefixBeforeNationalNumber; private StringBuffer prefixBeforeNationalNumber;
private StringBuffer nationalNumber; 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 * 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 numberFormat = format.getFormat();
String numberPattern = format.getPattern(); 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. // (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()) { if (unsupportedSyntax.find()) {
return false; return false;
} }
@ -134,7 +135,7 @@ public class AsYouTypeFormatter {
numberPattern = numberPattern.replaceAll("\\[([^\\[\\]])*\\]","\\\\d"); numberPattern = numberPattern.replaceAll("\\[([^\\[\\]])*\\]","\\\\d");
// Replace any standalone digit (not the one in d{}) with \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); formattingTemplate = getFormattingTemplate(numberPattern, numberFormat);
return true; return true;
@ -179,14 +180,12 @@ public class AsYouTypeFormatter {
* @param nextChar the most recently entered digit of a phone number. Formatting characters are * @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 * allowed, but they are removed from the result. Full width digits and Arabic-indic digits
* are allowed, and will be shown as they are. * 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) { public String inputDigit(char nextChar) {
accruedInput.append(nextChar); accruedInput.append(nextChar);
// * and # are normally used in mobile codes, which we do not format. // * and # are normally used in mobile codes, which we do not format.
if (nextChar == '*' || nextChar == '#') {
if (nextChar == '*' || nextChar == '#' || Character.isLetter(nextChar)) {
ableToFormat = false; ableToFormat = false;
} }
if (!ableToFormat) { if (!ableToFormat) {


+ 21
- 6
java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java View File

@ -177,6 +177,21 @@ public class AsYouTypeFormatterTest extends TestCase {
assertEquals("*12", formatter.inputDigit('2')); assertEquals("*12", formatter.inputDigit('2'));
assertEquals("*121", formatter.inputDigit('1')); assertEquals("*121", formatter.inputDigit('1'));
assertEquals("*121#", formatter.inputDigit('#')); 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() { public void testAsYouTypeFormatterGBFixedLine() {
@ -231,10 +246,10 @@ public class AsYouTypeFormatterTest extends TestCase {
assertEquals("021", formatter.inputDigit('1')); assertEquals("021", formatter.inputDigit('1'));
assertEquals("0211", formatter.inputDigit('1')); assertEquals("0211", formatter.inputDigit('1'));
assertEquals("02112", formatter.inputDigit('2')); 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() { public void testAsYouTypeFormatterDE() {
@ -244,8 +259,8 @@ public class AsYouTypeFormatterTest extends TestCase {
assertEquals("030", formatter.inputDigit('0')); assertEquals("030", formatter.inputDigit('0'));
assertEquals("0301", formatter.inputDigit('1')); assertEquals("0301", formatter.inputDigit('1'));
assertEquals("03012", formatter.inputDigit('2')); 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() { public void testAsYouTypeFormatterAR() {


Loading…
Cancel
Save