Class: MatchData

Inherits:
Object
  • Object
show all
Defined in:
mrbgems/mruby-regexp/src/regexp.c,
mrbgems/mruby-regexp/mrblib/regexp.rb

Overview

MatchData class

Instance Method Summary collapse

Instance Method Details

#[] ⇒ Object

The (start, length) and Range forms slice the groups the way Array#[] slices to_a. They are told apart from the group forms the way CRuby's match_aref() tells them apart: a second argument, unless it is nil, forces both arguments through integer conversion, so a name or a Range in the first position raises TypeError there; without one, only a Range slices, and everything that is not a name converts to a single index.



1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1208

static mrb_value
matchdata_aref(mrb_state *mrb, mrb_value self)
{
  mrb_value arg, len_v = mrb_nil_value();
  mrb_int argc = mrb_get_args(mrb, "o|o", &arg, &len_v);

  if ((argc < 2 || mrb_nil_p(len_v)) && !mrb_range_p(arg)) {
    return md_aref(mrb, self, arg);
  }

  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_nil_value();

  mrb_int beg, len;
  if (argc == 2 && !mrb_nil_p(len_v)) {
    beg = mrb_as_int(mrb, arg);
    len = mrb_as_int(mrb, len_v);
    if (len < 0) return mrb_nil_value();
    if (beg < 0) {
      beg += md->num_captures;
      if (beg < 0) return mrb_nil_value();
    }
    else if (beg > md->num_captures) {
      return mrb_nil_value();
    }
    if (len > md->num_captures - beg) len = md->num_captures - beg;
  }
  else if (mrb_range_beg_len(mrb, arg, &beg, &len, md->num_captures, TRUE) != MRB_RANGE_OK) {
    return mrb_nil_value();
  }

  mrb_value ary = mrb_ary_new_capa(mrb, len);
  for (mrb_int i = 0; i < len; i++) {
    mrb_ary_push(mrb, ary, md_nth(mrb, md, beg + i));
  }
  return ary;
}

#__group ⇒ Object

Private: what $& and $1 onward read, the group of that number, the whole match at 0. The compiler derives them from $~ with this and not with [], so that a program redefining [] moves $~[n] and leaves the names alone, as it does in CRuby, where they come from the backref. n arrives from the compiler and is never a name; a negative one, which only a direct call can pass, reads as no group rather than counting back.



1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1467

static mrb_value
matchdata_group(mrb_state *mrb, mrb_value self)
{
  mrb_int n;
  mrb_get_args(mrb, "i", &n);
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md || n < 0 || n >= md->num_captures) return mrb_nil_value();
  int s = md->captures[n*2];
  if (s < 0) return mrb_nil_value();
  return re_byte_substr(mrb, md->source, s, md->captures[n*2+1] - s);
}

#__last_group ⇒ Object

Private: what $+ reads, the last group that took part in the match, which is not necessarily the last group in the pattern. The compiler derives $+ from $~ with this, as it derives $1 with __group.



1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1482

static mrb_value
matchdata_last_group(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_nil_value();
  for (int g = md->num_captures - 1; g >= 1; g--) {
    int s = md->captures[g*2];
    if (s >= 0) {
      return re_byte_substr(mrb, md->source, s, md->captures[g*2+1] - s);
    }
  }
  return mrb_nil_value();
}

#__post_match ⇒ Object



1452
1453
1454
1455
1456
1457
1458
1459
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1452

static mrb_value
matchdata_post(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md || md->captures[1] < 0) return mrb_nil_value();
  int pos = md->captures[1];
  return re_byte_substr(mrb, md->source, pos, RSTRING_LEN(md->source) - pos);
}

#__pre_match ⇒ Object

MatchData#pre_match / #post_match, which $` and $' also read under the private names __pre_match and __post_match, so that a program redefining the public pair moves $~.pre_match and $~.post_match and leaves the two globals alone.



1444
1445
1446
1447
1448
1449
1450
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1444

static mrb_value
matchdata_pre(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md || md->captures[0] < 0) return mrb_nil_value();
  return re_byte_substr(mrb, md->source, 0, md->captures[0]);
}

