• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2011 The Libphonenumber Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Author: Tao Huang
16 //
17 // A mutable match of a phone number within a piece of text.
18 // Matches may be found using PhoneNumberUtil::FindNumbers.
19 //
20 // A match consists of the phone number as well as the start and end offsets of
21 // the corresponding subsequence of the searched text. Use raw_string() to
22 // obtain a copy of the matched subsequence.
23 //
24 // The following annotated example clarifies the relationship between the
25 // searched text, the match offsets, and the parsed number:
26 //
27 // string text = "Call me at +1 425 882-8080 for details.";
28 // const string country = "US";
29 //
30 // // Find the first phone number match:
31 // PhoneNumberMatcher matcher(text, country);
32 // if (matcher.HasNext()) {
33 //   PhoneNumberMatch match;
34 //   matcher.Next(&match);
35 // }
36 //
37 // // raw_string() contains the phone number as it appears in the text.
38 // "+1 425 882-8080" == match.raw_string();
39 //
40 // // start() and end() define the range of the matched subsequence.
41 // string subsequence = text.substr(match.start(), match.end());
42 // "+1 425 882-8080" == subsequence;
43 //
44 // // number() returns the the same result as PhoneNumberUtil::Parse()
45 // // invoked on raw_string().
46 // const PhoneNumberUtil& util = *PhoneNumberUtil::GetInstance();
47 // util.Parse(match.raw_string(), country).Equals(match.number());
48 //
49 // This class is a port of PhoneNumberMatch.java
50 
51 #ifndef I18N_PHONENUMBERS_PHONENUMBERMATCH_H_
52 #define I18N_PHONENUMBERS_PHONENUMBERMATCH_H_
53 
54 #include <string>
55 
56 #include "phonenumbers/base/basictypes.h"
57 #include "phonenumbers/phonenumber.pb.h"
58 
59 namespace i18n {
60 namespace phonenumbers {
61 
62 using std::string;
63 
64 class PhoneNumberMatch {
65  public:
66   // Creates a new match.
67   // - start is the index into the target text.
68   // - match is the matched string of the target text.
69   // - number is the matched phone number.
70   PhoneNumberMatch(int start,
71                    const string& raw_string,
72                    const PhoneNumber& number);
73 
74   // Default constructor.
75   PhoneNumberMatch();
76 
77   // This type is neither copyable nor movable.
78   PhoneNumberMatch(const PhoneNumberMatch&) = delete;
79   PhoneNumberMatch& operator=(const PhoneNumberMatch&) = delete;
80 
~PhoneNumberMatch()81   ~PhoneNumberMatch() {}
82 
83   // Returns the phone number matched by the receiver.
84   const PhoneNumber& number() const;
85 
86   // Returns the start index of the matched phone number within the searched
87   // text.
88   int start() const;
89 
90   // Returns the exclusive end index of the matched phone number within the
91   // searched text.
92   int end() const;
93 
94   // Returns the length of the text matched in the searched text.
95   int length() const;
96 
97   // Returns the raw string matched as a phone number in the searched text.
98   const string& raw_string() const;
99 
100   // Returns a string containing debug information.
101   string ToString() const;
102 
103   void set_start(int start);
104 
105   void set_raw_string(const string& raw_string);
106 
107   void set_number(const PhoneNumber& number);
108 
109   bool Equals(const PhoneNumberMatch& number) const;
110 
111   void CopyFrom(const PhoneNumberMatch& number);
112 
113  private:
114   // The start index into the text.
115   int start_;
116 
117   // The raw substring matched.
118   string raw_string_;
119 
120   // The matched phone number.
121   PhoneNumber number_;
122 
123 };
124 
125 }  // namespace phonenumbers
126 }  // namespace i18n
127 
128 #endif  // I18N_PHONENUMBERS_PHONENUMBERMATCH_H_
129