summaryrefslogtreecommitdiff
path: root/phonenumberutil.h
diff options
context:
space:
mode:
Diffstat (limited to 'phonenumberutil.h')
-rw-r--r--phonenumberutil.h81
1 files changed, 53 insertions, 28 deletions
diff --git a/phonenumberutil.h b/phonenumberutil.h
index d21542a..528e584 100644
--- a/phonenumberutil.h
+++ b/phonenumberutil.h
@@ -28,9 +28,9 @@
#include <utility>
#include <vector>
-#include "base/basictypes.h"
-#include "base/memory/scoped_ptr.h"
-#include "base/memory/singleton.h"
+#include "phonenumbers/base/basictypes.h"
+#include "phonenumbers/base/memory/scoped_ptr.h"
+#include "phonenumbers/base/memory/singleton.h"
#include "phonenumbers/phonenumber.pb.h"
class TelephoneNumber;
@@ -51,7 +51,6 @@ class AsYouTypeFormatter;
class Logger;
class NumberFormat;
class PhoneMetadata;
-class PhoneNumberMatcherRegExps;
class PhoneNumberRegExpsAndMappings;
class RegExp;
@@ -60,19 +59,18 @@ class RegExp;
// codes can be found here:
// http://www.iso.org/iso/english_country_names_and_code_elements
-#ifdef USE_GOOGLE_BASE
-class PhoneNumberUtil {
- friend struct DefaultSingletonTraits<PhoneNumberUtil>;
-#else
class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
- friend class Singleton<PhoneNumberUtil>;
-#endif
+ private:
friend class AsYouTypeFormatter;
friend class PhoneNumberMatcher;
friend class PhoneNumberMatcherRegExps;
friend class PhoneNumberMatcherTest;
friend class PhoneNumberRegExpsAndMappings;
friend class PhoneNumberUtilTest;
+ friend class ShortNumberUtil;
+ friend class ShortNumberUtilTest;
+ friend class Singleton<PhoneNumberUtil>;
+
public:
~PhoneNumberUtil();
static const char kRegionCodeForNonGeoEntity[];
@@ -81,10 +79,11 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// in ITU-T Recommendation E. 123. For example, the number of the Google
// Zürich office will be written as "+41 44 668 1800" in INTERNATIONAL
// format, and as "044 668 1800" in NATIONAL format. E164 format is as per
- // INTERNATIONAL format but with no formatting applied e.g. +41446681800.
+ // INTERNATIONAL format but with no formatting applied e.g. "+41446681800".
// RFC3966 is as per INTERNATIONAL format, but with all spaces and other
// separating symbols replaced with a hyphen, and with any phone number
- // extension appended with ";ext=".
+ // extension appended with ";ext=". It also will have a prefix of "tel:"
+ // added, e.g. "tel:+41-44-668-1800".
enum PhoneNumberFormat {
E164,
INTERNATIONAL,
@@ -155,16 +154,22 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// for.
void GetSupportedRegions(set<string>* regions) const;
+ // Populates a list with the region codes that match the specific country
+ // calling code. For non-geographical country calling codes, the region code
+ // 001 is returned. Also, in the case of no region code being found, the list
+ // is left unchanged.
+ void GetRegionCodesForCountryCallingCode(
+ int country_calling_code,
+ list<string>* region_codes) const;
+
// Gets a PhoneNumberUtil instance to carry out international phone number
// formatting, parsing, or validation. The instance is loaded with phone
// number metadata for a number of most commonly used regions, as specified by
// DEFAULT_REGIONS_.
//
// The PhoneNumberUtil is implemented as a singleton. Therefore, calling
- // getInstance multiple times will only result in one instance being created.
-#ifdef USE_GOOGLE_BASE
+ // GetInstance multiple times will only result in one instance being created.
static PhoneNumberUtil* GetInstance();
-#endif
// Returns true if the number is a valid vanity (alpha) number such as 800
// MICROSOFT. A valid vanity number will start with at least 3 digits and will
@@ -194,7 +199,7 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// a way that the resultant subscriber number should be diallable, at least on
// some devices. An example of how this could be used:
//
- // const PhoneNumberUtil& phone_util(PhoneNumberUtil::GetInstance());
+ // const PhoneNumberUtil& phone_util(*PhoneNumberUtil::GetInstance());
// PhoneNumber number;
// phone_util.Parse("16502530000", "US", &number);
// string national_significant_number;
@@ -213,8 +218,8 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// subscriber_number = national_significant_number;
// }
//
- // N.B.: area code is a very ambiguous concept, so the I18N team generally
- // recommends against using it for most purposes, but recommends using the
+ // N.B.: area code is a very ambiguous concept, so the authors generally
+ // recommend against using it for most purposes, but recommend using the
// more general national_number instead. Read the following carefully before
// deciding to use this method:
//
@@ -237,7 +242,7 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// there is a subscriber number part that follows. An example of how this
// could be used:
//
- // const PhoneNumberUtil& phone_util(PhoneNumberUtil::GetInstance());
+ // const PhoneNumberUtil& phone_util(*PhoneNumberUtil::GetInstance());
// PhoneNumber number;
// phone_util.Parse("16502530000", "US", &number);
// string national_significant_number;
@@ -381,6 +386,10 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// are examined.
// This is useful for determining for example whether a particular number is
// valid for Canada, rather than just a valid NANPA number.
+ // Warning: In most cases, you want to use IsValidNumber instead. For
+ // example, this method will mark numbers from British Crown dependencies
+ // such as the Isle of Man as invalid for the region "GB" (United Kingdom),
+ // since it has its own region code, "IM", which may be undesirable.
bool IsValidNumberForRegion(
const PhoneNumber& number,
const string& region_code) const;
@@ -487,6 +496,8 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// particular region is not performed. This can be done separately with
// IsValidNumber().
//
+ // number_to_parse can also be provided in RFC3966 format.
+ //
// default_region represents the country that we are expecting the number to
// be from. This is only used if the number being parsed is not written in
// international format. The country_code for the number in this case would be
@@ -536,9 +547,9 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
MatchType IsNumberMatchWithOneString(const PhoneNumber& first_number,
const string& second_number) const;
- // Overrides the default logging system. The provided logger destruction is
- // handled by this class (i.e don't delete it).
- static void SetLogger(Logger* logger);
+ // Overrides the default logging system. This takes ownership of the provided
+ // logger.
+ void SetLogger(Logger* logger);
// Gets an AsYouTypeFormatter for the specific region.
// Returns an AsYouTypeFormatter object, which could be used to format phone
@@ -565,7 +576,7 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
typedef pair<int, list<string>*> IntRegionsPair;
// The minimum and maximum length of the national significant number.
- static const size_t kMinLengthForNsn = 3;
+ static const size_t kMinLengthForNsn = 2;
// The ITU says the maximum length should be 15, but we have found longer
// numbers in Germany.
static const size_t kMaxLengthForNsn = 16;
@@ -622,15 +633,30 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// in a number, for use when matching.
const string& GetExtnPatternsForMatching() const;
+ // Checks if a number matches the plus chars pattern.
+ bool StartsWithPlusCharsPattern(const string& number) const;
+
// Checks whether a string contains only valid digits.
bool ContainsOnlyValidDigits(const string& s) const;
- // Checks if a format is eligible to be used by the AsYouTypeFormatter.
+ // Checks if a format is eligible to be used by the AsYouTypeFormatter. This
+ // method is here rather than in asyoutypeformatter.h since it depends on the
+ // valid punctuation declared by the phone number util.
bool IsFormatEligibleForAsYouTypeFormatter(const string& format) const;
+ // Helper function to check if the national prefix formatting rule has the
+ // first group only, i.e., does not start with the national prefix.
+ bool FormattingRuleHasFirstGroupOnly(
+ const string& national_prefix_formatting_rule) const;
+
// Trims unwanted end characters from a phone number string.
void TrimUnwantedEndChars(string* number) const;
+ // Tests whether a phone number has a geographical association. It checks if
+ // the number is associated to a certain region in the country where it
+ // belongs to. Note that this doesn't verify if the number is actually in use.
+ bool IsNumberGeographical(const PhoneNumber& phone_number) const;
+
// Helper function to check region code is not unknown or null.
bool IsValidRegionCode(const string& region_code) const;
@@ -651,10 +677,6 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// has already been checked.
int GetCountryCodeForValidRegion(const string& region_code) const;
- void GetRegionCodesForCountryCallingCode(
- int country_calling_code,
- list<string>* region_codes) const;
-
const NumberFormat* ChooseFormattingPatternForNumber(
const RepeatedPtrField<NumberFormat>& available_formats,
const string& national_number) const;
@@ -748,6 +770,9 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
bool check_region,
PhoneNumber* phone_number) const;
+ void BuildNationalNumberForParsing(const string& number_to_parse,
+ string* national_number) const;
+
// Returns true if the number can be dialled from outside the region, or
// unknown. If the number can only be dialled from within the region, returns
// false. Does not check the number is a valid number.