Skip to content

Commit

Permalink
Moved the private variants of RubyString#strLength to StringSupport a…
Browse files Browse the repository at this point in the history
…s static methods.
  • Loading branch information
nirvdrum committed Jan 5, 2015
1 parent ba8a97d commit 8ced6d2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
50 changes: 15 additions & 35 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -97,8 +97,6 @@
import static org.jruby.util.StringSupport.strLengthWithCodeRange;
import static org.jruby.util.StringSupport.toLower;
import static org.jruby.util.StringSupport.toUpper;
import static org.jruby.util.StringSupport.unpackArg;
import static org.jruby.util.StringSupport.unpackResult;
import static org.jruby.RubyEnumerator.SizeFn;

/**
Expand Down Expand Up @@ -345,27 +343,9 @@ private boolean isComparableViaCodeRangeWith(RubyString other) {
return false;
}

private int strLength(Encoding enc) {
if (singleByteOptimizable(enc)) return value.getRealSize();
return strLength(value, enc);
}

public final int strLength() {
if (singleByteOptimizable()) return value.getRealSize();
return strLength(value);
}

private int strLength(ByteList bytes) {
return strLength(bytes, bytes.getEncoding());
}

private int strLength(ByteList bytes, Encoding enc) {
if (isCodeRangeValid() && enc instanceof UTF8Encoding) return StringSupport.utf8Length(bytes);

long lencr = strLengthWithCodeRange(bytes, enc);
int cr = unpackArg(lencr);
if (cr != 0) setCodeRange(cr);
return unpackResult(lencr);
if (StringSupport.isSingleByteOptimizable(this, value.getEncoding())) return value.getRealSize();
return StringSupport.strLengthFromRubyString(this, value);
}

final int subLength(int pos) {
Expand Down Expand Up @@ -2750,12 +2730,12 @@ private IRubyObject indexCommon19(Ruby runtime, ThreadContext context, IRubyObje
context.setBackRef(holder[0]);
pos = subLength(pos);
} else if (sub instanceof RubyString) {
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));
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));
pos = subLength(pos);
} else {
IRubyObject tmp = sub.checkStringType();
if (tmp.isNil()) throw runtime.newTypeError("type mismatch: " + sub.getMetaClass().getName() + " given");
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));
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));
pos = subLength(pos);
}

Expand Down Expand Up @@ -2809,11 +2789,11 @@ private IRubyObject rindexCommon19(Ruby runtime, ThreadContext context, final IR
pos = subLength(pos);
}
} else if (sub instanceof RubyString) {
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));
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));
} else {
IRubyObject tmp = sub.checkStringType();
if (tmp.isNil()) throw runtime.newTypeError("type mismatch: " + sub.getMetaClass().getName() + " given");
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));
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));
}
if (pos >= 0) return RubyFixnum.newFixnum(runtime, pos);
return runtime.getNil();
Expand Down Expand Up @@ -2923,10 +2903,10 @@ private IRubyObject multibyteSubstr19(Ruby runtime, Encoding enc, int len, int b
if (p == -1) return runtime.getNil();
return makeShared19(runtime, p - s, e - p);
} else {
beg += strLength(enc);
beg += StringSupport.strLengthFromRubyString(this, value, enc);
if (beg < 0) return runtime.getNil();
}
} else if (beg > 0 && beg > strLength(enc)) {
} else if (beg > 0 && beg > StringSupport.strLengthFromRubyString(this, value, enc)) {
return runtime.getNil();
}
if (len == 0) {
Expand Down Expand Up @@ -3025,7 +3005,7 @@ public IRubyObject op_aref19(ThreadContext context, IRubyObject arg) {
return subpat19(runtime, context, (RubyRegexp)arg);
} else if (arg instanceof RubyString) {
RubyString str = (RubyString)arg;
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();
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();
} else if (arg instanceof RubyRange) {
int len = strLength();
int[] begLen = ((RubyRange) arg).begLenInt(len, 0);
Expand Down Expand Up @@ -3147,7 +3127,7 @@ public IRubyObject op_aset19(ThreadContext context, IRubyObject arg0, IRubyObjec
return arg1;
} else if (arg0 instanceof RubyString) {
RubyString orig = (RubyString)arg0;
int beg = StringSupport.index(this, this.value, this.strLength(this.checkEncoding(orig)), orig, orig.value, orig.strLength(this.checkEncoding(orig)), 0, this.checkEncoding(orig));
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));
if (beg < 0) throw context.runtime.newIndexError("string not matched");
beg = subLength(beg);
replaceInternal19(beg, orig.strLength(), arg1.convertToString());
Expand Down Expand Up @@ -3381,7 +3361,7 @@ public RubyBoolean include_p(ThreadContext context, IRubyObject obj) {
public RubyBoolean include_p19(ThreadContext context, IRubyObject obj) {
Ruby runtime = context.runtime;
RubyString coerced = obj.convertToString();
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();
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();
}

@JRubyMethod
Expand Down Expand Up @@ -3975,7 +3955,7 @@ private IRubyObject justify19(IRubyObject arg0, IRubyObject arg1, int jflag) {
RubyString padStr = arg1.convertToString();
ByteList pad = padStr.value;
Encoding enc = checkEncoding(padStr);
int padCharLen = padStr.strLength(enc);
int padCharLen = StringSupport.strLengthFromRubyString(padStr, padStr.value, enc);
if (pad.getRealSize() == 0 || padCharLen == 0) throw runtime.newArgumentError("zero width padding");
int width = RubyFixnum.num2int(arg0);
RubyString result = justifyCommon(runtime, pad,
Expand All @@ -3989,7 +3969,7 @@ private IRubyObject justify19(IRubyObject arg0, IRubyObject arg1, int jflag) {
}

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

Expand Down Expand Up @@ -4138,7 +4118,7 @@ public IRubyObject partition(ThreadContext context, IRubyObject arg, Block block
IRubyObject tmp = arg.checkStringType();
if (tmp.isNil()) throw runtime.newTypeError("type mismatch: " + arg.getMetaClass().getName() + " given");
sep = (RubyString)tmp;
pos = StringSupport.index(this, this.value, this.strLength(this.checkEncoding(sep)), sep, sep.value, sep.strLength(this.checkEncoding(sep)), 0, this.checkEncoding(sep));
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));
if (pos < 0) return partitionMismatch(runtime);
}

Expand Down Expand Up @@ -4170,7 +4150,7 @@ public IRubyObject rpartition(ThreadContext context, IRubyObject arg) {
IRubyObject tmp = arg.checkStringType();
if (tmp.isNil()) throw runtime.newTypeError("type mismatch: " + arg.getMetaClass().getName() + " given");
sep = (RubyString)tmp;
pos = StringSupport.rindex(value, this.strLength(this.checkEncoding(sep)), sep.value, sep.strLength(this.checkEncoding(sep)), subLength(value.getRealSize()), sep, this.checkEncoding(sep));
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));
if (pos < 0) return rpartitionMismatch(runtime);
}

Expand Down
18 changes: 18 additions & 0 deletions core/src/main/java/org/jruby/util/StringSupport.java
Expand Up @@ -864,6 +864,24 @@ public static int rindex(ByteList source, int sourceLen, ByteList subString, int
return -1;
}

public static int strLengthFromRubyString(RubyString rubyString, ByteList value, Encoding enc) {
if (isSingleByteOptimizable(rubyString, enc)) return value.getRealSize();
return strLengthFromRubyStringFull(rubyString, value, enc);
}

public static int strLengthFromRubyString(RubyString rubyString, ByteList bytes) {
return strLengthFromRubyStringFull(rubyString, bytes, bytes.getEncoding());
}

private static int strLengthFromRubyStringFull(RubyString rubyString, ByteList bytes, Encoding enc) {
if (rubyString.isCodeRangeValid() && enc instanceof UTF8Encoding) return utf8Length(bytes);

long lencr = strLengthWithCodeRange(bytes, enc);
int cr = unpackArg(lencr);
if (cr != 0) rubyString.setCodeRange(cr);
return unpackResult(lencr);
}

/**
* rb_str_tr / rb_str_tr_bang
*/
Expand Down

0 comments on commit 8ced6d2

Please sign in to comment.