Browse Source

Merge d9d2ef0cbb into cf446565d9

pull/3730/merge
KarolJakubKrawiec 4 days ago
committed by GitHub
parent
commit
b08f5f608e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
17 changed files with 864101 additions and 95 deletions
  1. +1
    -0
      cpp/CMakeLists.txt
  2. +855021
    -0
      cpp/src/phonenumbers/geocoding/geocoding_data.cc
  3. +32
    -0
      cpp/src/phonenumbers/parsingoptions.cc
  4. +56
    -0
      cpp/src/phonenumbers/parsingoptions.h
  5. +2678
    -0
      cpp/src/phonenumbers/phonemetadata.pb.cc
  6. +4360
    -0
      cpp/src/phonenumbers/phonemetadata.pb.h
  7. +639
    -0
      cpp/src/phonenumbers/phonenumber.pb.cc
  8. +751
    -0
      cpp/src/phonenumbers/phonenumber.pb.h
  9. +10
    -6
      cpp/src/phonenumbers/phonenumberutil.cc
  10. +31
    -4
      cpp/src/phonenumbers/phonenumberutil.h
  11. +1
    -0
      cpp/src/phonenumbers/region_code.h
  12. +285
    -0
      cpp/test/phonenumbers/geocoding/geocoding_test_data.cc
  13. +66
    -44
      cpp/test/phonenumbers/phonenumberutil_test.cc
  14. +66
    -0
      java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java
  15. +67
    -6
      java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
  16. +33
    -34
      java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
  17. +4
    -1
      pending_code_changes.txt

+ 1
- 0
cpp/CMakeLists.txt View File

