diff --git a/cpp/src/phonenumberutil.cc b/cpp/src/phonenumberutil.cc index dde9a1c72..a5b28ddf4 100644 --- a/cpp/src/phonenumberutil.cc +++ b/cpp/src/phonenumberutil.cc @@ -44,6 +44,7 @@ #include "phonenumber.pb.h" #include "regexp_adapter.h" #include "regexp_cache.h" +#include "region_code.h" #include "stl_util.h" #include "stringutil.h" #include "utf/unicodetext.h" @@ -1243,7 +1244,8 @@ void PhoneNumberUtil::GetRegionCodeForCountryCode( list region_codes; GetRegionCodesForCountryCallingCode(country_calling_code, ®ion_codes); - *region_code = (region_codes.size() > 0) ? region_codes.front() : "ZZ"; + *region_code = (region_codes.size() > 0) + ? region_codes.front() : RegionCode::GetUnknown(); } void PhoneNumberUtil::GetRegionCodeForNumber(const PhoneNumber& number, @@ -1257,7 +1259,7 @@ void PhoneNumberUtil::GetRegionCodeForNumber(const PhoneNumber& number, GetNationalSignificantNumber(number, &number_string); logger->Warning(string("Missing/invalid country code (") + SimpleItoa(country_calling_code) + ") for number " + number_string); - *region_code = "ZZ"; + *region_code = RegionCode::GetUnknown(); return; } if (region_codes.size() == 1) { @@ -1289,7 +1291,7 @@ void PhoneNumberUtil::GetRegionCodeForNumberFromRegionList( return; } } - *region_code = "ZZ"; + *region_code = RegionCode::GetUnknown(); } int PhoneNumberUtil::GetCountryCodeForRegion(const string& region_code) const { @@ -1935,7 +1937,7 @@ int PhoneNumberUtil::ExtractCountryCode(string* national_number) const { safe_strto32(national_number->substr(0, i), &potential_country_code); string region_code; GetRegionCodeForCountryCode(potential_country_code, ®ion_code); - if (region_code != "ZZ") { + if (region_code != RegionCode::GetUnknown()) { national_number->erase(0, i); return potential_country_code; } @@ -2102,23 +2104,23 @@ PhoneNumberUtil::MatchType PhoneNumberUtil::IsNumberMatchWithTwoStrings( const string& second_number) const { PhoneNumber first_number_as_proto; ErrorType error_type = - Parse(first_number, "ZZ", &first_number_as_proto); + Parse(first_number, RegionCode::GetUnknown(), &first_number_as_proto); if (error_type == NO_PARSING_ERROR) { return IsNumberMatchWithOneString(first_number_as_proto, second_number); } if (error_type == INVALID_COUNTRY_CODE_ERROR) { PhoneNumber second_number_as_proto; - ErrorType error_type = Parse(second_number, "ZZ", + ErrorType error_type = Parse(second_number, RegionCode::GetUnknown(), &second_number_as_proto); if (error_type == NO_PARSING_ERROR) { return IsNumberMatchWithOneString(second_number_as_proto, first_number); } if (error_type == INVALID_COUNTRY_CODE_ERROR) { - error_type = ParseHelper(first_number, "ZZ", false, false, - &first_number_as_proto); + error_type = ParseHelper(first_number, RegionCode::GetUnknown(), false, + false, &first_number_as_proto); if (error_type == NO_PARSING_ERROR) { - error_type = ParseHelper(second_number, "ZZ", false, false, - &second_number_as_proto); + error_type = ParseHelper(second_number, RegionCode::GetUnknown(), false, + false, &second_number_as_proto); if (error_type == NO_PARSING_ERROR) { return IsNumberMatch(first_number_as_proto, second_number_as_proto); } @@ -2137,7 +2139,7 @@ PhoneNumberUtil::MatchType PhoneNumberUtil::IsNumberMatchWithOneString( // attempting to parse it. PhoneNumber second_number_as_proto; ErrorType error_type = - Parse(second_number, "ZZ", &second_number_as_proto); + Parse(second_number, RegionCode::GetUnknown(), &second_number_as_proto); if (error_type == NO_PARSING_ERROR) { return IsNumberMatch(first_number, second_number_as_proto); } @@ -2149,7 +2151,7 @@ PhoneNumberUtil::MatchType PhoneNumberUtil::IsNumberMatchWithOneString( string first_number_region; GetRegionCodeForCountryCode(first_number.country_code(), &first_number_region); - if (first_number_region != "ZZ") { + if (first_number_region != RegionCode::GetUnknown()) { PhoneNumber second_number_with_first_number_region; Parse(second_number, first_number_region, &second_number_with_first_number_region); @@ -2162,8 +2164,8 @@ PhoneNumberUtil::MatchType PhoneNumberUtil::IsNumberMatchWithOneString( } else { // If the first number didn't have a valid country calling code, then we // parse the second number without one as well. - error_type = ParseHelper(second_number, "ZZ", false, false, - &second_number_as_proto); + error_type = ParseHelper(second_number, RegionCode::GetUnknown(), false, + false, &second_number_as_proto); if (error_type == NO_PARSING_ERROR) { return IsNumberMatch(first_number, second_number_as_proto); } diff --git a/cpp/src/phonenumberutil_test.cc b/cpp/src/phonenumberutil_test.cc index 969f4ae52..bd1b73d66 100644 --- a/cpp/src/phonenumberutil_test.cc +++ b/cpp/src/phonenumberutil_test.cc @@ -38,7 +38,9 @@ using google::protobuf::RepeatedPtrField; namespace { -// Class containing string constants of region codes for easier testing. +// Class containing string constants of region codes for easier testing. This is +// intended to replace region_code.h for testing in this file, with more +// constants defined. class RegionCode { public: static const string& AD() { @@ -131,8 +133,8 @@ class RegionCode { return s; } - // Official code for the unknown region. - static const string& ZZ() { + // Returns a region code string representing the "unknown" region. + static const string& GetUnknown() { static const string s = "ZZ"; return s; } @@ -1626,7 +1628,7 @@ TEST_F(PhoneNumberUtilTest, IsUnknown) { TEST_F(PhoneNumberUtilTest, GetCountryCodeForRegion) { EXPECT_EQ(1, phone_util_.GetCountryCodeForRegion(RegionCode::US())); EXPECT_EQ(64, phone_util_.GetCountryCodeForRegion(RegionCode::NZ())); - EXPECT_EQ(0, phone_util_.GetCountryCodeForRegion(RegionCode::ZZ())); + EXPECT_EQ(0, phone_util_.GetCountryCodeForRegion(RegionCode::GetUnknown())); // CS is already deprecated so the library doesn't support it. EXPECT_EQ(0, phone_util_.GetCountryCodeForRegion(RegionCode::CS())); } @@ -1652,7 +1654,7 @@ TEST_F(PhoneNumberUtilTest, GetNationalDiallingPrefixForRegion) { EXPECT_EQ("00", ndd_prefix); // Test cases with invalid regions. - GetNddPrefixForRegion(RegionCode::ZZ(), false, &ndd_prefix); + GetNddPrefixForRegion(RegionCode::GetUnknown(), false, &ndd_prefix); EXPECT_EQ("", ndd_prefix); // CS is already deprecated so the library doesn't support it. @@ -2588,7 +2590,7 @@ TEST_F(PhoneNumberUtilTest, FailedParseOnInvalidNumbers) { EXPECT_EQ(PhoneNumber::default_instance(), test_number); EXPECT_EQ(PhoneNumberUtil::INVALID_COUNTRY_CODE_ERROR, - phone_util_.Parse("123 456 7890", RegionCode::ZZ(), + phone_util_.Parse("123 456 7890", RegionCode::GetUnknown(), &test_number)); EXPECT_EQ(PhoneNumber::default_instance(), test_number); @@ -2624,19 +2626,19 @@ TEST_F(PhoneNumberUtilTest, ParseNumbersWithPlusWithNoRegion) { // code can be calculated. PhoneNumber result_proto; EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.Parse("+64 3 331 6005", RegionCode::ZZ(), + phone_util_.Parse("+64 3 331 6005", RegionCode::GetUnknown(), &result_proto)); EXPECT_EQ(nz_number, result_proto); // Test with full-width plus. result_proto.Clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.Parse("+64 3 331 6005", RegionCode::ZZ(), + phone_util_.Parse("+64 3 331 6005", RegionCode::GetUnknown(), &result_proto)); EXPECT_EQ(nz_number, result_proto); // Test with normal plus but leading characters that need to be stripped. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.Parse(" +64 3 331 6005", RegionCode::ZZ(), + phone_util_.Parse(" +64 3 331 6005", RegionCode::GetUnknown(), &result_proto)); EXPECT_EQ(nz_number, result_proto); @@ -2647,7 +2649,8 @@ TEST_F(PhoneNumberUtilTest, ParseNumbersWithPlusWithNoRegion) { nz_number.set_preferred_domestic_carrier_code(""); result_proto.Clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("+64 3 331 6005", RegionCode::ZZ(), + phone_util_.ParseAndKeepRawInput("+64 3 331 6005", + RegionCode::GetUnknown(), &result_proto)); EXPECT_EQ(nz_number, result_proto); } diff --git a/cpp/src/region_code.h b/cpp/src/region_code.h new file mode 100644 index 000000000..651156f18 --- /dev/null +++ b/cpp/src/region_code.h @@ -0,0 +1,37 @@ +// Copyright (C) 2011 Google Inc. +// +// 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. + +#ifndef I18N_PHONENUMBERS_REGION_CODE_H_ +#define I18N_PHONENUMBERS_REGION_CODE_H_ + +#include + +namespace i18n { +namespace phonenumbers { + +using std::string; + +class RegionCode { + public: + // Returns a region code string representing the "unknown" region. + static const string& GetUnknown() { + static const string s = "ZZ"; + return s; + } +}; + +} // namespace phonenumbers +} // namespace i18n + +#endif // I18N_PHONENUMBERS_REGION_CODE_H_