From 3f34988b151c5373e154844447c8cb14c1f092cd Mon Sep 17 00:00:00 2001 From: Philip Liard Date: Tue, 26 Apr 2011 09:14:14 +0000 Subject: [PATCH] CPP: Adding GetNddPrefixForRegion for libphonenumber. --- cpp/src/phonenumberutil.cc | 18 +++++++++++++++- cpp/src/phonenumberutil.h | 12 +++++++++++ cpp/src/phonenumberutil_test.cc | 37 +++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/cpp/src/phonenumberutil.cc b/cpp/src/phonenumberutil.cc index 9b004cf28..f57ee0c60 100644 --- a/cpp/src/phonenumberutil.cc +++ b/cpp/src/phonenumberutil.cc @@ -777,6 +777,23 @@ void PhoneNumberUtil::GetSupportedRegions(set* regions) const { } } +void PhoneNumberUtil::GetNddPrefixForRegion(const string& region_code, + bool strip_non_digits, + string* national_prefix) const { + DCHECK(national_prefix); + if (!IsValidRegionCode(region_code)) { + logger->Error("Invalid region code provided."); + return; + } + const PhoneMetadata* metadata = GetMetadataForRegion(region_code); + national_prefix->assign(metadata->national_prefix()); + if (strip_non_digits) { + // Note: if any other non-numeric symbols are ever used in national + // prefixes, these would have to be removed here as well. + strrmm(national_prefix, "~"); + } +} + bool PhoneNumberUtil::IsValidRegionCode(const string& region_code) const { return (region_to_metadata_map_->find(region_code) != region_to_metadata_map_->end()); @@ -951,7 +968,6 @@ void PhoneNumberUtil::FormatOutOfCountryCallingNumber( const string& calling_from, string* formatted_number) const { DCHECK(formatted_number); - if (!IsValidRegionCode(calling_from)) { logger->Info("Trying to format number from invalid region. International" " formatting applied."); diff --git a/cpp/src/phonenumberutil.h b/cpp/src/phonenumberutil.h index 95ed7fd8c..6a766df17 100644 --- a/cpp/src/phonenumberutil.h +++ b/cpp/src/phonenumberutil.h @@ -564,6 +564,18 @@ class PhoneNumberUtil { // Gets all the supported regions. void GetSupportedRegions(set* regions) const; + // Returns the national dialling prefix for a specific region. For example, + // this would be 1 for the United States, and 0 for New Zealand. Set + // stripNonDigits to true to strip symbols like "~" (which indicates a wait + // for a dialling tone) from the prefix returned. If no national prefix is + // present, we return an empty string. + // + // Set strip_non_digits to true to strip non-digits from the national + // dialling prefix. + void GetNddPrefixForRegion(const string& region_code, + bool strip_non_digits, + string* national_prefix) const; + // Helper function to check region code is not unknown or null. // // The region_code parameter is an ISO 3166-1 two-letter country code string. diff --git a/cpp/src/phonenumberutil_test.cc b/cpp/src/phonenumberutil_test.cc index a3781b0d6..560a8e39b 100644 --- a/cpp/src/phonenumberutil_test.cc +++ b/cpp/src/phonenumberutil_test.cc @@ -211,6 +211,14 @@ class PhoneNumberUtilTest : public testing::Test { phone_number); } + void GetNddPrefixForRegion(const string& region, + bool strip_non_digits, + string* ndd_prefix) const { + // For testing purposes, we check this is empty first. + ndd_prefix->clear(); + phone_util_.GetNddPrefixForRegion(region, strip_non_digits, ndd_prefix); + } + static bool Equals(const PhoneNumberDesc& expected_number, const PhoneNumberDesc& actual_number) { return ExactlySameAs(expected_number, actual_number); @@ -1581,6 +1589,35 @@ TEST_F(PhoneNumberUtilTest, GetCountryCodeForRegion) { EXPECT_EQ(0, phone_util_.GetCountryCodeForRegion(RegionCode::CS())); } +TEST_F(PhoneNumberUtilTest, GetNationalDiallingPrefixForRegion) { + string ndd_prefix; + GetNddPrefixForRegion(RegionCode::US(), false, &ndd_prefix); + EXPECT_EQ("1", ndd_prefix); + + // Test non-main country to see it gets the national dialling prefix for the + // main country with that country calling code. + GetNddPrefixForRegion(RegionCode::BS(), false, &ndd_prefix); + EXPECT_EQ("1", ndd_prefix); + + GetNddPrefixForRegion(RegionCode::NZ(), false, &ndd_prefix); + EXPECT_EQ("0", ndd_prefix); + + // Test case with non digit in the national prefix. + GetNddPrefixForRegion(RegionCode::AO(), false, &ndd_prefix); + EXPECT_EQ("0~0", ndd_prefix); + + GetNddPrefixForRegion(RegionCode::AO(), true, &ndd_prefix); + EXPECT_EQ("00", ndd_prefix); + + // Test cases with invalid regions. + GetNddPrefixForRegion(RegionCode::ZZ(), false, &ndd_prefix); + EXPECT_EQ("", ndd_prefix); + + // CS is already deprecated so the library doesn't support it. + GetNddPrefixForRegion(RegionCode::CS(), false, &ndd_prefix); + EXPECT_EQ("", ndd_prefix); +} + TEST_F(PhoneNumberUtilTest, IsViablePhoneNumber) { // Only one or two digits before strange non-possible punctuation. EXPECT_FALSE(IsViablePhoneNumber("12. March"));