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
28namespace i18n {
29namespace phonenumbers {
30
31PhoneNumberMatch::PhoneNumberMatch(int start,
32                                   const string& raw_string,
33                                   const PhoneNumber& number)
34    : start_(start), raw_string_(raw_string), number_(number) {
35}
36
37PhoneNumberMatch::PhoneNumberMatch()
38    : start_(-1), raw_string_(""), number_(PhoneNumber::default_instance()) {
39}
40
41const PhoneNumber& PhoneNumberMatch::number() const {
42  return number_;
43}
44
45int PhoneNumberMatch::start() const {
46  return start_;
47}
48
49int PhoneNumberMatch::end() const {
50  return start_ + raw_string_.length();
51}
52
53int PhoneNumberMatch::length() const {
54  return raw_string_.length();
55}
56
57const string& PhoneNumberMatch::raw_string() const {
58  return raw_string_;
59}
60
61void PhoneNumberMatch::set_start(int start) {
62  start_ = start;
63}
64
65void PhoneNumberMatch::set_raw_string(const string& raw_string) {
66  raw_string_ = raw_string;
67}
68
69void PhoneNumberMatch::set_number(const PhoneNumber& number) {
70  number_.CopyFrom(number);
71}
72
73string PhoneNumberMatch::ToString() const {
74  return StrCat("PhoneNumberMatch [", start(), ",", end(), ") ",
75                raw_string_.c_str());
76}
77
78bool 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
84void 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