Class: RE2::MatchData
Instance Method Summary collapse
-
#[](*args) ⇒ Object
Retrieve zero, one or more matches by index or name.
-
#begin(n) ⇒ Integer?
Returns the offset of the start of the nth element of the MatchData.
-
#captures ⇒ Array<String, nil>
Returns the array of submatches.
-
#deconstruct ⇒ Array<String, nil>
Returns the array of submatches.
-
#deconstruct_keys(keys) ⇒ Hash
Returns a hash of capturing group names to submatches for pattern matching.
-
#end(n) ⇒ Integer?
Returns the offset of the character following the end of the nth element of the MatchData.
- #initialize_copy(other) ⇒ Object
-
#inspect ⇒ String
Returns a printable version of the match.
-
#length ⇒ Integer
Returns the number of elements in the MatchData (including the overall match, submatches and any
nils). -
#match_length(n) ⇒ Integer?
Returns the length of the nth match in characters.
-
#named_captures(*args) ⇒ Object
Returns a hash of capturing group names to matched strings.
-
#names ⇒ Array<String>
Returns an array of names of named capturing groups.
-
#offset(n) ⇒ Array<Integer>?
Returns a two-element array containing the beginning and ending offsets of the nth match.
-
#post_match ⇒ String
Returns the portion of the original string after the match.
-
#pre_match ⇒ String
Returns the portion of the original string before the match.
-
#regexp ⇒ RE2::Regexp
Returns the Regexp used in the match.
-
#size ⇒ Integer
Returns the number of elements in the MatchData (including the overall match, submatches and any
nils). -
#string ⇒ String
Returns a frozen copy of the text supplied when matching.
-
#to_a ⇒ Array<String, nil>
Returns the array of matches including the overall match, submatches and any
nils. -
#to_s ⇒ String
Returns the entire matched string.
-
#values_at(*args) ⇒ Array<String, nil>
Returns an array of match values at the given indices or names.
Instance Method Details
#[](index) ⇒ String? #[](start, length) ⇒ Array<String, nil> #[](range) ⇒ Array<String, nil> #[](name) ⇒ String?
Retrieve zero, one or more matches by index or name.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 |
# File 'ext/re2/re2.cc', line 1005
static VALUE re2_matchdata_aref(int argc, VALUE *argv, const VALUE self) {
VALUE idx, rest;
rb_scan_args(argc, argv, "11", &idx, &rest);
if (TYPE(idx) == T_STRING) {
return re2_matchdata_named_match(
std::string(RSTRING_PTR(idx), RSTRING_LEN(idx)), self);
} else if (SYMBOL_P(idx)) {
return re2_matchdata_named_match(rb_id2name(SYM2ID(idx)), self);
} else if (!NIL_P(rest) || !RB_INTEGER_TYPE_P(idx) || NUM2INT(idx) < 0) {
return rb_ary_aref(argc, argv, re2_matchdata_to_a(self));
} else {
return re2_matchdata_nth_match(NUM2INT(idx), self);
}
}
|
#begin(n) ⇒ Integer?
Returns the offset of the start of the nth element of the RE2::MatchData.
711 712 713 714 715 716 717 718 719 720 721 722 |
# File 'ext/re2/re2.cc', line 711
static VALUE re2_matchdata_begin(const VALUE self, VALUE n) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2::StringPiece *match = re2_matchdata_find_match(n, self);
if (match == nullptr) {
return Qnil;
} else {
long offset = match->data() - RSTRING_PTR(m->text);
return LONG2NUM(rb_str_sublen(m->text, offset));
}
}
|
#captures ⇒ Array<String, nil>
Returns the array of submatches.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is
undefined).
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 |
# File 'ext/re2/re2.cc', line 1098
static VALUE re2_matchdata_deconstruct(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2_pattern *p = unwrap_re2_regexp(m->regexp);
VALUE array = rb_ary_new2(m->number_of_matches - 1);
for (int i = 1; i < m->number_of_matches; ++i) {
re2::StringPiece *match = &m->matches[i];
if (match->data() == nullptr) {
rb_ary_push(array, Qnil);
} else {
rb_ary_push(array, encoded_str_new(match->data(), match->size(),
p->pattern->options().encoding()));
}
}
return array;
}
|
#deconstruct ⇒ Array<String, nil>
Returns the array of submatches.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is
undefined).
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 |
# File 'ext/re2/re2.cc', line 1098
static VALUE re2_matchdata_deconstruct(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2_pattern *p = unwrap_re2_regexp(m->regexp);
VALUE array = rb_ary_new2(m->number_of_matches - 1);
for (int i = 1; i < m->number_of_matches; ++i) {
re2::StringPiece *match = &m->matches[i];
if (match->data() == nullptr) {
rb_ary_push(array, Qnil);
} else {
rb_ary_push(array, encoded_str_new(match->data(), match->size(),
p->pattern->options().encoding()));
}
}
return array;
}
|
#deconstruct_keys(keys) ⇒ Hash
Returns a hash of capturing group names to submatches for pattern matching.
As this is used by Ruby's pattern matching, it will return an empty hash if given more keys than there are capturing groups. Given keys will populate the hash in order but an invalid name will cause the hash to be immediately returned.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 |
# File 'ext/re2/re2.cc', line 1146
static VALUE re2_matchdata_deconstruct_keys(const VALUE self, const VALUE keys) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2_pattern *p = unwrap_re2_regexp(m->regexp);
const auto& groups = p->pattern->NamedCapturingGroups();
VALUE capturing_groups = rb_hash_new();
if (NIL_P(keys)) {
for (const auto& group : groups) {
rb_hash_aset(capturing_groups,
ID2SYM(rb_intern2(group.first.data(), group.first.size())),
re2_matchdata_nth_match(group.second, self));
}
} else {
Check_Type(keys, T_ARRAY);
if (p->pattern->NumberOfCapturingGroups() >= RARRAY_LEN(keys)) {
for (int i = 0; i < RARRAY_LEN(keys); ++i) {
VALUE key = rb_ary_entry(keys, i);
Check_Type(key, T_SYMBOL);
const char *name = rb_id2name(SYM2ID(key));
auto search = groups.find(name);
if (search != groups.end()) {
rb_hash_aset(capturing_groups, key, re2_matchdata_nth_match(search->second, self));
} else {
break;
}
}
}
}
return capturing_groups;
}
|
#end(n) ⇒ Integer?
Returns the offset of the character following the end of the nth element of the RE2::MatchData.
736 737 738 739 740 741 742 743 744 745 746 747 |
# File 'ext/re2/re2.cc', line 736
static VALUE re2_matchdata_end(const VALUE self, VALUE n) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2::StringPiece *match = re2_matchdata_find_match(n, self);
if (match == nullptr) {
return Qnil;
} else {
long offset = (match->data() - RSTRING_PTR(m->text)) + match->size();
return LONG2NUM(rb_str_sublen(m->text, offset));
}
}
|
#initialize_copy(other) ⇒ Object
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 |
# File 'ext/re2/re2.cc', line 1295
static VALUE re2_matchdata_initialize_copy(VALUE self, VALUE other) {
if (self == other) return self;
re2_matchdata *self_m;
re2_matchdata *other_m = unwrap_re2_matchdata(other);
TypedData_Get_Struct(self, re2_matchdata, &re2_matchdata_data_type, self_m);
if (self_m->matches) {
delete[] self_m->matches;
self_m->matches = nullptr;
}
self_m->number_of_matches = other_m->number_of_matches;
RB_OBJ_WRITE(self, &self_m->regexp, other_m->regexp);
RB_OBJ_WRITE(self, &self_m->text, other_m->text);
if (other_m->matches) {
self_m->matches = new(std::nothrow) re2::StringPiece[other_m->number_of_matches];
if (self_m->matches == nullptr) {
rb_raise(rb_eNoMemError,
"not enough memory to allocate StringPiece for matches");
}
for (int i = 0; i < other_m->number_of_matches; ++i) {
self_m->matches[i] = other_m->matches[i];
}
} else {
self_m->matches = nullptr;
}
return self;
}
|
#inspect ⇒ String
Returns a printable version of the match.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 |
# File 'ext/re2/re2.cc', line 1045
static VALUE re2_matchdata_inspect(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2_pattern *p = unwrap_re2_regexp(m->regexp);
std::ostringstream output;
output << "#<RE2::MatchData";
for (int i = 0; i < m->number_of_matches; ++i) {
output << " ";
if (i > 0) {
output << i << ":";
}
VALUE match = re2_matchdata_nth_match(i, self);
if (match == Qnil) {
output << "nil";
} else {
output << "\"";
output.write(RSTRING_PTR(match), RSTRING_LEN(match));
output << "\"";
}
}
output << ">";
return encoded_str_new(output.str().data(), output.str().length(),
p->pattern->options().encoding());
}
|
#length ⇒ Integer
Returns the number of elements in the RE2::MatchData (including the
overall match, submatches and any nils).
694 695 696 697 698 |
# File 'ext/re2/re2.cc', line 694
static VALUE re2_matchdata_size(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
return INT2FIX(m->number_of_matches);
}
|
#match_length(n) ⇒ Integer?
Returns the length of the nth match in characters. This is equivalent to
m[n].length but without allocating a new string.
847 848 849 850 851 852 853 854 855 856 857 858 859 860 |
# File 'ext/re2/re2.cc', line 847
static VALUE re2_matchdata_match_length(const VALUE self, VALUE n) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2::StringPiece *match = re2_matchdata_find_match(n, self);
if (match == nullptr) {
return Qnil;
}
long start = match->data() - RSTRING_PTR(m->text);
long end_pos = start + match->size();
long char_len = rb_str_sublen(m->text, end_pos) - rb_str_sublen(m->text, start);
return LONG2NUM(char_len);
}
|
#named_captures ⇒ Hash #named_captures(symbolize_names:) ⇒ Hash
Returns a hash of capturing group names to matched strings.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
1207 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 |
# File 'ext/re2/re2.cc', line 1207
static VALUE re2_matchdata_named_captures(int argc, VALUE *argv, const VALUE self) {
VALUE opts;
rb_scan_args(argc, argv, "0:", &opts);
bool symbolize = false;
if (!NIL_P(opts)) {
VALUE sym = rb_hash_aref(opts, ID2SYM(id_symbolize_names));
symbolize = RTEST(sym);
}
re2_matchdata *m = unwrap_re2_matchdata(self);
re2_pattern *p = unwrap_re2_regexp(m->regexp);
const auto& groups = p->pattern->NamedCapturingGroups();
VALUE result = rb_hash_new();
for (const auto& group : groups) {
VALUE key;
if (symbolize) {
key = ID2SYM(rb_intern2(group.first.data(), group.first.size()));
} else {
key = encoded_str_new(group.first.data(), group.first.size(),
p->pattern->options().encoding());
}
rb_hash_aset(result, key, re2_matchdata_nth_match(group.second, self));
}
return result;
}
|
#names ⇒ Array<String>
Returns an array of names of named capturing groups. Names are returned in alphabetical order rather than definition order, as RE2 stores named groups internally in a sorted map.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
1251 1252 1253 1254 1255 |
# File 'ext/re2/re2.cc', line 1251
static VALUE re2_matchdata_names(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
return re2_regexp_names(m->regexp);
}
|
#offset(n) ⇒ Array<Integer>?
Returns a two-element array containing the beginning and ending offsets of the nth match.
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 |
# File 'ext/re2/re2.cc', line 816
static VALUE re2_matchdata_offset(const VALUE self, VALUE n) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2::StringPiece *match = re2_matchdata_find_match(n, self);
if (match == nullptr) {
return Qnil;
}
long start = match->data() - RSTRING_PTR(m->text);
long end_pos = start + match->size();
VALUE array = rb_ary_new2(2);
rb_ary_push(array, LONG2NUM(rb_str_sublen(m->text, start)));
rb_ary_push(array, LONG2NUM(rb_str_sublen(m->text, end_pos)));
return array;
}
|
#post_match ⇒ String
Returns the portion of the original string after the match.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 |
# File 'ext/re2/re2.cc', line 788
static VALUE re2_matchdata_post_match(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2_pattern *p = unwrap_re2_regexp(m->regexp);
re2::StringPiece *match = &m->matches[0];
if (match->data() == nullptr) {
return Qnil;
}
long start = (match->data() - RSTRING_PTR(m->text)) + match->size();
long remaining = RSTRING_LEN(m->text) - start;
return encoded_str_new(RSTRING_PTR(m->text) + start, remaining,
p->pattern->options().encoding());
}
|
#pre_match ⇒ String
Returns the portion of the original string before the match.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
761 762 763 764 765 766 767 768 769 770 771 772 773 774 |
# File 'ext/re2/re2.cc', line 761
static VALUE re2_matchdata_pre_match(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2_pattern *p = unwrap_re2_regexp(m->regexp);
re2::StringPiece *match = &m->matches[0];
if (match->data() == nullptr) {
return Qnil;
}
long offset = match->data() - RSTRING_PTR(m->text);
return encoded_str_new(RSTRING_PTR(m->text), offset,
p->pattern->options().encoding());
}
|
#regexp ⇒ RE2::Regexp
Returns the Regexp used in the match.
870 871 872 873 874 |
# File 'ext/re2/re2.cc', line 870
static VALUE re2_matchdata_regexp(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
return m->regexp;
}
|
#size ⇒ Integer
Returns the number of elements in the RE2::MatchData (including the
overall match, submatches and any nils).
694 695 696 697 698 |
# File 'ext/re2/re2.cc', line 694
static VALUE re2_matchdata_size(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
return INT2FIX(m->number_of_matches);
}
|
#string ⇒ String
Returns a frozen copy of the text supplied when matching.
If the text was already a frozen string, returns the original.
461 462 463 464 465 |
# File 'ext/re2/re2.cc', line 461
static VALUE re2_matchdata_string(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
return m->text;
}
|
#to_a ⇒ Array<String, nil>
Returns the array of matches including the overall match, submatches and any
nils.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 |
# File 'ext/re2/re2.cc', line 909
static VALUE re2_matchdata_to_a(const VALUE self) {
re2_matchdata *m = unwrap_re2_matchdata(self);
re2_pattern *p = unwrap_re2_regexp(m->regexp);
VALUE array = rb_ary_new2(m->number_of_matches);
for (int i = 0; i < m->number_of_matches; ++i) {
re2::StringPiece *match = &m->matches[i];
if (match->data() == nullptr) {
rb_ary_push(array, Qnil);
} else {
rb_ary_push(array, encoded_str_new(match->data(), match->size(),
p->pattern->options().encoding()));
}
}
return array;
}
|
#to_s ⇒ String
Returns the entire matched string.
1029 1030 1031 |
# File 'ext/re2/re2.cc', line 1029 static VALUE re2_matchdata_to_s(const VALUE self) { return re2_matchdata_nth_match(0, self); } |
#values_at(*args) ⇒ Array<String, nil>
Returns an array of match values at the given indices or names.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 |
# File 'ext/re2/re2.cc', line 1273
static VALUE re2_matchdata_values_at(int argc, VALUE *argv, const VALUE self) {
unwrap_re2_matchdata(self);
VALUE result = rb_ary_new2(argc);
for (int i = 0; i < argc; ++i) {
VALUE idx = argv[i];
if (TYPE(idx) == T_STRING) {
rb_ary_push(result, re2_matchdata_named_match(
std::string(RSTRING_PTR(idx), RSTRING_LEN(idx)), self));
} else if (SYMBOL_P(idx)) {
rb_ary_push(result, re2_matchdata_named_match(
rb_id2name(SYM2ID(idx)), self));
} else {
rb_ary_push(result, re2_matchdata_nth_match(NUM2INT(idx), self));
}
}
return result;
}
|