OLD | NEW |
| (Empty) |
1 // Copyright (C) 2011 Google Inc. | |
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: George Yakovlev | |
16 // Philippe Liard | |
17 // | |
18 // Regexp adapter to allow a pluggable regexp engine. It has been introduced | |
19 // during the integration of the open-source version of this library into | |
20 // Chromium to be able to use the ICU Regex engine instead of RE2, which is not | |
21 // officially supported on Windows. | |
22 // Since RE2 was initially used in this library, the interface of this adapter | |
23 // is very close to the subset of the RE2 API used in phonenumberutil.cc. | |
24 | |
25 #ifndef I18N_PHONENUMBERS_REGEXP_ADAPTER_H_ | |
26 #define I18N_PHONENUMBERS_REGEXP_ADAPTER_H_ | |
27 | |
28 #include <cstddef> | |
29 #include <string> | |
30 | |
31 namespace i18n { | |
32 namespace phonenumbers { | |
33 | |
34 using std::string; | |
35 | |
36 // RegExpInput is the interface that abstracts the input that feeds the | |
37 // Consume() method of RegExp which may differ depending on its various | |
38 // implementations (StringPiece for RE2, UnicodeString for ICU Regex). | |
39 class RegExpInput { | |
40 public: | |
41 virtual ~RegExpInput() {} | |
42 | |
43 // Creates a new instance of the default RegExpInput implementation. The | |
44 // deletion of the returned instance is under the responsibility of the | |
45 // caller. | |
46 static RegExpInput* Create(const string& utf8_input); | |
47 | |
48 // Converts to a C++ string. | |
49 virtual string ToString() const = 0; | |
50 }; | |
51 | |
52 // The regular expression abstract class. It supports only functions used in | |
53 // phonenumberutil.cc. Consume(), Match() and Replace() methods must be | |
54 // implemented. | |
55 class RegExp { | |
56 public: | |
57 virtual ~RegExp() {} | |
58 | |
59 // Creates a new instance of the default RegExp implementation. The deletion | |
60 // of the returned instance is under the responsibility of the caller. | |
61 static RegExp* Create(const string& utf8_regexp); | |
62 | |
63 // Matches string to regular expression, returns true if expression was | |
64 // matched, false otherwise, advances position in the match. | |
65 // input_string - string to be searched. | |
66 // anchor_at_start - if true, match would be successful only if it appears at | |
67 // the beginning of the tested region of the string. | |
68 // matched_string1 - the first string extracted from the match. Can be NULL. | |
69 // matched_string2 - the second string extracted from the match. Can be NULL. | |
70 // matched_string3 - the third string extracted from the match. Can be NULL. | |
71 virtual bool Consume(RegExpInput* input_string, | |
72 bool anchor_at_start, | |
73 string* matched_string1, | |
74 string* matched_string2, | |
75 string* matched_string3) const = 0; | |
76 | |
77 // Helper methods calling the Consume method that assume the match must start | |
78 // at the beginning. | |
79 inline bool Consume(RegExpInput* input_string, | |
80 string* matched_string1, | |
81 string* matched_string2, | |
82 string* matched_string3) const { | |
83 return Consume(input_string, true, matched_string1, matched_string2, | |
84 matched_string3); | |
85 } | |
86 | |
87 inline bool Consume(RegExpInput* input_string, | |
88 string* matched_string1, | |
89 string* matched_string2) const { | |
90 return Consume(input_string, true, matched_string1, matched_string2, NULL); | |
91 } | |
92 | |
93 inline bool Consume(RegExpInput* input_string, string* matched_string) const { | |
94 return Consume(input_string, true, matched_string, NULL, NULL); | |
95 } | |
96 | |
97 inline bool Consume(RegExpInput* input_string) const { | |
98 return Consume(input_string, true, NULL, NULL, NULL); | |
99 } | |
100 | |
101 // Helper method calling the Consume method that assumes the match can start | |
102 // at any place in the string. | |
103 inline bool FindAndConsume(RegExpInput* input_string, | |
104 string* matched_string) const { | |
105 return Consume(input_string, false, matched_string, NULL, NULL); | |
106 } | |
107 | |
108 // Matches string to regular expression, returns true if the expression was | |
109 // matched, false otherwise. | |
110 // input_string - string to be searched. | |
111 // full_match - if true, match would be successful only if it matches the | |
112 // complete string. | |
113 // matched_string - the string extracted from the match. Can be NULL. | |
114 virtual bool Match(const string& input_string, | |
115 bool full_match, | |
116 string* matched_string) const = 0; | |
117 | |
118 // Helper methods calling the Match method with the right arguments. | |
119 inline bool PartialMatch(const string& input_string, | |
120 string* matched_string) const { | |
121 return Match(input_string, false, matched_string); | |
122 } | |
123 | |
124 inline bool PartialMatch(const string& input_string) const { | |
125 return Match(input_string, false, NULL); | |
126 } | |
127 | |
128 inline bool FullMatch(const string& input_string, | |
129 string* matched_string) const { | |
130 return Match(input_string, true, matched_string); | |
131 } | |
132 | |
133 inline bool FullMatch(const string& input_string) const { | |
134 return Match(input_string, true, NULL); | |
135 } | |
136 | |
137 // Replaces match(es) in 'string_to_process'. If 'global' is true, | |
138 // replaces all the matches, otherwise only the first match. | |
139 // replacement_string - text the matches are replaced with. The groups in the | |
140 // replacement string are referenced with the $[0-9] notation. | |
141 // Returns true if the pattern matches and a replacement occurs, false | |
142 // otherwise. | |
143 virtual bool Replace(string* string_to_process, | |
144 bool global, | |
145 const string& replacement_string) const = 0; | |
146 | |
147 // Helper methods calling the Replace method with the right arguments. | |
148 inline bool Replace(string* string_to_process, | |
149 const string& replacement_string) const { | |
150 return Replace(string_to_process, false, replacement_string); | |
151 } | |
152 | |
153 inline bool GlobalReplace(string* string_to_process, | |
154 const string& replacement_string) const { | |
155 return Replace(string_to_process, true, replacement_string); | |
156 } | |
157 }; | |
158 | |
159 } // namespace phonenumbers | |
160 } // namespace i18n | |
161 | |
162 #endif // I18N_PHONENUMBERS_REGEXP_ADAPTER_H_ | |
OLD | NEW |