Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions mrbgems/mruby-regexp/mrblib/string_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ class String
alias __split split

# `match` and `match?` accept a Regexp or a String and reject everything
# else. The check lives in C (see Regexp.__match_pattern) so that the
# else. The check lives in C (see Regexp.__check_pattern) so that the
# argument cannot steer it: it cannot pose as a Regexp, and there is no
# helper on String for a subclass to redefine. Compiling an accepted String
# stays here, so the check does not have to call back into the VM; `String
# ===` goes through `Module#===` and cannot be redefined either.
def match(re, pos = 0, &block)
re = Regexp.__match_pattern(re)
re = Regexp.__check_pattern(re)
re = Regexp.new(re) if String === re
re.match(self, pos, &block)
end

def match?(re, pos = 0)
re = Regexp.__match_pattern(re)
re = Regexp.__check_pattern(re)
re = Regexp.new(re) if String === re
re.match?(self, pos)
end
Expand All @@ -42,11 +42,11 @@ def sub(*args, &block)
raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 2)"
end
pattern, replacement = *args
pattern = Regexp.__match_pattern(pattern)
pattern = Regexp.__check_pattern(pattern)
# Unlike `match`, a String pattern is quoted rather than compiled: it is a
# literal here, the distinction CRuby draws between get_pat_quoted and
# get_pat. Only the quoting is taken from it: get_pat_quoted also accepts
# anything answering `to_str`, where `__match_pattern` keeps to a real
# anything answering `to_str`, where `__check_pattern` keeps to a real
# String, as `match` already does.
pattern = Regexp.new(Regexp.escape(pattern)) if String === pattern
# A replacement argument wins over the block, as in CRuby.
Expand All @@ -69,7 +69,7 @@ def gsub(*args, &block)
pattern, replacement = *args
# After the to_enum return above, so that `"abc".gsub(:b)` yields an
# Enumerator and raises on the first iteration, as CRuby does.
pattern = Regexp.__match_pattern(pattern)
pattern = Regexp.__check_pattern(pattern)
pattern = Regexp.new(Regexp.escape(pattern)) if String === pattern
# A replacement argument wins over the block, as in CRuby.
if args.length == 2
Expand Down Expand Up @@ -112,7 +112,7 @@ def gsub(*args, &block)
end

def scan(pattern)
pattern = Regexp.__match_pattern(pattern)
pattern = Regexp.__check_pattern(pattern)
pattern = Regexp.new(Regexp.escape(pattern)) if String === pattern
result = pattern.__scan(self)
if block_given?
Expand Down Expand Up @@ -149,8 +149,8 @@ def split(pattern = nil, *args)
end
return self.empty? ? [] : [self] if limit == 1
# nil and String patterns already went to __split above, so the String
# branch of the resolver is unreachable here and nothing needs quoting.
pattern = Regexp.__match_pattern(pattern)
# branch of the check is unreachable here and nothing needs quoting.
pattern = Regexp.__check_pattern(pattern)

result = []
field_start = 0
Expand Down
4 changes: 2 additions & 2 deletions mrbgems/mruby-regexp/src/regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ regexp_scan(mrb_state *mrb, mrb_value self)
needs no callback into the VM. CRuby names `nil`, `true` and `false` by
value and everything else by class. */
static mrb_value
regexp_match_pattern(mrb_state *mrb, mrb_value self)
regexp_check_pattern(mrb_state *mrb, mrb_value self)
{
mrb_value re;
mrb_get_args(mrb, "o", &re);
Expand Down Expand Up @@ -1157,7 +1157,7 @@ mrb_mruby_regexp_gem_init(mrb_state *mrb)
mrb_define_class_method(mrb, re, "escape", regexp_escape, MRB_ARGS_REQ(1));
mrb_define_class_method(mrb, re, "quote", regexp_escape, MRB_ARGS_REQ(1));
mrb_define_class_method(mrb, re, "__binary_string?", regexp_binary_string_p, MRB_ARGS_REQ(1));
mrb_define_class_method(mrb, re, "__match_pattern", regexp_match_pattern, MRB_ARGS_REQ(1));
mrb_define_class_method(mrb, re, "__check_pattern", regexp_check_pattern, MRB_ARGS_REQ(1));
Comment thread
coderabbitai[bot] marked this conversation as resolved.

/* Instance methods */
mrb_define_method(mrb, re, "match", regexp_match, MRB_ARGS_ARG(1, 1)|MRB_ARGS_BLOCK());
Expand Down
2 changes: 1 addition & 1 deletion mrbgems/mruby-regexp/test/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ def nil?
end

class StringMatchHelperOverride < String
private def __match_pattern(re)
private def __check_pattern(re)
Regexp.new(re.to_s)
end
end
Expand Down
Loading