#begin ⇒ Object



1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1361

static mrb_value
matchdata_begin(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;
  mrb_get_args(mrb, "o", &arg);

  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_nil_value();
  mrb_int idx = matchdata_group_arg(mrb, md, arg);
  int pos = md->captures[idx * 2];
  if (pos < 0) return mrb_nil_value();
  return mrb_int_value(mrb, re_byte_to_char(mrb, md->source, pos));
}

#captures ⇒ Object



1267
1268
1269
1270
1271
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1267

static mrb_value
matchdata_captures(mrb_state *mrb, mrb_value self)
{
  return matchdata_to_ary(mrb, self, 1);
}

#end ⇒ Object



1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1375

static mrb_value
matchdata_end(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;
  mrb_get_args(mrb, "o", &arg);

  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_nil_value();
  mrb_int idx = matchdata_group_arg(mrb, md, arg);
  int pos = md->captures[idx * 2 + 1];
  if (pos < 0) return mrb_nil_value();
  return mrb_int_value(mrb, re_byte_to_char(mrb, md->source, pos));
}

#inspect ⇒ Object

MatchData#inspect - the groups by number or name, e.g. #<MatchData "ab" 1:"a" 2:"b">



1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1638

static mrb_value
matchdata_inspect(mrb_state *mrb, mrb_value self)
{
  /* dup/clone leave the copy without match data. inspect answers rather than
     raising, as it does on an uninitialized Regexp, and CRuby's answer for a
     MatchData with no data is the bare class-and-address form. */
  mrb_match_data *md = DATA_CHECK_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_any_to_s(mrb, self);

  /* A match a literal String pattern made carries no Regexp until
     MatchData#regexp builds one. CRuby renders such a match as
     "#<MatchData: ab>", the whole match raw, and switches to the group
     listing once the memo is filled; the memo is mirrored here, so the
     switch comes with it. */
  if (mrb_nil_p(md->regexp)) {
    mrb_value result = mrb_str_new_lit(mrb, "#<MatchData: ");
    mrb_str_cat_str(mrb, result, re_byte_substr(mrb, md->source, md->captures[0],
                                                md->captures[1] - md->captures[0]));
    mrb_str_cat_lit(mrb, result, ">");
    return result;
  }

  mrb_regexp_pattern *pat = DATA_GET_PTR(mrb, md->regexp, &regexp_type, mrb_regexp_pattern);

  mrb_value result = mrb_str_new_lit(mrb, "#<MatchData");
  int ai = mrb_gc_arena_save(mrb);
  for (int i = 0; i < md->num_captures; i++) {
    mrb_str_cat_lit(mrb, result, " ");
    if (i > 0) {
      /* A group a name reaches is labeled with the name, not its number.
         Each group carries at most one name, so the first entry that names
         it is the whole answer; several groups of one name each show it. */
      const re_named_capture *nc = NULL;
      for (uint16_t j = 0; j < pat->num_named; j++) {
        if (pat->named_captures[j].group == i) {
          nc = &pat->named_captures[j];
          break;
        }
      }
      if (nc) {
        mrb_str_cat(mrb, result, nc->name, nc->name_len);
      }
      else {
        mrb_str_cat_str(mrb, result, mrb_integer_to_str(mrb, mrb_int_value(mrb, i), 10));
      }
      mrb_str_cat_lit(mrb, result, ":");
    }
    int s = md->captures[i * 2];
    if (s < 0) {
      mrb_str_cat_lit(mrb, result, "nil");
    }
    else {
      mrb_value grp = re_byte_substr(mrb, md->source, s, md->captures[i * 2 + 1] - s);
      mrb_str_cat_str(mrb, result, mrb_str_inspect(mrb, grp));
    }
    mrb_gc_arena_restore(mrb, ai);
  }
  mrb_str_cat_lit(mrb, result, ">");
  return result;
}

#length ⇒ Object

MatchData#length / #size



1499
1500
1501
1502
1503
1504
1505
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1499

static mrb_value
matchdata_length(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_fixnum_value(0);
  return mrb_fixnum_value(md->num_captures);
}

