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 // Implementation of a mutable match of a phone number within a piece of
18 // text. Matches may be found using PhoneNumberUtil::FindNumbers.
19
20 #include "phonenumbers/phonenumbermatch.h"
21
22 #include <string>
23
24 #include "phonenumbers/phonenumber.h"
25 #include "phonenumbers/phonenumber.pb.h"
26 #include "phonenumbers/stringutil.h"
27
28 namespace i18n {
29 namespace phonenumbers {
30
PhoneNumberMatch(int start,const string & raw_string,const PhoneNumber & number)31 PhoneNumberMatch::PhoneNumberMatch(int start,
32 const string& raw_string,
33 const PhoneNumber& number)
34 : start_(start), raw_string_(raw_string), number_(number) {
35 }
36
PhoneNumberMatch()37 PhoneNumberMatch::PhoneNumberMatch()
38 : start_(-1), raw_string_(""), number_(PhoneNumber::default_instance()) {
39 }
40
number() const41 const PhoneNumber& PhoneNumberMatch::number() const {
42 return number_;
43 }
44
start() const45 int PhoneNumberMatch::start() const {
46 return start_;
47 }
48
end() const49 int PhoneNumberMatch::end() const {
50 return static_cast<int>(start_ + raw_string_.length());
51 }
52
length() const53 int PhoneNumberMatch::length() const {
54 return static_cast<int>(raw_string_.length());
55 }
56
raw_string() const57 const string& PhoneNumberMatch::raw_string() const {
58 return raw_string_;
59 }
60
set_start(int start)61 void PhoneNumberMatch::set_start(int start) {
62 start_ = start;
63 }
64
set_raw_string(const string & raw_string)65 void PhoneNumberMatch::set_raw_string(const string& raw_string) {
66 raw_string_ = raw_string;
67 }
68
set_number(const PhoneNumber & number)69 void PhoneNumberMatch::set_number(const PhoneNumber& number) {
70 number_.CopyFrom(number);
71 }
72
ToString() const73 string PhoneNumberMatch::ToString() const {
74 return StrCat("PhoneNumberMatch [", start(), ",", end(), ") ",
75 raw_string_.c_str());
76 }
77
Equals(const PhoneNumberMatch & match) const78 bool PhoneNumberMatch::Equals(const PhoneNumberMatch& match) const {
79 return ExactlySameAs(match.number_, number_) &&
80 match.raw_string_.compare(raw_string_) == 0 &&
81 match.start_ == start_;
82 }
83
CopyFrom(const PhoneNumberMatch & match)84 void PhoneNumberMatch::CopyFrom(const PhoneNumberMatch& match) {
85 raw_string_ = match.raw_string();
86 start_ = match.start();
87 number_ = match.number();
88 }
89
90 } // namespace phonenumbers
91 } // namespace i18n
92