8000 Annotate Regexp#match? as leaf by k0kubun · Pull Request #9771 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

Annotate Regexp#match? as leaf #9771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
Annotate Regexp#match? as leaf
  • Loading branch information
k0kubun committed Jan 30, 2024
commit 98cbb48da914dd1f2055f0d39c455d26548a753a
1 change: 1 addition & 0 deletions .document
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ numeric.rb
nilclass.rb
pack.rb
ractor.rb
regexp.rb
string.rb
symbol.rb
timev.rb
Expand Down
4 changes: 4 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,7 @@ BUILTIN_RB_SRCS = \
$(srcdir)/array.rb \
$(srcdir)/kernel.rb \
$(srcdir)/ractor.rb \
$(srcdir)/regexp.rb \
$(srcdir)/symbol.rb \
$(srcdir)/timev.rb \
$(srcdir)/thread_sync.rb \
Expand Down Expand Up @@ -10317,6 +10318,7 @@ miniinit.$(OBJEXT): {$(VPATH)}prism/ast.h
miniinit.$(OBJEXT): {$(VPATH)}prism/version.h
miniinit.$(OBJEXT): {$(VPATH)}prism_compile.h
miniinit.$(OBJEXT): {$(VPATH)}ractor.rb
miniinit.$(OBJEXT): {$(VPATH)}regexp.rb
miniinit.$(OBJEXT): {$(VPATH)}rjit.rb
miniinit.$(OBJEXT): {$(VPATH)}rjit_c.rb
miniinit.$(OBJEXT): {$(VPATH)}ruby_assert.h
Expand Down Expand Up @@ -14150,6 +14152,8 @@ re.$(OBJEXT): {$(VPATH)}re.c
re.$(OBJEXT): {$(VPATH)}re.h
re.$(OBJEXT): {$(VPATH)}regenc.h
re.$(OBJEXT): {$(VPATH)}regex.h
re.$(OBJEXT): {$(VPATH)}regexp.rb
re.$(OBJEXT): {$(VPATH)}regexp.rbinc
re.$(OBJEXT): {$(VPATH)}regint.h
re.$(OBJEXT): {$(VPATH)}ruby_assert.h
re.$(OBJEXT): {$(VPATH)}ruby_atomic.h
Expand Down
1 change: 1 addition & 0 deletions inits.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ rb_call_builtin_inits(void)
#define BUILTIN(n) CALL(builtin_##n)
BUILTIN(gc);
BUILTIN(ractor);
BUILTIN(regexp);
BUILTIN(numeric);
BUILTIN(io);
BUILTIN(dir);
Expand Down
26 changes: 2 additions & 24 deletions re.c
Original file line number Diff line number Diff line change
Expand Up @@ -3775,29 +3775,6 @@ rb_reg_match_m(int argc, VALUE *argv, VALUE re)
return result;
}

/*
* call-seq:
* match?(string) -> true or false
* match?(string, offset = 0) -> true or false
*
* Returns <code>true</code> or <code>false</code> to indicate whether the
* regexp is matched or not without updating $~ and other related variables.
* If the second parameter is present, it specifies the position in the string
* to begin the search.
*
* /R.../.match?("Ruby") # => true
* /R.../.match?("Ruby", 1) # => false
* /P.../.match?("Ruby") # => false
* $& # => nil
*/

static VALUE
rb_reg_match_m_p(int argc, VALUE *argv, VALUE re)
{
long pos = rb_check_arity(argc, 1, 2) > 1 ? NUM2LONG(argv[1]) : 0;
return rb_reg_match_p(re, argv[0], pos);
}

VALUE
rb_reg_match_p(VALUE re, VALUE str, long pos)
{
Expand Down Expand Up @@ -4760,7 +4737,6 @@ Init_Regexp(void)
rb_define_method(rb_cRegexp, "===", rb_reg_eqq, 1);
rb_define_method(rb_cRegexp, "~", rb_reg_match2, 0);
rb_define_method(rb_cRegexp, "match", rb_reg_match_m, -1);
rb_define_method(rb_cRegexp, "match?", rb_reg_match_m_p, -1);
rb_define_method(rb_cRegexp, "to_s", rb_reg_to_s, 0);
rb_define_method(rb_cRegexp, "inspect", rb_reg_inspect, 0);
rb_define_method(rb_cRegexp, "source", rb_reg_source, 0);
Expand Down Expand Up @@ -4821,3 +4797,5 @@ Init_Regexp(void)
rb_define_method(rb_cMatch, "eql?", match_equal, 1);
rb_define_method(rb_cMatch, "==", match_equal, 1);
}

#include "regexp.rbinc"
19 changes: 19 additions & 0 deletions regexp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Regexp
# call-seq:
# match?(string) -> true or false
# match?(string, offset = 0) -> true or false
#
# Returns <code>true</code> or <code>false</code> to indicate whether the
# regexp is matched or not without updating $~ and other related variables.
# If the second parameter is present, it specifies the position in the string
# to begin the search.
#
# /R.../.match?("Ruby") # => true
# /R.../.match?("Ruby", 1) # => false
# /P.../.match?("Ruby") # => false
# $& # => nil
def match?(str, offset = 0)
Primitive.attr! :leaf
Primitive.cexpr! 'rb_reg_match_p(self, str, NUM2LONG(offset))'
end
end
0