diff --git a/cpp/src/phonenumbers/parsingOptions.cc b/cpp/src/phonenumbers/parsingOptions.cc new file mode 100644 index 0000000000..1a955bc0ac --- /dev/null +++ b/cpp/src/phonenumbers/parsingOptions.cc @@ -0,0 +1,20 @@ +#include "i18n/phonenumbers/parsingoptions.h" + +#include "i18n/identifiers/regioncode.h" + +namespace i18n { +namespace phonenumbers { + +ParsingOptions& ParsingOptions::SetDefaultRegion( + i18n_identifiers::RegionCode default_region) { + default_region_ = default_region; + return *this; +} + +ParsingOptions& ParsingOptions::SetKeepRawInput(bool keep_raw_input) { + keep_raw_input_ = keep_raw_input; + return *this; +} + +} // namespace phonenumbers +} // namespace i18n \ No newline at end of file diff --git a/cpp/src/phonenumbers/parsingOptions.h b/cpp/src/phonenumbers/parsingOptions.h new file mode 100644 index 0000000000..917a115262 --- /dev/null +++ b/cpp/src/phonenumbers/parsingOptions.h @@ -0,0 +1,43 @@ +#ifndef I18N_PHONENUMBERS_PARSINGOPTIONS_H_ +#define I18N_PHONENUMBERS_PARSINGOPTIONS_H_ + +#include "i18n/identifiers/regioncode.h" + +namespace i18n { +namespace phonenumbers { + +// Options for parsing a phone number. To be used with the ParseWithOptions +// method. +// Example: +// ParsingOptions().SetDefaultRegion(RegionCode::US()).SetKeepRawInput(true); +class ParsingOptions { + public: + ParsingOptions() = default; + + // Set the value for default_region_. + ParsingOptions& SetDefaultRegion( + i18n_identifiers::RegionCode default_region); + + // Set the value for keep_raw_input_. + ParsingOptions& SetKeepRawInput(bool keep_raw_input); + + private: + friend class PhoneNumberUtil; + + // The region we are expecting the number to be from. This is ignored if the + // number being parsed is written in international format. In case of national + // format, the country_code will be set to the one of this default region. If + // the number is guaranteed to start with a '+' followed by the country + // calling code, then RegionCode.ZZ or null can be supplied. + i18n_identifiers::RegionCode default_region_ = + i18n_identifiers::RegionCode::ZZ(); + + // Whether the raw input should be kept in the PhoneNumber object. If true, + // the raw_input field and country_code_source fields will be populated. + bool keep_raw_input_ = false; +}; + +} // namespace phonenumbers +} // namespace i18n + +#endif // I1 \ No newline at end of file diff --git a/cpp/src/phonenumbers/phonenumberutil.cc b/cpp/src/phonenumbers/phonenumberutil.cc index f1442974f3..2bcfe2154f 100644 --- a/cpp/src/phonenumbers/phonenumberutil.cc +++ b/cpp/src/phonenumbers/phonenumberutil.cc @@ -37,6 +37,7 @@ #include "phonenumbers/phonemetadata.pb.h" #include "phonenumbers/phonenumber.h" #include "phonenumbers/phonenumber.pb.h" +#include "phonenumbers/parsingoptions.h" #include "phonenumbers/regex_based_matcher.h" #include "phonenumbers/regexp_adapter.h" #include "phonenumbers/regexp_cache.h" @@ -47,6 +48,7 @@ #include "phonenumbers/utf/unicodetext.h" #include "phonenumbers/utf/utf.h" + namespace i18n { namespace phonenumbers { @@ -2127,15 +2129,17 @@ PhoneNumberUtil::ErrorType PhoneNumberUtil::Parse(const string& number_to_parse, const string& default_region, PhoneNumber* number) const { DCHECK(number); - return ParseHelper(number_to_parse, default_region, false, true, number); + return ParseWithOptions(number_to_parse, + ParsingOptions().SetDefaultRegion(default_region), + number); } -PhoneNumberUtil::ErrorType PhoneNumberUtil::ParseAndKeepRawInput( - const string& number_to_parse, - const string& default_region, +PhoneNumberUtil::ErrorType PhoneNumberUtil::ParseWithOptions( + absl::string_view number_to_parse, const ParsingOptions& options, PhoneNumber* number) const { DCHECK(number); - return ParseHelper(number_to_parse, default_region, true, true, number); + return ParseHelper(number_to_parse, options.default_region_, + options.keep_raw_input_, /*check_region=*/true, number); } // Checks to see that the region code used is valid, or if it is not valid, that diff --git a/cpp/src/phonenumbers/phonenumberutil.h b/cpp/src/phonenumbers/phonenumberutil.h index 14cfc670c7..d5e7e36398 100644 --- a/cpp/src/phonenumbers/phonenumberutil.h +++ b/cpp/src/phonenumbers/phonenumberutil.h @@ -29,9 +29,10 @@ #include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/base/memory/singleton.h" #include "phonenumbers/phonenumber.pb.h" - +#include "phonenumbers/parsingoptions.h" #include "absl/container/node_hash_set.h" #include "absl/container/node_hash_map.h" +#include "third_party/absl/base/macros.h" class TelephoneNumber; @@ -700,13 +701,29 @@ class PhoneNumberUtil : public Singleton { ErrorType Parse(const string& number_to_parse, const string& default_region, PhoneNumber* number) const; + // Parses a string and returns it in proto buffer format. This method differs // from Parse() in that it always populates the raw_input field of the // protocol buffer with number_to_parse as well as the country_code_source // field. - ErrorType ParseAndKeepRawInput(const string& number_to_parse, - const string& default_region, - PhoneNumber* number) const; + ABSL_DEPRECATE_AND_INLINE() + ErrorType ParseAndKeepRawInput( + absl::string_view number_to_parse, + i18n_identifiers::RegionCode default_region + PhoneNumber* number) const { + return ParseWithOptions(number_to_parse, + ParsingOptions() + .SetDefaultRegion(default_region) + .SetKeepRawInput(true), + number); + } + + // Parses a string and returns it in proto buffer format. This method differs + // from Parse() in that it allows the caller to change the behavior of the + // parser. See ParsingOptions for more details. + ErrorType ParseWithOptions(absl::string_view number_to_parse, + const ParsingOptions& options, + PhoneNumber* number) const; // Takes two phone numbers and compares them for equality. // diff --git a/cpp/test/phonenumbers/phonenumberutil_test.cc b/cpp/test/phonenumbers/phonenumberutil_test.cc index 4f69903492..75824b3475 100644 --- a/cpp/test/phonenumbers/phonenumberutil_test.cc +++ b/cpp/test/phonenumbers/phonenumberutil_test.cc @@ -64,9 +64,9 @@ class PhoneNumberUtilTest : public testing::Test { return phone_util_.GetMetadataForRegion(region_code); } - const PhoneMetadata* GetMetadataForNonGeographicalRegion( + const PhoneMetadata* GetMetadataForNonGeographicalEntity( int country_code) const { - return phone_util_.GetMetadataForNonGeographicalRegion(country_code); + return phone_util_.GetMetadataForNonGeographicalEntity(country_code); } void ExtractPossibleNumber(const string& number, @@ -341,7 +341,7 @@ TEST_F(PhoneNumberUtilTest, GetInstanceLoadARMetadata) { } TEST_F(PhoneNumberUtilTest, GetInstanceLoadInternationalTollFreeMetadata) { - const PhoneMetadata* metadata = GetMetadataForNonGeographicalRegion(800); + const PhoneMetadata* metadata = GetMetadataForNonGeographicalEntity(800); EXPECT_FALSE(metadata == NULL); EXPECT_EQ("001", metadata->id()); EXPECT_EQ(800, metadata->country_code()); @@ -2408,7 +2408,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { string formatted_number; EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("+442087654321", RegionCode::GB(), + phone_util_.ParseWithOptions("+442087654321", ParsingOptions().SetDefaultRegion(RegionCode::GB()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), &formatted_number); @@ -2417,7 +2418,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("02087654321", RegionCode::GB(), + phone_util_.ParseWithOptions("02087654321", ParsingOptions().SetDefaultRegion(RegionCode::GB()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), &formatted_number); @@ -2426,8 +2428,9 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("011442087654321", - RegionCode::US(), &phone_number)); + phone_util_.ParseWithOptions("011442087654321", ParsingOptions().SetDefaultRegion(RegionCode::US(),) + SetKeepRawInput(true), + &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); EXPECT_EQ("011 44 20 8765 4321", formatted_number); @@ -2435,7 +2438,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("442087654321", RegionCode::GB(), + phone_util_.ParseWithOptions("442087654321", ParsingOptions().SetDefaultRegion(RegionCode::GB()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), &formatted_number); @@ -2456,7 +2460,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("7345678901", RegionCode::US(), + phone_util_.ParseWithOptions("7345678901", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); @@ -2467,7 +2472,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("0734567 8901", RegionCode::US(), + phone_util_.ParseWithOptions("0734567 8901", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); @@ -2478,7 +2484,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("02-4567-8900", RegionCode::KR(), + phone_util_.ParseWithOptions("02-4567-8900", ParsingOptions().SetDefaultRegion(RegionCode::KR()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::KR(), &formatted_number); @@ -2487,8 +2494,9 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("01180012345678", - RegionCode::US(), &phone_number)); + phone_util_.ParseWithOptions("01180012345678", ParsingOptions().SetDefaultRegion(RegionCode::US(),) + SetKeepRawInput(true), + &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); EXPECT_EQ("011 800 1234 5678", formatted_number); @@ -2496,7 +2504,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("+80012345678", RegionCode::KR(), + phone_util_.ParseWithOptions("+80012345678", ParsingOptions().SetDefaultRegion(RegionCode::KR()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::KR(), &formatted_number); @@ -2507,7 +2516,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("2530000", RegionCode::US(), + phone_util_.ParseWithOptions("2530000", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); @@ -2517,7 +2527,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number with national prefix in the US. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("18003456789", RegionCode::US(), + phone_util_.ParseWithOptions("18003456789", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); @@ -2527,7 +2538,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number without national prefix in the UK. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("2087654321", RegionCode::GB(), + phone_util_.ParseWithOptions("2087654321", ParsingOptions().SetDefaultRegion(RegionCode::GB()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), &formatted_number); @@ -2547,7 +2559,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number with national prefix in Mexico. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("013312345678", RegionCode::MX(), + phone_util_.ParseWithOptions("013312345678", ParsingOptions().SetDefaultRegion(RegionCode::MX()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::MX(), &formatted_number); @@ -2557,7 +2570,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number without national prefix in Mexico. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("3312345678", RegionCode::MX(), + phone_util_.ParseWithOptions("3312345678", ParsingOptions().SetDefaultRegion(RegionCode::MX()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::MX(), &formatted_number); @@ -2567,7 +2581,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Italian fixed-line number. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("0212345678", RegionCode::IT(), + phone_util_.ParseWithOptions("0212345678", ParsingOptions().SetDefaultRegion(RegionCode::IT()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::IT(), &formatted_number); @@ -2577,7 +2592,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number with national prefix in Japan. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("00777012", RegionCode::JP(), + phone_util_.ParseWithOptions("00777012", ParsingOptions().SetDefaultRegion(RegionCode::JP()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::JP(), &formatted_number); @@ -2587,7 +2603,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number without national prefix in Japan. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("0777012", RegionCode::JP(), + phone_util_.ParseWithOptions("0777012", ParsingOptions().SetDefaultRegion(RegionCode::JP()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::JP(), &formatted_number); @@ -2597,7 +2614,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number with carrier code in Brazil. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("012 3121286979", RegionCode::BR(), + phone_util_.ParseWithOptions("012 3121286979", ParsingOptions().SetDefaultRegion(RegionCode::BR()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::BR(), &formatted_number); @@ -2609,8 +2627,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { // national prefix 044 is entered, we return the raw input as we don't want to // change the number entered. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("044(33)1234-5678", - RegionCode::MX(), + phone_util_.ParseWithOptions("044(33)1234-5678", ParsingOptions().SetDefaultRegion(RegionCode::MX()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::MX(), &formatted_number); @@ -2619,8 +2637,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("045(33)1234-5678", - RegionCode::MX(), + phone_util_.ParseWithOptions("045(33)1234-5678", ParsingOptions().SetDefaultRegion(RegionCode::MX()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::MX(), &formatted_number); @@ -2632,8 +2650,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("0012 16502530000", - RegionCode::AU(), + phone_util_.ParseWithOptions("0012 16502530000", ParsingOptions().SetDefaultRegion(RegionCode::AU()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::AU(), &formatted_number); @@ -2642,8 +2660,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("0011 16502530000", - RegionCode::AU(), + phone_util_.ParseWithOptions("0011 16502530000", ParsingOptions().SetDefaultRegion(RegionCode::AU()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::AU(), &formatted_number); @@ -2654,8 +2672,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("*1234", - RegionCode::JP(), + phone_util_.ParseWithOptions("*1234", ParsingOptions().SetDefaultRegion(RegionCode::JP()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::JP(), &formatted_number); @@ -2663,8 +2681,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("1234", - RegionCode::JP(), + phone_util_.ParseWithOptions("1234", ParsingOptions().SetDefaultRegion(RegionCode::JP()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::JP(), &formatted_number); @@ -3575,7 +3593,7 @@ TEST_F(PhoneNumberUtilTest, ParseNationalNumber) { EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, phone_util_.Parse("033316005", RegionCode::NZ(), &test_number)); EXPECT_EQ(nz_number, test_number); - // Some fields are not filled in by Parse, but only by ParseAndKeepRawInput. + // Some fields are not filled in by Parse, but only by ParseWithOptions. EXPECT_FALSE(nz_number.has_country_code_source()); EXPECT_EQ(PhoneNumber::UNSPECIFIED, nz_number.country_code_source()); @@ -4170,8 +4188,8 @@ TEST_F(PhoneNumberUtilTest, ParseNumbersWithPlusWithNoRegion) { nz_number.set_country_code_source(PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN); result_proto.Clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("+64 3 331 6005", - RegionCode::GetUnknown(), + phone_util_.ParseWithOptions("+64 3 331 6005", ParsingOptions().SetDefaultRegion( RegionCode::GetUnknown()), + SetKeepRawInput(true), &result_proto)); EXPECT_EQ(nz_number, result_proto); } @@ -4621,7 +4639,8 @@ TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { PhoneNumber test_number; EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("800 six-flags", RegionCode::US(), + phone_util_.ParseWithOptions("800 six-flags", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &test_number)); EXPECT_EQ(alpha_numeric_number, test_number); @@ -4630,7 +4649,8 @@ TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { alpha_numeric_number.set_country_code_source( PhoneNumber::FROM_NUMBER_WITHOUT_PLUS_SIGN); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("1800 six-flag", RegionCode::US(), + phone_util_.ParseWithOptions("1800 six-flag", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &test_number)); EXPECT_EQ(alpha_numeric_number, test_number); @@ -4638,7 +4658,8 @@ TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { alpha_numeric_number.set_country_code_source( PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("+1800 six-flag", RegionCode::CN(), + phone_util_.ParseWithOptions("+1800 six-flag", ParsingOptions().SetDefaultRegion(RegionCode::CN()), + SetKeepRawInput(true), &test_number)); EXPECT_EQ(alpha_numeric_number, test_number); @@ -4646,8 +4667,8 @@ TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { alpha_numeric_number.set_country_code_source( PhoneNumber::FROM_NUMBER_WITH_IDD); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("001800 six-flag", - RegionCode::NZ(), + phone_util_.ParseWithOptions("001800 six-flag", ParsingOptions().SetDefaultRegion( RegionCode::NZ()), + SetKeepRawInput(true), &test_number)); EXPECT_EQ(alpha_numeric_number, test_number); @@ -4665,8 +4686,8 @@ TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { korean_number.set_country_code_source(PhoneNumber::FROM_DEFAULT_COUNTRY); korean_number.set_preferred_domestic_carrier_code("81"); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("08122123456", - RegionCode::KR(), + phone_util_.ParseWithOptions("08122123456", ParsingOptions().SetDefaultRegion( RegionCode::KR()), + SetKeepRawInput(true), &test_number)); EXPECT_EQ(korean_number, test_number); } diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java new file mode 100644 index 0000000000..af3eb1aef0 --- /dev/null +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2024 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers; + +/** Options for the phone number parser. */ +public class ParsingOptions { + + /** + * Returns the region we are expecting the number to be from. This is ignored if the number being + * parsed is written in international format. In case of national format, the country_code will be + * set to the one of this default region. If the number is guaranteed to start with a '+' followed + * by the country calling code, then RegionCode.ZZ or null can be supplied. + */ + private boolean hasDefaultRegion; + private String defaultRegion_ = null; + public boolean hasDefaultRegion() { return hasDefaultRegion; } + public String getDefaultRegion() { return defaultRegion_; } + public ParsingOptions setDefaultRegion(String value) { + hasDefaultRegion = (value != null); + defaultRegion_ = value; + return this; + } + + /** + * Returns whether the raw input should be kept in the PhoneNumber object. If true, the raw_input + * field and country_code_source fields will be populated. + */ + private boolean hasKeepRawInput; + private boolean keepRawInput_ = false; + public boolean hasKeepRawInput() { return hasKeepRawInput; } + public boolean isKeepRawInput() { return keepRawInput_; } + public ParsingOptions setKeepRawInput(boolean value) { + if(value == false) { + + } + hasKeepRawInput = true; + keepRawInput_ = value; + return this; + } + + public ParsingOptions withDefaultRegion(String regionCode) { + return setDefaultRegion(regionCode); + } +} \ No newline at end of file diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index e85cb65b52..fd7e7dbf9d 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -3140,7 +3140,10 @@ private boolean checkRegionForParsing(CharSequence numberToParse, String default public PhoneNumber parse(CharSequence numberToParse, String defaultRegion) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); - parse(numberToParse, defaultRegion, phoneNumber); + parseWithOptions( + numberToParse, + new ParsingOptions().setDefaultRegion(defaultRegion), + phoneNumber); return phoneNumber; } @@ -3150,7 +3153,10 @@ public PhoneNumber parse(CharSequence numberToParse, String defaultRegion) */ public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber phoneNumber) throws NumberParseException { - parseHelper(numberToParse, defaultRegion, false, true, phoneNumber); + parseWithOptions( + numberToParse, + new ParsingOptions().setDefaultRegion(defaultRegion), + phoneNumber); } /** @@ -3166,22 +3172,77 @@ public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber * @return a phone number proto buffer filled with the parsed number * @throws NumberParseException if the string is not considered to be a viable phone number or if * no default region was supplied + * @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions)} instead. */ + @Deprecated public PhoneNumber parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); - parseAndKeepRawInput(numberToParse, defaultRegion, phoneNumber); + parseWithOptions( + numberToParse, + new ParsingOptions().setKeepRawInput(true).setDefaultRegion(defaultRegion), + phoneNumber); return phoneNumber; } /** * Same as{@link #parseAndKeepRawInput(CharSequence, String)}, but accepts a mutable * PhoneNumber as a parameter to decrease object creation when invoked many times. + * @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions, PhoneNumber)} instead. */ - public void parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion, - PhoneNumber phoneNumber) + @Deprecated + public void parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion, PhoneNumber phoneNumber) throws NumberParseException { - parseHelper(numberToParse, defaultRegion, true, true, phoneNumber); + parseWithOptions( + numberToParse, + new ParsingOptions().setKeepRawInput(true).setDefaultRegion(defaultRegion), + phoneNumber); + } + + /** + * Parses a string and returns it as a PhoneNumber object. The method is quite lenient and looks + * for a number in the input text (raw input) and does not check whether the string is definitely + * only a phone number. To do this, it ignores punctuation and white-space, as well as any text + * before the number (e.g. a leading "Tel: ") and trims the non-number bits. It will accept a + * number in any format (E164, national, international etc), assuming it can be interpreted with + * the options supplied. It also attempts to convert any alpha characters into digits if it thinks + * this is a vanity number of the type "1800 MICROSOFT". + * + *

