11767c5feSopenharmony_ci// Copyright (C) 2011 The Libphonenumber Authors
21767c5feSopenharmony_ci//
31767c5feSopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
41767c5feSopenharmony_ci// you may not use this file except in compliance with the License.
51767c5feSopenharmony_ci// You may obtain a copy of the License at
61767c5feSopenharmony_ci//
71767c5feSopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0
81767c5feSopenharmony_ci//
91767c5feSopenharmony_ci// Unless required by applicable law or agreed to in writing, software
101767c5feSopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
111767c5feSopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121767c5feSopenharmony_ci// See the License for the specific language governing permissions and
131767c5feSopenharmony_ci// limitations under the License.
141767c5feSopenharmony_ci//
151767c5feSopenharmony_ci// Author: Tao Huang
161767c5feSopenharmony_ci//
171767c5feSopenharmony_ci// A mutable match of a phone number within a piece of text.
181767c5feSopenharmony_ci// Matches may be found using PhoneNumberUtil::FindNumbers.
191767c5feSopenharmony_ci//
201767c5feSopenharmony_ci// A match consists of the phone number as well as the start and end offsets of
211767c5feSopenharmony_ci// the corresponding subsequence of the searched text. Use raw_string() to
221767c5feSopenharmony_ci// obtain a copy of the matched subsequence.
231767c5feSopenharmony_ci//
241767c5feSopenharmony_ci// The following annotated example clarifies the relationship between the
251767c5feSopenharmony_ci// searched text, the match offsets, and the parsed number:
261767c5feSopenharmony_ci//
271767c5feSopenharmony_ci// string text = "Call me at +1 425 882-8080 for details.";
281767c5feSopenharmony_ci// const string country = "US";
291767c5feSopenharmony_ci//
301767c5feSopenharmony_ci// // Find the first phone number match:
311767c5feSopenharmony_ci// PhoneNumberMatcher matcher(text, country);
321767c5feSopenharmony_ci// if (matcher.HasNext()) {
331767c5feSopenharmony_ci//   PhoneNumberMatch match;
341767c5feSopenharmony_ci//   matcher.Next(&match);
351767c5feSopenharmony_ci// }
361767c5feSopenharmony_ci//
371767c5feSopenharmony_ci// // raw_string() contains the phone number as it appears in the text.
381767c5feSopenharmony_ci// "+1 425 882-8080" == match.raw_string();
391767c5feSopenharmony_ci//
401767c5feSopenharmony_ci// // start() and end() define the range of the matched subsequence.
411767c5feSopenharmony_ci// string subsequence = text.substr(match.start(), match.end());
421767c5feSopenharmony_ci// "+1 425 882-8080" == subsequence;
431767c5feSopenharmony_ci//
441767c5feSopenharmony_ci// // number() returns the the same result as PhoneNumberUtil::Parse()
451767c5feSopenharmony_ci// // invoked on raw_string().
461767c5feSopenharmony_ci// const PhoneNumberUtil& util = *PhoneNumberUtil::GetInstance();
471767c5feSopenharmony_ci// util.Parse(match.raw_string(), country).Equals(match.number());
481767c5feSopenharmony_ci//
491767c5feSopenharmony_ci// This class is a port of PhoneNumberMatch.java
501767c5feSopenharmony_ci
511767c5feSopenharmony_ci#ifndef I18N_PHONENUMBERS_PHONENUMBERMATCH_H_
521767c5feSopenharmony_ci#define I18N_PHONENUMBERS_PHONENUMBERMATCH_H_
531767c5feSopenharmony_ci
541767c5feSopenharmony_ci#include <string>
551767c5feSopenharmony_ci
561767c5feSopenharmony_ci#include "phonenumbers/base/basictypes.h"
571767c5feSopenharmony_ci#include "phonenumbers/phonenumber.pb.h"
581767c5feSopenharmony_ci
591767c5feSopenharmony_cinamespace i18n {
601767c5feSopenharmony_cinamespace phonenumbers {
611767c5feSopenharmony_ci
621767c5feSopenharmony_ciusing std::string;
631767c5feSopenharmony_ci
641767c5feSopenharmony_ciclass PhoneNumberMatch {
651767c5feSopenharmony_ci public:
661767c5feSopenharmony_ci  // Creates a new match.
671767c5feSopenharmony_ci  // - start is the index into the target text.
681767c5feSopenharmony_ci  // - match is the matched string of the target text.
691767c5feSopenharmony_ci  // - number is the matched phone number.
701767c5feSopenharmony_ci  PhoneNumberMatch(int start,
711767c5feSopenharmony_ci                   const string& raw_string,
721767c5feSopenharmony_ci                   const PhoneNumber& number);
731767c5feSopenharmony_ci
741767c5feSopenharmony_ci  // Default constructor.
751767c5feSopenharmony_ci  PhoneNumberMatch();
761767c5feSopenharmony_ci
771767c5feSopenharmony_ci  // This type is neither copyable nor movable.
781767c5feSopenharmony_ci  PhoneNumberMatch(const PhoneNumberMatch&) = delete;
791767c5feSopenharmony_ci  PhoneNumberMatch& operator=(const PhoneNumberMatch&) = delete;
801767c5feSopenharmony_ci
811767c5feSopenharmony_ci  ~PhoneNumberMatch() {}
821767c5feSopenharmony_ci
831767c5feSopenharmony_ci  // Returns the phone number matched by the receiver.
841767c5feSopenharmony_ci  const PhoneNumber& number() const;
851767c5feSopenharmony_ci
861767c5feSopenharmony_ci  // Returns the start index of the matched phone number within the searched
871767c5feSopenharmony_ci  // text.
881767c5feSopenharmony_ci  int start() const;
891767c5feSopenharmony_ci
901767c5feSopenharmony_ci  // Returns the exclusive end index of the matched phone number within the
911767c5feSopenharmony_ci  // searched text.
921767c5feSopenharmony_ci  int end() const;
931767c5feSopenharmony_ci
941767c5feSopenharmony_ci  // Returns the length of the text matched in the searched text.
951767c5feSopenharmony_ci  int length() const;
961767c5feSopenharmony_ci
971767c5feSopenharmony_ci  // Returns the raw string matched as a phone number in the searched text.
981767c5feSopenharmony_ci  const string& raw_string() const;
991767c5feSopenharmony_ci
1001767c5feSopenharmony_ci  // Returns a string containing debug information.
1011767c5feSopenharmony_ci  string ToString() const;
1021767c5feSopenharmony_ci
1031767c5feSopenharmony_ci  void set_start(int start);
1041767c5feSopenharmony_ci
1051767c5feSopenharmony_ci  void set_raw_string(const string& raw_string);
1061767c5feSopenharmony_ci
1071767c5feSopenharmony_ci  void set_number(const PhoneNumber& number);
1081767c5feSopenharmony_ci
1091767c5feSopenharmony_ci  bool Equals(const PhoneNumberMatch& number) const;
1101767c5feSopenharmony_ci
1111767c5feSopenharmony_ci  void CopyFrom(const PhoneNumberMatch& number);
1121767c5feSopenharmony_ci
1131767c5feSopenharmony_ci private:
1141767c5feSopenharmony_ci  // The start index into the text.
1151767c5feSopenharmony_ci  int start_;
1161767c5feSopenharmony_ci
1171767c5feSopenharmony_ci  // The raw substring matched.
1181767c5feSopenharmony_ci  string raw_string_;
1191767c5feSopenharmony_ci
1201767c5feSopenharmony_ci  // The matched phone number.
1211767c5feSopenharmony_ci  PhoneNumber number_;
1221767c5feSopenharmony_ci
1231767c5feSopenharmony_ci};
1241767c5feSopenharmony_ci
1251767c5feSopenharmony_ci}  // namespace phonenumbers
1261767c5feSopenharmony_ci}  // namespace i18n
1271767c5feSopenharmony_ci
1281767c5feSopenharmony_ci#endif  // I18N_PHONENUMBERS_PHONENUMBERMATCH_H_
129