@ -267,6 +267,7 @@ set (
"src/phonenumbers/base/strings/string_piece.cc"
"src/phonenumbers/default_logger.cc"
"src/phonenumbers/logger.cc"
"src/phonenumbers/parsingoptions.cc"
"src/phonenumbers/phonemetadata.pb.cc" # Generated by Protocol Buffers.
"src/phonenumbers/phonenumber.cc"
"src/phonenumbers/phonenumber.pb.cc" # Generated by Protocol Buffers.


+ 855021
- 0
cpp/src/phonenumbers/geocoding/geocoding_data.cc
File diff suppressed because it is too large
View File


+ 32
- 0
cpp/src/phonenumbers/parsingoptions.cc View File

@ -0,0 +1,32 @@
// Copyright (C) 2009 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.
#include "phonenumbers/parsingoptions.h"
namespace i18n {
namespace phonenumbers {
ParsingOptions& ParsingOptions::SetDefaultRegion(
const std::string& 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

+ 56
- 0
cpp/src/phonenumbers/parsingoptions.h View File

@ -0,0 +1,56 @@
// Copyright (C) 2009 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.
#ifndef I18N_PHONENUMBERS_PARSINGOPTIONS_H_
#define I18N_PHONENUMBERS_PARSINGOPTIONS_H_
#include <string>
namespace i18n {
namespace phonenumbers {
// Options for parsing a phone number. To be used with the ParseWithOptions
// method.
// Example:
// ParsingOptions().SetDefaultRegion("US").SetKeepRawInput(true);
class ParsingOptions {
public:
ParsingOptions() : default_region_("ZZ"), keep_raw_input_(false) {};
// Set the value for default_region_.
ParsingOptions& SetDefaultRegion(
const std::string& 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 "ZZ" or null can be supplied.
std::string default_region_;
// 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_;
};
} // namespace phonenumbers
} // namespace i18n
#endif // I18N_PHONENUMBERS_PARSINGOPTIONS_H_

+ 2678
- 0
cpp/src/phonenumbers/phonemetadata.pb.cc
File diff suppressed because it is too large
View File


+ 4360
- 0
cpp/src/phonenumbers/phonemetadata.pb.h
File diff suppressed because it is too large
View File


+ 639
- 0
cpp/src/phonenumbers/phonenumber.pb.cc View File

@ -0,0 +1,639 @@
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: phonenumber.proto
#include "phonenumber.pb.h"
#include <algorithm>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/extension_set.h>
#include <google/protobuf/wire_format_lite.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
// @@protoc_insertion_point(includes)
#include <google/protobuf/port_def.inc>
PROTOBUF_PRAGMA_INIT_SEG
namespace _pb = ::PROTOBUF_NAMESPACE_ID;
namespace _pbi = _pb::internal;
namespace i18n {
namespace phonenumbers {
PROTOBUF_CONSTEXPR PhoneNumber::PhoneNumber(
::_pbi::ConstantInitialized): _impl_{
/*decltype(_impl_._has_bits_)*/{}
, /*decltype(_impl_._cached_size_)*/{}
, /*decltype(_impl_.extension_)*/{&::_pbi::fixed_address_empty_string, ::_pbi::ConstantInitialized{}}
, /*decltype(_impl_.raw_input_)*/{&::_pbi::fixed_address_empty_string, ::_pbi::ConstantInitialized{}}
, /*decltype(_impl_.preferred_domestic_carrier_code_)*/{&::_pbi::fixed_address_empty_string, ::_pbi::ConstantInitialized{}}
, /*decltype(_impl_.national_number_)*/uint64_t{0u}
, /*decltype(_impl_.country_code_)*/0
, /*decltype(_impl_.italian_leading_zero_)*/false
, /*decltype(_impl_.country_code_source_)*/0
, /*decltype(_impl_.number_of_leading_zeros_)*/1} {}
struct PhoneNumberDefaultTypeInternal {
PROTOBUF_CONSTEXPR PhoneNumberDefaultTypeInternal()
: _instance(::_pbi::ConstantInitialized{}) {}
~PhoneNumberDefaultTypeInternal() {}
union {
PhoneNumber _instance;
};
};
PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 PhoneNumberDefaultTypeInternal _PhoneNumber_default_instance_;
} // namespace phonenumbers
} // namespace i18n
namespace i18n {
namespace phonenumbers {
bool PhoneNumber_CountryCodeSource_IsValid(int value) {
switch (value) {
case 0:
case 1:
case 5:
case 10:
case 20:
return true;
default:
return false;
}
}
static ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed<std::string> PhoneNumber_CountryCodeSource_strings[5] = {};
static const char PhoneNumber_CountryCodeSource_names[] =
"FROM_DEFAULT_COUNTRY"
"FROM_NUMBER_WITHOUT_PLUS_SIGN"
"FROM_NUMBER_WITH_IDD"
"FROM_NUMBER_WITH_PLUS_SIGN"
"UNSPECIFIED";
static const ::PROTOBUF_NAMESPACE_ID::internal::EnumEntry PhoneNumber_CountryCodeSource_entries[] = {
{ {PhoneNumber_CountryCodeSource_names + 0, 20}, 20 },
{ {PhoneNumber_CountryCodeSource_names + 20, 29}, 10 },
{ {PhoneNumber_CountryCodeSource_names + 49, 20}, 5 },
{ {PhoneNumber_CountryCodeSource_names + 69, 26}, 1 },
{ {PhoneNumber_CountryCodeSource_names + 95, 11}, 0 },
};
static const int PhoneNumber_CountryCodeSource_entries_by_number[] = {
4, // 0 -> UNSPECIFIED
3, // 1 -> FROM_NUMBER_WITH_PLUS_SIGN
2, // 5 -> FROM_NUMBER_WITH_IDD
1, // 10 -> FROM_NUMBER_WITHOUT_PLUS_SIGN
0, // 20 -> FROM_DEFAULT_COUNTRY
};
const std::string& PhoneNumber_CountryCodeSource_Name(
PhoneNumber_CountryCodeSource value) {
static const bool dummy =
::PROTOBUF_NAMESPACE_ID::internal::InitializeEnumStrings(
PhoneNumber_CountryCodeSource_entries,
PhoneNumber_CountryCodeSource_entries_by_number,
5, PhoneNumber_CountryCodeSource_strings);
(void) dummy;
int idx = ::PROTOBUF_NAMESPACE_ID::internal::LookUpEnumName(
PhoneNumber_CountryCodeSource_entries,
PhoneNumber_CountryCodeSource_entries_by_number,
5, value);
return idx == -1 ? ::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString() :
PhoneNumber_CountryCodeSource_strings[idx].get();
}
bool PhoneNumber_CountryCodeSource_Parse(
::PROTOBUF_NAMESPACE_ID::ConstStringParam name, PhoneNumber_CountryCodeSource* value) {
int int_value;
bool success = ::PROTOBUF_NAMESPACE_ID::internal::LookUpEnumValue(
PhoneNumber_CountryCodeSource_entries, 5, name, &int_value);
if (success) {
*value = static_cast<PhoneNumber_CountryCodeSource>(int_value);
}
return success;
}
#if (__cplusplus < 201703) && (!defined(_MSC_VER) || (_MSC_VER >= 1900 && _MSC_VER < 1912))
constexpr PhoneNumber_CountryCodeSource PhoneNumber::UNSPECIFIED;
constexpr PhoneNumber_CountryCodeSource PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN;
constexpr PhoneNumber_CountryCodeSource PhoneNumber::FROM_NUMBER_WITH_IDD;
constexpr PhoneNumber_CountryCodeSource PhoneNumber::FROM_NUMBER_WITHOUT_PLUS_SIGN;
constexpr PhoneNumber_CountryCodeSource PhoneNumber::FROM_DEFAULT_COUNTRY;
constexpr PhoneNumber_CountryCodeSource PhoneNumber::CountryCodeSource_MIN;
constexpr PhoneNumber_CountryCodeSource PhoneNumber::CountryCodeSource_MAX;
constexpr int PhoneNumber::CountryCodeSource_ARRAYSIZE;
#endif // (__cplusplus < 201703) && (!defined(_MSC_VER) || (_MSC_VER >= 1900 && _MSC_VER < 1912))
// ===================================================================
class PhoneNumber::_Internal {
public:
using HasBits = decltype(std::declval<PhoneNumber>()._impl_._has_bits_);
static void set_has_country_code(HasBits* has_bits) {
(*has_bits)[0] |= 16u;
}
static void set_has_national_number(HasBits* has_bits) {
(*has_bits)[0] |= 8u;
}
static void set_has_extension(HasBits* has_bits) {
(*has_bits)[0] |= 1u;
}
static void set_has_italian_leading_zero(HasBits* has_bits) {
(*has_bits)[0] |= 32u;
}
static void set_has_number_of_leading_zeros(HasBits* has_bits) {
(*has_bits)[0] |= 128u;
}
static void set_has_raw_input(HasBits* has_bits) {
(*has_bits)[0] |= 2u;
}
static void set_has_country_code_source(HasBits* has_bits) {
(*has_bits)[0] |= 64u;
}
static void set_has_preferred_domestic_carrier_code(HasBits* has_bits) {
(*has_bits)[0] |= 4u;
}
static bool MissingRequiredFields(const HasBits& has_bits) {
return ((has_bits[0] & 0x00000018) ^ 0x00000018) != 0;
}
};
PhoneNumber::PhoneNumber(::PROTOBUF_NAMESPACE_ID::Arena* arena,
bool is_message_owned)
: ::PROTOBUF_NAMESPACE_ID::MessageLite(arena, is_message_owned) {
SharedCtor(arena, is_message_owned);
// @@protoc_insertion_point(arena_constructor:i18n.phonenumbers.PhoneNumber)
}
PhoneNumber::PhoneNumber(const PhoneNumber& from)
: ::PROTOBUF_NAMESPACE_ID::MessageLite() {
PhoneNumber* const _this = this; (void)_this;
new (&_impl_) Impl_{
decltype(_impl_._has_bits_){from._impl_._has_bits_}
, /*decltype(_impl_._cached_size_)*/{}
, decltype(_impl_.extension_){}
, decltype(_impl_.raw_input_){}
, decltype(_impl_.preferred_domestic_carrier_code_){}
, decltype(_impl_.national_number_){}
, decltype(_impl_.country_code_){}
, decltype(_impl_.italian_leading_zero_){}
, decltype(_impl_.country_code_source_){}
, decltype(_impl_.number_of_leading_zeros_){}};
_internal_metadata_.MergeFrom<std::string>(from._internal_metadata_);
_impl_.extension_.InitDefault();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
_impl_.extension_.Set("", GetArenaForAllocation());
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (from._internal_has_extension()) {
_this->_impl_.extension_.Set(from._internal_extension(),
_this->GetArenaForAllocation());
}
_impl_.raw_input_.InitDefault();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
_impl_.raw_input_.Set("", GetArenaForAllocation());
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (from._internal_has_raw_input()) {
_this->_impl_.raw_input_.Set(from._internal_raw_input(),
_this->GetArenaForAllocation());
}
_impl_.preferred_domestic_carrier_code_.InitDefault();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
_impl_.preferred_domestic_carrier_code_.Set("", GetArenaForAllocation());
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (from._internal_has_preferred_domestic_carrier_code()) {
_this->_impl_.preferred_domestic_carrier_code_.Set(from._internal_preferred_domestic_carrier_code(),
_this->GetArenaForAllocation());
}
::memcpy(&_impl_.national_number_, &from._impl_.national_number_,
static_cast<size_t>(reinterpret_cast<char*>(&_impl_.number_of_leading_zeros_) -
reinterpret_cast<char*>(&_impl_.national_number_)) + sizeof(_impl_.number_of_leading_zeros_));
// @@protoc_insertion_point(copy_constructor:i18n.phonenumbers.PhoneNumber)
}
inline void PhoneNumber::SharedCtor(
::_pb::Arena* arena, bool is_message_owned) {
(void)arena;
(void)is_message_owned;
new (&_impl_) Impl_{
decltype(_impl_._has_bits_){}
, /*decltype(_impl_._cached_size_)*/{}
, decltype(_impl_.extension_){}
, decltype(_impl_.raw_input_){}
, decltype(_impl_.preferred_domestic_carrier_code_){}
, decltype(_impl_.national_number_){uint64_t{0u}}
, decltype(_impl_.country_code_){0}
, decltype(_impl_.italian_leading_zero_){false}
, decltype(_impl_.country_code_source_){0}
, decltype(_impl_.number_of_leading_zeros_){1}
};
_impl_.extension_.InitDefault();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
_impl_.extension_.Set("", GetArenaForAllocation());
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
_impl_.raw_input_.InitDefault();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
_impl_.raw_input_.Set("", GetArenaForAllocation());
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
_impl_.preferred_domestic_carrier_code_.InitDefault();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
_impl_.preferred_domestic_carrier_code_.Set("", GetArenaForAllocation());
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
}
PhoneNumber::~PhoneNumber() {
// @@protoc_insertion_point(destructor:i18n.phonenumbers.PhoneNumber)
if (auto *arena = _internal_metadata_.DeleteReturnArena<std::string>()) {
(void)arena;
return;
}
SharedDtor();
}
inline void PhoneNumber::SharedDtor() {
GOOGLE_DCHECK(GetArenaForAllocation() == nullptr);
_impl_.extension_.Destroy();
_impl_.raw_input_.Destroy();
_impl_.preferred_domestic_carrier_code_.Destroy();
}
void PhoneNumber::SetCachedSize(int size) const {
_impl_._cached_size_.Set(size);
}
void PhoneNumber::Clear() {
// @@protoc_insertion_point(message_clear_start:i18n.phonenumbers.PhoneNumber)
uint32_t cached_has_bits = 0;
// Prevent compiler warnings about cached_has_bits being unused
(void) cached_has_bits;
cached_has_bits = _impl_._has_bits_[0];
if (cached_has_bits & 0x00000007u) {
if (cached_has_bits & 0x00000001u) {
_impl_.extension_.ClearNonDefaultToEmpty();
}
if (cached_has_bits & 0x00000002u) {
_impl_.raw_input_.ClearNonDefaultToEmpty();
}
if (cached_has_bits & 0x00000004u) {
_impl_.preferred_domestic_carrier_code_.ClearNonDefaultToEmpty();
}
}
if (cached_has_bits & 0x000000f8u) {
::memset(&_impl_.national_number_, 0, static_cast<size_t>(
reinterpret_cast<char*>(&_impl_.country_code_source_) -
reinterpret_cast<char*>(&_impl_.national_number_)) + sizeof(_impl_.country_code_source_));
_impl_.number_of_leading_zeros_ = 1;
}
_impl_._has_bits_.Clear();
_internal_metadata_.Clear<std::string>();
}
const char* PhoneNumber::_InternalParse(const char* ptr, ::_pbi::ParseContext* ctx) {
#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure
_Internal::HasBits has_bits{};
while (!ctx->Done(&ptr)) {
uint32_t tag;
ptr = ::_pbi::ReadTag(ptr, &tag);
switch (tag >> 3) {
// required int32 country_code = 1;
case 1:
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 8)) {
_Internal::set_has_country_code(&has_bits);
_impl_.country_code_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint32(&ptr);
CHK_(ptr);
} else
goto handle_unusual;
continue;
// required uint64 national_number = 2;
case 2:
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 16)) {
_Internal::set_has_national_number(&has_bits);
_impl_.national_number_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr);
CHK_(ptr);
} else
goto handle_unusual;
continue;
// optional string extension = 3;
case 3:
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 26)) {
auto str = _internal_mutable_extension();
ptr = ::_pbi::InlineGreedyStringParser(str, ptr, ctx);
CHK_(ptr);
} else
goto handle_unusual;
continue;
// optional bool italian_leading_zero = 4;
case 4:
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 32)) {
_Internal::set_has_italian_leading_zero(&has_bits);
_impl_.italian_leading_zero_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr);
CHK_(ptr);
} else
goto handle_unusual;
continue;
// optional string raw_input = 5;
case 5:
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 42)) {
auto str = _internal_mutable_raw_input();
ptr = ::_pbi::InlineGreedyStringParser(str, ptr, ctx);
CHK_(ptr);
} else
goto handle_unusual;
continue;
// optional .i18n.phonenumbers.PhoneNumber.CountryCodeSource country_code_source = 6;
case 6:
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 48)) {
uint64_t val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr);
CHK_(ptr);
if (PROTOBUF_PREDICT_TRUE(::i18n::phonenumbers::PhoneNumber_CountryCodeSource_IsValid(val))) {
_internal_set_country_code_source(static_cast<::i18n::phonenumbers::PhoneNumber_CountryCodeSource>(val));
} else {
::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(6, val, mutable_unknown_fields());
}
} else
goto handle_unusual;
continue;
// optional string preferred_domestic_carrier_code = 7;
case 7:
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 58)) {
auto str = _internal_mutable_preferred_domestic_carrier_code();
ptr = ::_pbi::InlineGreedyStringParser(str, ptr, ctx);
CHK_(ptr);
} else
goto handle_unusual;
continue;
// optional int32 number_of_leading_zeros = 8 [default = 1];
case 8:
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 64)) {
_Internal::set_has_number_of_leading_zeros(&has_bits);
_impl_.number_of_leading_zeros_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint32(&ptr);
CHK_(ptr);
} else
goto handle_unusual;
continue;
default:
goto handle_unusual;
} // switch
handle_unusual:
if ((tag == 0) || ((tag & 7) == 4)) {
CHK_(ptr);
ctx->SetLastTag(tag);
goto message_done;
}
ptr = UnknownFieldParse(
tag,
_internal_metadata_.mutable_unknown_fields<std::string>(),
ptr, ctx);
CHK_(ptr != nullptr);
} // while
message_done:
_impl_._has_bits_.Or(has_bits);
return ptr;
failure:
ptr = nullptr;
goto message_done;
#undef CHK_
}
uint8_t* PhoneNumber::_InternalSerialize(
uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const {
// @@protoc_insertion_point(serialize_to_array_start:i18n.phonenumbers.PhoneNumber)
uint32_t cached_has_bits = 0;
(void) cached_has_bits;
cached_has_bits = _impl_._has_bits_[0];
// required int32 country_code = 1;
if (cached_has_bits & 0x00000010u) {
target = stream->EnsureSpace(target);
target = ::_pbi::WireFormatLite::WriteInt32ToArray(1, this->_internal_country_code(), target);
}
// required uint64 national_number = 2;
if (cached_has_bits & 0x00000008u) {
target = stream->EnsureSpace(target);
target = ::_pbi::WireFormatLite::WriteUInt64ToArray(2, this->_internal_national_number(), target);
}
// optional string extension = 3;
if (cached_has_bits & 0x00000001u) {
target = stream->WriteStringMaybeAliased(
3, this->_internal_extension(), target);
}
// optional bool italian_leading_zero = 4;
if (cached_has_bits & 0x00000020u) {
target = stream->EnsureSpace(target);
target = ::_pbi::WireFormatLite::WriteBoolToArray(4, this->_internal_italian_leading_zero(), target);
}
// optional string raw_input = 5;
if (cached_has_bits & 0x00000002u) {
target = stream->WriteStringMaybeAliased(
5, this->_internal_raw_input(), target);
}
// optional .i18n.phonenumbers.PhoneNumber.CountryCodeSource country_code_source = 6;
if (cached_has_bits & 0x00000040u) {
target = stream->EnsureSpace(target);
target = ::_pbi::WireFormatLite::WriteEnumToArray(
6, this->_internal_country_code_source(), target);
}
// optional string preferred_domestic_carrier_code = 7;
if (cached_has_bits & 0x00000004u) {
target = stream->WriteStringMaybeAliased(
7, this->_internal_preferred_domestic_carrier_code(), target);
}
// optional int32 number_of_leading_zeros = 8 [default = 1];
if (cached_has_bits & 0x00000080u) {
target = stream->EnsureSpace(target);
target = ::_pbi::WireFormatLite::WriteInt32ToArray(8, this->_internal_number_of_leading_zeros(), target);
}
if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) {
target = stream->WriteRaw(_internal_metadata_.unknown_fields<std::string>(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(),
static_cast<int>(_internal_metadata_.unknown_fields<std::string>(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target);
}
// @@protoc_insertion_point(serialize_to_array_end:i18n.phonenumbers.PhoneNumber)
return target;
}
size_t PhoneNumber::RequiredFieldsByteSizeFallback() const {
// @@protoc_insertion_point(required_fields_byte_size_fallback_start:i18n.phonenumbers.PhoneNumber)
size_t total_size = 0;
if (_internal_has_national_number()) {
// required uint64 national_number = 2;
total_size += ::_pbi::WireFormatLite::UInt64SizePlusOne(this->_internal_national_number());
}
if (_internal_has_country_code()) {
// required int32 country_code = 1;
total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(this->_internal_country_code());
}
return total_size;
}
size_t PhoneNumber::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:i18n.phonenumbers.PhoneNumber)
size_t total_size = 0;
if (((_impl_._has_bits_[0] & 0x00000018) ^ 0x00000018) == 0) { // All required fields are present.
// required uint64 national_number = 2;
total_size += ::_pbi::WireFormatLite::UInt64SizePlusOne(this->_internal_national_number());
// required int32 country_code = 1;
total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(this->_internal_country_code());
} else {
total_size += RequiredFieldsByteSizeFallback();
}
uint32_t cached_has_bits = 0;
// Prevent compiler warnings about cached_has_bits being unused
(void) cached_has_bits;
cached_has_bits = _impl_._has_bits_[0];
if (cached_has_bits & 0x00000007u) {
// optional string extension = 3;
if (cached_has_bits & 0x00000001u) {
total_size += 1 +
::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize(
this->_internal_extension());
}
// optional string raw_input = 5;
if (cached_has_bits & 0x00000002u) {
total_size += 1 +
::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize(
this->_internal_raw_input());
}
// optional string preferred_domestic_carrier_code = 7;
if (cached_has_bits & 0x00000004u) {
total_size += 1 +
::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize(
this->_internal_preferred_domestic_carrier_code());
}
}
if (cached_has_bits & 0x000000e0u) {
// optional bool italian_leading_zero = 4;
if (cached_has_bits & 0x00000020u) {
total_size += 1 + 1;
}
// optional .i18n.phonenumbers.PhoneNumber.CountryCodeSource country_code_source = 6;
if (cached_has_bits & 0x00000040u) {
total_size += 1 +
::_pbi::WireFormatLite::EnumSize(this->_internal_country_code_source());
}
// optional int32 number_of_leading_zeros = 8 [default = 1];
if (cached_has_bits & 0x00000080u) {
total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(this->_internal_number_of_leading_zeros());
}
}
if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) {
total_size += _internal_metadata_.unknown_fields<std::string>(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size();
}
int cached_size = ::_pbi::ToCachedSize(total_size);
SetCachedSize(cached_size);
return total_size;
}
void PhoneNumber::CheckTypeAndMergeFrom(
const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) {
MergeFrom(*::_pbi::DownCast<const PhoneNumber*>(
&from));
}
void PhoneNumber::MergeFrom(const PhoneNumber& from) {
PhoneNumber* const _this = this;
// @@protoc_insertion_point(class_specific_merge_from_start:i18n.phonenumbers.PhoneNumber)
GOOGLE_DCHECK_NE(&from, _this);
uint32_t cached_has_bits = 0;
(void) cached_has_bits;
cached_has_bits = from._impl_._has_bits_[0];
if (cached_has_bits & 0x000000ffu) {
if (cached_has_bits & 0x00000001u) {
_this->_internal_set_extension(from._internal_extension());
}
if (cached_has_bits & 0x00000002u) {
_this->_internal_set_raw_input(from._internal_raw_input());
}
if (cached_has_bits & 0x00000004u) {
_this->_internal_set_preferred_domestic_carrier_code(from._internal_preferred_domestic_carrier_code());
}
if (cached_has_bits & 0x00000008u) {
_this->_impl_.national_number_ = from._impl_.national_number_;
}
if (cached_has_bits & 0x00000010u) {
_this->_impl_.country_code_ = from._impl_.country_code_;
}
if (cached_has_bits & 0x00000020u) {
_this->_impl_.italian_leading_zero_ = from._impl_.italian_leading_zero_;
}
if (cached_has_bits & 0x00000040u) {
_this->_impl_.country_code_source_ = from._impl_.country_code_source_;
}
if (cached_has_bits & 0x00000080u) {
_this->_impl_.number_of_leading_zeros_ = from._impl_.number_of_leading_zeros_;
}
_this->_impl_._has_bits_[0] |= cached_has_bits;
}
_this->_internal_metadata_.MergeFrom<std::string>(from._internal_metadata_);
}
void PhoneNumber::CopyFrom(const PhoneNumber& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:i18n.phonenumbers.PhoneNumber)
if (&from == this) return;
Clear();
MergeFrom(from);
}
bool PhoneNumber::IsInitialized() const {
if (_Internal::MissingRequiredFields(_impl_._has_bits_)) return false;
return true;
}
void PhoneNumber::InternalSwap(PhoneNumber* other) {
using std::swap;
auto* lhs_arena = GetArenaForAllocation();
auto* rhs_arena = other->GetArenaForAllocation();
_internal_metadata_.InternalSwap(&other->_internal_metadata_);
swap(_impl_._has_bits_[0], other->_impl_._has_bits_[0]);
::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::InternalSwap(
&_impl_.extension_, lhs_arena,
&other->_impl_.extension_, rhs_arena
);
::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::InternalSwap(
&_impl_.raw_input_, lhs_arena,
&other->_impl_.raw_input_, rhs_arena
);
::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::InternalSwap(
&_impl_.preferred_domestic_carrier_code_, lhs_arena,
&other->_impl_.preferred_domestic_carrier_code_, rhs_arena
);
::PROTOBUF_NAMESPACE_ID::internal::memswap<
PROTOBUF_FIELD_OFFSET(PhoneNumber, _impl_.country_code_source_)
+ sizeof(PhoneNumber::_impl_.country_code_source_)
- PROTOBUF_FIELD_OFFSET(PhoneNumber, _impl_.national_number_)>(
reinterpret_cast<char*>(&_impl_.national_number_),
reinterpret_cast<char*>(&other->_impl_.national_number_));
swap(_impl_.number_of_leading_zeros_, other->_impl_.number_of_leading_zeros_);
}
std::string PhoneNumber::GetTypeName() const {
return "i18n.phonenumbers.PhoneNumber";
}
// @@protoc_insertion_point(namespace_scope)
} // namespace phonenumbers
} // namespace i18n
PROTOBUF_NAMESPACE_OPEN
template<> PROTOBUF_NOINLINE ::i18n::phonenumbers::PhoneNumber*
Arena::CreateMaybeMessage< ::i18n::phonenumbers::PhoneNumber >(Arena* arena) {
return Arena::CreateMessageInternal< ::i18n::phonenumbers::PhoneNumber >(arena);
}
PROTOBUF_NAMESPACE_CLOSE
// @@protoc_insertion_point(global_scope)
#include <google/protobuf/port_undef.inc>

