Browse Source

CPP: Adding GetNddPrefixForRegion for libphonenumber.

pull/567/head
Philip Liard 15 years ago
committed by Mihaela Rosca
parent
commit
3f34988b15
3 changed files with 66 additions and 1 deletions
  1. +17
    -1
      cpp/src/phonenumberutil.cc
  2. +12
    -0
      cpp/src/phonenumberutil.h
  3. +37
    -0
      cpp/src/phonenumberutil_test.cc

+ 17
- 1
cpp/src/phonenumberutil.cc View File

@ -777,6 +777,23 @@ void PhoneNumberUtil::GetSupportedRegions(set<string>* 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.");


+ 12
- 0
cpp/src/phonenumberutil.h View File

@ -564,6 +564,18 @@ class PhoneNumberUtil {
// Gets all the supported regions.
void GetSupportedRegions(set<string>* 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.


+ 37
- 0
cpp/src/phonenumberutil_test.cc View File

@ -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"));


Loading…
Cancel
Save