This method will throw a {@link com.google.i18n.phonenumbers.NumberParseException} if the + * number is not considered to be a possible number. Note that validation of whether the number is + * actually a valid number for a particular region is not performed. This can be done separately + * with {@link #isValidNumber}. + * + *

Note this method canonicalizes the phone number such that different representations can be + * easily compared, no matter what form it was originally entered in (e.g. national, + * international). If you want to record context about the number being parsed, such as the raw + * input that was entered, how the country code was derived etc. then set the `keepRawInput` + * option in the options accordingly. + * + * @param numberToParse number that we are attempting to parse. This can contain formatting such + * as +, ( and -, as well as a phone number extension. It can also be provided in RFC3966 + * format. + * @param options options to configure the behavior of the parser. See {@link ParsingOptions} for + * more details. + * @return a phone number proto buffer filled with the parsed number. + * @throws NumberParseException if the string is not considered to be a viable phone number (e.g. + * too few or too many digits) or if no default region was supplied and the number is not in + * international format (does not start with +). + */ + public PhoneNumber parseWithOptions(CharSequence numberToParse, ParsingOptions options) + throws NumberParseException { + PhoneNumber phoneNumber = new PhoneNumber(); + parseWithOptions(numberToParse, options, phoneNumber); + return phoneNumber; + } + + /** + * Same as{@link #parseWithOptions(CharSequence, ParsingOptions)}, but accepts a mutable + * PhoneNumber as a parameter to decrease object creation when invoked many times. + */ + public void parseWithOptions(CharSequence numberToParse, ParsingOptions options, PhoneNumber phoneNumber) + throws NumberParseException { + parseHelper(numberToParse, options.getDefaultRegion(), options.isKeepRawInput(), true, phoneNumber); } /** diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 6bdef41a7c..66b33fbeeb 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -1006,16 +1006,16 @@ public void testFormatNumberWithExtension() { } public void testFormatInOriginalFormat() throws Exception { - PhoneNumber number1 = phoneUtil.parseAndKeepRawInput("+442087654321", RegionCode.GB); + PhoneNumber number1 = phoneUtil.parseWithOptions("+442087654321", new ParsingOptions().setDefaultRegion(RegionCode.GB).setKeepRawInput(true)); assertEquals("+44 20 8765 4321", phoneUtil.formatInOriginalFormat(number1, RegionCode.GB)); - PhoneNumber number2 = phoneUtil.parseAndKeepRawInput("02087654321", RegionCode.GB); + PhoneNumber number2 = phoneUtil.parseWithOptions("02087654321", new ParsingOptions().setDefaultRegion(RegionCode.GB).setKeepRawInput(true)); assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number2, RegionCode.GB)); - PhoneNumber number3 = phoneUtil.parseAndKeepRawInput("011442087654321", RegionCode.US); + PhoneNumber number3 = phoneUtil.parseWithOptions("011442087654321", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("011 44 20 8765 4321", phoneUtil.formatInOriginalFormat(number3, RegionCode.US)); - PhoneNumber number4 = phoneUtil.parseAndKeepRawInput("442087654321", RegionCode.GB); + PhoneNumber number4 = phoneUtil.parseWithOptions("442087654321", new ParsingOptions().setDefaultRegion(RegionCode.GB).setKeepRawInput(true)); assertEquals("44 20 8765 4321", phoneUtil.formatInOriginalFormat(number4, RegionCode.GB)); PhoneNumber number5 = phoneUtil.parse("+442087654321", RegionCode.GB); @@ -1023,80 +1023,80 @@ public void testFormatInOriginalFormat() throws Exception { // Invalid numbers that we have a formatting pattern for should be formatted properly. Note area // codes starting with 7 are intentionally excluded in the test metadata for testing purposes. - PhoneNumber number6 = phoneUtil.parseAndKeepRawInput("7345678901", RegionCode.US); + PhoneNumber number6 = phoneUtil.parseWithOptions("7345678901", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("734 567 8901", phoneUtil.formatInOriginalFormat(number6, RegionCode.US)); // US is not a leading zero country, and the presence of the leading zero leads us to format the // number using raw_input. - PhoneNumber number7 = phoneUtil.parseAndKeepRawInput("0734567 8901", RegionCode.US); + PhoneNumber number7 = phoneUtil.parseWithOptions("0734567 8901", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("0734567 8901", phoneUtil.formatInOriginalFormat(number7, RegionCode.US)); // This number is valid, but we don't have a formatting pattern for it. Fall back to the raw // input. - PhoneNumber number8 = phoneUtil.parseAndKeepRawInput("02-4567-8900", RegionCode.KR); + PhoneNumber number8 = phoneUtil.parseWithOptions("02-4567-8900", new ParsingOptions().setDefaultRegion(RegionCode.KR).setKeepRawInput(true)); assertEquals("02-4567-8900", phoneUtil.formatInOriginalFormat(number8, RegionCode.KR)); - PhoneNumber number9 = phoneUtil.parseAndKeepRawInput("01180012345678", RegionCode.US); + PhoneNumber number9 = phoneUtil.parseWithOptions("01180012345678", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("011 800 1234 5678", phoneUtil.formatInOriginalFormat(number9, RegionCode.US)); - PhoneNumber number10 = phoneUtil.parseAndKeepRawInput("+80012345678", RegionCode.KR); + PhoneNumber number10 = phoneUtil.parseWithOptions("+80012345678", new ParsingOptions().setDefaultRegion(RegionCode.KR).setKeepRawInput(true)); assertEquals("+800 1234 5678", phoneUtil.formatInOriginalFormat(number10, RegionCode.KR)); // US local numbers are formatted correctly, as we have formatting patterns for them. - PhoneNumber localNumberUS = phoneUtil.parseAndKeepRawInput("2530000", RegionCode.US); + PhoneNumber localNumberUS = phoneUtil.parseWithOptions("2530000", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("253 0000", phoneUtil.formatInOriginalFormat(localNumberUS, RegionCode.US)); PhoneNumber numberWithNationalPrefixUS = - phoneUtil.parseAndKeepRawInput("18003456789", RegionCode.US); + phoneUtil.parseWithOptions("18003456789", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("1 800 345 6789", phoneUtil.formatInOriginalFormat(numberWithNationalPrefixUS, RegionCode.US)); PhoneNumber numberWithoutNationalPrefixGB = - phoneUtil.parseAndKeepRawInput("2087654321", RegionCode.GB); + phoneUtil.parseWithOptions("2087654321", new ParsingOptions().setDefaultRegion(RegionCode.GB).setKeepRawInput(true)); assertEquals("20 8765 4321", phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixGB, RegionCode.GB)); // Make sure no metadata is modified as a result of the previous function call. assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number5, RegionCode.GB)); PhoneNumber numberWithNationalPrefixMX = - phoneUtil.parseAndKeepRawInput("013312345678", RegionCode.MX); + phoneUtil.parseWithOptions("013312345678", new ParsingOptions().setDefaultRegion(RegionCode.MX).setKeepRawInput(true)); assertEquals("01 33 1234 5678", phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX, RegionCode.MX)); PhoneNumber numberWithoutNationalPrefixMX = - phoneUtil.parseAndKeepRawInput("3312345678", RegionCode.MX); + phoneUtil.parseWithOptions("3312345678", new ParsingOptions().setDefaultRegion(RegionCode.MX).setKeepRawInput(true)); assertEquals("33 1234 5678", phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixMX, RegionCode.MX)); PhoneNumber italianFixedLineNumber = - phoneUtil.parseAndKeepRawInput("0212345678", RegionCode.IT); + phoneUtil.parseWithOptions("0212345678", new ParsingOptions().setDefaultRegion(RegionCode.IT).setKeepRawInput(true)); assertEquals("02 1234 5678", phoneUtil.formatInOriginalFormat(italianFixedLineNumber, RegionCode.IT)); PhoneNumber numberWithNationalPrefixJP = - phoneUtil.parseAndKeepRawInput("00777012", RegionCode.JP); + phoneUtil.parseWithOptions("00777012", new ParsingOptions().setDefaultRegion(RegionCode.JP).setKeepRawInput(true)); assertEquals("0077-7012", phoneUtil.formatInOriginalFormat(numberWithNationalPrefixJP, RegionCode.JP)); PhoneNumber numberWithoutNationalPrefixJP = - phoneUtil.parseAndKeepRawInput("0777012", RegionCode.JP); + phoneUtil.parseWithOptions("0777012", new ParsingOptions().setDefaultRegion(RegionCode.JP).setKeepRawInput(true)); assertEquals("0777012", phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixJP, RegionCode.JP)); PhoneNumber numberWithCarrierCodeBR = - phoneUtil.parseAndKeepRawInput("012 3121286979", RegionCode.BR); + phoneUtil.parseWithOptions("012 3121286979", new ParsingOptions().setDefaultRegion(RegionCode.BR).setKeepRawInput(true)); assertEquals("012 3121286979", phoneUtil.formatInOriginalFormat(numberWithCarrierCodeBR, RegionCode.BR)); // The default national prefix used in this case is 045. When a number with national prefix 044 // is entered, we return the raw input as we don't want to change the number entered. PhoneNumber numberWithNationalPrefixMX1 = - phoneUtil.parseAndKeepRawInput("044(33)1234-5678", RegionCode.MX); + phoneUtil.parseWithOptions("044(33)1234-5678", new ParsingOptions().setDefaultRegion(RegionCode.MX).setKeepRawInput(true)); assertEquals("044(33)1234-5678", phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX1, RegionCode.MX)); PhoneNumber numberWithNationalPrefixMX2 = - phoneUtil.parseAndKeepRawInput("045(33)1234-5678", RegionCode.MX); + phoneUtil.parseWithOptions("045(33)1234-5678", new ParsingOptions().setDefaultRegion(RegionCode.MX).setKeepRawInput(true)); assertEquals("045 33 1234 5678", phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX2, RegionCode.MX)); @@ -1104,19 +1104,19 @@ public void testFormatInOriginalFormat() throws Exception { // prefix 0012 is entered, we return the raw input as we don't want to change the number // entered. PhoneNumber outOfCountryNumberFromAU1 = - phoneUtil.parseAndKeepRawInput("0012 16502530000", RegionCode.AU); + phoneUtil.parseWithOptions("0012 16502530000", new ParsingOptions().setDefaultRegion(RegionCode.AU).setKeepRawInput(true)); assertEquals("0012 16502530000", phoneUtil.formatInOriginalFormat(outOfCountryNumberFromAU1, RegionCode.AU)); PhoneNumber outOfCountryNumberFromAU2 = - phoneUtil.parseAndKeepRawInput("0011 16502530000", RegionCode.AU); + phoneUtil.parseWithOptions("0011 16502530000", new ParsingOptions().setDefaultRegion(RegionCode.AU).setKeepRawInput(true)); assertEquals("0011 1 650 253 0000", phoneUtil.formatInOriginalFormat(outOfCountryNumberFromAU2, RegionCode.AU)); // Test the star sign is not removed from or added to the original input by this method. - PhoneNumber starNumber = phoneUtil.parseAndKeepRawInput("*1234", RegionCode.JP); + PhoneNumber starNumber = phoneUtil.parseWithOptions("*1234", new ParsingOptions().setDefaultRegion(RegionCode.JP).setKeepRawInput(true)); assertEquals("*1234", phoneUtil.formatInOriginalFormat(starNumber, RegionCode.JP)); - PhoneNumber numberWithoutStar = phoneUtil.parseAndKeepRawInput("1234", RegionCode.JP); + PhoneNumber numberWithoutStar = phoneUtil.parseWithOptions("1234", new ParsingOptions().setDefaultRegion(RegionCode.JP).setKeepRawInput(true)); assertEquals("1234", phoneUtil.formatInOriginalFormat(numberWithoutStar, RegionCode.JP)); // Test an invalid national number without raw input is just formatted as the national number. @@ -2085,7 +2085,7 @@ public void testMaybeExtractCountryCode() { public void testParseNationalNumber() throws Exception { // National prefix attached. assertEquals(NZ_NUMBER, phoneUtil.parse("033316005", RegionCode.NZ)); - // Some fields are not filled in by parse, but only by parseAndKeepRawInput. + // Some fields are not filled in by parse, but only by parseWithOptions with the keepRawInput option set. assertFalse(NZ_NUMBER.hasCountryCodeSource()); assertEquals(CountryCodeSource.UNSPECIFIED, NZ_NUMBER.getCountryCodeSource()); @@ -2580,10 +2580,9 @@ public void testParseNumbersWithPlusWithNoRegion() throws Exception { PhoneNumber nzNumberWithRawInput = new PhoneNumber().mergeFrom(NZ_NUMBER). setRawInput("+64 3 331 6005"). setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN); - assertEquals(nzNumberWithRawInput, phoneUtil.parseAndKeepRawInput("+64 3 331 6005", - RegionCode.ZZ)); + assertEquals(nzNumberWithRawInput, phoneUtil.parseWithOptions("+64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.ZZ).setKeepRawInput(true))); // Null is also allowed for the region code in these cases. - assertEquals(nzNumberWithRawInput, phoneUtil.parseAndKeepRawInput("+64 3 331 6005", null)); + assertEquals(nzNumberWithRawInput, phoneUtil.parseWithOptions("+64 3 331 6005", new ParsingOptions().setDefaultRegion(null).setKeepRawInput(true))); } public void testParseNumberTooShortIfNationalPrefixStripped() throws Exception { @@ -2833,28 +2832,28 @@ public void testParseAndKeepRaw() throws Exception { setRawInput("800 six-flags"). setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY); assertEquals(alphaNumericNumber, - phoneUtil.parseAndKeepRawInput("800 six-flags", RegionCode.US)); + phoneUtil.parseWithOptions("800 six-flags", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true))); PhoneNumber shorterAlphaNumber = new PhoneNumber(). setCountryCode(1).setNationalNumber(8007493524L). setRawInput("1800 six-flag"). setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN); assertEquals(shorterAlphaNumber, - phoneUtil.parseAndKeepRawInput("1800 six-flag", RegionCode.US)); + phoneUtil.parseWithOptions("1800 six-flag", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true))); shorterAlphaNumber.setRawInput("+1800 six-flag"). setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN); assertEquals(shorterAlphaNumber, - phoneUtil.parseAndKeepRawInput("+1800 six-flag", RegionCode.NZ)); + phoneUtil.parseWithOptions("+1800 six-flag", new ParsingOptions().setDefaultRegion(RegionCode.NZ).setKeepRawInput(true))); shorterAlphaNumber.setRawInput("001800 six-flag"). setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_IDD); assertEquals(shorterAlphaNumber, - phoneUtil.parseAndKeepRawInput("001800 six-flag", RegionCode.NZ)); + phoneUtil.parseWithOptions("001800 six-flag", new ParsingOptions().setDefaultRegion(RegionCode.NZ).setKeepRawInput(true))); // Invalid region code supplied. try { - phoneUtil.parseAndKeepRawInput("123 456 7890", "CS"); + phoneUtil.parseWithOptions("123 456 7890", new ParsingOptions().setDefaultRegion("CS").setKeepRawInput(true)); fail("Deprecated region code not allowed: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2867,7 +2866,7 @@ public void testParseAndKeepRaw() throws Exception { koreanNumber.setCountryCode(82).setNationalNumber(22123456).setRawInput("08122123456"). setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY). setPreferredDomesticCarrierCode("81"); - assertEquals(koreanNumber, phoneUtil.parseAndKeepRawInput("08122123456", RegionCode.KR)); + assertEquals(koreanNumber, phoneUtil.parseWithOptions("08122123456", new ParsingOptions().setDefaultRegion(RegionCode.KR).setKeepRawInput(true))); } public void testParseItalianLeadingZeros() throws Exception { diff --git a/pending_code_changes.txt b/pending_code_changes.txt index 8b13789179..15a8e10952 100644 --- a/pending_code_changes.txt +++ b/pending_code_changes.txt @@ -1 +1,4 @@ - +Code changes: + - Introduced new function parseWithOptions to customize the parser's behaviour + - Deprecated parseAndKeepRawInput in favor of parseWithOptions + - \ No newline at end of file