From 36db91716b625945e2590b781c6a1c257080e5c8 Mon Sep 17 00:00:00 2001 From: SG-Kang <101150768+SG-Kang@users.noreply.github.com> Date: Mon, 23 Dec 2024 22:37:17 +0900 Subject: [PATCH 1/2] Add "()" when string.match() is executed in js (#3769) * Fix regex logic * Simplify code * Fix variable name * Add case insensitive option for regexp --- javascript/i18n/phonenumbers/phonenumberutil.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/javascript/i18n/phonenumbers/phonenumberutil.js b/javascript/i18n/phonenumbers/phonenumberutil.js index d530dae5e..c64cd1f92 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil.js +++ b/javascript/i18n/phonenumbers/phonenumberutil.js @@ -4759,8 +4759,7 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.canBeInternationallyDialled = */ i18n.phonenumbers.PhoneNumberUtil.matchesEntirely = function(regex, str) { /** @type {Array.} */ - var matchedGroups = (typeof regex == 'string') ? - str.match('^(?:' + regex + ')$') : str.match(regex); + var matchedGroups = str.match(new RegExp('^(?:' + (typeof regex == 'string' ? regex : regex.source) + ')$', 'i')); if (matchedGroups && matchedGroups[0].length == str.length) { return true; } From bf08a3c486e5aaa9c356ecd8632c1777cf570934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Silvio=20Br=C3=A4ndle?= Date: Mon, 23 Dec 2024 14:38:12 +0100 Subject: [PATCH 2/2] Strip extension before appending formatted extension. (#3724) * Strip extension before appending formatted extension. * Add pending code change --------- Co-authored-by: Tijana Vislavski Gradina --- cpp/src/phonenumbers/phonenumberutil.cc | 11 +++++++++++ cpp/test/phonenumbers/phonenumberutil_test.cc | 10 ++++++++++ .../com/google/i18n/phonenumbers/PhoneNumberUtil.java | 7 +++++-- .../google/i18n/phonenumbers/PhoneNumberUtilTest.java | 9 ++++++++- pending_code_changes.txt | 3 ++- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/cpp/src/phonenumbers/phonenumberutil.cc b/cpp/src/phonenumbers/phonenumberutil.cc index f1442974f..c0c9d09a0 100644 --- a/cpp/src/phonenumbers/phonenumberutil.cc +++ b/cpp/src/phonenumbers/phonenumberutil.cc @@ -1710,6 +1710,17 @@ void PhoneNumberUtil::FormatOutOfCountryKeepingAlphaChars( PrefixNumberWithCountryCallingCode(country_code, INTERNATIONAL, formatted_number); } + std::string region_code; + GetRegionCodeForCountryCode(country_code, ®ion_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( diff --git a/cpp/test/phonenumbers/phonenumberutil_test.cc b/cpp/test/phonenumbers/phonenumberutil_test.cc index 4f6990349..97cf1f08a 100644 --- a/cpp/test/phonenumbers/phonenumberutil_test.cc +++ b/cpp/test/phonenumbers/phonenumberutil_test.cc @@ -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(); diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index e85cb65b5..b37bc48c3 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -1878,8 +1878,11 @@ public class PhoneNumberUtil { 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); diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 6bdef41a7..2b9345c4c 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -675,7 +675,7 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { 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"); @@ -701,6 +701,13 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { 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(); diff --git a/pending_code_changes.txt b/pending_code_changes.txt index 8b1378917..d67706acc 100644 --- a/pending_code_changes.txt +++ b/pending_code_changes.txt @@ -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. \ No newline at end of file