Skip to content

Commit bf08a3c

Browse files
Strip extension before appending formatted extension. (#3724)
* Strip extension before appending formatted extension. * Add pending code change --------- Co-authored-by: Tijana Vislavski Gradina <[email protected]>
1 parent 36db917 commit bf08a3c

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

cpp/src/phonenumbers/phonenumberutil.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,17 @@ void PhoneNumberUtil::FormatOutOfCountryKeepingAlphaChars(
17101710
PrefixNumberWithCountryCallingCode(country_code, INTERNATIONAL,
17111711
formatted_number);
17121712
}
1713+
std::string region_code;
1714+
GetRegionCodeForCountryCode(country_code, &region_code);
1715+
// Metadata cannot be null because the country code is valid.
1716+
const PhoneMetadata* metadata_for_region =
1717+
GetMetadataForRegionOrCallingCode(country_code, region_code);
1718+
// Strip any extension
1719+
std::string extension;
1720+
MaybeStripExtension(formatted_number, &extension);
1721+
// Append the formatted extension
1722+
MaybeAppendFormattedExtension(number, *metadata_for_region, INTERNATIONAL,
1723+
formatted_number);
17131724
}
17141725

17151726
const NumberFormat* PhoneNumberUtil::ChooseFormattingPatternForNumber(

cpp/test/phonenumbers/phonenumberutil_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,16 @@ TEST_F(PhoneNumberUtilTest, FormatOutOfCountryKeepingAlphaChars) {
935935
&formatted_number);
936936
EXPECT_EQ("1 800 SIX-FLAG", formatted_number);
937937

938+
// Testing a number with extension.
939+
formatted_number.clear();
940+
PhoneNumber alpha_numeric_number_with_extn;
941+
phone_util_.ParseAndKeepRawInput("800 SIX-flag ext. 1234", RegionCode::US(),
942+
&alpha_numeric_number_with_extn);
943+
// preferredExtnPrefix for US is " extn. " (in test metadata)
944+
phone_util_.FormatOutOfCountryKeepingAlphaChars(
945+
alpha_numeric_number_with_extn, RegionCode::AU(), &formatted_number);
946+
EXPECT_EQ("0011 1 800 SIX-FLAG extn. 1234", formatted_number);
947+
938948
// Testing that if the raw input doesn't exist, it is formatted using
939949
// FormatOutOfCountryCallingNumber.
940950
alpha_numeric_number.clear_raw_input();

java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,8 +1878,11 @@ public String formatOutOfCountryKeepingAlphaChars(PhoneNumber number,
18781878
String regionCode = getRegionCodeForCountryCode(countryCode);
18791879
// Metadata cannot be null because the country calling code is valid.
18801880
PhoneMetadata metadataForRegion = getMetadataForRegionOrCallingCode(countryCode, regionCode);
1881-
maybeAppendFormattedExtension(number, metadataForRegion,
1882-
PhoneNumberFormat.INTERNATIONAL, formattedNumber);
1881+
// Strip any extension
1882+
maybeStripExtension(formattedNumber);
1883+
// Append the formatted extension
1884+
maybeAppendFormattedExtension(
1885+
number, metadataForRegion, PhoneNumberFormat.INTERNATIONAL, formattedNumber);
18831886
if (internationalPrefixForFormatting.length() > 0) {
18841887
formattedNumber.insert(0, " ").insert(0, countryCode).insert(0, " ")
18851888
.insert(0, internationalPrefixForFormatting);

java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ public void testFormatOutOfCountryWithPreferredIntlPrefix() {
675675
phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.UZ));
676676
}
677677

678-
public void testFormatOutOfCountryKeepingAlphaChars() {
678+
public void testFormatOutOfCountryKeepingAlphaChars() throws Exception {
679679
PhoneNumber alphaNumericNumber = new PhoneNumber();
680680
alphaNumericNumber.setCountryCode(1).setNationalNumber(8007493524L)
681681
.setRawInput("1800 six-flag");
@@ -701,6 +701,13 @@ public void testFormatOutOfCountryKeepingAlphaChars() {
701701
assertEquals("1 800 SIX-FLAG",
702702
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.BS));
703703

704+
// Testing a number with extension.
705+
PhoneNumber alphaNumericNumberWithExtn =
706+
phoneUtil.parseAndKeepRawInput("800 SIX-flag ext. 1234", RegionCode.US);
707+
assertEquals(
708+
"0011 1 800 SIX-FLAG extn. 1234",
709+
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumberWithExtn, RegionCode.AU));
710+
704711
// Testing that if the raw input doesn't exist, it is formatted using
705712
// formatOutOfCountryCallingNumber.
706713
alphaNumericNumber.clearRawInput();

pending_code_changes.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
1+
Code changes:
2+
- Fixed a bug where the extension was appended twice in formatOutOfCountryKeepingAlphaChars in the Java version and updated FormatOutOfCountryKeepingAlphaChars in the C++ version to format the extension.

0 commit comments

Comments
 (0)