+ 751
- 0
cpp/src/phonenumbers/phonenumber.pb.h View File

@ -0,0 +1,751 @@
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: phonenumber.proto
#ifndef GOOGLE_PROTOBUF_INCLUDED_phonenumber_2eproto
#define GOOGLE_PROTOBUF_INCLUDED_phonenumber_2eproto
#include <limits>
#include <string>
#include <google/protobuf/port_def.inc>
#if PROTOBUF_VERSION < 3021000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.
#endif
#if 3021012 < PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
#endif
#include <google/protobuf/port_undef.inc>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/arena.h>
#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/metadata_lite.h>
#include <google/protobuf/message_lite.h>
#include <google/protobuf/repeated_field.h> // IWYU pragma: export
#include <google/protobuf/extension_set.h> // IWYU pragma: export
#include <google/protobuf/generated_enum_util.h>
// @@protoc_insertion_point(includes)
#include <google/protobuf/port_def.inc>
#define PROTOBUF_INTERNAL_EXPORT_phonenumber_2eproto
PROTOBUF_NAMESPACE_OPEN
namespace internal {
class AnyMetadata;
} // namespace internal
PROTOBUF_NAMESPACE_CLOSE
// Internal implementation detail -- do not use these members.
struct TableStruct_phonenumber_2eproto {
static const uint32_t offsets[];
};
namespace i18n {
namespace phonenumbers {
class PhoneNumber;
struct PhoneNumberDefaultTypeInternal;
extern PhoneNumberDefaultTypeInternal _PhoneNumber_default_instance_;
} // namespace phonenumbers
} // namespace i18n
PROTOBUF_NAMESPACE_OPEN
template<> ::i18n::phonenumbers::PhoneNumber* Arena::CreateMaybeMessage<::i18n::phonenumbers::PhoneNumber>(Arena*);
PROTOBUF_NAMESPACE_CLOSE
namespace i18n {
namespace phonenumbers {
enum PhoneNumber_CountryCodeSource : int {
PhoneNumber_CountryCodeSource_UNSPECIFIED = 0,
PhoneNumber_CountryCodeSource_FROM_NUMBER_WITH_PLUS_SIGN = 1,
PhoneNumber_CountryCodeSource_FROM_NUMBER_WITH_IDD = 5,
PhoneNumber_CountryCodeSource_FROM_NUMBER_WITHOUT_PLUS_SIGN = 10,
PhoneNumber_CountryCodeSource_FROM_DEFAULT_COUNTRY = 20
};
bool PhoneNumber_CountryCodeSource_IsValid(int value);
constexpr PhoneNumber_CountryCodeSource PhoneNumber_CountryCodeSource_CountryCodeSource_MIN = PhoneNumber_CountryCodeSource_UNSPECIFIED;
constexpr PhoneNumber_CountryCodeSource PhoneNumber_CountryCodeSource_CountryCodeSource_MAX = PhoneNumber_CountryCodeSource_FROM_DEFAULT_COUNTRY;
constexpr int PhoneNumber_CountryCodeSource_CountryCodeSource_ARRAYSIZE = PhoneNumber_CountryCodeSource_CountryCodeSource_MAX + 1;
const std::string& PhoneNumber_CountryCodeSource_Name(PhoneNumber_CountryCodeSource value);
template<typename T>
inline const std::string& PhoneNumber_CountryCodeSource_Name(T enum_t_value) {
static_assert(::std::is_same<T, PhoneNumber_CountryCodeSource>::value ||
::std::is_integral<T>::value,
"Incorrect type passed to function PhoneNumber_CountryCodeSource_Name.");
return PhoneNumber_CountryCodeSource_Name(static_cast<PhoneNumber_CountryCodeSource>(enum_t_value));
}
bool PhoneNumber_CountryCodeSource_Parse(
::PROTOBUF_NAMESPACE_ID::ConstStringParam name, PhoneNumber_CountryCodeSource* value);
// ===================================================================
class PhoneNumber final :
public ::PROTOBUF_NAMESPACE_ID::MessageLite /* @@protoc_insertion_point(class_definition:i18n.phonenumbers.PhoneNumber) */ {
public:
inline PhoneNumber() : PhoneNumber(nullptr) {}
~PhoneNumber() override;
explicit PROTOBUF_CONSTEXPR PhoneNumber(::PROTOBUF_NAMESPACE_ID::internal::ConstantInitialized);
PhoneNumber(const PhoneNumber& from);
PhoneNumber(PhoneNumber&& from) noexcept
: PhoneNumber() {
*this = ::std::move(from);
}
inline PhoneNumber& operator=(const PhoneNumber& from) {
CopyFrom(from);
return *this;
}
inline PhoneNumber& operator=(PhoneNumber&& from) noexcept {
if (this == &from) return *this;
if (GetOwningArena() == from.GetOwningArena()
#ifdef PROTOBUF_FORCE_COPY_IN_MOVE
&& GetOwningArena() != nullptr
#endif // !PROTOBUF_FORCE_COPY_IN_MOVE
) {
InternalSwap(&from);
} else {
CopyFrom(from);
}
return *this;
}
inline const std::string& unknown_fields() const {
return _internal_metadata_.unknown_fields<std::string>(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString);
}
inline std::string* mutable_unknown_fields() {
return _internal_metadata_.mutable_unknown_fields<std::string>();
}
static const PhoneNumber& default_instance() {
return *internal_default_instance();
}
static inline const PhoneNumber* internal_default_instance() {
return reinterpret_cast<const PhoneNumber*>(
&_PhoneNumber_default_instance_);
}
static constexpr int kIndexInFileMessages =
0;
friend void swap(PhoneNumber& a, PhoneNumber& b) {
a.Swap(&b);
}
inline void Swap(PhoneNumber* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetOwningArena() != nullptr &&
GetOwningArena() == other->GetOwningArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetOwningArena() == other->GetOwningArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
InternalSwap(other);
} else {
::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other);
}
}
void UnsafeArenaSwap(PhoneNumber* other) {
if (other == this) return;
GOOGLE_DCHECK(GetOwningArena() == other->GetOwningArena());
InternalSwap(other);
}
// implements Message ----------------------------------------------
PhoneNumber* New(::PROTOBUF_NAMESPACE_ID::Arena* arena = nullptr) const final {
return CreateMaybeMessage<PhoneNumber>(arena);
}
void CheckTypeAndMergeFrom(const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) final;
void CopyFrom(const PhoneNumber& from);
void MergeFrom(const PhoneNumber& from);
PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final;
bool IsInitialized() const final;
size_t ByteSizeLong() const final;
const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final;
uint8_t* _InternalSerialize(
uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final;
int GetCachedSize() const final { return _impl_._cached_size_.Get(); }
private:
void SharedCtor(::PROTOBUF_NAMESPACE_ID::Arena* arena, bool is_message_owned);
void SharedDtor();
void SetCachedSize(int size) const;
void InternalSwap(PhoneNumber* other);
private:
friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata;
static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() {
return "i18n.phonenumbers.PhoneNumber";
}
protected:
explicit PhoneNumber(::PROTOBUF_NAMESPACE_ID::Arena* arena,
bool is_message_owned = false);
public:
std::string GetTypeName() const final;
// nested types ----------------------------------------------------
typedef PhoneNumber_CountryCodeSource CountryCodeSource;
static constexpr CountryCodeSource UNSPECIFIED =
PhoneNumber_CountryCodeSource_UNSPECIFIED;
static constexpr CountryCodeSource FROM_NUMBER_WITH_PLUS_SIGN =
PhoneNumber_CountryCodeSource_FROM_NUMBER_WITH_PLUS_SIGN;
static constexpr CountryCodeSource FROM_NUMBER_WITH_IDD =
PhoneNumber_CountryCodeSource_FROM_NUMBER_WITH_IDD;
static constexpr CountryCodeSource FROM_NUMBER_WITHOUT_PLUS_SIGN =
PhoneNumber_CountryCodeSource_FROM_NUMBER_WITHOUT_PLUS_SIGN;
static constexpr CountryCodeSource FROM_DEFAULT_COUNTRY =
PhoneNumber_CountryCodeSource_FROM_DEFAULT_COUNTRY;
static inline bool CountryCodeSource_IsValid(int value) {
return PhoneNumber_CountryCodeSource_IsValid(value);
}
static constexpr CountryCodeSource CountryCodeSource_MIN =
PhoneNumber_CountryCodeSource_CountryCodeSource_MIN;
static constexpr CountryCodeSource CountryCodeSource_MAX =
PhoneNumber_CountryCodeSource_CountryCodeSource_MAX;
static constexpr int CountryCodeSource_ARRAYSIZE =
PhoneNumber_CountryCodeSource_CountryCodeSource_ARRAYSIZE;
template<typename T>
static inline const std::string& CountryCodeSource_Name(T enum_t_value) {
static_assert(::std::is_same<T, CountryCodeSource>::value ||
::std::is_integral<T>::value,
"Incorrect type passed to function CountryCodeSource_Name.");
return PhoneNumber_CountryCodeSource_Name(enum_t_value);
}
static inline bool CountryCodeSource_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name,
CountryCodeSource* value) {
return PhoneNumber_CountryCodeSource_Parse(name, value);
}
// accessors -------------------------------------------------------
enum : int {
kExtensionFieldNumber = 3,
kRawInputFieldNumber = 5,
kPreferredDomesticCarrierCodeFieldNumber = 7,
kNationalNumberFieldNumber = 2,
kCountryCodeFieldNumber = 1,
kItalianLeadingZeroFieldNumber = 4,
kCountryCodeSourceFieldNumber = 6,
kNumberOfLeadingZerosFieldNumber = 8,
};
// optional string extension = 3;
bool has_extension() const;
private:
bool _internal_has_extension() const;
public:
void clear_extension();
const std::string& extension() const;
template <typename ArgT0 = const std::string&, typename... ArgT>
void set_extension(ArgT0&& arg0, ArgT... args);
std::string* mutable_extension();
PROTOBUF_NODISCARD std::string* release_extension();
void set_allocated_extension(std::string* extension);
private:
const std::string& _internal_extension() const;
inline PROTOBUF_ALWAYS_INLINE void _internal_set_extension(const std::string& value);
std::string* _internal_mutable_extension();
public:
// optional string raw_input = 5;
bool has_raw_input() const;
private:
bool _internal_has_raw_input() const;
public:
void clear_raw_input();
const std::string& raw_input() const;
template <typename ArgT0 = const std::string&, typename... ArgT>
void set_raw_input(ArgT0&& arg0, ArgT... args);
std::string* mutable_raw_input();
PROTOBUF_NODISCARD std::string* release_raw_input();
void set_allocated_raw_input(std::string* raw_input);
private:
const std::string& _internal_raw_input() const;
inline PROTOBUF_ALWAYS_INLINE void _internal_set_raw_input(const std::string& value);
std::string* _internal_mutable_raw_input();
public:
// optional string preferred_domestic_carrier_code = 7;
bool has_preferred_domestic_carrier_code() const;
private:
bool _internal_has_preferred_domestic_carrier_code() const;
public:
void clear_preferred_domestic_carrier_code();
const std::string& preferred_domestic_carrier_code() const;
template <typename ArgT0 = const std::string&, typename... ArgT>
void set_preferred_domestic_carrier_code(ArgT0&& arg0, ArgT... args);
std::string* mutable_preferred_domestic_carrier_code();
PROTOBUF_NODISCARD std::string* release_preferred_domestic_carrier_code();
void set_allocated_preferred_domestic_carrier_code(std::string* preferred_domestic_carrier_code);
private:
const std::string& _internal_preferred_domestic_carrier_code() const;
inline PROTOBUF_ALWAYS_INLINE void _internal_set_preferred_domestic_carrier_code(const std::string& value);
std::string* _internal_mutable_preferred_domestic_carrier_code();
public:
// required uint64 national_number = 2;
bool has_national_number() const;
private:
bool _internal_has_national_number() const;
public:
void clear_national_number();
uint64_t national_number() const;
void set_national_number(uint64_t value);
private:
uint64_t _internal_national_number() const;
void _internal_set_national_number(uint64_t value);
public:
// required int32 country_code = 1;
bool has_country_code() const;
private:
bool _internal_has_country_code() const;
public:
void clear_country_code();
int32_t country_code() const;
void set_country_code(int32_t value);
private:
int32_t _internal_country_code() const;
void _internal_set_country_code(int32_t value);
public:
// optional bool italian_leading_zero = 4;
bool has_italian_leading_zero() const;
private:
bool _internal_has_italian_leading_zero() const;
public:
void clear_italian_leading_zero();
bool italian_leading_zero() const;
void set_italian_leading_zero(bool value);
private:
bool _internal_italian_leading_zero() const;
void _internal_set_italian_leading_zero(bool value);
public:
// optional .i18n.phonenumbers.PhoneNumber.CountryCodeSource country_code_source = 6;
bool has_country_code_source() const;
private:
bool _internal_has_country_code_source() const;
public:
void clear_country_code_source();
::i18n::phonenumbers::PhoneNumber_CountryCodeSource country_code_source() const;
void set_country_code_source(::i18n::phonenumbers::PhoneNumber_CountryCodeSource value);
private:
::i18n::phonenumbers::PhoneNumber_CountryCodeSource _internal_country_code_source() const;
void _internal_set_country_code_source(::i18n::phonenumbers::PhoneNumber_CountryCodeSource value);
public:
// optional int32 number_of_leading_zeros = 8 [default = 1];
bool has_number_of_leading_zeros() const;
private:
bool _internal_has_number_of_leading_zeros() const;
public:
void clear_number_of_leading_zeros();
int32_t number_of_leading_zeros() const;
void set_number_of_leading_zeros(int32_t value);
private:
int32_t _internal_number_of_leading_zeros() const;
void _internal_set_number_of_leading_zeros(int32_t value);
public:
// @@protoc_insertion_point(class_scope:i18n.phonenumbers.PhoneNumber)
private:
class _Internal;
// helper for ByteSizeLong()
size_t RequiredFieldsByteSizeFallback() const;
template <typename T> friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper;
typedef void InternalArenaConstructable_;
typedef void DestructorSkippable_;
struct Impl_ {
::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_;
mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_;
::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr extension_;
::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr raw_input_;
::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr preferred_domestic_carrier_code_;
uint64_t national_number_;
int32_t country_code_;
bool italian_leading_zero_;
int country_code_source_;
int32_t number_of_leading_zeros_;
};
union { Impl_ _impl_; };
friend struct ::TableStruct_phonenumber_2eproto;
};
// ===================================================================
// ===================================================================
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif // __GNUC__
// PhoneNumber
// required int32 country_code = 1;
inline bool PhoneNumber::_internal_has_country_code() const {
bool value = (_impl_._has_bits_[0] & 0x00000010u) != 0;
return value;
}
inline bool PhoneNumber::has_country_code() const {
return _internal_has_country_code();
}
inline void PhoneNumber::clear_country_code() {
_impl_.country_code_ = 0;
_impl_._has_bits_[0] &= ~0x00000010u;
}
inline int32_t PhoneNumber::_internal_country_code() const {
return _impl_.country_code_;
}
inline int32_t PhoneNumber::country_code() const {
// @@protoc_insertion_point(field_get:i18n.phonenumbers.PhoneNumber.country_code)
return _internal_country_code();
}
inline void PhoneNumber::_internal_set_country_code(int32_t value) {
_impl_._has_bits_[0] |= 0x00000010u;
_impl_.country_code_ = value;
}
inline void PhoneNumber::set_country_code(int32_t value) {
_internal_set_country_code(value);
// @@protoc_insertion_point(field_set:i18n.phonenumbers.PhoneNumber.country_code)
}
// required uint64 national_number = 2;
inline bool PhoneNumber::_internal_has_national_number() const {
bool value = (_impl_._has_bits_[0] & 0x00000008u) != 0;
return value;
}
inline bool PhoneNumber::has_national_number() const {
return _internal_has_national_number();
}
inline void PhoneNumber::clear_national_number() {
_impl_.national_number_ = uint64_t{0u};
_impl_._has_bits_[0] &= ~0x00000008u;
}
inline uint64_t PhoneNumber::_internal_national_number() const {
return _impl_.national_number_;
}
inline uint64_t PhoneNumber::national_number() const {
// @@protoc_insertion_point(field_get:i18n.phonenumbers.PhoneNumber.national_number)
return _internal_national_number();
}
inline void PhoneNumber::_internal_set_national_number(uint64_t value) {
_impl_._has_bits_[0] |= 0x00000008u;
_impl_.national_number_ = value;
}
inline void PhoneNumber::set_national_number(uint64_t value) {
_internal_set_national_number(value);
// @@protoc_insertion_point(field_set:i18n.phonenumbers.PhoneNumber.national_number)
}
// optional string extension = 3;
inline bool PhoneNumber::_internal_has_extension() const {
bool value = (_impl_._has_bits_[0] & 0x00000001u) != 0;
return value;
}
inline bool PhoneNumber::has_extension() const {
return _internal_has_extension();
}
inline void PhoneNumber::clear_extension() {
_impl_.extension_.ClearToEmpty();
_impl_._has_bits_[0] &= ~0x00000001u;
}
inline const std::string& PhoneNumber::extension() const {
// @@protoc_insertion_point(field_get:i18n.phonenumbers.PhoneNumber.extension)
return _internal_extension();
}
template <typename ArgT0, typename... ArgT>
inline PROTOBUF_ALWAYS_INLINE
void PhoneNumber::set_extension(ArgT0&& arg0, ArgT... args) {
_impl_._has_bits_[0] |= 0x00000001u;
_impl_.extension_.Set(static_cast<ArgT0 &&>(arg0), args..., GetArenaForAllocation());
// @@protoc_insertion_point(field_set:i18n.phonenumbers.PhoneNumber.extension)
}
inline std::string* PhoneNumber::mutable_extension() {
std::string* _s = _internal_mutable_extension();
// @@protoc_insertion_point(field_mutable:i18n.phonenumbers.PhoneNumber.extension)
return _s;
}
inline const std::string& PhoneNumber::_internal_extension() const {
return _impl_.extension_.Get();
}
inline void PhoneNumber::_internal_set_extension(const std::string& value) {
_impl_._has_bits_[0] |= 0x00000001u;
_impl_.extension_.Set(value, GetArenaForAllocation());
}
inline std::string* PhoneNumber::_internal_mutable_extension() {
_impl_._has_bits_[0] |= 0x00000001u;
return _impl_.extension_.Mutable(GetArenaForAllocation());
}
inline std::string* PhoneNumber::release_extension() {
// @@protoc_insertion_point(field_release:i18n.phonenumbers.PhoneNumber.extension)
if (!_internal_has_extension()) {
return nullptr;
}
_impl_._has_bits_[0] &= ~0x00000001u;
auto* p = _impl_.extension_.Release();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (_impl_.extension_.IsDefault()) {
_impl_.extension_.Set("", GetArenaForAllocation());
}
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
return p;
}
inline void PhoneNumber::set_allocated_extension(std::string* extension) {
if (extension != nullptr) {
_impl_._has_bits_[0] |= 0x00000001u;
} else {
_impl_._has_bits_[0] &= ~0x00000001u;
}
_impl_.extension_.SetAllocated(extension, GetArenaForAllocation());
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (_impl_.extension_.IsDefault()) {
_impl_.extension_.Set("", GetArenaForAllocation());
}
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
// @@protoc_insertion_point(field_set_allocated:i18n.phonenumbers.PhoneNumber.extension)
}
// optional bool italian_leading_zero = 4;
inline bool PhoneNumber::_internal_has_italian_leading_zero() const {
bool value = (_impl_._has_bits_[0] & 0x00000020u) != 0;
return value;
}
inline bool PhoneNumber::has_italian_leading_zero() const {
return _internal_has_italian_leading_zero();
}
inline void PhoneNumber::clear_italian_leading_zero() {
_impl_.italian_leading_zero_ = false;
_impl_._has_bits_[0] &= ~0x00000020u;
}
inline bool PhoneNumber::_internal_italian_leading_zero() const {
return _impl_.italian_leading_zero_;
}
inline bool PhoneNumber::italian_leading_zero() const {
// @@protoc_insertion_point(field_get:i18n.phonenumbers.PhoneNumber.italian_leading_zero)
return _internal_italian_leading_zero();
}
inline void PhoneNumber::_internal_set_italian_leading_zero(bool value) {
_impl_._has_bits_[0] |= 0x00000020u;
_impl_.italian_leading_zero_ = value;
}
inline void PhoneNumber::set_italian_leading_zero(bool value) {
_internal_set_italian_leading_zero(value);
// @@protoc_insertion_point(field_set:i18n.phonenumbers.PhoneNumber.italian_leading_zero)
}
// optional int32 number_of_leading_zeros = 8 [default = 1];
inline bool PhoneNumber::_internal_has_number_of_leading_zeros() const {
bool value = (_impl_._has_bits_[0] & 0x00000080u) != 0;
return value;
}
inline bool PhoneNumber::has_number_of_leading_zeros() const {
return _internal_has_number_of_leading_zeros();
}
inline void PhoneNumber::clear_number_of_leading_zeros() {
_impl_.number_of_leading_zeros_ = 1;
_impl_._has_bits_[0] &= ~0x00000080u;
}
inline int32_t PhoneNumber::_internal_number_of_leading_zeros() const {
return _impl_.number_of_leading_zeros_;
}
inline int32_t PhoneNumber::number_of_leading_zeros() const {
// @@protoc_insertion_point(field_get:i18n.phonenumbers.PhoneNumber.number_of_leading_zeros)
return _internal_number_of_leading_zeros();
}
inline void PhoneNumber::_internal_set_number_of_leading_zeros(int32_t value) {
_impl_._has_bits_[0] |= 0x00000080u;
_impl_.number_of_leading_zeros_ = value;
}
inline void PhoneNumber::set_number_of_leading_zeros(int32_t value) {
_internal_set_number_of_leading_zeros(value);
// @@protoc_insertion_point(field_set:i18n.phonenumbers.PhoneNumber.number_of_leading_zeros)
}
// optional string raw_input = 5;
inline bool PhoneNumber::_internal_has_raw_input() const {
bool value = (_impl_._has_bits_[0] & 0x00000002u) != 0;
return value;
}
inline bool PhoneNumber::has_raw_input() const {
return _internal_has_raw_input();
}
inline void PhoneNumber::clear_raw_input() {
_impl_.raw_input_.ClearToEmpty();
_impl_._has_bits_[0] &= ~0x00000002u;
}
inline const std::string& PhoneNumber::raw_input() const {
// @@protoc_insertion_point(field_get:i18n.phonenumbers.PhoneNumber.raw_input)
return _internal_raw_input();
}
template <typename ArgT0, typename... ArgT>
inline PROTOBUF_ALWAYS_INLINE
void PhoneNumber::set_raw_input(ArgT0&& arg0, ArgT... args) {
_impl_._has_bits_[0] |= 0x00000002u;
_impl_.raw_input_.Set(static_cast<ArgT0 &&>(arg0), args..., GetArenaForAllocation());
// @@protoc_insertion_point(field_set:i18n.phonenumbers.PhoneNumber.raw_input)
}
inline std::string* PhoneNumber::mutable_raw_input() {
std::string* _s = _internal_mutable_raw_input();
// @@protoc_insertion_point(field_mutable:i18n.phonenumbers.PhoneNumber.raw_input)
return _s;
}
inline const std::string& PhoneNumber::_internal_raw_input() const {
return _impl_.raw_input_.Get();
}
inline void PhoneNumber::_internal_set_raw_input(const std::string& value) {
_impl_._has_bits_[0] |= 0x00000002u;
_impl_.raw_input_.Set(value, GetArenaForAllocation());
}
inline std::string* PhoneNumber::_internal_mutable_raw_input() {
_impl_._has_bits_[0] |= 0x00000002u;
return _impl_.raw_input_.Mutable(GetArenaForAllocation());
}
inline std::string* PhoneNumber::release_raw_input() {
// @@protoc_insertion_point(field_release:i18n.phonenumbers.PhoneNumber.raw_input)
if (!_internal_has_raw_input()) {
return nullptr;
}
_impl_._has_bits_[0] &= ~0x00000002u;
auto* p = _impl_.raw_input_.Release();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (_impl_.raw_input_.IsDefault()) {
_impl_.raw_input_.Set("", GetArenaForAllocation());
}
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
return p;
}
inline void PhoneNumber::set_allocated_raw_input(std::string* raw_input) {
if (raw_input != nullptr) {
_impl_._has_bits_[0] |= 0x00000002u;
} else {
_impl_._has_bits_[0] &= ~0x00000002u;
}
_impl_.raw_input_.SetAllocated(raw_input, GetArenaForAllocation());
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (_impl_.raw_input_.IsDefault()) {
_impl_.raw_input_.Set("", GetArenaForAllocation());
}
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
// @@protoc_insertion_point(field_set_allocated:i18n.phonenumbers.PhoneNumber.raw_input)
}
// optional .i18n.phonenumbers.PhoneNumber.CountryCodeSource country_code_source = 6;
inline bool PhoneNumber::_internal_has_country_code_source() const {
bool value = (_impl_._has_bits_[0] & 0x00000040u) != 0;
return value;
}
inline bool PhoneNumber::has_country_code_source() const {
return _internal_has_country_code_source();
}
inline void PhoneNumber::clear_country_code_source() {
_impl_.country_code_source_ = 0;
_impl_._has_bits_[0] &= ~0x00000040u;
}
inline ::i18n::phonenumbers::PhoneNumber_CountryCodeSource PhoneNumber::_internal_country_code_source() const {
return static_cast< ::i18n::phonenumbers::PhoneNumber_CountryCodeSource >(_impl_.country_code_source_);
}
inline ::i18n::phonenumbers::PhoneNumber_CountryCodeSource PhoneNumber::country_code_source() const {
// @@protoc_insertion_point(field_get:i18n.phonenumbers.PhoneNumber.country_code_source)
return _internal_country_code_source();
}
inline void PhoneNumber::_internal_set_country_code_source(::i18n::phonenumbers::PhoneNumber_CountryCodeSource value) {
assert(::i18n::phonenumbers::PhoneNumber_CountryCodeSource_IsValid(value));
_impl_._has_bits_[0] |= 0x00000040u;
_impl_.country_code_source_ = value;
}
inline void PhoneNumber::set_country_code_source(::i18n::phonenumbers::PhoneNumber_CountryCodeSource value) {
_internal_set_country_code_source(value);
// @@protoc_insertion_point(field_set:i18n.phonenumbers.PhoneNumber.country_code_source)
}
// optional string preferred_domestic_carrier_code = 7;
inline bool PhoneNumber::_internal_has_preferred_domestic_carrier_code() const {
bool value = (_impl_._has_bits_[0] & 0x00000004u) != 0;
return value;
}
inline bool PhoneNumber::has_preferred_domestic_carrier_code() const {
return _internal_has_preferred_domestic_carrier_code();
}
inline void PhoneNumber::clear_preferred_domestic_carrier_code() {
_impl_.preferred_domestic_carrier_code_.ClearToEmpty();
_impl_._has_bits_[0] &= ~0x00000004u;
}
inline const std::string& PhoneNumber::preferred_domestic_carrier_code() const {
// @@protoc_insertion_point(field_get:i18n.phonenumbers.PhoneNumber.preferred_domestic_carrier_code)
return _internal_preferred_domestic_carrier_code();
}
template <typename ArgT0, typename... ArgT>
inline PROTOBUF_ALWAYS_INLINE
void PhoneNumber::set_preferred_domestic_carrier_code(ArgT0&& arg0, ArgT... args) {
_impl_._has_bits_[0] |= 0x00000004u;
_impl_.preferred_domestic_carrier_code_.Set(static_cast<ArgT0 &&>(arg0), args..., GetArenaForAllocation());
// @@protoc_insertion_point(field_set:i18n.phonenumbers.PhoneNumber.preferred_domestic_carrier_code)
}
inline std::string* PhoneNumber::mutable_preferred_domestic_carrier_code() {
std::string* _s = _internal_mutable_preferred_domestic_carrier_code();
// @@protoc_insertion_point(field_mutable:i18n.phonenumbers.PhoneNumber.preferred_domestic_carrier_code)
return _s;
}
inline const std::string& PhoneNumber::_internal_preferred_domestic_carrier_code() const {
return _impl_.preferred_domestic_carrier_code_.Get();
}
inline void PhoneNumber::_internal_set_preferred_domestic_carrier_code(const std::string& value) {
_impl_._has_bits_[0] |= 0x00000004u;
_impl_.preferred_domestic_carrier_code_.Set(value, GetArenaForAllocation());
}
inline std::string* PhoneNumber::_internal_mutable_preferred_domestic_carrier_code() {
_impl_._has_bits_[0] |= 0x00000004u;
return _impl_.preferred_domestic_carrier_code_.Mutable(GetArenaForAllocation());
}
inline std::string* PhoneNumber::release_preferred_domestic_carrier_code() {
// @@protoc_insertion_point(field_release:i18n.phonenumbers.PhoneNumber.preferred_domestic_carrier_code)
if (!_internal_has_preferred_domestic_carrier_code()) {
return nullptr;
}
_impl_._has_bits_[0] &= ~0x00000004u;
auto* p = _impl_.preferred_domestic_carrier_code_.Release();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (_impl_.preferred_domestic_carrier_code_.IsDefault()) {
_impl_.preferred_domestic_carrier_code_.Set("", GetArenaForAllocation());
}
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
return p;
}
inline void PhoneNumber::set_allocated_preferred_domestic_carrier_code(std::string* preferred_domestic_carrier_code) {
if (preferred_domestic_carrier_code != nullptr) {
_impl_._has_bits_[0] |= 0x00000004u;
} else {
_impl_._has_bits_[0] &= ~0x00000004u;
}
_impl_.preferred_domestic_carrier_code_.SetAllocated(preferred_domestic_carrier_code, GetArenaForAllocation());
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (_impl_.preferred_domestic_carrier_code_.IsDefault()) {
_impl_.preferred_domestic_carrier_code_.Set("", GetArenaForAllocation());
}
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
// @@protoc_insertion_point(field_set_allocated:i18n.phonenumbers.PhoneNumber.preferred_domestic_carrier_code)
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif // __GNUC__
// @@protoc_insertion_point(namespace_scope)
} // namespace phonenumbers
} // namespace i18n
PROTOBUF_NAMESPACE_OPEN
template <> struct is_proto_enum< ::i18n::phonenumbers::PhoneNumber_CountryCodeSource> : ::std::true_type {};
PROTOBUF_NAMESPACE_CLOSE
// @@protoc_insertion_point(global_scope)
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_phonenumber_2eproto

