Fix formatting of phone number contains all zeros.
Bug: 5149719
Change-Id: I3f357755a7b5163c28e55e1fd73623e6dbe9aef2
diff --git a/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
index d6eae62..1163b42 100644
--- a/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
+++ b/java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
@@ -978,6 +978,12 @@
* @return the formatted phone number
*/
public String format(PhoneNumber number, PhoneNumberFormat numberFormat) {
+ if (number.getNationalNumber() == 0 && number.hasRawInput()) {
+ String rawInput = number.getRawInput();
+ if (rawInput.length() > 0) {
+ return rawInput;
+ }
+ }
StringBuilder formattedNumber = new StringBuilder(20);
format(number, numberFormat, formattedNumber);
return formattedNumber.toString();
diff --git a/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
index aa0537a..c23a614 100644
--- a/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
+++ b/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
@@ -98,6 +98,11 @@
new PhoneNumber().setCountryCode(1).setNationalNumber(650253000L);
private static final PhoneNumber US_TOLLFREE =
new PhoneNumber().setCountryCode(1).setNationalNumber(8002530000L);
+ private static final PhoneNumber US_SPOOF =
+ new PhoneNumber().setCountryCode(1).setNationalNumber(0L);
+ private static final PhoneNumber US_SPOOF_WITH_RAW_INPUT =
+ new PhoneNumber().setCountryCode(1).setNationalNumber(0L)
+ .setRawInput("000-000-0000");
// Class containing string constants of region codes for easier testing.
private static class RegionCode {
@@ -345,6 +350,11 @@
assertEquals("900 253 0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.NATIONAL));
assertEquals("+1 900 253 0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.INTERNATIONAL));
assertEquals("+1-900-253-0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.RFC3966));
+ // Numbers with all zeros in the national number part will be formatted by using the raw_input
+ // if that is available no matter which format is specified.
+ assertEquals("000-000-0000",
+ phoneUtil.format(US_SPOOF_WITH_RAW_INPUT, PhoneNumberFormat.NATIONAL));
+ assertEquals("0", phoneUtil.format(US_SPOOF, PhoneNumberFormat.NATIONAL));
}
public void testFormatBSNumber() {