Skip to content

Commit

Permalink
Create ParsingOptions for cpp
Browse files Browse the repository at this point in the history
In this commit we added the ParsingOptions from the java version which will make parsing in different ways easier
  • Loading branch information
KarolJakubKrawiec committed Dec 12, 2024
1 parent 5155f58 commit 9d7975a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 54 deletions.
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

0 comments on commit 9d7975a

Please sign in to comment.