Skip to content

Commit 8ced6d2

Browse files
committed
Moved the private variants of RubyString#strLength to StringSupport as static methods.
1 parent ba8a97d commit 8ced6d2

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

core/src/main/java/org/jruby/RubyString.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@
9797
import static org.jruby.util.StringSupport.strLengthWithCodeRange;
9898
import static org.jruby.util.StringSupport.toLower;
9999
import static org.jruby.util.StringSupport.toUpper;
100-
import static org.jruby.util.StringSupport.unpackArg;
101-
import static org.jruby.util.StringSupport.unpackResult;
102100
import static org.jruby.RubyEnumerator.SizeFn;
103101

104102
/**
@@ -345,27 +343,9 @@ private boolean isComparableViaCodeRangeWith(RubyString other) {
345343
return false;
346344
}
347345

348-
private int strLength(Encoding enc) {
349-
if (singleByteOptimizable(enc)) return value.getRealSize();
350-
return strLength(value, enc);
351-
}
352-
353346
public final int strLength() {
354-
if (singleByteOptimizable()) return value.getRealSize();
355-
return strLength(value);
356-
}
357-
358-
private int strLength(ByteList bytes) {
359-
return strLength(bytes, bytes.getEncoding());
360-
}
361-
362-
private int strLength(ByteList bytes, Encoding enc) {
363-
if (isCodeRangeValid() && enc instanceof UTF8Encoding) return StringSupport.utf8Length(bytes);
364-
365-
long lencr = strLengthWithCodeRange(bytes, enc);
366-
int cr = unpackArg(lencr);
367-
if (cr != 0) setCodeRange(cr);
368-
return unpackResult(lencr);
347+
if (StringSupport.isSingleByteOptimizable(this, value.getEncoding())) return value.getRealSize();
348+
return StringSupport.strLengthFromRubyString(this, value);
369349
}
370350

371351
final int subLength(int pos) {
@@ -2750,12 +2730,12 @@ private IRubyObject indexCommon19(Ruby runtime, ThreadContext context, IRubyObje
27502730
context.setBackRef(holder[0]);
27512731
pos = subLength(pos);
27522732
} else if (sub instanceof RubyString) {
2753-
pos = StringSupport.index(this, this.value, this.strLength(this.checkEncoding((RubyString) sub)), (RubyString) sub, ((RubyString) sub).value, ((RubyString) sub).strLength(this.checkEncoding((RubyString) sub)), pos, this.checkEncoding((RubyString) sub));
2733+
pos = StringSupport.index(this, this.value, StringSupport.strLengthFromRubyString(this, this.value, this.checkEncoding((RubyString) sub)), (RubyString) sub, ((RubyString) sub).value, StringSupport.strLengthFromRubyString(((RubyString) sub), ((RubyString) sub).value, this.checkEncoding((RubyString) sub)), pos, this.checkEncoding((RubyString) sub));
27542734
pos = subLength(pos);
27552735
} else {
27562736
IRubyObject tmp = sub.checkStringType();
27572737
if (tmp.isNil()) throw runtime.newTypeError("type mismatch: " + sub.getMetaClass().getName() + " given");
2758-
pos = StringSupport.index(this, this.value, this.strLength(this.checkEncoding((RubyString) tmp)), (RubyString) tmp, ((RubyString) tmp).value, ((RubyString) tmp).strLength(this.checkEncoding((RubyString) tmp)), pos, this.checkEncoding((RubyString) tmp));
2738+
pos = StringSupport.index(this, this.value, StringSupport.strLengthFromRubyString(this, this.value, this.checkEncoding((RubyString) tmp)), (RubyString) tmp, ((RubyString) tmp).value, StringSupport.strLengthFromRubyString(((RubyString) tmp), ((RubyString) tmp).value, this.checkEncoding((RubyString) tmp)), pos, this.checkEncoding((RubyString) tmp));
27592739
pos = subLength(pos);
27602740
}
27612741

@@ -2809,11 +2789,11 @@ private IRubyObject rindexCommon19(Ruby runtime, ThreadContext context, final IR
28092789
pos = subLength(pos);
28102790
}
28112791
} else if (sub instanceof RubyString) {
2812-
pos = StringSupport.rindex(value, this.strLength(this.checkEncoding((RubyString) sub)), ((RubyString) sub).value, ((RubyString) sub).strLength(this.checkEncoding((RubyString) sub)), pos, (RubyString) sub, this.checkEncoding((RubyString) sub));
2792+
pos = StringSupport.rindex(value, StringSupport.strLengthFromRubyString(this, this.value, this.checkEncoding((RubyString) sub)), ((RubyString) sub).value, StringSupport.strLengthFromRubyString(((RubyString) sub), ((RubyString) sub).value, this.checkEncoding((RubyString) sub)), pos, (RubyString) sub, this.checkEncoding((RubyString) sub));
28132793
} else {
28142794
IRubyObject tmp = sub.checkStringType();
28152795
if (tmp.isNil()) throw runtime.newTypeError("type mismatch: " + sub.getMetaClass().getName() + " given");
2816-
pos = StringSupport.rindex(value, this.strLength(this.checkEncoding((RubyString) tmp)), ((RubyString) tmp).value, ((RubyString) tmp).strLength(this.checkEncoding((RubyString) tmp)), pos, (RubyString) tmp, this.checkEncoding((RubyString) tmp));
2796+
pos = StringSupport.rindex(value, StringSupport.strLengthFromRubyString(this, this.value, this.checkEncoding((RubyString) tmp)), ((RubyString) tmp).value, StringSupport.strLengthFromRubyString(((RubyString) tmp), ((RubyString) tmp).value, this.checkEncoding((RubyString) tmp)), pos, (RubyString) tmp, this.checkEncoding((RubyString) tmp));
28172797
}
28182798
if (pos >= 0) return RubyFixnum.newFixnum(runtime, pos);
28192799
return runtime.getNil();
@@ -2923,10 +2903,10 @@ private IRubyObject multibyteSubstr19(Ruby runtime, Encoding enc, int len, int b
29232903
if (p == -1) return runtime.getNil();
29242904
return makeShared19(runtime, p - s, e - p);
29252905
} else {
2926-
beg += strLength(enc);
2906+
beg += StringSupport.strLengthFromRubyString(this, value, enc);
29272907
if (beg < 0) return runtime.getNil();
29282908
}
2929-
} else if (beg > 0 && beg > strLength(enc)) {
2909+
} else if (beg > 0 && beg > StringSupport.strLengthFromRubyString(this, value, enc)) {
29302910
return runtime.getNil();
29312911
}
29322912
if (len == 0) {
@@ -3025,7 +3005,7 @@ public IRubyObject op_aref19(ThreadContext context, IRubyObject arg) {
30253005
return subpat19(runtime, context, (RubyRegexp)arg);
30263006
} else if (arg instanceof RubyString) {
30273007
RubyString str = (RubyString)arg;
3028-
return StringSupport.index(this, this.value, this.strLength(this.checkEncoding(str)), str, str.value, str.strLength(this.checkEncoding(str)), 0, this.checkEncoding(str)) != -1 ? str.strDup(runtime) : runtime.getNil();
3008+
return StringSupport.index(this, this.value, StringSupport.strLengthFromRubyString(this, this.value, this.checkEncoding(str)), str, str.value, StringSupport.strLengthFromRubyString(str, str.value, this.checkEncoding(str)), 0, this.checkEncoding(str)) != -1 ? str.strDup(runtime) : runtime.getNil();
30293009
} else if (arg instanceof RubyRange) {
30303010
int len = strLength();
30313011
int[] begLen = ((RubyRange) arg).begLenInt(len, 0);
@@ -3147,7 +3127,7 @@ public IRubyObject op_aset19(ThreadContext context, IRubyObject arg0, IRubyObjec
31473127
return arg1;
31483128
} else if (arg0 instanceof RubyString) {
31493129
RubyString orig = (RubyString)arg0;
3150-
int beg = StringSupport.index(this, this.value, this.strLength(this.checkEncoding(orig)), orig, orig.value, orig.strLength(this.checkEncoding(orig)), 0, this.checkEncoding(orig));
3130+
int beg = StringSupport.index(this, this.value, StringSupport.strLengthFromRubyString(this, this.value, this.checkEncoding(orig)), orig, orig.value, StringSupport.strLengthFromRubyString(orig, orig.value, this.checkEncoding(orig)), 0, this.checkEncoding(orig));
31513131
if (beg < 0) throw context.runtime.newIndexError("string not matched");
31523132
beg = subLength(beg);
31533133
replaceInternal19(beg, orig.strLength(), arg1.convertToString());
@@ -3381,7 +3361,7 @@ public RubyBoolean include_p(ThreadContext context, IRubyObject obj) {
33813361
public RubyBoolean include_p19(ThreadContext context, IRubyObject obj) {
33823362
Ruby runtime = context.runtime;
33833363
RubyString coerced = obj.convertToString();
3384-
return StringSupport.index(this, this.value, this.strLength(this.checkEncoding(coerced)), coerced, coerced.value, coerced.strLength(this.checkEncoding(coerced)), 0, this.checkEncoding(coerced)) == -1 ? runtime.getFalse() : runtime.getTrue();
3364+
return StringSupport.index(this, this.value, StringSupport.strLengthFromRubyString(this, this.value, this.checkEncoding(coerced)), coerced, coerced.value, StringSupport.strLengthFromRubyString(coerced, coerced.value, this.checkEncoding(coerced)), 0, this.checkEncoding(coerced)) == -1 ? runtime.getFalse() : runtime.getTrue();
33853365
}
33863366

33873367
@JRubyMethod
@@ -3975,7 +3955,7 @@ private IRubyObject justify19(IRubyObject arg0, IRubyObject arg1, int jflag) {
39753955
RubyString padStr = arg1.convertToString();
39763956
ByteList pad = padStr.value;
39773957
Encoding enc = checkEncoding(padStr);
3978-
int padCharLen = padStr.strLength(enc);
3958+
int padCharLen = StringSupport.strLengthFromRubyString(padStr, padStr.value, enc);
39793959
if (pad.getRealSize() == 0 || padCharLen == 0) throw runtime.newArgumentError("zero width padding");
39803960
int width = RubyFixnum.num2int(arg0);
39813961
RubyString result = justifyCommon(runtime, pad,
@@ -3989,7 +3969,7 @@ private IRubyObject justify19(IRubyObject arg0, IRubyObject arg1, int jflag) {
39893969
}
39903970

39913971
private RubyString justifyCommon(Ruby runtime, ByteList pad, int padCharLen, boolean padSinglebyte, Encoding enc, int width, int jflag) {
3992-
int len = strLength(enc);
3972+
int len = StringSupport.strLengthFromRubyString(this, value, enc);
39933973
if (width < 0 || len >= width) return strDup(runtime);
39943974
int n = width - len;
39953975

@@ -4138,7 +4118,7 @@ public IRubyObject partition(ThreadContext context, IRubyObject arg, Block block
41384118
IRubyObject tmp = arg.checkStringType();
41394119
if (tmp.isNil()) throw runtime.newTypeError("type mismatch: " + arg.getMetaClass().getName() + " given");
41404120
sep = (RubyString)tmp;
4141-
pos = StringSupport.index(this, this.value, this.strLength(this.checkEncoding(sep)), sep, sep.value, sep.strLength(this.checkEncoding(sep)), 0, this.checkEncoding(sep));
4121+
pos = StringSupport.index(this, this.value, StringSupport.strLengthFromRubyString(this, this.value, this.checkEncoding(sep)), sep, sep.value, StringSupport.strLengthFromRubyString(sep, sep.value, this.checkEncoding(sep)), 0, this.checkEncoding(sep));
41424122
if (pos < 0) return partitionMismatch(runtime);
41434123
}
41444124

@@ -4170,7 +4150,7 @@ public IRubyObject rpartition(ThreadContext context, IRubyObject arg) {
41704150
IRubyObject tmp = arg.checkStringType();
41714151
if (tmp.isNil()) throw runtime.newTypeError("type mismatch: " + arg.getMetaClass().getName() + " given");
41724152
sep = (RubyString)tmp;
4173-
pos = StringSupport.rindex(value, this.strLength(this.checkEncoding(sep)), sep.value, sep.strLength(this.checkEncoding(sep)), subLength(value.getRealSize()), sep, this.checkEncoding(sep));
4153+
pos = StringSupport.rindex(value, StringSupport.strLengthFromRubyString(this, this.value, this.checkEncoding(sep)), sep.value, StringSupport.strLengthFromRubyString(sep, sep.value, this.checkEncoding(sep)), subLength(value.getRealSize()), sep, this.checkEncoding(sep));
41744154
if (pos < 0) return rpartitionMismatch(runtime);
41754155
}
41764156

core/src/main/java/org/jruby/util/StringSupport.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,24 @@ public static int rindex(ByteList source, int sourceLen, ByteList subString, int
864864
return -1;
865865
}
866866

867+
public static int strLengthFromRubyString(RubyString rubyString, ByteList value, Encoding enc) {
868+
if (isSingleByteOptimizable(rubyString, enc)) return value.getRealSize();
869+
return strLengthFromRubyStringFull(rubyString, value, enc);
870+
}
871+
872+
public static int strLengthFromRubyString(RubyString rubyString, ByteList bytes) {
873+
return strLengthFromRubyStringFull(rubyString, bytes, bytes.getEncoding());
874+
}
875+
876+
private static int strLengthFromRubyStringFull(RubyString rubyString, ByteList bytes, Encoding enc) {
877+
if (rubyString.isCodeRangeValid() && enc instanceof UTF8Encoding) return utf8Length(bytes);
878+
879+
long lencr = strLengthWithCodeRange(bytes, enc);
880+
int cr = unpackArg(lencr);
881+
if (cr != 0) rubyString.setCodeRange(cr);
882+
return unpackResult(lencr);
883+
}
884+
867885
/**
868886
* rb_str_tr / rb_str_tr_bang
869887
*/

0 commit comments

Comments
 (0)