Skip to content

Commit

Permalink
Strip extension before appending formatted extension. (#3724)
Browse files Browse the repository at this point in the history
* Strip extension before appending formatted extension.

* Add pending code change

---------

Co-authored-by: Tijana Vislavski Gradina <[email protected]>
  • Loading branch information
silvio2402 and tvislavski authored Dec 23, 2024
1 parent 36db917 commit bf08a3c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
11 changes: 11 additions & 0 deletions cpp/src/phonenumbers/phonenumberutil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,17 @@ void PhoneNumberUtil::FormatOutOfCountryKeepingAlphaChars(
PrefixNumberWithCountryCallingCode(country_code, INTERNATIONAL,
formatted_number);
}
std::string region_code;
GetRegionCodeForCountryCode(country_code, &region_code);
// Metadata cannot be null because the country code is valid.
const PhoneMetadata* metadata_for_region =
GetMetadataForRegionOrCallingCode(country_code, region_code);
// Strip any extension
std::string extension;
MaybeStripExtension(formatted_number, &extension);
// Append the formatted extension
MaybeAppendFormattedExtension(number, *metadata_for_region, INTERNATIONAL,
formatted_number);
}

const NumberFormat* PhoneNumberUtil::ChooseFormattingPatternForNumber(
Expand Down
10 changes: 10 additions & 0 deletions cpp/test/phonenumbers/phonenumberutil_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,16 @@ TEST_F(PhoneNumberUtilTest, FormatOutOfCountryKeepingAlphaChars) {
&formatted_number);
EXPECT_EQ("1 800 SIX-FLAG", formatted_number);

// Testing a number with extension.
formatted_number.clear();
PhoneNumber alpha_numeric_number_with_extn;
phone_util_.ParseAndKeepRawInput("800 SIX-flag ext. 1234", RegionCode::US(),
&alpha_numeric_number_with_extn);
// preferredExtnPrefix for US is " extn. " (in test metadata)
phone_util_.FormatOutOfCountryKeepingAlphaChars(
alpha_numeric_number_with_extn, RegionCode::AU(), &formatted_number);
EXPECT_EQ("0011 1 800 SIX-FLAG extn. 1234", formatted_number);

// Testing that if the raw input doesn't exist, it is formatted using
// FormatOutOfCountryCallingNumber.
alpha_numeric_number.clear_raw_input();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1878,8 +1878,11 @@ public String formatOutOfCountryKeepingAlphaChars(PhoneNumber number,
String regionCode = getRegionCodeForCountryCode(countryCode);
// Metadata cannot be null because the country calling code is valid.
PhoneMetadata metadataForRegion = getMetadataForRegionOrCallingCode(countryCode, regionCode);
maybeAppendFormattedExtension(number, metadataForRegion,
PhoneNumberFormat.INTERNATIONAL, formattedNumber);
// Strip any extension
maybeStripExtension(formattedNumber);
// Append the formatted extension
maybeAppendFormattedExtension(
number, metadataForRegion, PhoneNumberFormat.INTERNATIONAL, formattedNumber);
if (internationalPrefixForFormatting.length() > 0) {
formattedNumber.insert(0, " ").insert(0, countryCode).insert(0, " ")
.insert(0, internationalPrefixForFormatting);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ public void testFormatOutOfCountryWithPreferredIntlPrefix() {
phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.UZ));
}

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

// Testing a number with extension.
PhoneNumber alphaNumericNumberWithExtn =
phoneUtil.parseAndKeepRawInput("800 SIX-flag ext. 1234", RegionCode.US);
assertEquals(
"0011 1 800 SIX-FLAG extn. 1234",
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumberWithExtn, RegionCode.AU));

// Testing that if the raw input doesn't exist, it is formatted using
// formatOutOfCountryCallingNumber.
alphaNumericNumber.clearRawInput();
Expand Down
3 changes: 2 additions & 1 deletion pending_code_changes.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

Code changes:
- 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 comments on commit bf08a3c

Please sign in to comment.