+ 10
- 6
cpp/src/phonenumbers/phonenumberutil.cc View File

@ -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"
@ -46,6 +47,7 @@
#include "phonenumbers/stringutil.h"
#include "phonenumbers/utf/unicodetext.h"
#include "phonenumbers/utf/utf.h"
#include "absl/strings/string_view.h"
namespace i18n {
namespace phonenumbers {
@ -2138,15 +2140,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,
PhoneNumber* number) const {
PhoneNumberUtil::ErrorType PhoneNumberUtil::ParseWithOptions(const string& 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


+ 31
- 4
cpp/src/phonenumbers/phonenumberutil.h View File

@ -29,9 +29,12 @@
#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 "absl/base/macros.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
class TelephoneNumber;
@ -714,13 +717,29 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
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(
const string& number_to_parse,
const string& 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(const string& number_to_parse,
const ParsingOptions& options,
PhoneNumber* number) const;
// Takes two phone numbers and compares them for equality.
//
@ -976,6 +995,14 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
bool check_region,
PhoneNumber* phone_number) const;
ErrorType ParseHelper(const absl::string_view number_to_parse,
const string& default_region,
bool keep_raw_input,
bool check_region,
PhoneNumber* phone_number) const;
absl::optional<string> ExtractPhoneContext(
const string& number_to_extract_from,
size_t index_of_phone_context) const;


+ 1
- 0
cpp/src/phonenumbers/region_code.h View File

@ -24,6 +24,7 @@ using std::string;
class RegionCode {
public:
// Returns a region code string representing the "unknown" region.
static const char* GetUnknown() {
return ZZ();


+ 285
- 0
cpp/test/phonenumbers/geocoding/geocoding_test_data.cc View File

@ -0,0 +1,285 @@
// Copyright (C) 2012 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.
//
// This file is generated automatically, do not edit it manually.
#include "phonenumbers/geocoding/geocoding_test_data.h"
#include <cstdint>
namespace i18n {
namespace phonenumbers {
namespace {
const int32_t prefix_1_de_prefixes[] = {
1201,
1650,
};
const char* prefix_1_de_descriptions[] = {
"New Jersey",
"Kalifornien",
};
const int32_t prefix_1_de_possible_lengths[] = {
4,
};
const PrefixDescriptions prefix_1_de = {
prefix_1_de_prefixes,
sizeof(prefix_1_de_prefixes)/sizeof(*prefix_1_de_prefixes),
prefix_1_de_descriptions,
prefix_1_de_possible_lengths,
sizeof(prefix_1_de_possible_lengths)/sizeof(*prefix_1_de_possible_lengths),
};
const int32_t prefix_1_en_prefixes[] = {
1201,
1212,
1650,
1989,
1212812,
1617423,
1650960,
};
const char* prefix_1_en_descriptions[] = {
"NJ",
"NY",
"CA",
"MA",
"New York, NY",
"Boston, MA",
"Mountain View, CA",
};
const int32_t prefix_1_en_possible_lengths[] = {
4, 7,
};
const PrefixDescriptions prefix_1_en = {
prefix_1_en_prefixes,
sizeof(prefix_1_en_prefixes)/sizeof(*prefix_1_en_prefixes),
prefix_1_en_descriptions,
prefix_1_en_possible_lengths,
sizeof(prefix_1_en_possible_lengths)/sizeof(*prefix_1_en_possible_lengths),
};
const int32_t prefix_54_en_prefixes[] = {
542214,
};
const char* prefix_54_en_descriptions[] = {
"La Plata",
};
const int32_t prefix_54_en_possible_lengths[] = {
6,
};
const PrefixDescriptions prefix_54_en = {
prefix_54_en_prefixes,
sizeof(prefix_54_en_prefixes)/sizeof(*prefix_54_en_prefixes),
prefix_54_en_descriptions,
prefix_54_en_possible_lengths,
sizeof(prefix_54_en_possible_lengths)/sizeof(*prefix_54_en_possible_lengths),
};
const int32_t prefix_82_en_prefixes[] = {
822,
8210,
8231,
8232,
8233,
8241,
8242,
8243,
8251,
8252,
8253,
8254,
8255,
8261,
8262,
8263,
8264,
};
const char* prefix_82_en_descriptions[] = {
"Seoul",
"Mobile prefix, should not be geocoded.",
"Gyeonggi",
"Incheon",
"Gangwon",
"Chungnam",
"Daejeon",
"Chungbuk",
"Busan",
"Ulsan",
"Daegu",
"Gyeongbuk",
"Gyeongnam",
"Jeonnam",
"Gwangju",
"Jeonbuk",
"Jeju",
};
const int32_t prefix_82_en_possible_lengths[] = {
3, 4,
};
const PrefixDescriptions prefix_82_en = {
prefix_82_en_prefixes,
sizeof(prefix_82_en_prefixes)/sizeof(*prefix_82_en_prefixes),
prefix_82_en_descriptions,
prefix_82_en_possible_lengths,
sizeof(prefix_82_en_possible_lengths)/sizeof(*prefix_82_en_possible_lengths),
};
const int32_t prefix_82_ko_prefixes[] = {
822,
8231,
8232,
8233,
8241,
8242,
8243,
8251,
8252,
8253,
8254,
8255,
8261,
8262,
8263,
};
const char* prefix_82_ko_descriptions[] = {
"\xec""\x84""\x9c""\xec""\x9a""\xb8",
"\xea""\xb2""\xbd""\xea""\xb8""\xb0",
"\xec""\x9d""\xb8""\xec""\xb2""\x9c",
"\xea""\xb0""\x95""\xec""\x9b""\x90",
"\xec""\xb6""\xa9""\xeb""\x82""\xa8",
"\xeb""\x8c""\x80""\xec""\xa0""\x84",
"\xec""\xb6""\xa9""\xeb""\xb6""\x81",
"\xeb""\xb6""\x80""\xec""\x82""\xb0",
"\xec""\x9a""\xb8""\xec""\x82""\xb0",
"\xeb""\x8c""\x80""\xea""\xb5""\xac",
"\xea""\xb2""\xbd""\xeb""\xb6""\x81",
"\xea""\xb2""\xbd""\xeb""\x82""\xa8",
"\xec""\xa0""\x84""\xeb""\x82""\xa8",
"\xea""\xb4""\x91""\xec""\xa3""\xbc",
"\xec""\xa0""\x84""\xeb""\xb6""\x81",
};
const int32_t prefix_82_ko_possible_lengths[] = {
3, 4,
};
const PrefixDescriptions prefix_82_ko = {
prefix_82_ko_prefixes,
sizeof(prefix_82_ko_prefixes)/sizeof(*prefix_82_ko_prefixes),
prefix_82_ko_descriptions,
prefix_82_ko_possible_lengths,
sizeof(prefix_82_ko_possible_lengths)/sizeof(*prefix_82_ko_possible_lengths),
};
const char* prefix_language_code_pairs[] = {
"1_de",
"1_en",
"54_en",
"82_en",
"82_ko",
};
const PrefixDescriptions* prefixes_descriptions[] = {
&prefix_1_de,
&prefix_1_en,
&prefix_54_en,
&prefix_82_en,
&prefix_82_ko,
};
const char* country_1[] = {
"de",
"en",
};
const CountryLanguages country_1_languages = {
country_1,
sizeof(country_1)/sizeof(*country_1),
};
const char* country_54[] = {
"en",
};
const CountryLanguages country_54_languages = {
country_54,
sizeof(country_54)/sizeof(*country_54),
};
const char* country_82[] = {
"en",
"ko",
};
const CountryLanguages country_82_languages = {
country_82,
sizeof(country_82)/sizeof(*country_82),
};
const CountryLanguages* countries_languages[] = {
&country_1_languages,
&country_54_languages,
&country_82_languages,
};
const int country_calling_codes[] = {
1,
54,
82,
};
} // namespace
const int* get_test_country_calling_codes() {
return country_calling_codes;
}
int get_test_country_calling_codes_size() {
return sizeof(country_calling_codes)
/sizeof(*country_calling_codes);
}
const CountryLanguages* get_test_country_languages(int index) {
return countries_languages[index];
}
const char** get_test_prefix_language_code_pairs() {
return prefix_language_code_pairs;
}
int get_test_prefix_language_code_pairs_size() {
return sizeof(prefix_language_code_pairs)
/sizeof(*prefix_language_code_pairs);
}
const PrefixDescriptions* get_test_prefix_descriptions(int index) {
return prefixes_descriptions[index];
}
} // namespace phonenumbers
} // namespace i18n

+ 66
- 44
cpp/test/phonenumbers/phonenumberutil_test.cc View File

@ -37,6 +37,7 @@
#include "phonenumbers/phonenumber.h"
#include "phonenumbers/phonenumber.pb.h"
#include "phonenumbers/test_util.h"
#include "phonenumbers/parsingoptions.h"
namespace i18n {
namespace phonenumbers {
@ -64,7 +65,7 @@ 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);
}
@ -341,7 +342,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());
@ -2418,7 +2419,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);
@ -2427,7 +2429,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);
@ -2436,8 +2439,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);
@ -2445,7 +2449,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);
@ -2466,7 +2471,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);
@ -2477,7 +2483,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);
@ -2488,7 +2495,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);
@ -2497,8 +2505,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);
@ -2506,7 +2515,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);
@ -2517,7 +2527,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);
@ -2527,7 +2538,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);
@ -2537,7 +2549,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);
@ -2557,7 +2570,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);
@ -2567,7 +2581,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);
@ -2577,7 +2592,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);
@ -2587,7 +2603,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);
@ -2597,7 +2614,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);
@ -2607,7 +2625,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);
@ -2619,8 +2638,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);
@ -2629,8 +2648,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);
@ -2642,8 +2661,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);
@ -2652,8 +2671,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);
@ -2664,8 +2683,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);
@ -2673,8 +2692,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);
@ -3585,7 +3604,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());
@ -4180,8 +4199,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);
}
@ -4631,7 +4650,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);
@ -4640,7 +4660,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);
@ -4648,7 +4669,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);
@ -4656,8 +4678,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);
@ -4675,8 +4697,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);
}


