re2 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/README.md +12 -11
  2. data/ext/re2/re2.cc +343 -400
  3. data/test/re2_test.rb +68 -110
  4. metadata +4 -4
data/README.md CHANGED
@@ -15,14 +15,18 @@ You can then install the library via RubyGems with `gem install re2` or `gem ins
15
15
  Usage
16
16
  -----
17
17
 
18
- You can use re2 as a mostly drop-in replacement for Ruby's own [Regexp][] class:
18
+ You can use re2 as a mostly drop-in replacement for Ruby's own [Regexp][] and [MatchData][] classes:
19
19
 
20
20
  $ irb -rubygems
21
21
  > require 're2'
22
- > r = RE2.compile('w(\d)(\d+)')
23
- => /w(\d)(\d+)/
24
- > r.match("w1234")
25
- => ["w1234", "1", "234"]
22
+ > r = RE2::Regexp.compile('w(\d)(\d+)')
23
+ => #<RE2::Regexp /w(\d)(\d+)/>
24
+ > m = r.match("w1234")
25
+ => #<RE2::MatchData "w1234" 1:"1" 2:"234">
26
+ > m[1]
27
+ => "1"
28
+ > m.string
29
+ => "w1234"
26
30
  > r =~ "w1234"
27
31
  => true
28
32
  > r !~ "bob"
@@ -33,7 +37,7 @@ You can use re2 as a mostly drop-in replacement for Ruby's own [Regexp][] class:
33
37
  Features
34
38
  --------
35
39
 
36
- * Pre-compiling regular expressions with [`RE2.new(re)`](http://code.google.com/p/re2/source/browse/re2/re2.h#96), `RE2.compile(re)` or `RE2(re)` (including specifying options, e.g. `RE2.new("pattern", :case_sensitive => false)`
40
+ * Pre-compiling regular expressions with [`RE2::Regexp.new(re)`](http://code.google.com/p/re2/source/browse/re2/re2.h#96), `RE2::Regexp.compile(re)` or `RE2(re)` (including specifying options, e.g. `RE2::Regexp.new("pattern", :case_sensitive => false)`
37
41
 
38
42
  * Extracting matches with `re2.match(text)` (and an exact number of matches with `re2.match(text, number_of_matches)` such as `re2.match("123-234", 2)`)
39
43
 
@@ -45,15 +49,11 @@ Features
45
49
 
46
50
  * Checking the options for an expression with `re2.options` or individually with `re2.case_sensitive?`
47
51
 
48
- * Performing full matches with [`RE2::FullMatch(text, re)`](http://code.google.com/p/re2/source/browse/re2/re2.h#30)
49
-
50
- * Performing partial matches with [`RE2::PartialMatch(text, re)`](http://code.google.com/p/re2/source/browse/re2/re2.h#82)
51
-
52
52
  * Performing in-place replacement with [`RE2::Replace(str, pattern, replace)`](http://code.google.com/p/re2/source/browse/re2/re2.h#335)
53
53
 
54
54
  * Performing in-place global replacement with [`RE2::GlobalReplace(str, pattern, replace)`](http://code.google.com/p/re2/source/browse/re2/re2.h#352)
55
55
 
56
- * Escaping regular expressions with [`RE2::QuoteMeta(unquoted)`](http://code.google.com/p/re2/source/browse/re2/re2.h#377), `RE2.escape(unquoted)` or `RE2.quote(unquoted)`
56
+ * Escaping regular expressions with [`RE2::Regexp.escape(unquoted)`](http://code.google.com/p/re2/source/browse/re2/re2.h#377), `RE2::Regexp.quote(unquoted)` or `RE2::QuoteMeta(unquoted)`
57
57
 
58
58
  re2.cc should be well-documented so feel free to consult this file to see what can currently be used.
59
59
 
@@ -67,4 +67,5 @@ All feedback should go to the mailing list: ruby.re2@librelist.com
67
67
  [ruby-dev]: http://packages.debian.org/ruby-dev
68
68
  [build-essential]: http://packages.debian.org/build-essential
69
69
  [Regexp]: http://ruby-doc.org/core/classes/Regexp.html
70
+ [MatchData]: http://ruby-doc.org/core/classes/MatchData.html
70
71
 
@@ -19,19 +19,39 @@ extern "C" {
19
19
  # define RSTRING_LEN(x) (RSTRING(x)->len)
20
20
  #endif
21
21
 
22
- struct re2_pattern {
22
+ typedef struct {
23
23
  RE2 *pattern;
24
- };
24
+ } re2_pattern;
25
25
 
26
- VALUE re2_cRE2;
26
+ typedef struct {
27
+ re2::StringPiece *matches;
28
+ int number_of_matches;
29
+ VALUE regexp, string;
30
+ } re2_matchdata;
31
+
32
+ VALUE re2_mRE2, re2_cRegexp, re2_cMatchData;
27
33
 
28
34
  /* Symbols used in RE2 options. */
29
35
  static ID id_utf8, id_posix_syntax, id_longest_match, id_log_errors,
30
36
  id_max_mem, id_literal, id_never_nl, id_case_sensitive,
31
37
  id_perl_classes, id_word_boundary, id_one_line;
32
38
 
39
+ void re2_matchdata_mark(re2_matchdata* self)
40
+ {
41
+ rb_gc_mark(self->regexp);
42
+ rb_gc_mark(self->string);
43
+ }
44
+
45
+ void re2_matchdata_free(re2_matchdata* self)
46
+ {
47
+ if (self->matches) {
48
+ delete[] self->matches;
49
+ }
50
+ free(self);
51
+ }
52
+
33
53
  void
34
- re2_free(re2_pattern* self)
54
+ re2_regexp_free(re2_pattern* self)
35
55
  {
36
56
  if (self->pattern) {
37
57
  delete self->pattern;
@@ -40,10 +60,193 @@ extern "C" {
40
60
  }
41
61
 
42
62
  static VALUE
43
- re2_allocate(VALUE klass)
63
+ re2_matchdata_allocate(VALUE klass)
64
+ {
65
+ re2_matchdata *m;
66
+ return Data_Make_Struct(klass, re2_matchdata, re2_matchdata_mark, re2_matchdata_free, m);
67
+ }
68
+
69
+ /*
70
+ * call-seq:
71
+ * match.string -> string
72
+ *
73
+ * Returns a frozen copy of the string passed into +match+.
74
+ *
75
+ * m = RE2('(\d+)').match("bob 123")
76
+ * m.string #=> "bob 123"
77
+ */
78
+ static VALUE
79
+ re2_matchdata_string(VALUE self)
80
+ {
81
+ re2_matchdata *m;
82
+ Data_Get_Struct(self, re2_matchdata, m);
83
+
84
+ return m->string;
85
+ }
86
+
87
+ /*
88
+ * call-seq:
89
+ * match.size -> integer
90
+ * match.length -> integer
91
+ *
92
+ * Returns the number of elements in the match array (including nils).
93
+ *
94
+ * m = RE2('(\d+)').match("bob 123")
95
+ * m.length #=> 2
96
+ * m.size #=> 2
97
+ */
98
+ static VALUE
99
+ re2_matchdata_size(VALUE self)
100
+ {
101
+ re2_matchdata *m;
102
+ Data_Get_Struct(self, re2_matchdata, m);
103
+
104
+ return INT2FIX(m->number_of_matches);
105
+ }
106
+
107
+ /*
108
+ * call-seq:
109
+ * match.regexp -> RE2::Regexp
110
+ *
111
+ * Return the RE2::Regexp used in the match.
112
+ *
113
+ * m = RE2('(\d+)').match("bob 123")
114
+ * m.regexp #=> #<RE2::Regexp /(\d+)/>
115
+ */
116
+ static VALUE
117
+ re2_matchdata_regexp(VALUE self)
118
+ {
119
+ re2_matchdata *m;
120
+ Data_Get_Struct(self, re2_matchdata, m);
121
+ return m->regexp;
122
+ }
123
+
124
+ static VALUE
125
+ re2_regexp_allocate(VALUE klass)
44
126
  {
45
127
  re2_pattern *p;
46
- return Data_Make_Struct(klass, re2_pattern, 0, re2_free, p);
128
+ return Data_Make_Struct(klass, re2_pattern, 0, re2_regexp_free, p);
129
+ }
130
+
131
+ /*
132
+ * call-seq:
133
+ * match.to_a -> array
134
+ *
135
+ * Returns the array of matches.
136
+ *
137
+ * m = RE2('(\d+)').match("bob 123")
138
+ * m.to_a #=> ["123", "123"]
139
+ */
140
+ static VALUE
141
+ re2_matchdata_to_a(VALUE self)
142
+ {
143
+ int i;
144
+ re2_matchdata *m;
145
+ Data_Get_Struct(self, re2_matchdata, m);
146
+ VALUE array = rb_ary_new2(m->number_of_matches);
147
+ for (i = 0; i < m->number_of_matches; i++) {
148
+ if (m->matches[i].empty()) {
149
+ rb_ary_store(array, i, Qnil);
150
+ } else {
151
+ rb_ary_store(array, i, rb_str_new2(m->matches[i].as_string().c_str()));
152
+ }
153
+ }
154
+ return array;
155
+ }
156
+
157
+ static VALUE
158
+ re2_matchdata_nth_match(int nth, VALUE self)
159
+ {
160
+ re2_matchdata *m;
161
+ Data_Get_Struct(self, re2_matchdata, m);
162
+
163
+ if (nth >= m->number_of_matches || m->matches[nth].empty()) {
164
+ return Qnil;
165
+ } else {
166
+ return rb_str_new2(m->matches[nth].as_string().c_str());
167
+ }
168
+ }
169
+
170
+ /*
171
+ * call-seq:
172
+ * match[i] -> string
173
+ * match[start, length] -> array
174
+ * match[range] -> array
175
+ *
176
+ * Access the match data as an array.
177
+ *
178
+ * m = RE2('(\d+)').match("bob 123")
179
+ * m[0] #=> "123"
180
+ * m[0, 1] #=> ["123"]
181
+ * m[0...1] #=> ["123"]
182
+ * m[0..1] #=> ["123", "123"]
183
+ */
184
+ static VALUE
185
+ re2_matchdata_aref(int argc, VALUE *argv, VALUE self)
186
+ {
187
+ VALUE idx, rest;
188
+ rb_scan_args(argc, argv, "11", &idx, &rest);
189
+
190
+ if (!NIL_P(rest) || !FIXNUM_P(idx) || FIX2INT(idx) < 0) {
191
+ return rb_ary_aref(argc, argv, re2_matchdata_to_a(self));
192
+ } else {
193
+ return re2_matchdata_nth_match(FIX2INT(idx), self);
194
+ }
195
+ }
196
+
197
+ /*
198
+ * call-seq:
199
+ * match.to_s -> string
200
+ *
201
+ * Returns the entire matched string.
202
+ */
203
+ static VALUE
204
+ re2_matchdata_to_s(VALUE self)
205
+ {
206
+ return re2_matchdata_nth_match(0, self);
207
+ }
208
+
209
+ /*
210
+ * call-seq:
211
+ * match.inspect -> string
212
+ *
213
+ * Returns a printable version of the match.
214
+ *
215
+ * m = RE2('(\d+)').match("bob 123")
216
+ * m.inspect #=> "#<RE2::MatchData \"123\" 1:\"123\">"
217
+ */
218
+ static VALUE
219
+ re2_matchdata_inspect(VALUE self)
220
+ {
221
+ int i;
222
+ re2_matchdata *m;
223
+ VALUE match, result;
224
+
225
+ Data_Get_Struct(self, re2_matchdata, m);
226
+
227
+ result = rb_str_buf_new2("#<RE2::MatchData");
228
+
229
+ for (i = 0; i < m->number_of_matches; i++) {
230
+ rb_str_buf_cat2(result, " ");
231
+
232
+ if (i > 0) {
233
+ char buf[sizeof(i)*3+1];
234
+ snprintf(buf, sizeof(buf), "%d", i);
235
+ rb_str_buf_cat2(result, buf);
236
+ rb_str_buf_cat2(result, ":");
237
+ }
238
+
239
+ match = re2_matchdata_nth_match(i, self);
240
+
241
+ if (match == Qnil) {
242
+ rb_str_buf_cat2(result, "nil");
243
+ } else {
244
+ rb_str_buf_append(result, rb_str_inspect(match));
245
+ }
246
+ }
247
+ rb_str_buf_cat2(result, ">");
248
+
249
+ return result;
47
250
  }
48
251
 
49
252
  /*
@@ -58,15 +261,15 @@ extern "C" {
58
261
  re2_re2(int argc, VALUE *argv, VALUE self)
59
262
  {
60
263
  UNUSED(self);
61
- return rb_class_new_instance(argc, argv, re2_cRE2);
264
+ return rb_class_new_instance(argc, argv, re2_cRegexp);
62
265
  }
63
266
 
64
267
  /*
65
268
  * call-seq:
66
- * RE2.new(pattern) -> re2
67
- * RE2.new(pattern, options) -> re2
68
- * RE2.compile(pattern) -> re2
69
- * RE2.compile(pattern, options) -> re2
269
+ * RE2::Regexp.new(pattern) -> re2
270
+ * RE2::Regexp.new(pattern, options) -> re2
271
+ * RE2::Regexp.compile(pattern) -> re2
272
+ * RE2::Regexp.compile(pattern, options) -> re2
70
273
  *
71
274
  * Returns a new RE2 object with a compiled version of
72
275
  * +pattern+ stored inside.
@@ -107,13 +310,12 @@ extern "C" {
107
310
  * when in posix_syntax mode (default false)
108
311
  */
109
312
  static VALUE
110
- re2_initialize(int argc, VALUE *argv, VALUE self)
313
+ re2_regexp_initialize(int argc, VALUE *argv, VALUE self)
111
314
  {
112
315
  VALUE pattern, options, utf8, posix_syntax, longest_match, log_errors,
113
316
  max_mem, literal, never_nl, case_sensitive, perl_classes,
114
317
  word_boundary, one_line;
115
318
  re2_pattern *p;
116
- RE2::Options *re2_options;
117
319
 
118
320
  rb_scan_args(argc, argv, "11", &pattern, &options);
119
321
  Data_Get_Struct(self, re2_pattern, p);
@@ -123,68 +325,64 @@ extern "C" {
123
325
  rb_raise(rb_eArgError, "options should be a hash");
124
326
  }
125
327
 
126
- re2_options = new (std::nothrow) RE2::Options();
127
-
128
- if (re2_options == 0) {
129
- rb_raise(rb_eNoMemError, "not enough memory to allocate RE2::Options");
130
- }
328
+ RE2::Options re2_options;
131
329
 
132
330
  utf8 = rb_hash_aref(options, ID2SYM(id_utf8));
133
331
  if (!NIL_P(utf8)) {
134
- re2_options->set_utf8(RTEST(utf8));
332
+ re2_options.set_utf8(RTEST(utf8));
135
333
  }
136
334
 
137
335
  posix_syntax = rb_hash_aref(options, ID2SYM(id_posix_syntax));
138
336
  if (!NIL_P(posix_syntax)) {
139
- re2_options->set_posix_syntax(RTEST(posix_syntax));
337
+ re2_options.set_posix_syntax(RTEST(posix_syntax));
140
338
  }
141
339
 
142
340
  longest_match = rb_hash_aref(options, ID2SYM(id_longest_match));
143
341
  if (!NIL_P(longest_match)) {
144
- re2_options->set_longest_match(RTEST(longest_match));
342
+ re2_options.set_longest_match(RTEST(longest_match));
145
343
  }
146
344
 
147
345
  log_errors = rb_hash_aref(options, ID2SYM(id_log_errors));
148
346
  if (!NIL_P(log_errors)) {
149
- re2_options->set_log_errors(RTEST(log_errors));
347
+ re2_options.set_log_errors(RTEST(log_errors));
150
348
  }
151
349
 
152
350
  max_mem = rb_hash_aref(options, ID2SYM(id_max_mem));
153
351
  if (!NIL_P(max_mem)) {
154
- re2_options->set_max_mem(NUM2INT(max_mem));
352
+ re2_options.set_max_mem(NUM2INT(max_mem));
155
353
  }
156
354
 
157
355
  literal = rb_hash_aref(options, ID2SYM(id_literal));
158
356
  if (!NIL_P(literal)) {
159
- re2_options->set_literal(RTEST(literal));
357
+ re2_options.set_literal(RTEST(literal));
160
358
  }
161
359
 
162
360
  never_nl = rb_hash_aref(options, ID2SYM(id_never_nl));
163
361
  if (!NIL_P(never_nl)) {
164
- re2_options->set_never_nl(RTEST(never_nl));
362
+ re2_options.set_never_nl(RTEST(never_nl));
165
363
  }
166
364
 
167
365
  case_sensitive = rb_hash_aref(options, ID2SYM(id_case_sensitive));
168
366
  if (!NIL_P(case_sensitive)) {
169
- re2_options->set_case_sensitive(RTEST(case_sensitive));
367
+ re2_options.set_case_sensitive(RTEST(case_sensitive));
170
368
  }
171
369
 
172
370
  perl_classes = rb_hash_aref(options, ID2SYM(id_perl_classes));
173
371
  if (!NIL_P(perl_classes)) {
174
- re2_options->set_perl_classes(RTEST(perl_classes));
372
+ re2_options.set_perl_classes(RTEST(perl_classes));
175
373
  }
176
374
 
177
375
  word_boundary = rb_hash_aref(options, ID2SYM(id_word_boundary));
178
376
  if (!NIL_P(word_boundary)) {
179
- re2_options->set_word_boundary(RTEST(word_boundary));
377
+ re2_options.set_word_boundary(RTEST(word_boundary));
180
378
  }
181
379
 
182
380
  one_line = rb_hash_aref(options, ID2SYM(id_one_line));
183
381
  if (!NIL_P(one_line)) {
184
- re2_options->set_one_line(RTEST(one_line));
382
+ re2_options.set_one_line(RTEST(one_line));
185
383
  }
186
384
 
187
- p->pattern = new (std::nothrow) RE2(StringValuePtr(pattern), *re2_options);
385
+ p->pattern = new (std::nothrow) RE2(StringValuePtr(pattern), re2_options);
188
386
  } else {
189
387
  p->pattern = new (std::nothrow) RE2(StringValuePtr(pattern));
190
388
  }
@@ -200,22 +398,20 @@ extern "C" {
200
398
  * call-seq:
201
399
  * re2.inspect -> string
202
400
  *
203
- * Returns a printable version of the regular expression +re2+,
204
- * surrounded by forward slashes.
401
+ * Returns a printable version of the regular expression +re2+.
205
402
  *
206
- * re2 = RE2.new("woo?")
207
- * re2.inspect #=> "/woo?/"
403
+ * re2 = RE2::Regexp.new("woo?")
404
+ * re2.inspect #=> "#<RE2::Regexp /woo?/>"
208
405
  */
209
406
  static VALUE
210
- re2_inspect(VALUE self)
407
+ re2_regexp_inspect(VALUE self)
211
408
  {
212
- VALUE result = rb_str_buf_new(2);
213
409
  re2_pattern *p;
410
+ VALUE result = rb_str_buf_new2("#<RE2::Regexp /");
214
411
 
215
- rb_str_buf_cat2(result, "/");
216
412
  Data_Get_Struct(self, re2_pattern, p);
217
413
  rb_str_buf_cat2(result, p->pattern->pattern().c_str());
218
- rb_str_buf_cat2(result, "/");
414
+ rb_str_buf_cat2(result, "/>");
219
415
 
220
416
  return result;
221
417
  }
@@ -230,11 +426,11 @@ extern "C" {
230
426
  *
231
427
  * Returns a string version of the regular expression +re2+.
232
428
  *
233
- * re2 = RE2.new("woo?")
429
+ * re2 = RE2::Regexp.new("woo?")
234
430
  * re2.to_s #=> "woo?"
235
431
  */
236
432
  static VALUE
237
- re2_to_s(VALUE self)
433
+ re2_regexp_to_s(VALUE self)
238
434
  {
239
435
  re2_pattern *p;
240
436
  Data_Get_Struct(self, re2_pattern, p);
@@ -248,11 +444,11 @@ extern "C" {
248
444
  * Returns whether or not the regular expression +re2+
249
445
  * was compiled successfully or not.
250
446
  *
251
- * re2 = RE2.new("woo?")
447
+ * re2 = RE2::Regexp.new("woo?")
252
448
  * re2.ok? #=> true
253
449
  */
254
450
  static VALUE
255
- re2_ok(VALUE self)
451
+ re2_regexp_ok(VALUE self)
256
452
  {
257
453
  re2_pattern *p;
258
454
  Data_Get_Struct(self, re2_pattern, p);
@@ -266,11 +462,11 @@ extern "C" {
266
462
  * Returns whether or not the regular expression +re2+
267
463
  * was compiled with the utf8 option set to true.
268
464
  *
269
- * re2 = RE2.new("woo?", :utf8 => true)
465
+ * re2 = RE2::Regexp.new("woo?", :utf8 => true)
270
466
  * re2.utf8? #=> true
271
467
  */
272
468
  static VALUE
273
- re2_utf8(VALUE self)
469
+ re2_regexp_utf8(VALUE self)
274
470
  {
275
471
  re2_pattern *p;
276
472
  Data_Get_Struct(self, re2_pattern, p);
@@ -284,11 +480,11 @@ extern "C" {
284
480
  * Returns whether or not the regular expression +re2+
285
481
  * was compiled with the posix_syntax option set to true.
286
482
  *
287
- * re2 = RE2.new("woo?", :posix_syntax => true)
483
+ * re2 = RE2::Regexp.new("woo?", :posix_syntax => true)
288
484
  * re2.posix_syntax? #=> true
289
485
  */
290
486
  static VALUE
291
- re2_posix_syntax(VALUE self)
487
+ re2_regexp_posix_syntax(VALUE self)
292
488
  {
293
489
  re2_pattern *p;
294
490
  Data_Get_Struct(self, re2_pattern, p);
@@ -302,11 +498,11 @@ extern "C" {
302
498
  * Returns whether or not the regular expression +re2+
303
499
  * was compiled with the longest_match option set to true.
304
500
  *
305
- * re2 = RE2.new("woo?", :longest_match => true)
501
+ * re2 = RE2::Regexp.new("woo?", :longest_match => true)
306
502
  * re2.longest_match? #=> true
307
503
  */
308
504
  static VALUE
309
- re2_longest_match(VALUE self)
505
+ re2_regexp_longest_match(VALUE self)
310
506
  {
311
507
  re2_pattern *p;
312
508
  Data_Get_Struct(self, re2_pattern, p);
@@ -320,11 +516,11 @@ extern "C" {
320
516
  * Returns whether or not the regular expression +re2+
321
517
  * was compiled with the log_errors option set to true.
322
518
  *
323
- * re2 = RE2.new("woo?", :log_errors => true)
519
+ * re2 = RE2::Regexp.new("woo?", :log_errors => true)
324
520
  * re2.log_errors? #=> true
325
521
  */
326
522
  static VALUE
327
- re2_log_errors(VALUE self)
523
+ re2_regexp_log_errors(VALUE self)
328
524
  {
329
525
  re2_pattern *p;
330
526
  Data_Get_Struct(self, re2_pattern, p);
@@ -338,11 +534,11 @@ extern "C" {
338
534
  * Returns the max_mem setting for the regular expression
339
535
  * +re2+.
340
536
  *
341
- * re2 = RE2.new("woo?", :max_mem => 1024)
537
+ * re2 = RE2::Regexp.new("woo?", :max_mem => 1024)
342
538
  * re2.max_mem #=> 1024
343
539
  */
344
540
  static VALUE
345
- re2_max_mem(VALUE self)
541
+ re2_regexp_max_mem(VALUE self)
346
542
  {
347
543
  re2_pattern *p;
348
544
  Data_Get_Struct(self, re2_pattern, p);
@@ -356,11 +552,11 @@ extern "C" {
356
552
  * Returns whether or not the regular expression +re2+
357
553
  * was compiled with the literal option set to true.
358
554
  *
359
- * re2 = RE2.new("woo?", :literal => true)
555
+ * re2 = RE2::Regexp.new("woo?", :literal => true)
360
556
  * re2.literal? #=> true
361
557
  */
362
558
  static VALUE
363
- re2_literal(VALUE self)
559
+ re2_regexp_literal(VALUE self)
364
560
  {
365
561
  re2_pattern *p;
366
562
  Data_Get_Struct(self, re2_pattern, p);
@@ -374,11 +570,11 @@ extern "C" {
374
570
  * Returns whether or not the regular expression +re2+
375
571
  * was compiled with the never_nl option set to true.
376
572
  *
377
- * re2 = RE2.new("woo?", :never_nl => true)
573
+ * re2 = RE2::Regexp.new("woo?", :never_nl => true)
378
574
  * re2.never_nl? #=> true
379
575
  */
380
576
  static VALUE
381
- re2_never_nl(VALUE self)
577
+ re2_regexp_never_nl(VALUE self)
382
578
  {
383
579
  re2_pattern *p;
384
580
  Data_Get_Struct(self, re2_pattern, p);
@@ -392,11 +588,11 @@ extern "C" {
392
588
  * Returns whether or not the regular expression +re2+
393
589
  * was compiled with the case_sensitive option set to true.
394
590
  *
395
- * re2 = RE2.new("woo?", :case_sensitive => true)
591
+ * re2 = RE2::Regexp.new("woo?", :case_sensitive => true)
396
592
  * re2.case_sensitive? #=> true
397
593
  */
398
594
  static VALUE
399
- re2_case_sensitive(VALUE self)
595
+ re2_regexp_case_sensitive(VALUE self)
400
596
  {
401
597
  re2_pattern *p;
402
598
  Data_Get_Struct(self, re2_pattern, p);
@@ -411,13 +607,13 @@ extern "C" {
411
607
  * Returns whether or not the regular expression +re2+
412
608
  * was compiled with the case_sensitive option set to false.
413
609
  *
414
- * re2 = RE2.new("woo?", :case_sensitive => true)
610
+ * re2 = RE2::Regexp.new("woo?", :case_sensitive => true)
415
611
  * re2.case_insensitive? #=> false
416
612
  */
417
613
  static VALUE
418
- re2_case_insensitive(VALUE self)
614
+ re2_regexp_case_insensitive(VALUE self)
419
615
  {
420
- return BOOL2RUBY(re2_case_sensitive(self) != Qtrue);
616
+ return BOOL2RUBY(re2_regexp_case_sensitive(self) != Qtrue);
421
617
  }
422
618
 
423
619
  /*
@@ -427,11 +623,11 @@ extern "C" {
427
623
  * Returns whether or not the regular expression +re2+
428
624
  * was compiled with the perl_classes option set to true.
429
625
  *
430
- * re2 = RE2.new("woo?", :perl_classes => true)
626
+ * re2 = RE2::Regexp.new("woo?", :perl_classes => true)
431
627
  * re2.perl_classes? #=> true
432
628
  */
433
629
  static VALUE
434
- re2_perl_classes(VALUE self)
630
+ re2_regexp_perl_classes(VALUE self)
435
631
  {
436
632
  re2_pattern *p;
437
633
  Data_Get_Struct(self, re2_pattern, p);
@@ -445,11 +641,11 @@ extern "C" {
445
641
  * Returns whether or not the regular expression +re2+
446
642
  * was compiled with the word_boundary option set to true.
447
643
  *
448
- * re2 = RE2.new("woo?", :word_boundary => true)
644
+ * re2 = RE2::Regexp.new("woo?", :word_boundary => true)
449
645
  * re2.word_boundary? #=> true
450
646
  */
451
647
  static VALUE
452
- re2_word_boundary(VALUE self)
648
+ re2_regexp_word_boundary(VALUE self)
453
649
  {
454
650
  re2_pattern *p;
455
651
  Data_Get_Struct(self, re2_pattern, p);
@@ -463,11 +659,11 @@ extern "C" {
463
659
  * Returns whether or not the regular expression +re2+
464
660
  * was compiled with the one_line option set to true.
465
661
  *
466
- * re2 = RE2.new("woo?", :one_line => true)
662
+ * re2 = RE2::Regexp.new("woo?", :one_line => true)
467
663
  * re2.one_line? #=> true
468
664
  */
469
665
  static VALUE
470
- re2_one_line(VALUE self)
666
+ re2_regexp_one_line(VALUE self)
471
667
  {
472
668
  re2_pattern *p;
473
669
  Data_Get_Struct(self, re2_pattern, p);
@@ -482,7 +678,7 @@ extern "C" {
482
678
  * error string.
483
679
  */
484
680
  static VALUE
485
- re2_error(VALUE self)
681
+ re2_regexp_error(VALUE self)
486
682
  {
487
683
  re2_pattern *p;
488
684
  Data_Get_Struct(self, re2_pattern, p);
@@ -497,7 +693,7 @@ extern "C" {
497
693
  * the offending portion of the regexp.
498
694
  */
499
695
  static VALUE
500
- re2_error_arg(VALUE self)
696
+ re2_regexp_error_arg(VALUE self)
501
697
  {
502
698
  re2_pattern *p;
503
699
  Data_Get_Struct(self, re2_pattern, p);
@@ -513,7 +709,7 @@ extern "C" {
513
709
  * than smaller numbers.
514
710
  */
515
711
  static VALUE
516
- re2_program_size(VALUE self)
712
+ re2_regexp_program_size(VALUE self)
517
713
  {
518
714
  re2_pattern *p;
519
715
  Data_Get_Struct(self, re2_pattern, p);
@@ -528,7 +724,7 @@ extern "C" {
528
724
  * +re2+.
529
725
  */
530
726
  static VALUE
531
- re2_options(VALUE self)
727
+ re2_regexp_options(VALUE self)
532
728
  {
533
729
  VALUE options;
534
730
  re2_pattern *p;
@@ -584,7 +780,7 @@ extern "C" {
584
780
  * count: if the regexp is "(a)(b)", returns 2.
585
781
  */
586
782
  static VALUE
587
- re2_number_of_capturing_groups(VALUE self)
783
+ re2_regexp_number_of_capturing_groups(VALUE self)
588
784
  {
589
785
  re2_pattern *p;
590
786
 
@@ -606,20 +802,20 @@ extern "C" {
606
802
  * that number of matches will be returned (padded with nils if
607
803
  * there are insufficient matches).
608
804
  *
609
- * r = RE2.new('w(o)(o)')
805
+ * r = RE2::Regexp.new('w(o)(o)')
610
806
  * r.match('woo') #=> ["woo", "o", "o"]
611
807
  * r.match('woo', 0) #=> true
612
808
  * r.match('bob', 0) #=> false
613
809
  * r.match('woo', 1) #=> ["woo", "o"]
614
810
  */
615
811
  static VALUE
616
- re2_match(int argc, VALUE *argv, VALUE self)
812
+ re2_regexp_match(int argc, VALUE *argv, VALUE self)
617
813
  {
618
814
  int n;
619
815
  bool matched;
620
816
  re2_pattern *p;
621
- VALUE text, number_of_matches, matches;
622
- re2::StringPiece *string_matches, *text_as_string_piece;
817
+ re2_matchdata *m;
818
+ VALUE text, number_of_matches, matchdata;
623
819
 
624
820
  rb_scan_args(argc, argv, "11", &text, &number_of_matches);
625
821
 
@@ -631,52 +827,33 @@ extern "C" {
631
827
  n = p->pattern->NumberOfCapturingGroups();
632
828
  }
633
829
 
634
- text_as_string_piece = new (std::nothrow) re2::StringPiece(StringValuePtr(text));
635
-
636
- if (text_as_string_piece == 0) {
637
- rb_raise(rb_eNoMemError, "not enough memory to allocate StringPiece for text");
638
- }
830
+ re2::StringPiece text_as_string_piece(StringValuePtr(text));
639
831
 
640
832
  if (n == 0) {
641
- matched = p->pattern->Match(*text_as_string_piece, 0, RE2::UNANCHORED, 0, 0);
642
-
643
- delete text_as_string_piece;
644
-
833
+ matched = p->pattern->Match(text_as_string_piece, 0, RE2::UNANCHORED, 0, 0);
645
834
  return BOOL2RUBY(matched);
646
-
647
835
  } else {
648
836
 
649
837
  /* Because match returns the whole match as well. */
650
838
  n += 1;
651
839
 
652
- string_matches = new (std::nothrow) re2::StringPiece[n];
840
+ matchdata = rb_class_new_instance(0, 0, re2_cMatchData);
841
+ Data_Get_Struct(matchdata, re2_matchdata, m);
842
+ m->matches = new (std::nothrow) re2::StringPiece[n];
843
+ m->regexp = self;
844
+ m->string = rb_str_dup_frozen(text);
653
845
 
654
- if (string_matches == 0) {
655
- rb_raise(rb_eNoMemError, "not enough memory to allocate array of StringPieces for matches");
846
+ if (m->matches == 0) {
847
+ rb_raise(rb_eNoMemError, "not enough memory to allocate StringPieces for matches");
656
848
  }
657
849
 
658
- matched = p->pattern->Match(*text_as_string_piece, 0, RE2::UNANCHORED, string_matches, n);
850
+ m->number_of_matches = n;
659
851
 
660
- delete text_as_string_piece;
852
+ matched = p->pattern->Match(text_as_string_piece, 0, RE2::UNANCHORED, m->matches, n);
661
853
 
662
854
  if (matched) {
663
- matches = rb_ary_new();
664
-
665
- for (int i = 0; i < n; i++) {
666
- if (!string_matches[i].empty()) {
667
- rb_ary_push(matches, rb_str_new2(string_matches[i].as_string().c_str()));
668
- } else {
669
- rb_ary_push(matches, Qnil);
670
- }
671
- }
672
-
673
- delete[] string_matches;
674
-
675
- return matches;
855
+ return matchdata;
676
856
  } else {
677
-
678
- delete[] string_matches;
679
-
680
857
  return Qnil;
681
858
  }
682
859
  }
@@ -691,258 +868,13 @@ extern "C" {
691
868
  * Equivalent to +re2.match(text, 0)+.
692
869
  */
693
870
  static VALUE
694
- re2_match_query(VALUE self, VALUE text)
871
+ re2_regexp_match_query(VALUE self, VALUE text)
695
872
  {
696
873
  VALUE argv[2];
697
874
  argv[0] = text;
698
875
  argv[1] = INT2FIX(0);
699
876
 
700
- return re2_match(2, argv, self);
701
- }
702
-
703
- /*
704
- * call-seq:
705
- * re2 !~ text -> true or false
706
- *
707
- * Returns true or false to indicate an unsuccessful match.
708
- * Equivalent to +!re2.match(text, 0)+.
709
- */
710
- static VALUE
711
- re2_bang_tilde(VALUE self, VALUE text)
712
- {
713
- return BOOL2RUBY(re2_match_query(self, text) != Qtrue);
714
- }
715
-
716
- /*
717
- * call-seq:
718
- * RE2::FullMatch(text, re) -> true or false
719
- *
720
- * Returns whether or not a full match for +re2+ was
721
- * found in text.
722
- *
723
- * RE2::FullMatch("woo", "wo+") #=> true
724
- * RE2::FullMatch("woo", "a") #=> false
725
- * re2 = RE2.new("woo")
726
- * RE2::FullMatch("woo", re2) #=> true
727
- */
728
- static VALUE
729
- re2_FullMatch(VALUE self, VALUE text, VALUE re)
730
- {
731
- UNUSED(self);
732
- bool result;
733
- re2_pattern *p;
734
-
735
- if (rb_obj_is_kind_of(re, re2_cRE2)) {
736
- Data_Get_Struct(re, re2_pattern, p);
737
- result = RE2::FullMatch(StringValuePtr(text), *p->pattern);
738
- } else {
739
- result = RE2::FullMatch(StringValuePtr(text), StringValuePtr(re));
740
- }
741
-
742
- return BOOL2RUBY(result);
743
- }
744
-
745
- /*
746
- * call-seq:
747
- * RE2::FullMatchN(text, re) -> array of matches
748
- *
749
- * Returns an array of successful matches as defined in
750
- * +re+ for +text+.
751
- *
752
- * RE2::FullMatchN("woo", "w(oo)") #=> ["oo"]
753
- */
754
- static VALUE
755
- re2_FullMatchN(VALUE self, VALUE text, VALUE re)
756
- {
757
- UNUSED(self);
758
- int n;
759
- bool matched, re2_given;
760
- re2_pattern *p;
761
- VALUE matches;
762
- RE2 *compiled_pattern;
763
- RE2::Arg *argv;
764
- const RE2::Arg **args;
765
- std::string *string_matches;
766
-
767
- re2_given = rb_obj_is_kind_of(re, re2_cRE2);
768
-
769
- if (re2_given) {
770
- Data_Get_Struct(re, re2_pattern, p);
771
- compiled_pattern = p->pattern;
772
- } else {
773
- compiled_pattern = new (std::nothrow) RE2(StringValuePtr(re));
774
-
775
- if (compiled_pattern == 0) {
776
- rb_raise(rb_eNoMemError, "not enough memory to allocate RE2 object for pattern");
777
- }
778
- }
779
-
780
- n = compiled_pattern->NumberOfCapturingGroups();
781
-
782
- argv = new (std::nothrow) RE2::Arg[n];
783
- args = new (std::nothrow) const RE2::Arg*[n];
784
- string_matches = new (std::nothrow) std::string[n];
785
-
786
- if (argv == 0) {
787
- rb_raise(rb_eNoMemError, "not enough memory to allocate array of RE2::Args");
788
- }
789
-
790
- if (args == 0) {
791
- rb_raise(rb_eNoMemError, "not enough memory to allocate array of pointers to RE2::Args");
792
- }
793
-
794
- if (string_matches == 0) {
795
- rb_raise(rb_eNoMemError, "not enough memory to allocate array of strings for matches");
796
- }
797
-
798
- for (int i = 0; i < n; i++) {
799
- args[i] = &argv[i];
800
- argv[i] = &string_matches[i];
801
- }
802
-
803
- matched = RE2::FullMatchN(StringValuePtr(text), *compiled_pattern, args, n);
804
-
805
- if (!re2_given) {
806
- delete compiled_pattern;
807
- }
808
-
809
- delete[] argv;
810
- delete[] args;
811
-
812
- if (matched) {
813
- matches = rb_ary_new();
814
-
815
- for (int i = 0; i < n; i++) {
816
- if (!string_matches[i].empty()) {
817
- rb_ary_push(matches, rb_str_new2(string_matches[i].c_str()));
818
- } else {
819
- rb_ary_push(matches, Qnil);
820
- }
821
- }
822
-
823
- delete[] string_matches;
824
-
825
- return matches;
826
- } else {
827
- delete[] string_matches;
828
- return Qnil;
829
- }
830
- }
831
-
832
- /*
833
- * call-seq:
834
- * RE2::PartialMatchN(text, re) -> array of matches
835
- *
836
- * Returns an array of successful matches as defined in
837
- * +re+ for +text+.
838
- *
839
- * RE2::PartialMatchN("woo", "w(oo)") #=> ["oo"]
840
- */
841
- static VALUE
842
- re2_PartialMatchN(VALUE self, VALUE text, VALUE re)
843
- {
844
- UNUSED(self);
845
- int n;
846
- bool matched, re2_given;
847
- re2_pattern *p;
848
- VALUE matches;
849
- RE2 *compiled_pattern;
850
- RE2::Arg *argv;
851
- const RE2::Arg **args;
852
- std::string *string_matches;
853
-
854
- re2_given = rb_obj_is_kind_of(re, re2_cRE2);
855
-
856
- if (re2_given) {
857
- Data_Get_Struct(re, re2_pattern, p);
858
- compiled_pattern = p->pattern;
859
- } else {
860
- compiled_pattern = new (std::nothrow) RE2(StringValuePtr(re));
861
-
862
- if (compiled_pattern == 0) {
863
- rb_raise(rb_eNoMemError, "not enough memory to allocate RE2 object for pattern");
864
- }
865
- }
866
-
867
- n = compiled_pattern->NumberOfCapturingGroups();
868
-
869
- argv = new (std::nothrow) RE2::Arg[n];
870
- args = new (std::nothrow) const RE2::Arg*[n];
871
- string_matches = new (std::nothrow) std::string[n];
872
-
873
- if (argv == 0) {
874
- rb_raise(rb_eNoMemError, "not enough memory to allocate array of RE2::Args");
875
- }
876
-
877
- if (args == 0) {
878
- rb_raise(rb_eNoMemError, "not enough memory to allocate array of pointers to RE2::Args");
879
- }
880
-
881
- if (string_matches == 0) {
882
- rb_raise(rb_eNoMemError, "not enough memory to allocate array of strings for matches");
883
- }
884
-
885
- for (int i = 0; i < n; i++) {
886
- args[i] = &argv[i];
887
- argv[i] = &string_matches[i];
888
- }
889
-
890
- matched = RE2::PartialMatchN(StringValuePtr(text), *compiled_pattern, args, n);
891
-
892
- if (!re2_given) {
893
- delete compiled_pattern;
894
- }
895
-
896
- delete[] argv;
897
- delete[] args;
898
-
899
- if (matched) {
900
- matches = rb_ary_new();
901
-
902
- for (int i = 0; i < n; i++) {
903
- if (!string_matches[i].empty()) {
904
- rb_ary_push(matches, rb_str_new2(string_matches[i].c_str()));
905
- } else {
906
- rb_ary_push(matches, Qnil);
907
- }
908
- }
909
-
910
- delete[] string_matches;
911
-
912
- return matches;
913
- } else {
914
- delete[] string_matches;
915
- return Qnil;
916
- }
917
- }
918
-
919
- /*
920
- * call-seq:
921
- * RE2::PartialMatch(text, re) -> true or false
922
- *
923
- * Returns whether or not a partial match for +re2+ was
924
- * found in text.
925
- *
926
- * RE2::PartialMatch("woo", "o+") #=> true
927
- * RE2::PartialMatch("woo", "a") #=> false
928
- * re2 = RE2.new("oo?")
929
- * RE2::PartialMatch("woo", re2) #=> true
930
- */
931
- static VALUE
932
- re2_PartialMatch(VALUE self, VALUE text, VALUE re)
933
- {
934
- UNUSED(self);
935
- bool result;
936
- re2_pattern *p;
937
-
938
- if (rb_obj_is_kind_of(re, re2_cRE2)) {
939
- Data_Get_Struct(re, re2_pattern, p);
940
- result = RE2::PartialMatch(StringValuePtr(text), *p->pattern);
941
- } else {
942
- result = RE2::PartialMatch(StringValuePtr(text), StringValuePtr(re));
943
- }
944
-
945
- return BOOL2RUBY(result);
877
+ return re2_regexp_match(2, argv, self);
946
878
  }
947
879
 
948
880
  /*
@@ -971,7 +903,7 @@ extern "C" {
971
903
  re2::StringPiece rewrite_as_string_piece(StringValuePtr(rewrite));
972
904
 
973
905
  // Do the replacement.
974
- if (rb_obj_is_kind_of(pattern, re2_cRE2)) {
906
+ if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
975
907
  Data_Get_Struct(pattern, re2_pattern, p);
976
908
  RE2::Replace(&str_as_string, *p->pattern, rewrite_as_string_piece);
977
909
  } else {
@@ -1013,7 +945,7 @@ extern "C" {
1013
945
  VALUE repl;
1014
946
 
1015
947
  // Do the replacement.
1016
- if (rb_obj_is_kind_of(pattern, re2_cRE2)) {
948
+ if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
1017
949
  Data_Get_Struct(pattern, re2_pattern, p);
1018
950
  RE2::GlobalReplace(&str_as_string, *p->pattern, rewrite_as_string_piece);
1019
951
  } else {
@@ -1032,8 +964,8 @@ extern "C" {
1032
964
  /*
1033
965
  * call-seq:
1034
966
  * RE2::QuoteMeta(str) -> str
1035
- * RE2.escape(str) -> str
1036
- * RE2.quote(str) -> str
967
+ * RE2::Regexp.escape(str) -> str
968
+ * RE2::Regexp.quote(str) -> str
1037
969
  *
1038
970
  * Returns a version of str with all potentially meaningful regexp
1039
971
  * characters escaped. The returned string, used as a regular
@@ -1052,48 +984,59 @@ extern "C" {
1052
984
  void
1053
985
  Init_re2()
1054
986
  {
1055
- re2_cRE2 = rb_define_class("RE2", rb_cObject);
1056
- rb_define_alloc_func(re2_cRE2, (VALUE (*)(VALUE))re2_allocate);
1057
- rb_define_method(re2_cRE2, "initialize", (VALUE (*)(...))re2_initialize, -1);
1058
- rb_define_method(re2_cRE2, "ok?", (VALUE (*)(...))re2_ok, 0);
1059
- rb_define_method(re2_cRE2, "error", (VALUE (*)(...))re2_error, 0);
1060
- rb_define_method(re2_cRE2, "error_arg", (VALUE (*)(...))re2_error_arg, 0);
1061
- rb_define_method(re2_cRE2, "program_size", (VALUE (*)(...))re2_program_size, 0);
1062
- rb_define_method(re2_cRE2, "options", (VALUE (*)(...))re2_options, 0);
1063
- rb_define_method(re2_cRE2, "number_of_capturing_groups", (VALUE (*)(...))re2_number_of_capturing_groups, 0);
1064
- rb_define_method(re2_cRE2, "match", (VALUE (*)(...))re2_match, -1);
1065
- rb_define_method(re2_cRE2, "match?", (VALUE (*)(...))re2_match_query, 1);
1066
- rb_define_method(re2_cRE2, "=~", (VALUE (*)(...))re2_match_query, 1);
1067
- rb_define_method(re2_cRE2, "===", (VALUE (*)(...))re2_match_query, 1);
1068
- rb_define_method(re2_cRE2, "!~", (VALUE (*)(...))re2_bang_tilde, 1);
1069
- rb_define_method(re2_cRE2, "to_s", (VALUE (*)(...))re2_to_s, 0);
1070
- rb_define_method(re2_cRE2, "to_str", (VALUE (*)(...))re2_to_s, 0);
1071
- rb_define_method(re2_cRE2, "pattern", (VALUE (*)(...))re2_to_s, 0);
1072
- rb_define_method(re2_cRE2, "source", (VALUE (*)(...))re2_to_s, 0);
1073
- rb_define_method(re2_cRE2, "inspect", (VALUE (*)(...))re2_inspect, 0);
1074
- rb_define_method(re2_cRE2, "utf8?", (VALUE (*)(...))re2_utf8, 0);
1075
- rb_define_method(re2_cRE2, "posix_syntax?", (VALUE (*)(...))re2_posix_syntax, 0);
1076
- rb_define_method(re2_cRE2, "longest_match?", (VALUE (*)(...))re2_longest_match, 0);
1077
- rb_define_method(re2_cRE2, "log_errors?", (VALUE (*)(...))re2_log_errors, 0);
1078
- rb_define_method(re2_cRE2, "max_mem", (VALUE (*)(...))re2_max_mem, 0);
1079
- rb_define_method(re2_cRE2, "literal?", (VALUE (*)(...))re2_literal, 0);
1080
- rb_define_method(re2_cRE2, "never_nl?", (VALUE (*)(...))re2_never_nl, 0);
1081
- rb_define_method(re2_cRE2, "case_sensitive?", (VALUE (*)(...))re2_case_sensitive, 0);
1082
- rb_define_method(re2_cRE2, "case_insensitive?", (VALUE (*)(...))re2_case_insensitive, 0);
1083
- rb_define_method(re2_cRE2, "casefold?", (VALUE (*)(...))re2_case_insensitive, 0);
1084
- rb_define_method(re2_cRE2, "perl_classes?", (VALUE (*)(...))re2_perl_classes, 0);
1085
- rb_define_method(re2_cRE2, "word_boundary?", (VALUE (*)(...))re2_word_boundary, 0);
1086
- rb_define_method(re2_cRE2, "one_line?", (VALUE (*)(...))re2_one_line, 0);
1087
- rb_define_singleton_method(re2_cRE2, "FullMatch", (VALUE (*)(...))re2_FullMatch, 2);
1088
- rb_define_singleton_method(re2_cRE2, "FullMatchN", (VALUE (*)(...))re2_FullMatchN, 2);
1089
- rb_define_singleton_method(re2_cRE2, "PartialMatch", (VALUE (*)(...))re2_PartialMatch, 2);
1090
- rb_define_singleton_method(re2_cRE2, "PartialMatchN", (VALUE (*)(...))re2_PartialMatchN, 2);
1091
- rb_define_singleton_method(re2_cRE2, "Replace", (VALUE (*)(...))re2_Replace, 3);
1092
- rb_define_singleton_method(re2_cRE2, "GlobalReplace", (VALUE (*)(...))re2_GlobalReplace, 3);
1093
- rb_define_singleton_method(re2_cRE2, "QuoteMeta", (VALUE (*)(...))re2_QuoteMeta, 1);
1094
- rb_define_singleton_method(re2_cRE2, "escape", (VALUE (*)(...))re2_QuoteMeta, 1);
1095
- rb_define_singleton_method(re2_cRE2, "quote", (VALUE (*)(...))re2_QuoteMeta, 1);
1096
- rb_define_singleton_method(re2_cRE2, "compile", (VALUE (*)(...))rb_class_new_instance, -1);
987
+ re2_mRE2 = rb_define_module("RE2");
988
+ re2_cRegexp = rb_define_class_under(re2_mRE2, "Regexp", rb_cObject);
989
+ re2_cMatchData = rb_define_class_under(re2_mRE2, "MatchData", rb_cObject);
990
+
991
+ rb_define_alloc_func(re2_cRegexp, (VALUE (*)(VALUE))re2_regexp_allocate);
992
+ rb_define_alloc_func(re2_cMatchData, (VALUE (*)(VALUE))re2_matchdata_allocate);
993
+
994
+ rb_define_method(re2_cMatchData, "string", (VALUE (*)(...))re2_matchdata_string, 0);
995
+ rb_define_method(re2_cMatchData, "regexp", (VALUE (*)(...))re2_matchdata_regexp, 0);
996
+ rb_define_method(re2_cMatchData, "to_a", (VALUE (*)(...))re2_matchdata_to_a, 0);
997
+ rb_define_method(re2_cMatchData, "size", (VALUE (*)(...))re2_matchdata_size, 0);
998
+ rb_define_method(re2_cMatchData, "length", (VALUE (*)(...))re2_matchdata_size, 0);
999
+ rb_define_method(re2_cMatchData, "[]", (VALUE (*)(...))re2_matchdata_aref, -1);
1000
+ rb_define_method(re2_cMatchData, "to_s", (VALUE (*)(...))re2_matchdata_to_s, 0);
1001
+ rb_define_method(re2_cMatchData, "inspect", (VALUE (*)(...))re2_matchdata_inspect, 0);
1002
+
1003
+ rb_define_method(re2_cRegexp, "initialize", (VALUE (*)(...))re2_regexp_initialize, -1);
1004
+ rb_define_method(re2_cRegexp, "ok?", (VALUE (*)(...))re2_regexp_ok, 0);
1005
+ rb_define_method(re2_cRegexp, "error", (VALUE (*)(...))re2_regexp_error, 0);
1006
+ rb_define_method(re2_cRegexp, "error_arg", (VALUE (*)(...))re2_regexp_error_arg, 0);
1007
+ rb_define_method(re2_cRegexp, "program_size", (VALUE (*)(...))re2_regexp_program_size, 0);
1008
+ rb_define_method(re2_cRegexp, "options", (VALUE (*)(...))re2_regexp_options, 0);
1009
+ rb_define_method(re2_cRegexp, "number_of_capturing_groups", (VALUE (*)(...))re2_regexp_number_of_capturing_groups, 0);
1010
+ rb_define_method(re2_cRegexp, "match", (VALUE (*)(...))re2_regexp_match, -1);
1011
+ rb_define_method(re2_cRegexp, "match?", (VALUE (*)(...))re2_regexp_match_query, 1);
1012
+ rb_define_method(re2_cRegexp, "=~", (VALUE (*)(...))re2_regexp_match_query, 1);
1013
+ rb_define_method(re2_cRegexp, "===", (VALUE (*)(...))re2_regexp_match_query, 1);
1014
+ rb_define_method(re2_cRegexp, "to_s", (VALUE (*)(...))re2_regexp_to_s, 0);
1015
+ rb_define_method(re2_cRegexp, "to_str", (VALUE (*)(...))re2_regexp_to_s, 0);
1016
+ rb_define_method(re2_cRegexp, "pattern", (VALUE (*)(...))re2_regexp_to_s, 0);
1017
+ rb_define_method(re2_cRegexp, "source", (VALUE (*)(...))re2_regexp_to_s, 0);
1018
+ rb_define_method(re2_cRegexp, "inspect", (VALUE (*)(...))re2_regexp_inspect, 0);
1019
+ rb_define_method(re2_cRegexp, "utf8?", (VALUE (*)(...))re2_regexp_utf8, 0);
1020
+ rb_define_method(re2_cRegexp, "posix_syntax?", (VALUE (*)(...))re2_regexp_posix_syntax, 0);
1021
+ rb_define_method(re2_cRegexp, "longest_match?", (VALUE (*)(...))re2_regexp_longest_match, 0);
1022
+ rb_define_method(re2_cRegexp, "log_errors?", (VALUE (*)(...))re2_regexp_log_errors, 0);
1023
+ rb_define_method(re2_cRegexp, "max_mem", (VALUE (*)(...))re2_regexp_max_mem, 0);
1024
+ rb_define_method(re2_cRegexp, "literal?", (VALUE (*)(...))re2_regexp_literal, 0);
1025
+ rb_define_method(re2_cRegexp, "never_nl?", (VALUE (*)(...))re2_regexp_never_nl, 0);
1026
+ rb_define_method(re2_cRegexp, "case_sensitive?", (VALUE (*)(...))re2_regexp_case_sensitive, 0);
1027
+ rb_define_method(re2_cRegexp, "case_insensitive?", (VALUE (*)(...))re2_regexp_case_insensitive, 0);
1028
+ rb_define_method(re2_cRegexp, "casefold?", (VALUE (*)(...))re2_regexp_case_insensitive, 0);
1029
+ rb_define_method(re2_cRegexp, "perl_classes?", (VALUE (*)(...))re2_regexp_perl_classes, 0);
1030
+ rb_define_method(re2_cRegexp, "word_boundary?", (VALUE (*)(...))re2_regexp_word_boundary, 0);
1031
+ rb_define_method(re2_cRegexp, "one_line?", (VALUE (*)(...))re2_regexp_one_line, 0);
1032
+
1033
+ rb_define_module_function(re2_mRE2, "Replace", (VALUE (*)(...))re2_Replace, 3);
1034
+ rb_define_module_function(re2_mRE2, "GlobalReplace", (VALUE (*)(...))re2_GlobalReplace, 3);
1035
+ rb_define_module_function(re2_mRE2, "QuoteMeta", (VALUE (*)(...))re2_QuoteMeta, 1);
1036
+ rb_define_singleton_method(re2_cRegexp, "escape", (VALUE (*)(...))re2_QuoteMeta, 1);
1037
+ rb_define_singleton_method(re2_cRegexp, "quote", (VALUE (*)(...))re2_QuoteMeta, 1);
1038
+ rb_define_singleton_method(re2_cRegexp, "compile", (VALUE (*)(...))rb_class_new_instance, -1);
1039
+
1097
1040
  rb_define_global_function("RE2", (VALUE (*)(...))re2_re2, -1);
1098
1041
 
1099
1042
  /* Create the symbols used in options. */