Browse Source

Merge eca651e6c0 into cf446565d9

pull/3357/merge
Cory Kramer 4 days ago
committed by GitHub
parent
commit
6d883dc2d6
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 30 deletions
  1. +18
    -25
      cpp/src/phonenumbers/phonenumbermatcher.cc
  2. +5
    -5
      cpp/src/phonenumbers/phonenumbermatcher.h

+ 18
- 25
cpp/src/phonenumbers/phonenumbermatcher.cc View File

@ -26,14 +26,15 @@
#endif // I18N_PHONENUMBERS_USE_ICU_REGEXP #endif // I18N_PHONENUMBERS_USE_ICU_REGEXP
#include <ctype.h> #include <ctype.h>
#include <stddef.h>
#include <functional>
#include <limits> #include <limits>
#include <map> #include <map>
#include <memory> #include <memory>
#include <stddef.h>
#include <string> #include <string>
#include <unicode/uchar.h>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <unicode/uchar.h>
#include "phonenumbers/alternate_format.h" #include "phonenumbers/alternate_format.h"
#include "phonenumbers/base/logging.h" #include "phonenumbers/base/logging.h"
@ -409,7 +410,7 @@ PhoneNumberMatcher::PhoneNumberMatcher(const PhoneNumberUtil& util,
last_match_(NULL), last_match_(NULL),
search_index_(0), search_index_(0),
is_input_valid_utf8_(true) { is_input_valid_utf8_(true) {
is_input_valid_utf8_ = IsInputUtf8();
is_input_valid_utf8_ = IsInputUtf8();
} }
PhoneNumberMatcher::PhoneNumberMatcher(const string& text, PhoneNumberMatcher::PhoneNumberMatcher(const string& text,
@ -541,12 +542,8 @@ bool PhoneNumberMatcher::VerifyAccordingToLeniency(
!IsNationalPrefixPresentIfRequired(number)) { !IsNationalPrefixPresentIfRequired(number)) {
return false; return false;
} }
ResultCallback4<bool, const PhoneNumberUtil&, const PhoneNumber&,
const string&, const std::vector<string>&>* callback =
NewPermanentCallback(&AllNumberGroupsRemainGrouped);
bool is_valid = CheckNumberGroupingIsValid(number, candidate, callback);
delete(callback);
return is_valid;
return CheckNumberGroupingIsValid(number, candidate,
&AllNumberGroupsRemainGrouped);
} }
case PhoneNumberMatcher::EXACT_GROUPING: { case PhoneNumberMatcher::EXACT_GROUPING: {
if (!phone_util_.IsValidNumber(number) || if (!phone_util_.IsValidNumber(number) ||
@ -556,13 +553,10 @@ bool PhoneNumberMatcher::VerifyAccordingToLeniency(
!IsNationalPrefixPresentIfRequired(number)) { !IsNationalPrefixPresentIfRequired(number)) {
return false; return false;
} }
ResultCallback4<bool, const PhoneNumberUtil&, const PhoneNumber&,
const string&, const std::vector<string>&>* callback =
NewPermanentCallback(
this, &PhoneNumberMatcher::AllNumberGroupsAreExactlyPresent);
bool is_valid = CheckNumberGroupingIsValid(number, candidate, callback);
delete(callback);
return is_valid;
return CheckNumberGroupingIsValid(
number, candidate,
std::bind_front(&PhoneNumberMatcher::AllNumberGroupsAreExactlyPresent,
this));
} }
default: default:
LOG(ERROR) << "No implementation defined for verification for leniency " LOG(ERROR) << "No implementation defined for verification for leniency "
@ -691,17 +685,16 @@ bool PhoneNumberMatcher::Find(int index, PhoneNumberMatch* match) {
} }
bool PhoneNumberMatcher::CheckNumberGroupingIsValid( bool PhoneNumberMatcher::CheckNumberGroupingIsValid(
const PhoneNumber& phone_number,
const string& candidate,
ResultCallback4<bool, const PhoneNumberUtil&, const PhoneNumber&,
const string&, const std::vector<string>&>* checker) const {
DCHECK(checker);
const PhoneNumber &phone_number, const string &candidate,
std::function<bool(const PhoneNumberUtil &, const PhoneNumber &,
const std::string &, const std::vector<std::string> &)>
checker) const {
string normalized_candidate = string normalized_candidate =
NormalizeUTF8::NormalizeDecimalDigits(candidate); NormalizeUTF8::NormalizeDecimalDigits(candidate);
std::vector<string> formatted_number_groups; std::vector<string> formatted_number_groups;
GetNationalNumberGroups(phone_number, &formatted_number_groups); GetNationalNumberGroups(phone_number, &formatted_number_groups);
if (checker->Run(phone_util_, phone_number, normalized_candidate,
formatted_number_groups)) {
if (checker(phone_util_, phone_number, normalized_candidate,
formatted_number_groups)) {
return true; return true;
} }
// If this didn't pass, see if there are any alternate formats that match, and // If this didn't pass, see if there are any alternate formats that match, and
@ -730,8 +723,8 @@ bool PhoneNumberMatcher::CheckNumberGroupingIsValid(
formatted_number_groups.clear(); formatted_number_groups.clear();
GetNationalNumberGroupsForPattern(phone_number, &*it, GetNationalNumberGroupsForPattern(phone_number, &*it,
&formatted_number_groups); &formatted_number_groups);
if (checker->Run(phone_util_, phone_number, normalized_candidate,
formatted_number_groups)) {
if (checker(phone_util_, phone_number, normalized_candidate,
formatted_number_groups)) {
return true; return true;
} }
} }


+ 5
- 5
cpp/src/phonenumbers/phonenumbermatcher.h View File

@ -22,12 +22,12 @@
#ifndef I18N_PHONENUMBERS_PHONENUMBERMATCHER_H_ #ifndef I18N_PHONENUMBERS_PHONENUMBERMATCHER_H_
#define I18N_PHONENUMBERS_PHONENUMBERMATCHER_H_ #define I18N_PHONENUMBERS_PHONENUMBERMATCHER_H_
#include <functional>
#include <string> #include <string>
#include <vector> #include <vector>
#include "phonenumbers/base/basictypes.h" #include "phonenumbers/base/basictypes.h"
#include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/base/memory/scoped_ptr.h"
#include "phonenumbers/callback.h"
#include "phonenumbers/regexp_adapter.h" #include "phonenumbers/regexp_adapter.h"
namespace i18n { namespace i18n {
@ -134,10 +134,10 @@ class PhoneNumberMatcher {
PhoneNumberMatch* match); PhoneNumberMatch* match);
bool CheckNumberGroupingIsValid( bool CheckNumberGroupingIsValid(
const PhoneNumber& phone_number,
const string& candidate,
ResultCallback4<bool, const PhoneNumberUtil&, const PhoneNumber&,
const string&, const vector<string>&>* checker) const;
const PhoneNumber &phone_number, const string &candidate,
std::function<bool(const PhoneNumberUtil &, const PhoneNumber &,
const std::string &, const std::vector<std::string> &)>
checker) const;
// Helper method to get the national-number part of a number, formatted // Helper method to get the national-number part of a number, formatted
// without any national prefix, and return it as a set of digit blocks that // without any national prefix, and return it as a set of digit blocks that


Loading…
Cancel
Save