Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(822)

Side by Side Diff: third_party/libphonenumber/cpp/src/regexp_adapter_re2.cc

Issue 6803005: Autofill phone number enhancements and integration of Phone Number Util Library: part 1 (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 #include "regexp_adapter.h"
17
18 #include <re2/re2.h>
19 #include <re2/stringpiece.h>
20 #include <re2/re2.h>
21
22 namespace {
23 scoped_ptr<RE2Cache> re2_cache;
24 } // namespace
25
26 class RE2RegularExpressionInput : public RegularExpressionInput {
27 public:
28 RE2RegularExpressionInput(const char* utf8_input);
29
30 virtual bool ConsumeRegExp(std::string const& reg_exp,
31 bool beginning_only,
32 std::string* matched_string1,
33 std::string* matched_string2);
34 virtual std::string ToString() const;
35 private:
36 StringPiece utf8_input_;
37 };
38
39
40 class RE2RegularExpression : public reg_exp::RegularExpression {
41 public:
42 RE2RegularExpression(const char* utf8_regexp);
43
44 virtual bool Consume(reg_exp::RegularExpressionInput* input_string,
45 bool beginning_only,
46 std::string* matched_string1,
47 std::string* matched_string2,
48 std::string* matched_string3) const;
49
50 virtual bool Match(const char* input_string,
51 bool full_match,
52 std::string* matched_string) const;
53
54 virtual bool Replace(std::string* string_to_process,
55 bool global,
56 const char* replacement_string) const;
57 private:
58 RE2 utf8_regexp_;
59 };
60
61 RE2RegularExpressionInput::RE2RegularExpressionInput(const char* utf8_input)
62 : utf8_input_(utf8_input) {
63 DCHECK(utf8_input);
64 }
65
66 bool RE2RegularExpressionInput::ConsumeRegExp(std::string const& reg_exp,
67 bool beginning_only,
68 std::string* matched_string1,
69 std::string* matched_string2) {
70 if (beginning_only) {
71 if (matched_string2)
72 return RE2::Consume(&utf8_input_,
73 RE2Cache::ScopedAccess(re2_cache.get(), reg_exp),
74 matched_string1, matched_string2);
75 else if (matched_string1)
76 return RE2::Consume(&utf8_input_,
77 RE2Cache::ScopedAccess(re2_cache.get(), reg_exp),
78 matched_string1);
79 else
80 return RE2::Consume(&utf8_input_,
81 RE2Cache::ScopedAccess(re2_cache.get(), reg_exp));
82 } else {
83 if (matched_string2)
84 return RE2::FindAndConsume(&utf8_input_,
85 RE2Cache::ScopedAccess(re2_cache.get(),
86 reg_exp),
87 matched_string1, matched_string2);
88 else if (matched_string1)
89 return RE2::FindAndConsume(&utf8_input_,
90 RE2Cache::ScopedAccess(re2_cache.get(),
91 reg_exp),
92 matched_string1);
93 else
94 return RE2::FindAndConsume(&utf8_input_,
95 RE2Cache::ScopedAccess(re2_cache.get(),
96 reg_exp));
97 }
98 }
99
100 std::string RE2RegularExpressionInput::ToString() const {
101 utf8_input_.ToString();
102 }
103
104 RE2RegularExpression::RE2RegularExpression(const char* utf8_regexp)
105 : utf8_regexp_(utf8_regexp) {
106 DCHECK(utf8_regexp);
107 }
108
109 bool RE2RegularExpression::Consume(RegularExpressionInput* input_string,
110 bool beginning_only,
111 std::string* matched_string1,
112 std::string* matched_string2,
113 std::string* matched_string3) const {
114 DCHECK(input_string);
115 // matched_string1 may be NULL
116 // matched_string2 may be NULL
117 if (beginning_only) {
118 if (matched_string3) {
119 return RE2::Consume(input_string, utf8_regexp_,
120 matched_string1, matched_string2, matched_string3);
121 } else if (matched_string2) {
122 return RE2::Consume(input_string, utf8_regexp_,
123 matched_string1, matched_string2);
124 } else if (matched_string1) {
125 return RE2::Consume(input_string, utf8_regexp_, matched_string1);
126 } else {
127 return RE2::Consume(input_string, utf8_regexp_);
128 }
129 } else {
130 if (matched_string3) {
131 return RE2::FindAndConsume(input_string, utf8_regexp_,
132 matched_string1, matched_string2,
133 matched_string3);
134 } else if (matched_string2) {
135 return RE2::FindAndConsume(input_string, utf8_regexp_,
136 matched_string1, matched_string2);
137 } else if (matched_string1) {
138 return RE2::FindAndConsume(input_string, utf8_regexp_, matched_string1);
139 } else {
140 return RE2::FindAndConsume(input_string, utf8_regexp_);
141 }
142 }
143 }
144
145 bool RE2RegularExpression::Match(const char* input_string,
146 bool full_match,
147 std::string* matched_string) const {
148 DCHECK(input_string);
149 // matched_string may be NULL
150 if (full_match) {
151 if (matched_string)
152 return RE2::FullMatch(input_string, matched_string);
153 else
154 return RE2::FullMatch(input_string);
155 } else {
156 if (matched_string)
157 return RE2::PartialMatch(input_string, matched_string);
158 else
159 return RE2::PartialMatch(input_string);
160 }
161 }
162
163 bool RE2RegularExpression::Replace(std::string* string_to_process,
164 bool global,
165 const char* replacement_string) const {
166 DCHECK(string_to_process);
167 DCHECK(replacement_string);
168 if (global) {
169 StringPiece str(replacement_string);
170 return RE2::GlobalReplace(string_to_process, str);
171 } else {
172 return RE2::Replace(string_to_process, replacement_string);
173 }
174 }
175
176
177 namespace reg_exp {
178
179 RegularExpressionInput* CreateRegularExpressionInput(const char* utf8_input) {
180 if (!re2_cache.get())
181 re2_cache.reset(new RE2Cache(64));
182 return new RE2RegularExpressionInput(utf8_input);
183 }
184
185 RegularExpression* CreateRegularExpression(const char* utf8_regexp) {
186 if (!re2_cache.get())
187 re2_cache.reset(new RE2Cache(64));
188 return new RE2RegularExpression(utf8_regexp);
189 }
190
191 } // namespace reg_exp
192
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698