Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new class ParseWithOptions #3730

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cpp/src/phonenumbers/parsingOptions.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "i18n/phonenumbers/parsingoptions.h"

#include "i18n/identifiers/regioncode.h"

namespace i18n {
namespace phonenumbers {

ParsingOptions& ParsingOptions::SetDefaultRegion(
i18n_identifiers::RegionCode default_region) {
default_region_ = default_region;
return *this;
}

ParsingOptions& ParsingOptions::SetKeepRawInput(bool keep_raw_input) {
keep_raw_input_ = keep_raw_input;
return *this;
}

} // namespace phonenumbers
} // namespace i18n
43 changes: 43 additions & 0 deletions cpp/src/phonenumbers/parsingOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef I18N_PHONENUMBERS_PARSINGOPTIONS_H_
#define I18N_PHONENUMBERS_PARSINGOPTIONS_H_

#include "i18n/identifiers/regioncode.h"

namespace i18n {
namespace phonenumbers {

// Options for parsing a phone number. To be used with the ParseWithOptions
// method.
// Example:
// ParsingOptions().SetDefaultRegion(RegionCode::US()).SetKeepRawInput(true);
class ParsingOptions {
public:
ParsingOptions() = default;

// Set the value for default_region_.
ParsingOptions& SetDefaultRegion(
i18n_identifiers::RegionCode default_region);

// Set the value for keep_raw_input_.
ParsingOptions& SetKeepRawInput(bool keep_raw_input);

private:
friend class PhoneNumberUtil;

// The region we are expecting the number to be from. This is ignored if the
// number being parsed is written in international format. In case of national
// format, the country_code will be set to the one of this default region. If
// the number is guaranteed to start with a '+' followed by the country
// calling code, then RegionCode.ZZ or null can be supplied.
i18n_identifiers::RegionCode default_region_ =
i18n_identifiers::RegionCode::ZZ();

// Whether the raw input should be kept in the PhoneNumber object. If true,
// the raw_input field and country_code_source fields will be populated.
bool keep_raw_input_ = false;
};

} // namespace phonenumbers
} // namespace i18n

#endif // I1
14 changes: 9 additions & 5 deletions cpp/src/phonenumbers/phonenumberutil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -47,6 +48,7 @@
#include "phonenumbers/utf/unicodetext.h"
#include "phonenumbers/utf/utf.h"


namespace i18n {
namespace phonenumbers {

Expand Down Expand Up @@ -2127,15 +2129,17 @@ PhoneNumberUtil::ErrorType PhoneNumberUtil::Parse(const string& number_to_parse,
const string& default_region,
PhoneNumber* number) const {
DCHECK(number);
return ParseHelper(number_to_parse, default_region, false, true, number);
return ParseWithOptions(number_to_parse,
ParsingOptions().SetDefaultRegion(default_region),
number);
}

PhoneNumberUtil::ErrorType PhoneNumberUtil::ParseAndKeepRawInput(
const string& number_to_parse,
const string& default_region,
PhoneNumberUtil::ErrorType PhoneNumberUtil::ParseWithOptions(
absl::string_view number_to_parse, const ParsingOptions& options,
PhoneNumber* number) const {
DCHECK(number);
return ParseHelper(number_to_parse, default_region, true, true, number);
return ParseHelper(number_to_parse, options.default_region_,
options.keep_raw_input_, /*check_region=*/true, number);
}

// Checks to see that the region code used is valid, or if it is not valid, that
Expand Down
25 changes: 21 additions & 4 deletions cpp/src/phonenumbers/phonenumberutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
#include "phonenumbers/base/memory/scoped_ptr.h"
#include "phonenumbers/base/memory/singleton.h"
#include "phonenumbers/phonenumber.pb.h"

#include "phonenumbers/parsingoptions.h"
#include "absl/container/node_hash_set.h"
#include "absl/container/node_hash_map.h"
#include "third_party/absl/base/macros.h"

class TelephoneNumber;

Expand Down Expand Up @@ -700,13 +701,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(
absl::string_view number_to_parse,
i18n_identifiers::RegionCode default_region
PhoneNumber* number) const {
return ParseWithOptions(number_to_parse,
ParsingOptions()
.SetDefaultRegion(default_region)
.SetKeepRawInput(true),
number);
}

// Parses a string and returns it in proto buffer format. This method differs
// from Parse() in that it allows the caller to change the behavior of the
// parser. See ParsingOptions for more details.
ErrorType ParseWithOptions(absl::string_view number_to_parse,
const ParsingOptions& options,
PhoneNumber* number) const;

// Takes two phone numbers and compares them for equality.
//
Expand Down
Loading