static Object* get_match_data(STATE, OnigRegion *region, String* string, Regexp* regexp, int pos) { MatchData* md = state->new_object<MatchData>(G(matchdata)); md->source(state, string->string_dup(state)); md->regexp(state, regexp); Tuple* tup = Tuple::from(state, 2, Fixnum::from(region->beg[0] + pos), Fixnum::from(region->end[0] + pos)); md->full(state, tup); md->region(state, _md_region_to_tuple(state, region, pos)); return md; }
static Object* get_match_data(STATE, OnigRegion *region, String* string, Regexp* regexp, int pos) { MatchData* md = state->new_object<MatchData>(G(matchdata)); md->source(state, string->string_dup(state)); md->regexp(state, regexp); // Unsure if the first region (the full match) can be less than 0 (meaning // the match was length length but did still match). We have to support // that for the > 0 case, so we'll do the same for 0. // if(region->beg[0] < 0) { Tuple* tup = Tuple::from(state, 2, Fixnum::from(region->beg[0]), Fixnum::from(region->end[0])); md->full(state, tup); } else { Tuple* tup = Tuple::from(state, 2, Fixnum::from(region->beg[0] + pos), Fixnum::from(region->end[0] + pos)); md->full(state, tup); } md->region(state, _md_region_to_tuple(state, region, pos)); return md; }
void test_match_start() { String *pat = String::create(state, "."); Regexp* re = Regexp::create(state); re->initialize(state, pat, Fixnum::from(0)); String *input = String::create(state, "abc"); Fixnum* start = Fixnum::from(1); MatchData* matches = (MatchData*)re->match_start(state, input, start); TS_ASSERT(!matches->nil_p()); TS_ASSERT_EQUALS(matches->region()->num_fields(), 0); TS_ASSERT_EQUALS(as<Integer>(matches->full()->at(state, 0))->to_native(), 1); TS_ASSERT_EQUALS(as<Integer>(matches->full()->at(state, 1))->to_native(), 2); }
void test_match_region() { String *pat = String::create(state, "."); Regexp* re = Regexp::create(state); re->initialize(state, pat, Fixnum::from(0), Qnil); String *input = String::create(state, "abc"); Fixnum* start = Fixnum::from(0); Fixnum* end = Fixnum::from(3); Object* forward = Qtrue; MatchData* matches = (MatchData*)re->match_region(state, input, start, end, forward); TS_ASSERT(!matches->nil_p()); TS_ASSERT_EQUALS(matches->region()->num_fields(), 0U); TS_ASSERT_EQUALS(as<Integer>(matches->full()->at(state, 0))->to_native(), 0); TS_ASSERT_EQUALS(as<Integer>(matches->full()->at(state, 1))->to_native(), 1); }
void test_match_region_with_backward_captures() { String *pat = String::create(state, ".(.)"); Regexp* re = Regexp::create(state); re->initialize(state, pat, Fixnum::from(0)); String *input = String::create(state, "abc"); Fixnum* start = Fixnum::from(0); Fixnum* end = Fixnum::from(3); Object* forward = cFalse; MatchData* matches = re->match_region(state, input, start, end, forward); TS_ASSERT(!matches->nil_p()); TS_ASSERT_EQUALS(as<Integer>(matches->full()->at(state, 0))->to_native(), 1); TS_ASSERT_EQUALS(as<Integer>(matches->full()->at(state, 1))->to_native(), 3); TS_ASSERT_EQUALS(matches->region()->num_fields(), 1); TS_ASSERT_EQUALS(as<Integer>(as<Tuple>(matches->region()->at(state, 0))->at(state, 0))->to_native(), 2); TS_ASSERT_EQUALS(as<Integer>(as<Tuple>(matches->region()->at(state, 0))->at(state, 1))->to_native(), 3); }