Skip to content

Commit

Permalink
Attempt to use ByteList 2.0 with proper CharSequence logic.
Browse files Browse the repository at this point in the history
The changes here move away from using CharSequence methods from
ByteList when the intent is to traverse bytes rather than decoded
characters. Places where a ByteList is not typed as such in code
(type is CharSequence or Object) or where it is concatenated into
a String using `+` were not detected by my search and may still
be incorrect.
  • Loading branch information
headius committed Apr 4, 2018
1 parent e75b913 commit 3a08ac3
Show file tree
Hide file tree
Showing 48 changed files with 241 additions and 248 deletions.
2 changes: 1 addition & 1 deletion core/pom.rb
Expand Up @@ -52,7 +52,7 @@
jar 'com.github.jnr:jffi:${jffi.version}:native'

jar 'org.jruby.joni:joni:2.1.15'
jar 'org.jruby.extras:bytelist:1.0.15'
jar 'org.jruby.extras:bytelist:2.0.0-SNAPSHOT'
jar 'org.jruby.jcodings:jcodings:1.0.27'
jar 'org.jruby:dirgra:0.3'

Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Expand Up @@ -177,7 +177,7 @@ DO NOT MODIFIY - GENERATED CODE
<dependency>
<groupId>org.jruby.extras</groupId>
<artifactId>bytelist</artifactId>
<version>1.0.15</version>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jruby.jcodings</groupId>
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/jruby/RubyArgsFile.java
Expand Up @@ -199,7 +199,7 @@ public boolean next_argv(ThreadContext context) {

private static boolean filenameEqlDash(final RubyString filename) {
final ByteList filenameBytes = filename.getByteList();
return filenameBytes.length() == 1 && filenameBytes.get(0) == '-';
return filenameBytes.byteLength() == 1 && filenameBytes.get(0) == '-';
}

public static ArgsFileData getArgsFileData(Ruby runtime) {
Expand Down Expand Up @@ -805,8 +805,8 @@ public static IRubyObject read(ThreadContext context, IRubyObject recv, IRubyObj
continue;
}
} else if(args.length >= 1) {
if (((RubyString)str).getByteList().length() < len) {
len -= ((RubyString)str).getByteList().length();
if (((RubyString)str).getByteList().byteLength() < len) {
len -= ((RubyString)str).getByteList().byteLength();
args[0] = runtime.newFixnum(len);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyArray.java
Expand Up @@ -1981,7 +1981,7 @@ public IRubyObject join19(final ThreadContext context, IRubyObject sep) {
return joinAny(context, sepString, i, joinStrings(sepString, i, result));
}

len += ((RubyString) tmp).getByteList().length();
len += ((RubyString) tmp).getByteList().byteLength();
}

return joinStrings(sepString, realLength, (RubyString) RubyString.newStringLight(runtime, len).infectBy(this));
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyComplex.java
Expand Up @@ -1121,7 +1121,7 @@ private static RubyNumeric convertString(ThreadContext context, final IRubyObjec
// MRI: string_to_c_strict
private static RubyNumeric str_to_c_strict(ThreadContext context, RubyString str) {
IRubyObject[] ary = str_to_c_internal(context, str);
if (ary[0] == context.nil || ary[1].convertToString().getByteList().length() > 0) {
if (ary[0] == context.nil || ary[1].convertToString().getByteList().byteLength() > 0) {
throw context.runtime.newArgumentError("invalid value for convert(): " + str.inspect(context.runtime));
}
return (RubyNumeric) ary[0]; // (RubyComplex)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyIO.java
Expand Up @@ -2625,7 +2625,7 @@ private static void putsSingle(ThreadContext context, Ruby runtime, IRubyObject
write(context, maybeIO, line);
}

if (line.length() == 0 || !line.endsWith(separator.getByteList())) {
if (line.byteLength() == 0 || !line.endsWith(separator.getByteList())) {
write(context, maybeIO, separator);
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyKernel.java
Expand Up @@ -1406,9 +1406,9 @@ public static IRubyObject test(ThreadContext context, IRubyObject recv, IRubyObj
if (args[0] instanceof RubyFixnum) {
cmd = (int)((RubyFixnum) args[0]).getLongValue();
} else if (args[0] instanceof RubyString &&
((RubyString) args[0]).getByteList().length() > 0) {
((RubyString) args[0]).getByteList().byteLength() > 0) {
// MRI behavior: use first byte of string value if len > 0
cmd = ((RubyString) args[0]).getByteList().charAt(0);
cmd = ((RubyString) args[0]).getByteList().get(0);
} else {
cmd = (int) args[0].convertToInteger().getLongValue();
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyMarshal.java
Expand Up @@ -127,7 +127,7 @@ public static IRubyObject load(ThreadContext context, IRubyObject recv, IRubyObj
if (str != context.nil) {
tainted = in.isTaint();
ByteList bytes = ((RubyString) str).getByteList();
rawInput = new ByteArrayInputStream(bytes.getUnsafeBytes(), bytes.begin(), bytes.length());
rawInput = new ByteArrayInputStream(bytes.getUnsafeBytes(), bytes.begin(), bytes.byteLength());
} else if (sites(context).respond_to_getc.respondsTo(context, in, in) &&
sites(context).respond_to_read.respondsTo(context, in, in)) {
tainted = true;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyMatchData.java
Expand Up @@ -732,7 +732,7 @@ public IRubyObject post_match(ThreadContext context) {
check();
if (begin == -1) return context.nil;

final int strLen = str.getByteList().length();
final int strLen = str.getByteList().byteLength();
return makeShared(context.runtime, str, end, strLen - end).infectBy(this);
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyRational.java
Expand Up @@ -1378,7 +1378,7 @@ static IRubyObject[] str_to_r_internal(final ThreadContext context, final RubySt

if (si != nil) {
ByteList siBytes = si.convertToString().getByteList();
if (siBytes.length() > 0 && siBytes.get(0) == '-') v = f_negate(context, v);
if (siBytes.byteLength() > 0 && siBytes.get(0) == '-') v = f_negate(context, v);
}

if (exp != nil) {
Expand All @@ -1395,7 +1395,7 @@ static IRubyObject[] str_to_r_internal(final ThreadContext context, final RubySt

private static RubyNumeric str_to_r_strict(ThreadContext context, RubyString str) {
IRubyObject[] ary = str_to_r_internal(context, str);
if (ary[0] == context.nil || ary[1].convertToString().getByteList().length() > 0) {
if (ary[0] == context.nil || ary[1].convertToString().getByteList().byteLength() > 0) {
throw context.runtime.newArgumentError("invalid value for convert(): " + str.inspect(context.runtime));
}
return (RubyNumeric) ary[0]; // (RubyRational)
Expand Down
40 changes: 20 additions & 20 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -1005,7 +1005,7 @@ public final void resize(int length) {
if (value.getRealSize() > length) {
modify();
value.setRealSize(length);
} else if (value.length() < length) {
} else if (value.byteLength() < length) {
modify();
value.length(length);
}
Expand Down Expand Up @@ -1053,7 +1053,7 @@ public static String bytesToString(byte[] bytes, int beg, int len) {
}

public static String byteListToString(ByteList bytes) {
return bytesToString(bytes.getUnsafeBytes(), bytes.begin(), bytes.length());
return bytesToString(bytes.getUnsafeBytes(), bytes.begin(), bytes.byteLength());
}

public static String bytesToString(byte[] bytes) {
Expand Down Expand Up @@ -2498,7 +2498,7 @@ public RubyBoolean empty_p(ThreadContext context) {
}

public boolean isEmpty() {
return value.length() == 0;
return value.byteLength() == 0;
}

/** rb_str_append
Expand Down Expand Up @@ -2669,7 +2669,7 @@ public RubyString crypt(ThreadContext context, IRubyObject other) {
otherStr.modify();
otherStr.associateEncoding(ascii8bit);
ByteList otherBL = otherStr.getByteList();
if (otherBL.length() < 2) {
if (otherBL.byteLength() < 2) {
throw context.runtime.newArgumentError("salt too short (need >=2 bytes)");
}

Expand Down Expand Up @@ -3192,7 +3192,7 @@ public final IRubyObject substr(int beg, int len) {

/* rb_str_substr */
public final IRubyObject substr(Ruby runtime, int beg, int len) {
int length = value.length();
int length = value.byteLength();
if (len < 0 || beg > length) return runtime.getNil();

if (beg < 0) {
Expand All @@ -3206,7 +3206,7 @@ public final IRubyObject substr(Ruby runtime, int beg, int len) {

/* str_byte_substr */
private IRubyObject byteSubstr(Ruby runtime, int beg, int len) {
int length = value.length();
int length = value.byteLength();

if (len < 0 || beg > length) return runtime.getNil();

Expand All @@ -3228,7 +3228,7 @@ private IRubyObject byteARef(Ruby runtime, IRubyObject idx) {
final int index;

if (idx instanceof RubyRange){
int[] begLen = ((RubyRange) idx).begLenInt(getByteList().length(), 0);
int[] begLen = ((RubyRange) idx).begLenInt(getByteList().byteLength(), 0);
return begLen == null ? runtime.getNil() : byteSubstr(runtime, begLen[0], begLen[1]);
} else if (idx instanceof RubyFixnum) {
index = RubyNumeric.fix2int((RubyFixnum)idx);
Expand All @@ -3238,15 +3238,15 @@ private IRubyObject byteARef(Ruby runtime, IRubyObject idx) {
if (RubyRange.isRangeLike(context, idx, sites.respond_to_begin, sites.respond_to_end)) {
RubyRange range = RubyRange.rangeFromRangeLike(context, idx, sites.begin, sites.end, sites.exclude_end);

int[] begLen = range.begLenInt(getByteList().length(), 0);
int[] begLen = range.begLenInt(getByteList().byteLength(), 0);
return begLen == null ? runtime.getNil() : byteSubstr(runtime, begLen[0], begLen[1]);
} else {
index = RubyNumeric.num2int(idx);
}
}

IRubyObject obj = byteSubstr(runtime, index, 1);
if (obj.isNil() || ((RubyString)obj).getByteList().length() == 0) return runtime.getNil();
if (obj.isNil() || ((RubyString)obj).getByteList().byteLength() == 0) return runtime.getNil();
return obj;
}

Expand Down Expand Up @@ -3652,7 +3652,7 @@ final IRubyObject uptoCommon(ThreadContext context, IRubyObject arg, boolean exc
IRubyObject b = stringToInum(10);
IRubyObject e = end.stringToInum(10);

RubyArray argsArr = RubyArray.newArray(runtime, RubyFixnum.newFixnum(runtime, value.length()), context.nil);
RubyArray argsArr = RubyArray.newArray(runtime, RubyFixnum.newFixnum(runtime, value.byteLength()), context.nil);

if (b instanceof RubyFixnum && e instanceof RubyFixnum) {
long bl = RubyNumeric.fix2long(b);
Expand All @@ -3661,7 +3661,7 @@ final IRubyObject uptoCommon(ThreadContext context, IRubyObject arg, boolean exc
while (bl <= el) {
if (excl && bl == el) break;
argsArr.eltSetOk(1, RubyFixnum.newFixnum(runtime, bl));
ByteList to = new ByteList(value.length() + 5);
ByteList to = new ByteList(value.byteLength() + 5);
Sprintf.sprintf(to, "%.*d", argsArr);
RubyString str = RubyString.newStringNoCopy(runtime, to, USASCIIEncoding.INSTANCE, CR_7BIT);
block.yield(context, asSymbol ? runtime.newSymbol(str.toString()) : str);
Expand All @@ -3673,7 +3673,7 @@ final IRubyObject uptoCommon(ThreadContext context, IRubyObject arg, boolean exc

while (op.call(context, b, b, e).isTrue()) {
argsArr.eltSetOk(1, b);
ByteList to = new ByteList(value.length() + 5);
ByteList to = new ByteList(value.byteLength() + 5);
Sprintf.sprintf(to, "%.*d", argsArr);
RubyString str = RubyString.newStringNoCopy(runtime, to, USASCIIEncoding.INSTANCE, CR_7BIT);
block.yield(context, asSymbol ? runtime.newSymbol(str.toString()) : str);
Expand Down Expand Up @@ -3702,7 +3702,7 @@ private IRubyObject uptoCommonNoDigits(ThreadContext context, RubyString end, bo
if (next == null) break;
current = next.convertToString();
if (excl && current.op_equal19(context, end).isTrue()) break;
if (current.getByteList().length() > end.getByteList().length() || current.getByteList().length() == 0) break;
if (current.getByteList().byteLength() > end.getByteList().length() || current.getByteList().length() == 0) break;
}
return this;
}
Expand Down Expand Up @@ -4933,19 +4933,19 @@ public IRubyObject count(ThreadContext context, IRubyObject arg) {
final ByteList countValue = countStr.getByteList();
final Encoding enc = checkEncoding(countStr);

if ( countValue.length() == 1 && enc.isAsciiCompatible() ) {
if ( countValue.byteLength() == 1 && enc.isAsciiCompatible() ) {
final byte[] countBytes = countValue.unsafeBytes();
final int begin = countValue.begin(), size = countValue.length();
final int begin = countValue.begin(), size = countValue.byteLength();
if ( enc.isReverseMatchAllowed(countBytes, begin, begin + size) && ! isCodeRangeBroken() ) {
if ( value.length() == 0 ) return RubyFixnum.zero(runtime);
if ( value.byteLength() == 0 ) return RubyFixnum.zero(runtime);

int n = 0;
int[] len_p = {0};
int c = EncodingUtils.encCodepointLength(runtime, countBytes, begin, begin + size, len_p, enc);

final byte[] bytes = value.unsafeBytes();
int i = value.begin();
final int end = i + value.length();
final int end = i + value.byteLength();
while ( i < end ) {
if ( ( bytes[i++] & 0xff ) == c ) n++;
}
Expand All @@ -4963,7 +4963,7 @@ public IRubyObject count(ThreadContext context, IRubyObject arg) {
public IRubyObject count(ThreadContext context, IRubyObject[] args) {
final Ruby runtime = context.runtime;

if ( value.length() == 0 ) return RubyFixnum.zero(runtime);
if ( value.byteLength() == 0 ) return RubyFixnum.zero(runtime);

RubyString countStr = args[0].convertToString();
Encoding enc = checkEncoding(countStr);
Expand Down Expand Up @@ -5365,7 +5365,7 @@ public RubyString each_byte(ThreadContext context, Block block) {
Ruby runtime = context.runtime;
// Check the length every iteration, since
// the block can modify this string.
for (int i = 0; i < value.length(); i++) {
for (int i = 0; i < value.byteLength(); i++) {
block.yield(context, runtime.newFixnum(value.get(i) & 0xFF));
}
return this;
Expand Down Expand Up @@ -5690,7 +5690,7 @@ private RubySymbol checkSpecialCasesIntern(ByteList value) {

for (int i = 0; i < opTable.length; i++) {
String op = opTable[i][1];
if (value.toString().equals(op)) {
if (value.toByteString().equals(op)) {
return getRuntime().getSymbolTable().getSymbol(opTable[i][0]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyStruct.java
Expand Up @@ -184,7 +184,7 @@ public static RubyClass newInstance(IRubyObject recv, IRubyObject[] args, Block
if (args.length > 0) {
IRubyObject firstArgAsString = args[0].checkStringType();
if (!firstArgAsString.isNil()) {
name = ((RubyString)firstArgAsString).getByteList().toString();
name = ((RubyString)firstArgAsString).getByteList().toByteString();
} else if (args[0].isNil()) {
nilName = true;
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/RubySymbol.java
Expand Up @@ -757,7 +757,7 @@ public RubySymbol getSymbol(ByteList bytes, boolean hard) {

if (symbol == null) {
bytes = bytes.dup();
symbol = createSymbol(bytes.toString(), bytes, hash, hard);
symbol = createSymbol(bytes.toByteString(), bytes, hash, hard);
}

return symbol;
Expand Down Expand Up @@ -1009,7 +1009,7 @@ private static int javaStringHashCode(String str) {

private static int javaStringHashCode(ByteList iso8859) {
int h = 0;
int length = iso8859.length();
int length = iso8859.byteLength();
if (length > 0) {
byte val[] = iso8859.getUnsafeBytes();
int begin = iso8859.begin();
Expand Down Expand Up @@ -1047,9 +1047,9 @@ public static String objectToSymbolString(IRubyObject object) {
return ((RubySymbol) object).toString();
}
if (object instanceof RubyString) {
return ((RubyString) object).getByteList().toString();
return ((RubyString) object).getByteList().toByteString();
}
return object.convertToString().getByteList().toString();
return object.convertToString().getByteList().toByteString();
}

private static final class SymbolProcBody extends ContextAwareBlockBody {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ext/digest/RubyDigest.java
Expand Up @@ -159,7 +159,7 @@ public static RubyString hexencode(IRubyObject self, IRubyObject arg) {
@JRubyMethod(name = "bubblebabble", required = 1, meta = true)
public static RubyString bubblebabble(IRubyObject recv, IRubyObject arg) {
final ByteList bytes = arg.convertToString().getByteList();
return RubyString.newString(recv.getRuntime(), BubbleBabble.bubblebabble(bytes.unsafeBytes(), bytes.begin(), bytes.length()));
return RubyString.newString(recv.getRuntime(), BubbleBabble.bubblebabble(bytes.unsafeBytes(), bytes.begin(), bytes.byteLength()));
}

private static class Metadata {
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/org/jruby/ext/ffi/AbstractMemory.java
Expand Up @@ -1853,22 +1853,22 @@ public IRubyObject read_array_of_string(ThreadContext context, IRubyObject rbLen
public IRubyObject put_string(ThreadContext context, IRubyObject offArg, IRubyObject strArg) {
long off = getOffset(offArg);
ByteList bl = strArg.convertToString().getByteList();
getMemoryIO().putZeroTerminatedByteArray(off, bl.getUnsafeBytes(), bl.begin(), bl.length());
getMemoryIO().putZeroTerminatedByteArray(off, bl.getUnsafeBytes(), bl.begin(), bl.byteLength());
return this;
}

@JRubyMethod(name = "write_string")
public IRubyObject write_string(ThreadContext context, IRubyObject strArg) {
ByteList bl = strArg.convertToString().getByteList();
getMemoryIO().put(0, bl.getUnsafeBytes(), bl.begin(), bl.length());
getMemoryIO().put(0, bl.getUnsafeBytes(), bl.begin(), bl.byteLength());
return this;
}

@JRubyMethod(name = "write_string")
public IRubyObject write_string(ThreadContext context, IRubyObject strArg, IRubyObject lenArg) {
ByteList bl = strArg.convertToString().getByteList();
getMemoryIO().put(0, bl.getUnsafeBytes(), bl.begin(),
Math.min(bl.length(), (int) org.jruby.RubyInteger.num2long(lenArg)));
Math.min(bl.byteLength(), (int) org.jruby.RubyInteger.num2long(lenArg)));
return this;
}

Expand All @@ -1879,11 +1879,11 @@ public IRubyObject get_bytes(ThreadContext context, IRubyObject offArg, IRubyObj
}

private IRubyObject putBytes(ThreadContext context, long off, ByteList bl, int idx, int len) {
if (idx < 0 || idx > bl.length()) {
if (idx < 0 || idx > bl.byteLength()) {
throw context.runtime.newRangeError("invalid string index");
}

if (len < 0 || len > (bl.length() - idx)) {
if (len < 0 || len > (bl.byteLength() - idx)) {
throw context.runtime.newRangeError("invalid length");
}
getMemoryIO().put(off, bl.getUnsafeBytes(), bl.begin() + idx, len);
Expand All @@ -1895,7 +1895,7 @@ private IRubyObject putBytes(ThreadContext context, long off, ByteList bl, int i
public IRubyObject put_bytes(ThreadContext context, IRubyObject[] args) {
ByteList bl = args[1].convertToString().getByteList();
int idx = args.length > 2 ? Util.int32Value(args[2]) : 0;
int len = args.length > 3 ? Util.int32Value(args[3]) : (bl.length() - idx);
int len = args.length > 3 ? Util.int32Value(args[3]) : (bl.byteLength() - idx);

return putBytes(context, getOffset(args[0]), bl, idx, len);
}
Expand All @@ -1909,7 +1909,7 @@ public IRubyObject read_bytes(ThreadContext context, IRubyObject lenArg) {
public IRubyObject write_bytes(ThreadContext context, IRubyObject[] args) {
ByteList bl = args[0].convertToString().getByteList();
int idx = args.length > 1 ? Util.int32Value(args[1]) : 0;
int len = args.length > 2 ? Util.int32Value(args[2]) : (bl.length() - idx);
int len = args.length > 2 ? Util.int32Value(args[2]) : (bl.byteLength() - idx);
return putBytes(context, 0, bl, idx, len);
}

Expand Down

0 comments on commit 3a08ac3

Please sign in to comment.