blob: 8cc7dc3f285a58bb8c9e1c0f9b98363925c26302 [file] [log] [blame]
philip.liard@gmail.com6a0a07f2011-09-21 17:43:54 +00001// 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
philip.liard@gmail.comaf4a2ce2013-04-30 11:35:55 +000056#include "phonenumbers/base/basictypes.h"
philip.liard@gmail.com6a0a07f2011-09-21 17:43:54 +000057#include "phonenumbers/phonenumber.pb.h"
58
59namespace i18n {
60namespace phonenumbers {
61
62using std::string;
63
64class 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 ~PhoneNumberMatch() {}
78
79 // Returns the phone number matched by the receiver.
80 const PhoneNumber& number() const;
81
82 // Returns the start index of the matched phone number within the searched
83 // text.
84 int start() const;
85
86 // Returns the exclusive end index of the matched phone number within the
87 // searched text.
88 int end() const;
89
90 // Returns the length of the text matched in the searched text.
91 int length() const;
92
93 // Returns the raw string matched as a phone number in the searched text.
94 const string& raw_string() const;
95
96 // Returns a string containing debug information.
97 string ToString() const;
98
99 void set_start(int start);
100
101 void set_raw_string(const string& raw_string);
102
103 void set_number(const PhoneNumber& number);
104
105 bool Equals(const PhoneNumberMatch& number) const;
106
107 void CopyFrom(const PhoneNumberMatch& number);
108
109 private:
110 // The start index into the text.
111 int start_;
112
113 // The raw substring matched.
114 string raw_string_;
115
116 // The matched phone number.
117 PhoneNumber number_;
118
119 DISALLOW_COPY_AND_ASSIGN(PhoneNumberMatch);
120};
121
122} // namespace phonenumbers
123} // namespace i18n
124
125#endif // I18N_PHONENUMBERS_PHONENUMBERMATCH_H_