From bb2f203040e7078fbc516c06d615972aa13ccd58 Mon Sep 17 00:00:00 2001 From: mandlil <138015259+mandlil@users.noreply.github.com> Date: Wed, 27 Dec 2023 17:30:21 +0530 Subject: [PATCH 1/2] E164 parsing issue with +3758105261733 (national only number formatted as E.164). --- .../google/i18n/phonenumbers/PhoneNumberUtil.java | 7 +++++-- .../i18n/phonenumbers/PhoneNumberUtilTest.java | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index c49189fa2e..1c03a70d45 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -3020,9 +3020,12 @@ boolean maybeStripNationalPrefixAndCarrierCode( if (transformRule == null || transformRule.length() == 0 || prefixMatcher.group(numOfGroups) == null) { // If the original number was viable, and the resultant number is not, we return. - if (isViableOriginalNumber + if ((isViableOriginalNumber && !matcherApi.matchNationalNumber( - number.substring(prefixMatcher.end()), generalDesc, false)) { + number.substring(prefixMatcher.end()), generalDesc, false))|| + (getNumberTypeHelper(number.toString(), metadata) != PhoneNumberType.UNKNOWN + && getNumberTypeHelper(number.substring(prefixMatcher.end()), metadata) + == PhoneNumberType.UNKNOWN)) { return false; } if (carrierCode != null && numOfGroups > 0 && prefixMatcher.group(numOfGroups) != null) { diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index cb56077e4a..082245e532 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -1866,6 +1866,21 @@ public void testMaybeStripNationalPrefix() { assertEquals("Should have had no change - after stripping, it wouldn't have matched " + "the national rule.", strippedNumber, numberToStrip.toString()); + metadata.setNationalPrefixForParsing("0|80?"); + metadata.setPremiumRate( + metadata + .getGeneralDescBuilder() + .setNationalNumberPattern("(?:810|902)\d{7}")); + metadata.setPremiumRate(metadata.getGeneralDescBuilder().addPossibleLength(10)); + numberToStrip = new StringBuilder("8105261733"); + strippedNumber = "8105261733"; + assertFalse( + phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata.build(), null)); + assertEquals( + "Should have had no change - after stripping, it wouldn't have matched " + + "the national rule.", + strippedNumber, + numberToStrip.toString()); // Test extracting carrier selection code. metadata.setNationalPrefixForParsing("0(81)?"); numberToStrip = new StringBuilder("08122123456"); From 088ed31a845e84b0a6e9e9eac023025ac35c25a1 Mon Sep 17 00:00:00 2001 From: mandlil <138015259+mandlil@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:57:40 +0530 Subject: [PATCH 2/2] Update PhoneNumberUtilTest.java --- .../com/google/i18n/phonenumbers/PhoneNumberUtilTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 082245e532..7b82d56d65 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -1843,6 +1843,9 @@ public void testMaybeStripNationalPrefix() { metadata .getGeneralDescBuilder() .setNationalNumberPattern("\\d{4,8}"); + metadata + .getGeneralDescBuilder() + .addPossibleLength(6); StringBuilder numberToStrip = new StringBuilder("34356778"); String strippedNumber = "356778"; assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata.build(), null)); @@ -1870,7 +1873,7 @@ public void testMaybeStripNationalPrefix() { metadata.setPremiumRate( metadata .getGeneralDescBuilder() - .setNationalNumberPattern("(?:810|902)\d{7}")); + .setNationalNumberPattern("(?:810|902)\\d{7}")); metadata.setPremiumRate(metadata.getGeneralDescBuilder().addPossibleLength(10)); numberToStrip = new StringBuilder("8105261733"); strippedNumber = "8105261733";