Class: RE2::Scanner

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/re2/scanner.rb,
ext/re2/re2.cc

Instance Method Summary collapse

Instance Method Details

#eachObject



5
6
7
8
9
10
11
12
13
# File 'lib/re2/scanner.rb', line 5

def each
  if block_given?
    while matches = scan
      yield matches
    end
  else
    to_enum(:each)
  end
end

#eof?Boolean

Returns whether the scanner has consumed all input or not.

Examples:

c = RE2::Regexp.new('(\d+)').scan("foo")
c.eof? #=> true

Returns:

  • (Boolean)

    whether the scanner has consumed all input or not



184
185
186
187
188
189
# File 'ext/re2/re2.cc', line 184

static VALUE re2_scanner_eof(VALUE self) {
  re2_scanner *c;
  Data_Get_Struct(self, re2_scanner, c);

  return BOOL2RUBY(c->eof);
}

#regexpRE2::Regexp

Returns the Regexp used in the scanner.

Examples:

c = RE2::Regexp.new('(\d+)').scan("bob 123")
c.regexp    #=> #<RE2::Regexp /(\d+)/>

Returns:



421
422
423
424
425
426
# File 'ext/re2/re2.cc', line 421

static VALUE re2_scanner_regexp(VALUE self) {
  re2_scanner *c;
  Data_Get_Struct(self, re2_scanner, c);

  return c->regexp;
}

#rewindObject

Rewind the scanner to the start of the string.

Examples:

s = RE2::Regexp.new('(\d+)').scan("1 2 3")
e = s.to_enum
e.scan #=> ["1"]
e.scan #=> ["2"]
s.rewind
e.scan #=> ["1"]


202
203
204
205
206
207
208
209
210
# File 'ext/re2/re2.cc', line 202

static VALUE re2_scanner_rewind(VALUE self) {
  re2_scanner *c;
  Data_Get_Struct(self, re2_scanner, c);

  c->input = new(nothrow) re2::StringPiece(StringValuePtr(c->text));
  c->eof = false;

  return self;
}

#scanArray<String>

Scan the given text incrementally for matches, returning an array of matches on each subsequent call. Returns nil if no matches are found.

Examples:

s = RE2::Regexp.new('(\w+)').scan("Foo bar baz")
s.scan #=> ["Foo"]
s.scan #=> ["bar"]

Returns:

  • (Array<String>)

    the matches.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'ext/re2/re2.cc', line 222

static VALUE re2_scanner_scan(VALUE self) {
  int i;
  size_t original_input_size, new_input_size;
  bool input_advanced;
  re2_pattern *p;
  re2_scanner *c;
  VALUE result;

  Data_Get_Struct(self, re2_scanner, c);
  Data_Get_Struct(c->regexp, re2_pattern, p);

  vector<RE2::Arg> argv(c->number_of_capturing_groups);
  vector<RE2::Arg*> args(c->number_of_capturing_groups);
  vector<string> matches(c->number_of_capturing_groups);

  if (c->eof) {
    return Qnil;
  }

  original_input_size = c->input->size();

  for (i = 0; i < c->number_of_capturing_groups; i++) {
    matches[i] = "";
    argv[i] = &matches[i];
    args[i] = &argv[i];
  }

  if (RE2::FindAndConsumeN(c->input, *p->pattern, &args[0],
        c->number_of_capturing_groups)) {
    result = rb_ary_new2(c->number_of_capturing_groups);
    new_input_size = c->input->size();
    input_advanced = new_input_size < original_input_size;

    for (i = 0; i < c->number_of_capturing_groups; i++) {
      if (matches[i].empty()) {
        rb_ary_push(result, Qnil);
      } else {
        rb_ary_push(result, ENCODED_STR_NEW(matches[i].data(),
              matches[i].size(),
              p->pattern->options().encoding() == RE2::Options::EncodingUTF8 ? "UTF-8" : "ISO-8859-1"));
      }
    }

    /* Check whether we've exhausted the input yet. */
    c->eof = new_input_size == 0;

    /* If the match didn't advance the input, we need to do this ourselves. */
    if (!input_advanced && new_input_size > 0) {
      c->input->remove_prefix(1);
    }
  } else {
    result = Qnil;
  }

  return result;
}

#stringString

Returns the string passed into the scanner.

Examples:

c = RE2::Regexp.new('(\d+)').scan("foo")
c.string #=> "foo"

Returns:

  • (String)

    the original string.



169
170
171
172
173
174
# File 'ext/re2/re2.cc', line 169

static VALUE re2_scanner_string(VALUE self) {
  re2_scanner *c;
  Data_Get_Struct(self, re2_scanner, c);

  return c->text;
}