Skip to content

Commit

Permalink
A few small cleanup tweaks in Pack
Browse files Browse the repository at this point in the history
* Consolidate deprecated forms
* Access runtime outside of loop
  • Loading branch information
headius committed Dec 21, 2023
1 parent 4365e40 commit 1fd1f73
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
10 changes: 5 additions & 5 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -6586,11 +6586,6 @@ private static long unpackOffset(ThreadContext context, IRubyObject opt) {
return offset;
}

@Deprecated // not used
public RubyArray unpack(IRubyObject obj) {
return Pack.unpack(getRuntime(), this.value, stringValue(obj).value);
}

public void empty() {
value = ByteList.EMPTY_BYTELIST;
shareLevel = SHARE_LEVEL_BYTELIST;
Expand Down Expand Up @@ -7244,4 +7239,9 @@ public IRubyObject op_match19(ThreadContext context, IRubyObject other) {
@Deprecated
public IRubyObject scan19(ThreadContext context, IRubyObject arg, Block block) { return scan(context, arg, block); }

@Deprecated // not used
public RubyArray unpack(IRubyObject obj) {
return Pack.unpack(getRuntime(), this.value, stringValue(obj).value);
}

}
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ext/nkf/RubyNKF.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,9 @@ private ByteList decodeMimeString(String str) {

final RubyArray array;
if ('B' == encode || 'b' == encode) { // BASE64
array = Pack.unpack(context.runtime, body, PACK_BASE64);
array = Pack.unpack(context, body, PACK_BASE64);
} else { // Qencode
array = Pack.unpack(context.runtime, body, PACK_QENCODE);
array = Pack.unpack(context, body, PACK_QENCODE);
}
RubyString s = (RubyString) array.entry(0);
ByteList decodeStr = s.asString().getByteList();
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ext/socket/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public IRubyObject linger(ThreadContext context) {

@JRubyMethod
public IRubyObject unpack(ThreadContext context, IRubyObject arg0) {
return Pack.unpack(context.runtime, data, arg0.convertToString().getByteList());
return Pack.unpack(context, data, arg0.convertToString().getByteList());
}

@JRubyMethod
Expand Down
72 changes: 38 additions & 34 deletions core/src/main/java/org/jruby/util/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ public static ByteList packInt_i(ByteList result, int s) {
return result;
}

public static void encodeUM(Ruby runtime, ByteList lCurElemString, int occurrences, boolean ignoreStar, char type, ByteList result) {
private static void encodeUM(Ruby runtime, ByteList lCurElemString, int occurrences, boolean ignoreStar, char type, ByteList result) {
if (occurrences == 0 && type == 'm' && !ignoreStar) {
encodes(runtime, result, lCurElemString.getUnsafeBytes(),
lCurElemString.getBegin(), lCurElemString.length(),
Expand Down Expand Up @@ -616,12 +616,12 @@ private static ByteList encodes(Ruby runtime, ByteList io2Append,byte[] charsToE
return io2Append;
}

public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList formatString) {
return unpackWithBlock(runtime.getCurrentContext(), runtime, encodedString, formatString, Block.NULL_BLOCK);
public static RubyArray unpack(ThreadContext context, ByteList encodedString, ByteList formatString) {
return unpackWithBlock(context, RubyString.newStringLight(context.runtime, encodedString), formatString, Block.NULL_BLOCK);
}

/**
* @see Pack#unpackWithBlock(ThreadContext, Ruby, ByteList, ByteList, Block)
* @see Pack#unpackWithBlock(ThreadContext, RubyString, ByteList, Block)
* @param context
* @param encoded
* @param formatString
Expand Down Expand Up @@ -1618,11 +1618,6 @@ private static int checkLimit(Ruby runtime, ByteBuffer encode, int limit) {
return limit;
}

@Deprecated
public static RubyArray unpackWithBlock(ThreadContext context, Ruby runtime, ByteList encodedString, ByteList formatString, Block block) {
return unpackWithBlock(context, RubyString.newStringLight(runtime, encodedString), formatString, block);
}

private static void appendOrYield(ThreadContext context, Block block, RubyArray result, IRubyObject item, int mode) {
if (mode == UNPACK_BLOCK) {
block.yield(context, item);
Expand Down Expand Up @@ -1795,7 +1790,7 @@ public static IRubyObject decode(ThreadContext context, Ruby runtime, ByteBuffer
return context.nil;
}

public static int encode(Ruby runtime, int occurrences, ByteList result,
private static int encode(Ruby runtime, int occurrences, ByteList result,
RubyArray list, int index, ConverterExecutor converter) {
int listSize = list.size();

Expand Down Expand Up @@ -1923,27 +1918,6 @@ private static final ByteList grow(ByteList i2Grow, byte[]iPads, int iLength) {
return i2Grow;
}

/**
* Same as pack but defaults tainting of output to false.
*/
public static RubyString pack(Ruby runtime, RubyArray list, ByteList formatString) {
RubyString buffer = runtime.newString();
return packCommon(runtime.getCurrentContext(), list, formatString, executor(), buffer);
}

@Deprecated
public static RubyString pack(ThreadContext context, Ruby runtime, RubyArray list, RubyString formatString) {
RubyString buffer = runtime.newString();
return pack(context, list, formatString, buffer);
}

@Deprecated
public static void decode(ThreadContext context, Ruby runtime, ByteBuffer encode, int occurrences,
RubyArray result, Block block, Converter converter) {
decode(context, runtime, encode, occurrences,
result, block, converter, block.isGiven() ? UNPACK_BLOCK : UNPACK_ARRAY);
}

public static RubyString pack(ThreadContext context, RubyArray list, RubyString formatString, RubyString buffer) {
return packCommon(context, list, formatString.getByteList(), executor(), buffer);
}
Expand Down Expand Up @@ -2178,17 +2152,18 @@ private static void pack_w(ThreadContext context, RubyArray list, ByteList resul
}

private static void pack_U(ThreadContext context, RubyArray list, ByteList result, PackInts packInts, int occurrences) {
Ruby runtime = context.runtime;
while (occurrences-- > 0) {
if (packInts.listSize-- <= 0) throw context.runtime.newArgumentError(sTooFew);
if (packInts.listSize-- <= 0) throw runtime.newArgumentError(sTooFew);

IRubyObject from = list.eltInternal(packInts.idx++);
int code = from == context.nil ? 0 : RubyNumeric.num2int(from);

if (code < 0) throw context.runtime.newRangeError("pack(U): value out of range");
if (code < 0) throw runtime.newRangeError("pack(U): value out of range");

int len = result.getRealSize();
result.ensure(len + 6);
result.setRealSize(len + utf8Decode(context.runtime, result.getUnsafeBytes(), result.getBegin() + len, code));
result.setRealSize(len + utf8Decode(runtime, result.getUnsafeBytes(), result.getBegin() + len, code));
}
}

Expand Down Expand Up @@ -2711,4 +2686,33 @@ private static void encodeShortBigEndian(ByteList result, int s) {
result.append((byte) ((s & 0xff00) >> 8)).append((byte) (s & 0xff));
}

@Deprecated
public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList formatString) {
return unpackWithBlock(runtime.getCurrentContext(), runtime, encodedString, formatString, Block.NULL_BLOCK);
}

@Deprecated
public static RubyString pack(Ruby runtime, RubyArray list, ByteList formatString) {
RubyString buffer = runtime.newString();
return packCommon(runtime.getCurrentContext(), list, formatString, executor(), buffer);
}

@Deprecated
public static RubyString pack(ThreadContext context, Ruby runtime, RubyArray list, RubyString formatString) {
RubyString buffer = runtime.newString();
return pack(context, list, formatString, buffer);
}

@Deprecated
public static void decode(ThreadContext context, Ruby runtime, ByteBuffer encode, int occurrences,
RubyArray result, Block block, Converter converter) {
decode(context, runtime, encode, occurrences,
result, block, converter, block.isGiven() ? UNPACK_BLOCK : UNPACK_ARRAY);
}

@Deprecated
public static RubyArray unpackWithBlock(ThreadContext context, Ruby runtime, ByteList encodedString, ByteList formatString, Block block) {
return unpackWithBlock(context, RubyString.newStringLight(runtime, encodedString), formatString, block);
}

}

0 comments on commit 1fd1f73

Please sign in to comment.