@@ -1136,14 +1136,12 @@ public IRubyObject match_m(ThreadContext context, IRubyObject str, IRubyObject p
1136
1136
1137
1137
@ JRubyMethod (name = "match?" )
1138
1138
public IRubyObject match_p (ThreadContext context , IRubyObject str ) {
1139
- IRubyObject [] dummy = new IRubyObject [1 ];
1140
- return context .runtime .newBoolean (matchPos (context , str , null , dummy , 0 ) >= 0 );
1139
+ return matchP (context , str , 0 );
1141
1140
}
1142
1141
1143
1142
@ JRubyMethod (name = "match?" )
1144
1143
public IRubyObject match_p (ThreadContext context , IRubyObject str , IRubyObject pos ) {
1145
- IRubyObject [] dummy = new IRubyObject [1 ];
1146
- return context .runtime .newBoolean (matchPos (context , str , null , dummy , RubyNumeric .num2int (pos )) >= 0 );
1144
+ return matchP (context , str , RubyNumeric .num2int (pos ));
1147
1145
}
1148
1146
1149
1147
private IRubyObject matchCommon (ThreadContext context , IRubyObject str , int pos , boolean setBackref , Block block ) {
@@ -1183,6 +1181,35 @@ private int matchPos(ThreadContext context, IRubyObject arg, RubyString[] strp,
1183
1181
return search (context , str , pos , false , holder );
1184
1182
}
1185
1183
1184
+ private RubyBoolean matchP (ThreadContext context , IRubyObject arg , int pos ) {
1185
+ if (arg == context .nil ) return context .fals ;
1186
+ RubyString str = arg instanceof RubySymbol ? ((RubySymbol ) arg ).to_s (context .runtime ) : arg .convertToString ();
1187
+
1188
+ if (pos != 0 ) {
1189
+ if (pos < 0 ) {
1190
+ pos += str .strLength ();
1191
+ if (pos < 0 ) return context .fals ;
1192
+ }
1193
+ pos = str .rbStrOffset (pos );
1194
+ }
1195
+
1196
+ final Regex reg = preparePattern (str );
1197
+
1198
+ final ByteList strBL = str .getByteList ();
1199
+ final int beg = strBL .begin ();
1200
+
1201
+ Matcher matcher = reg .matcherNoRegion (strBL .unsafeBytes (), beg , beg + strBL .realSize ());
1202
+
1203
+ int result ;
1204
+ try {
1205
+ result = matcherSearch (context , matcher , beg + pos , beg + strBL .realSize (), RE_OPTION_NONE );
1206
+ } catch (JOniException je ) {
1207
+ throw context .runtime .newRegexpError (je .getMessage ());
1208
+ }
1209
+
1210
+ return result == -1 ? context .fals : context .tru ;
1211
+ }
1212
+
1186
1213
/**
1187
1214
* MRI: rb_reg_search
1188
1215
*
0 commit comments