+ 66
- 0
java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java View File

@ -0,0 +1,66 @@
/*
* 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 {
private boolean hasDefaultRegion;
/**
* 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 String defaultRegion_ = null;
public boolean hasDefaultRegion() { return hasDefaultRegion; }
/**
* Returns the value of {@link #defaultRegion_}.
*/
public String getDefaultRegion() { return defaultRegion_; }
/**
* Sets the {@link #defaultRegion_} to the given value.
*/
public ParsingOptions setDefaultRegion(String value) {
hasDefaultRegion = (value != null);
defaultRegion_ = value;
return this;
}
/**
* 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;
/**
* Returns the value of {@link #hasKeepRawInput}.
*/
public boolean hasKeepRawInput() { return hasKeepRawInput; }
/**
* Returns the value of {@link #keepRawInput_}.
*/
public boolean isKeepRawInput() { return keepRawInput_; }
/**
* Decides with the given value if it should keep the raw input.
*/
public ParsingOptions setKeepRawInput(boolean value) {
hasKeepRawInput = true;
keepRawInput_ = value;
return this;
}
}

+ 67
- 6
java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java View File

@ -3156,7 +3156,10 @@ public class PhoneNumberUtil {
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;
}
@ -3166,7 +3169,10 @@ public class PhoneNumberUtil {
*/
public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber phoneNumber)
throws NumberParseException {
parseHelper(numberToParse, defaultRegion, false, true, phoneNumber);
parseWithOptions(
numberToParse,
new ParsingOptions().setDefaultRegion(defaultRegion),
phoneNumber);
}
/**
@ -3182,22 +3188,77 @@ public class PhoneNumberUtil {
* @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".
*
* <p>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}.
*
* <p>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);
}
/**


+ 33
- 34
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File

@ -1013,16 +1013,16 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase {
}
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);
@ -1030,80 +1030,80 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase {
// 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));
@ -1111,19 +1111,19 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase {
// 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.
@ -2092,7 +2092,7 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase {
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());
@ -2587,10 +2587,9 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase {
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 {
@ -2840,28 +2839,28 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase {
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.
@ -2874,7 +2873,7 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase {
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 {


+ 4
- 1
pending_code_changes.txt View File

@ -1 +1,4 @@
Code changes:
- Introduced new function parseWithOptions to customize the parser's behaviour
- Deprecated parseAndKeepRawInput in favor of parseWithOptions
-

Loading…
Cancel
Save