summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroes@google.com <roes@google.com@ee073f10-1060-11df-b6a4-87a95322a99c>2013-10-08 13:09:13 +0000
committerroes@google.com <roes@google.com@ee073f10-1060-11df-b6a4-87a95322a99c>2013-10-08 13:09:13 +0000
commitb779a05159b4c448605b32f2529b80b12caec300 (patch)
treedcace7542a6747f19f8edf1e02ac2f97f829a203
parentf7b0d3f0da91816c0b3263a640de0c06e7e807d9 (diff)
downloadphonenumbers-b779a05159b4c448605b32f2529b80b12caec300.tar.gz
JS/C++: Added mobile token support to phone number util
git-svn-id: http://libphonenumber.googlecode.com/svn/trunk/cpp/src/phonenumbers@619 ee073f10-1060-11df-b6a4-87a95322a99c
-rw-r--r--phonenumberutil.cc44
-rw-r--r--phonenumberutil.h7
2 files changed, 42 insertions, 9 deletions
diff --git a/phonenumberutil.cc b/phonenumberutil.cc
index 41cfd34..d966a35 100644
--- a/phonenumberutil.cc
+++ b/phonenumberutil.cc
@@ -473,6 +473,9 @@ class PhoneNumberRegExpsAndMappings {
alpha_phone_mappings_.insert(make_pair(c, c));
all_plus_number_grouping_symbols_.insert(make_pair(c, c));
}
+
+ mobile_token_mappings_.insert(make_pair(52, '1'));
+ mobile_token_mappings_.insert(make_pair(54, '9'));
}
// Small string helpers since StrCat has a maximum number of arguments. These
@@ -526,6 +529,12 @@ class PhoneNumberRegExpsAndMappings {
// such as "-" and " ".
map<char32, char> all_plus_number_grouping_symbols_;
+ // Map of country calling codes that use a mobile token before the area code.
+ // One example of when this is relevant is when determining the length of the
+ // national destination code, which should be the length of the area code plus
+ // the length of the mobile token.
+ map<int, char> mobile_token_mappings_;
+
// Pattern that makes it easy to distinguish whether a region has a unique
// international dialing prefix or not. If a region has a unique international
// prefix (e.g. 011 in USA), it will be represented as a string that contains
@@ -610,6 +619,7 @@ class PhoneNumberRegExpsAndMappings {
alpha_mappings_(),
alpha_phone_mappings_(),
all_plus_number_grouping_symbols_(),
+ mobile_token_mappings_(),
unique_international_prefix_(regexp_factory_->CreateRegExp(
/* "[\\d]+(?:[~⁓∼~][\\d]+)?" */
"[\\d]+(?:[~\xE2\x81\x93\xE2\x88\xBC\xEF\xBD\x9E][\\d]+)?")),
@@ -2185,19 +2195,35 @@ int PhoneNumberUtil::GetLengthOfNationalDestinationCode(
third_group = digit_group;
}
}
- string region_code;
- GetRegionCodeForCountryCode(number.country_code(), &region_code);
- if (region_code == "AR" &&
- GetNumberType(number) == MOBILE) {
- // Argentinian mobile numbers, when formatted in the international format,
- // are in the form of +54 9 NDC XXXX.... As a result, we take the length of
- // the third group (NDC) and add 1 for the digit 9, which also forms part of
- // the national significant number.
- return third_group.size() + 1;
+
+ if (GetNumberType(number) == MOBILE) {
+ // For example Argentinian mobile numbers, when formatted in the
+ // international format, are in the form of +54 9 NDC XXXX.... As a result,
+ // we take the length of the third group (NDC) and add the length of the
+ // mobile token, which also forms part of the national significant number.
+ // This assumes that the mobile token is always formatted separately from
+ // the rest of the phone number.
+ string mobile_token;
+ GetCountryMobileToken(number.country_code(), &mobile_token);
+ if (!mobile_token.empty()) {
+ return third_group.size() + mobile_token.size();
+ }
}
return ndc.size();
}
+void PhoneNumberUtil::GetCountryMobileToken(int country_calling_code,
+ string* mobile_token) const {
+ DCHECK(mobile_token);
+ map<int, char>::iterator it = reg_exps_->mobile_token_mappings_.find(
+ country_calling_code);
+ if (it != reg_exps_->mobile_token_mappings_.end()) {
+ *mobile_token = it->second;
+ } else {
+ mobile_token->assign("");
+ }
+}
+
void PhoneNumberUtil::NormalizeDigitsOnly(string* number) const {
DCHECK(number);
const RegExp& non_digits_pattern = reg_exps_->regexp_cache_->GetRegExp(
diff --git a/phonenumberutil.h b/phonenumberutil.h
index ca5bdd0..195628f 100644
--- a/phonenumberutil.h
+++ b/phonenumberutil.h
@@ -272,6 +272,13 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// GetLengthOfGeographicalAreaCode().
int GetLengthOfNationalDestinationCode(const PhoneNumber& number) const;
+ // Returns the mobile token for the provided country calling code if it has
+ // one, otherwise returns an empty string. A mobile token is a number inserted
+ // before the area code when dialing a mobile number from that country from
+ // abroad.
+ void GetCountryMobileToken(int country_calling_code,
+ string* mobile_token) const;
+
// Formats a phone number in the specified format using default rules. Note
// that this does not promise to produce a phone number that the user can
// dial from where they are - although we do format in either NATIONAL or