Skip to content

Commit

Permalink
Simplify calls to StringSupport.index by calculating arguments rather…
Browse files Browse the repository at this point in the history
… than passing them.
  • Loading branch information
nirvdrum committed Jan 5, 2015
1 parent 863a2fa commit ca55072
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
12 changes: 6 additions & 6 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -2732,12 +2732,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, StringSupport.strLengthFromRubyString(this, this.checkEncoding((RubyString) sub)), (RubyString) sub, StringSupport.strLengthFromRubyString(((RubyString) sub), this.checkEncoding((RubyString) sub)), pos, this.checkEncoding((RubyString) sub));
pos = StringSupport.index(this, (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, StringSupport.strLengthFromRubyString(this, this.checkEncoding((RubyString) tmp)), (RubyString) tmp, StringSupport.strLengthFromRubyString(((RubyString) tmp), this.checkEncoding((RubyString) tmp)), pos, this.checkEncoding((RubyString) tmp));
pos = StringSupport.index(this, (RubyString) tmp, pos, this.checkEncoding((RubyString) tmp));
pos = subLength(pos);
}

Expand Down Expand Up @@ -3007,7 +3007,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, StringSupport.strLengthFromRubyString(this, this.checkEncoding(str)), str, StringSupport.strLengthFromRubyString(str, this.checkEncoding(str)), 0, this.checkEncoding(str)) != -1 ? str.strDup(runtime) : runtime.getNil();
return StringSupport.index(this, 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 @@ -3129,7 +3129,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, StringSupport.strLengthFromRubyString(this, this.checkEncoding(orig)), orig, StringSupport.strLengthFromRubyString(orig, this.checkEncoding(orig)), 0, this.checkEncoding(orig));
int beg = StringSupport.index(this, 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 @@ -3363,7 +3363,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, StringSupport.strLengthFromRubyString(this, this.checkEncoding(coerced)), coerced, StringSupport.strLengthFromRubyString(coerced, this.checkEncoding(coerced)), 0, this.checkEncoding(coerced)) == -1 ? runtime.getFalse() : runtime.getTrue();
return StringSupport.index(this, coerced, 0, this.checkEncoding(coerced)) == -1 ? runtime.getFalse() : runtime.getTrue();
}

@JRubyMethod
Expand Down Expand Up @@ -4120,7 +4120,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, StringSupport.strLengthFromRubyString(this, this.checkEncoding(sep)), sep, StringSupport.strLengthFromRubyString(sep, this.checkEncoding(sep)), 0, this.checkEncoding(sep));
pos = StringSupport.index(this, sep, 0, this.checkEncoding(sep));
if (pos < 0) return partitionMismatch(runtime);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public StringIndexPrimitiveNode(StringIndexPrimitiveNode prev) {

@Specialization
public Object stringIndex(RubyString string, RubyString pattern, int start) {
final int index = StringSupport.index(string, string.length(),
pattern, pattern.length(),
final int index = StringSupport.index(string,
pattern,
start, string.getBytes().getEncoding());

if (index == -1) {
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/org/jruby/util/StringSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -1186,8 +1186,12 @@ public static boolean isSingleByteOptimizable(CodeRangeable string, Encoding enc
return string.getCodeRange() == CR_7BIT || encoding.maxLength() == 1;
}

public static int index(CodeRangeable sourceString, int sourceLen, CodeRangeable otherString, int otherLen, int offset, Encoding enc) {
public static int index(CodeRangeable sourceString, CodeRangeable otherString, int offset, Encoding enc) {
if (otherString.scanForCodeRange() == CR_BROKEN) return -1;

int sourceLen = strLengthFromRubyString(sourceString);
int otherLen = strLengthFromRubyString(otherString);

if (offset < 0) {
offset += sourceLen;
if (offset < 0) return -1;
Expand Down

0 comments on commit ca55072

Please sign in to comment.