@@ -102,99 +102,6 @@ public static RubyClass createScannerClass(final Ruby runtime) {
102102 return scannerClass ;
103103 }
104104
105- // Provided temporarily to bridge the gap between joni 2.1 and 2.2
106- private static final RegionAdapter REGION_ADAPTER ;
107- static {
108- RegionAdapter adapter ;
109- try {
110- Region .class .getMethod ("newRegion" , int .class , int .class );
111- // ok, proceed with factory-based adapter
112- adapter = new FactoryRegionAdapter ();
113- } catch (NoSuchMethodException | SecurityException ex ) {
114- adapter = new OldRegionAdapter ();
115- }
116- REGION_ADAPTER = adapter ;
117- }
118-
119- private interface RegionAdapter {
120- Region newRegion (int beg , int end );
121- int getBeg (Region region , int index );
122- int getEnd (Region region , int index );
123- int setBeg (Region region , int index , int value );
124- int setEnd (Region region , int index , int value );
125- int getNumRegs (Region region );
126- }
127-
128- private static class OldRegionAdapter implements RegionAdapter {
129- @ Override
130- @ SuppressWarnings ("deprecation" )
131- public Region newRegion (int beg , int end ) {
132- return new Region (beg , end );
133- }
134-
135- @ Override
136- @ SuppressWarnings ("deprecation" )
137- public int getBeg (Region region , int index ) {
138- return region .beg [index ];
139- }
140-
141- @ Override
142- @ SuppressWarnings ("deprecation" )
143- public int getEnd (Region region , int index ) {
144- return region .end [index ];
145- }
146-
147- @ Override
148- @ SuppressWarnings ("deprecation" )
149- public int setBeg (Region region , int index , int value ) {
150- return region .beg [index ] = value ;
151- }
152-
153- @ Override
154- @ SuppressWarnings ("deprecation" )
155- public int setEnd (Region region , int index , int value ) {
156- return region .end [index ] = value ;
157- }
158-
159- @ Override
160- @ SuppressWarnings ("deprecation" )
161- public int getNumRegs (Region region ) {
162- return region .numRegs ;
163- }
164- }
165-
166- private static class FactoryRegionAdapter implements RegionAdapter {
167- @ Override
168- public Region newRegion (int beg , int end ) {
169- return Region .newRegion (beg , end );
170- }
171-
172- @ Override
173- public int getBeg (Region region , int index ) {
174- return region .getBeg (index );
175- }
176-
177- @ Override
178- public int getEnd (Region region , int index ) {
179- return region .getEnd (index );
180- }
181-
182- @ Override
183- public int setBeg (Region region , int index , int value ) {
184- return region .setBeg (index , value );
185- }
186-
187- @ Override
188- public int setEnd (Region region , int index , int value ) {
189- return region .setEnd (index , value );
190- }
191-
192- @ Override
193- public int getNumRegs (Region region ) {
194- return region .getNumRegs ();
195- }
196- }
197-
198105 private void clearMatched () {
199106 matched = false ;
200107 }
@@ -224,7 +131,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject string) {
224131 public IRubyObject initialize (ThreadContext context , IRubyObject string , IRubyObject dupOrOpts ) {
225132 this .str = string .convertToString ();
226133 this .fixedAnchor = ArgsUtil .extractKeywordArg (context , "fixed_anchor" , dupOrOpts ).isTrue ();
227- this .regs = REGION_ADAPTER .newRegion (0 , 0 );
134+ this .regs = Region .newRegion (0 , 0 );
228135
229136 return this ;
230137 }
@@ -392,7 +299,7 @@ private IRubyObject scan(ThreadContext context, IRubyObject regex, boolean succp
392299
393300 Region matchRegion = matcher .getRegion ();
394301 if (matchRegion == null ) {
395- regs = REGION_ADAPTER .newRegion (matcher .getBegin (), matcher .getEnd ());
302+ regs = Region .newRegion (matcher .getBegin (), matcher .getEnd ());
396303 } else {
397304 regs = matchRegion ;
398305 }
@@ -438,17 +345,17 @@ private IRubyObject scan(ThreadContext context, IRubyObject regex, boolean succp
438345
439346 private int lastMatchLength () {
440347 if (fixedAnchor ) {
441- return REGION_ADAPTER .getEnd (regs , 0 ) - prev ;
348+ return regs .getEnd (0 ) - prev ;
442349 } else {
443- return REGION_ADAPTER .getEnd (regs , 0 );
350+ return regs .getEnd (0 );
444351 }
445352 }
446353
447354 private void succ () {
448355 if (fixedAnchor ) {
449- this .curr = REGION_ADAPTER .getEnd (regs , 0 );
356+ this .curr = regs .getEnd (0 );
450357 } else {
451- this .curr += REGION_ADAPTER .getEnd (regs , 0 );
358+ this .curr += regs .getEnd (0 );
452359 }
453360 }
454361
@@ -471,9 +378,9 @@ private int restLen() {
471378 // MRI: set_registers
472379 private void setRegisters (int length ) {
473380 if (fixedAnchor ) {
474- regs = REGION_ADAPTER .newRegion (curr , curr + length );
381+ regs = Region .newRegion (curr , curr + length );
475382 } else {
476- regs = REGION_ADAPTER .newRegion (0 , length );
383+ regs = Region .newRegion (0 , length );
477384 }
478385 }
479386
@@ -530,9 +437,9 @@ public IRubyObject search_full(ThreadContext context, IRubyObject regex, IRubyOb
530437 // MRI: adjust_register_to_matched
531438 private void adjustRegisters () {
532439 if (fixedAnchor ) {
533- regs = REGION_ADAPTER .newRegion (prev , curr );
440+ regs = Region .newRegion (prev , curr );
534441 } else {
535- regs = REGION_ADAPTER .newRegion (0 , curr - prev );
442+ regs = Region .newRegion (0 , curr - prev );
536443 }
537444 }
538445
@@ -572,8 +479,8 @@ public IRubyObject getchCommon(ThreadContext context) {
572479 adjustRegisters ();
573480
574481 return extractRange (runtime ,
575- adjustRegisterPosition (REGION_ADAPTER .getBeg (regs , 0 )),
576- adjustRegisterPosition (REGION_ADAPTER .getEnd (regs , 0 )));
482+ adjustRegisterPosition (regs .getBeg (0 )),
483+ adjustRegisterPosition (regs .getEnd (0 )));
577484 }
578485
579486 @ JRubyMethod (name = "get_byte" )
@@ -589,8 +496,8 @@ public IRubyObject get_byte(ThreadContext context) {
589496 adjustRegisters ();
590497
591498 return extractRange (context .runtime ,
592- adjustRegisterPosition (REGION_ADAPTER .getBeg (regs , 0 )),
593- adjustRegisterPosition (REGION_ADAPTER .getEnd (regs , 0 )));
499+ adjustRegisterPosition (regs .getBeg (0 )),
500+ adjustRegisterPosition (regs .getEnd (0 )));
594501 }
595502
596503 @ JRubyMethod (name = "getbyte" )
@@ -687,15 +594,15 @@ public IRubyObject matched(ThreadContext context) {
687594 check (context );
688595 if (!isMatched ()) return context .nil ;
689596 return extractRange (context .runtime ,
690- adjustRegisterPosition (REGION_ADAPTER .getBeg (regs , 0 )),
691- adjustRegisterPosition (REGION_ADAPTER .getEnd (regs , 0 )));
597+ adjustRegisterPosition (regs .getBeg (0 )),
598+ adjustRegisterPosition (regs .getEnd (0 )));
692599 }
693600
694601 @ JRubyMethod (name = "matched_size" )
695602 public IRubyObject matched_size (ThreadContext context ) {
696603 check (context );
697604 if (!isMatched ()) return context .nil ;
698- return RubyFixnum .newFixnum (context .runtime , REGION_ADAPTER .getEnd (regs , 0 ) - REGION_ADAPTER .getBeg (regs , 0 ));
605+ return RubyFixnum .newFixnum (context .runtime , regs .getEnd (0 ) - regs .getBeg (0 ));
699606 }
700607
701608 @ JRubyMethod (name = "matchedsize" )
@@ -727,16 +634,16 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject idx) {
727634 }
728635
729636 private IRubyObject extractRegion (ThreadContext context , int i ) {
730- int numRegs = REGION_ADAPTER .getNumRegs (regs );
637+ int numRegs = regs .getNumRegs ();
731638
732639 if (i < 0 ) i += numRegs ;
733- if (i < 0 || i >= numRegs || REGION_ADAPTER .getBeg (regs , i ) == -1 ) {
640+ if (i < 0 || i >= numRegs || regs .getBeg (i ) == -1 ) {
734641 return context .nil ;
735642 }
736643
737644 return extractRange (context .runtime ,
738- adjustRegisterPosition (REGION_ADAPTER .getBeg (regs , i )),
739- adjustRegisterPosition (REGION_ADAPTER .getEnd (regs , i )));
645+ adjustRegisterPosition (regs .getBeg (i )),
646+ adjustRegisterPosition (regs .getEnd (i )));
740647 }
741648
742649 @ JRubyMethod (name = "pre_match" )
@@ -745,7 +652,7 @@ public IRubyObject pre_match(ThreadContext context) {
745652 if (!isMatched ()) {
746653 return context .nil ;
747654 }
748- return extractRange (context .runtime , 0 , adjustRegisterPosition (REGION_ADAPTER .getBeg (regs , 0 )));
655+ return extractRange (context .runtime , 0 , adjustRegisterPosition (regs .getBeg (0 )));
749656 }
750657
751658 @ JRubyMethod (name = "post_match" )
@@ -757,7 +664,7 @@ public IRubyObject post_match(ThreadContext context) {
757664 }
758665
759666 return extractRange (context .runtime ,
760- adjustRegisterPosition (REGION_ADAPTER .getEnd (regs , 0 )),
667+ adjustRegisterPosition (regs .getEnd (0 )),
761668 str .getByteList ().getRealSize ());
762669 }
763670
@@ -885,7 +792,7 @@ private IRubyObject inspect2() {
885792 @ JRubyMethod (name = "size" )
886793 public IRubyObject size (ThreadContext context ) {
887794 if (!isMatched ()) return context .nil ;
888- return context .runtime .newFixnum (REGION_ADAPTER .getNumRegs (regs ));
795+ return context .runtime .newFixnum (regs .getNumRegs ());
889796 }
890797
891798 @ JRubyMethod (name = "captures" )
@@ -897,17 +804,17 @@ public IRubyObject captures(ThreadContext context) {
897804
898805 Ruby runtime = context .runtime ;
899806
900- numRegs = REGION_ADAPTER .getNumRegs (regs );
807+ numRegs = regs .getNumRegs ();
901808 newAry = RubyArray .newArray (runtime , numRegs );
902809
903810 for (i = 1 ; i < numRegs ; i ++) {
904811 IRubyObject str ;
905- if (REGION_ADAPTER .getBeg (regs , i ) == -1 ) {
812+ if (regs .getBeg (i ) == -1 ) {
906813 str = context .nil ;
907814 } else {
908815 str = extractRange (runtime ,
909- adjustRegisterPosition (REGION_ADAPTER .getBeg (regs , i )),
910- adjustRegisterPosition (REGION_ADAPTER .getEnd (regs , i )));
816+ adjustRegisterPosition (regs .getBeg (i )),
817+ adjustRegisterPosition (regs .getEnd (i )));
911818 }
912819 newAry .push (str );
913820 }
0 commit comments