Skip to content

Commit c6804b7

Browse files
authored
JRuby extension: drop old-Joni-version support (#76)
This PR removes the code path for OldRegionAdapter, now that all published JRuby versions are using "new API" for `Region`, so no adapting is necessary. ## Details headius mentioned in an off-GitHub conversation: > Yeah we had a transition period where it supported both old and new but now all released JRuby should be on the new version > (Using Region) should be fine, especially since 9.3 can't really install the gem (it will be ignored unless explicitly activated) and 9.4 has had the new region logic for several releases ## TODO - [x] Remove the Interface, and use Region as-is. - [x] Remove the usage of the intermediate interface
1 parent 124cb7f commit c6804b7

2 files changed

Lines changed: 31 additions & 124 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- debug
2727
# We need to update 'jruby-XXX' in "if:" too when we update
2828
# this.
29-
- jruby-9.4.1.0
29+
- jruby-9.4.5.0
3030
- truffleruby
3131
- truffleruby-head
3232
include:
@@ -61,7 +61,7 @@ jobs:
6161
- uses: actions/upload-artifact@v4
6262
if: >-
6363
matrix.os == 'ubuntu-latest' &&
64-
(matrix.ruby == '3.1' || matrix.ruby == 'jruby-9.4.1.0')
64+
(matrix.ruby == '3.1' || matrix.ruby == 'jruby-9.4.5.0')
6565
with:
6666
name: gem-${{ matrix.os }}-${{ matrix.ruby }}
6767
path: pkg/
@@ -70,7 +70,7 @@ jobs:
7070
if: >-
7171
startsWith(github.ref, 'refs/tags/') &&
7272
matrix.os == 'ubuntu-latest' &&
73-
(matrix.ruby == '3.1' || matrix.ruby == 'jruby-9.4.1.0')
73+
(matrix.ruby == '3.1' || matrix.ruby == 'jruby-9.4.5.0')
7474
run: |
7575
ruby \
7676
-e 'print("## strscan "); \

ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java

Lines changed: 28 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)