#named_captures ⇒ Object

MatchData#named_captures



1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1510

static mrb_value
matchdata_named_captures(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_hash_new(mrb);

  mrb_regexp_pattern *pat = NULL;
  if (!mrb_nil_p(md->regexp)) {
    pat = DATA_GET_PTR(mrb, md->regexp, &regexp_type, mrb_regexp_pattern);
  }
  if (!pat || pat->num_named == 0) return mrb_hash_new(mrb);

  /* Each name gets the group re_name_to_group() picks for it, the same one
     MatchData#[] reads, so a name given to several groups answers with the
     one that took part in the match; walking the entries in order would
     instead leave the value of whichever group the pattern spelled the name
     on last. Duplicates re-resolve to the same group and overwrite with the
     same value, and the first entry of each name fixes its key's position. */
  mrb_value result = mrb_hash_new_capa(mrb, pat->num_named);
  for (uint16_t i = 0; i < pat->num_named; i++) {
    mrb_value name = mrb_str_new(mrb, pat->named_captures[i].name, pat->named_captures[i].name_len);
    int group = re_name_to_group(md->captures, md->num_captures, pat,
                                 pat->named_captures[i].name, pat->named_captures[i].name_len);
    mrb_value val = mrb_nil_value();
    if (group >= 0 && group < md->num_captures) {
      int s = md->captures[group * 2];
      int e = md->captures[group * 2 + 1];
      if (s >= 0) val = re_byte_substr(mrb, md->source, s, e - s);
    }
    mrb_hash_set(mrb, result, name, val);
  }
  return result;
}

#names ⇒ Object

named_captures is implemented in C via md->regexp



39
40
41
# File 'mrbgems/mruby-regexp/mrblib/regexp.rb', line 39

def names
  regexp.names
end

#offset ⇒ Object

The two offsets begin and end report, read in one call. CRuby's rb_match_offset() reads the argument the way begin and end read theirs, so a name reaches its group and an argument that reaches none raises. A group that took no part in the match has neither offset, and the pair is [nil, nil] rather than nil: the method always answers an array of two.



1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1398

static mrb_value
matchdata_offset(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;
  mrb_get_args(mrb, "o", &arg);

  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_nil_value();
  mrb_int idx = matchdata_group_arg(mrb, md, arg);
  int beg = md->captures[idx * 2];
  if (beg < 0) return mrb_assoc_new(mrb, mrb_nil_value(), mrb_nil_value());
  int end = md->captures[idx * 2 + 1];
  return mrb_assoc_new(mrb, mrb_int_value(mrb, re_byte_to_char(mrb, md->source, beg)),
                       mrb_int_value(mrb, re_byte_to_char(mrb, md->source, end)));
}

#post_match ⇒ Object



1452
1453
1454
1455
1456
1457
1458
1459
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1452

static mrb_value
matchdata_post(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md || md->captures[1] < 0) return mrb_nil_value();
  int pos = md->captures[1];
  return re_byte_substr(mrb, md->source, pos, RSTRING_LEN(md->source) - pos);
}

#pre_match ⇒ Object

MatchData#pre_match / #post_match, which $` and $' also read under the private names __pre_match and __post_match, so that a program redefining the public pair moves $~.pre_match and $~.post_match and leaves the two globals alone.



1444
1445
1446
1447
1448
1449
1450
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1444

static mrb_value
matchdata_pre(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md || md->captures[0] < 0) return mrb_nil_value();
  return re_byte_substr(mrb, md->source, 0, md->captures[0]);
}

#regexp ⇒ Object

MatchData#regexp - the Regexp used

A literal String pattern leaves none behind: __sub_lit and __gsub_lit search for its bytes without compiling anything to search with, and the paths that do quote one to search with withhold it from the match (see exec_match()). So the Regexp named here is built here, out of the bytes the match reports, the first time something asks for one; group 0 of a literal match spans the pattern itself, whichever path made it. That is what CRuby does with a match against a String pattern, down to the memo the answer is kept in. A call that never asks pays for no compile at all.



1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1602

