diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-12-12 02:56:12 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-12-12 02:56:12 +0000 |
commit | 6dd5ee752ae1d6268619b96dec7d31e993cc59d9 (patch) | |
tree | 85a4ba28f3d0b584438569244f45d12dedec138e /string.c | |
parent | 3f3fc0182ec0c60899e9bea5639b19981acc4a9a (diff) |
String#match? and Symbol#match?
* string.c (rb_str_match_m_p): inverse of Regexp#match?. based on
the patch by Herwin Weststrate <herwin@snt.utwente.nl>.
[Fix GH-1483] [Feature #12898]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57053 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 41 |
1 files changed, 41 insertions, 0 deletions
@@ -3601,6 +3601,32 @@ rb_str_match_m(int argc, VALUE *argv, VALUE str) return result; } +/* + * call-seq: + * str.match?(pattern) -> true or false + * str.match?(pattern, pos) -> true or false + * + * Converts _pattern_ to a +Regexp+ (if it isn't already one), then + * returns a +true+ or +false+ indicates whether the regexp is + * matched _str_ or not without updating <code>$~</code> and other + * related variables. If the second parameter is present, it + * specifies the position in the string to begin the search. + * + * "Ruby".match?(/R.../) #=> true + * "Ruby".match?(/R.../, 1) #=> false + * "Ruby".match?(/P.../) #=> false + * $& #=> nil + */ + +static VALUE +rb_str_match_m_p(int argc, VALUE *argv, VALUE str) +{ + VALUE re; + rb_check_arity(argc, 1, 2); + re = get_pat(argv[0]); + return rb_reg_match_p(re, str, argc > 1 ? NUM2LONG(argv[1]) : 0); +} + enum neighbor_char { NEIGHBOR_NOT_CHAR, NEIGHBOR_FOUND, @@ -9740,6 +9766,19 @@ sym_match_m(int argc, VALUE *argv, VALUE sym) /* * call-seq: + * sym.match?(obj) -> true or false + * + * Returns <code>sym.to_s.match?(obj)</code>. + */ + +static VALUE +sym_match_m_p(int argc, VALUE *argv, VALUE sym) +{ + return rb_str_match_m_p(argc, argv, sym); +} + +/* + * call-seq: * sym[idx] -> char * sym[b, n] -> string * sym.slice(idx) -> char @@ -9924,6 +9963,7 @@ Init_String(void) rb_define_method(rb_cString, "empty?", rb_str_empty, 0); rb_define_method(rb_cString, "=~", rb_str_match, 1); rb_define_method(rb_cString, "match", rb_str_match_m, -1); + rb_define_method(rb_cString, "match?", rb_str_match_m_p, -1); rb_define_method(rb_cString, "succ", rb_str_succ, 0); rb_define_method(rb_cString, "succ!", rb_str_succ_bang, 0); rb_define_method(rb_cString, "next", rb_str_succ, 0); @@ -10070,6 +10110,7 @@ Init_String(void) rb_define_method(rb_cSymbol, "size", sym_length, 0); rb_define_method(rb_cSymbol, "empty?", sym_empty, 0); rb_define_method(rb_cSymbol, "match", sym_match_m, -1); + rb_define_method(rb_cSymbol, "match?", sym_match_m_p, -1); rb_define_method(rb_cSymbol, "upcase", sym_upcase, -1); rb_define_method(rb_cSymbol, "downcase", sym_downcase, -1); |