Skip to content

Commit c02ced6

Browse files
committed
Add MatchData#match and MatchData#match_length
1 parent 216e5c9 commit c02ced6

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Compatibility:
8989
* Add optional `Hash` argument to `Enumerable#tally` (#2733, @andrykonchin).
9090
* Update `$LOAD_PATH.resolve_feature_path` to return `nil` instead of raising `LoadError` when feature isn't found (#2733, @andrykonchin).
9191
* Add `objspace/trace` file (#2733, @andrykonchin).
92+
* Add `MatchData#match` and `MatchData#match_length` (#2733, @horakivo).
9293

9394
Performance:
9495

spec/tags/core/matchdata/match_length_tags.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

spec/tags/core/matchdata/match_tags.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ public boolean isTruffleBootMainMethod(SharedMethodInfo info) {
10131013
"/core/truffle/ctype.rb",
10141014
"/core/truffle/integer_operations.rb",
10151015
"/core/integer.rb",
1016+
"/core/match_data.rb",
10161017
"/core/regexp.rb",
10171018
"/core/transcoding.rb",
10181019
"/core/encoding.rb",
@@ -1058,7 +1059,6 @@ public boolean isTruffleBootMainMethod(SharedMethodInfo info) {
10581059
"/core/posix.rb",
10591060
"/core/main.rb",
10601061
"/core/post.rb",
1061-
"/core/match_data.rb",
10621062
POST_BOOT_FILE
10631063
};
10641064

src/main/ruby/truffleruby/core/match_data.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ def values_at(*indexes)
137137
out
138138
end
139139

140+
def match(n)
141+
# Similar, but #match accepts only single index/name, but not a range or an optional length.
142+
number = Truffle::Type.rb_check_convert_type(n, Integer, :to_int)
143+
return self[number] if number
144+
# To convert the last type (String) we used rb_convert_type instead of rb_check_convert_type which throws an exception
145+
name = Truffle::Type.rb_check_convert_type(n, Symbol, :to_sym) || Truffle::Type.rb_convert_type(n, String, :to_str)
146+
self[name]
147+
end
148+
149+
def match_length(n)
150+
match(n)&.length
151+
end
152+
140153
def to_s
141154
self[0]
142155
end

test/mri/excludes/TestRegexp.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,3 @@
3737
exclude :test_rindex_regexp, "needs investigation"
3838
exclude :test_yoshidam_net_20041111_1, "needs investigation"
3939
exclude :test_match_control_meta_escape, "<0> expected but was"
40-
exclude :test_match_matchsubstring, "NoMethodError: undefined method `match' for #<MatchData \"HX1138\" 1:\"H\" 2:\"X\" 3:\"113\" 4:\"8\" 5:nil>"
41-
exclude :test_match_match_length, "NoMethodError: undefined method `match_length' for #<MatchData \"HX1138\" 1:\"H\" 2:\"X\" 3:\"113\" 4:\"8\" 5:nil>"

0 commit comments

Comments
 (0)