static mrb_value
matchdata_regexp(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_nil_value();
  if (mrb_nil_p(md->regexp)) {
    /* Group 0 of a literal match spans the pattern itself, and a literal
       match is the only thing that arrives here without a Regexp. */
    mrb_value lit = re_byte_substr(mrb, md->source, md->captures[0],
                                   md->captures[1] - md->captures[0]);
    mrb_value re = re_quoted_regexp(mrb, lit);
    /* The instance variable is what keeps it reachable: the struct beside it
       is C-allocated and the GC does not scan it. */
    mrb_iv_set(mrb, self, MRB_SYM(regexp), re);
    md->regexp = re;
  }
  return md->regexp;
}

#size ⇒ Object

MatchData#length / #size



1499
1500
1501
1502
1503
1504
1505
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1499

static mrb_value
matchdata_length(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_fixnum_value(0);
  return mrb_fixnum_value(md->num_captures);
}

#string ⇒ Object

MatchData#string - the original string (frozen copy)



1547
1548
1549
1550
1551
1552
1553
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1547

static mrb_value
matchdata_string(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_nil_value();
  return md->source;
}

#to_a ⇒ Object



1273
1274
1275
1276
1277
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1273

static mrb_value
matchdata_to_a(mrb_state *mrb, mrb_value self)
{
  return matchdata_to_ary(mrb, self, 0);
}

#to_s ⇒ Object

MatchData#to_s - full match string



1624
1625
1626
1627
1628
1629
1630
1631
1632
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1624

static mrb_value
matchdata_to_s(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md || md->captures[0] < 0) return mrb_nil_value();
  int s = md->captures[0];
  int e = md->captures[1];
  return re_byte_substr(mrb, md->source, s, e - s);
}

#values_at ⇒ Object

Read the arguments the way CRuby's rb_match_values_at() does. A String or Symbol is the name of a named capture, looked up with the same rule as MatchData#[], so a name the pattern does not carry raises and one that did not take part in the match reads as nil. A Range reads the groups at its positions, the way Array#values_at reads its indexes: a negative bound counts back from the last group, so -num_captures reaches the whole match, and the positions past the last group pad nil. A range that starts before the match raises RangeError, the way an Array range index raises. Everything else converts to an integer and reads the group as MatchData#[] reads it, so a negative one never reaches the whole match and one out of range reads as nil.



1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
# File 'mrbgems/mruby-regexp/src/regexp.c', line 1294

static mrb_value
matchdata_values_at(mrb_state *mrb, mrb_value self)
{
  mrb_match_data *md = DATA_GET_PTR(mrb, self, &matchdata_type, mrb_match_data);
  if (!md) return mrb_ary_new(mrb);

  mrb_int argc = mrb_get_argc(mrb);
  const mrb_value *argv = mrb_get_argv(mrb);
  mrb_value ary = mrb_ary_new_capa(mrb, argc);
  for (mrb_int i = 0; i < argc; i++) {
    mrb_value v = argv[i];
    if (mrb_string_p(v) || mrb_symbol_p(v)) {
      mrb_ary_push(mrb, ary, md_nth(mrb, md, matchdata_name_to_group(mrb, md, v)));
    }
    else if (mrb_range_p(v)) {
      mrb_int beg, len;
      switch (mrb_range_beg_len(mrb, v, &beg, &len, md->num_captures, FALSE)) {
      case MRB_RANGE_OK:
        for (mrb_int j = 0; j < len; j++) {
          mrb_ary_push(mrb, ary, md_nth(mrb, md, beg + j));
        }
        break;
      case MRB_RANGE_OUT:
        mrb_raisef(mrb, E_RANGE_ERROR, "%v out of range", v);
        break;
      default:
        break;
      }
    }
    else {
      mrb_int idx = mrb_as_int(mrb, v);
      if (idx < 0) {
        idx += md->num_captures;
        if (idx <= 0) {
          mrb_ary_push(mrb, ary, mrb_nil_value());
          continue;
        }
      }
      mrb_ary_push(mrb, ary, md_nth(mrb, md, idx));
    }
  }
  return ary;
}