From 1fd1f731c41f91731557c0ddf6ce8857b97e5bbd Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Thu, 21 Dec 2023 11:34:47 -0600 Subject: [PATCH] A few small cleanup tweaks in Pack * Consolidate deprecated forms * Access runtime outside of loop --- core/src/main/java/org/jruby/RubyString.java | 10 +-- .../main/java/org/jruby/ext/nkf/RubyNKF.java | 4 +- .../java/org/jruby/ext/socket/Option.java | 2 +- core/src/main/java/org/jruby/util/Pack.java | 72 ++++++++++--------- 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/core/src/main/java/org/jruby/RubyString.java b/core/src/main/java/org/jruby/RubyString.java index 09ea2a90c4c..29aee67554b 100644 --- a/core/src/main/java/org/jruby/RubyString.java +++ b/core/src/main/java/org/jruby/RubyString.java @@ -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; @@ -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); + } + } diff --git a/core/src/main/java/org/jruby/ext/nkf/RubyNKF.java b/core/src/main/java/org/jruby/ext/nkf/RubyNKF.java index 261d114e11b..4225987cdc9 100644 --- a/core/src/main/java/org/jruby/ext/nkf/RubyNKF.java +++ b/core/src/main/java/org/jruby/ext/nkf/RubyNKF.java @@ -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(); diff --git a/core/src/main/java/org/jruby/ext/socket/Option.java b/core/src/main/java/org/jruby/ext/socket/Option.java index 03310f25d6f..09a465ff893 100644 --- a/core/src/main/java/org/jruby/ext/socket/Option.java +++ b/core/src/main/java/org/jruby/ext/socket/Option.java @@ -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 diff --git a/core/src/main/java/org/jruby/util/Pack.java b/core/src/main/java/org/jruby/util/Pack.java index dbf9c291b23..4076cf35ef6 100644 --- a/core/src/main/java/org/jruby/util/Pack.java +++ b/core/src/main/java/org/jruby/util/Pack.java @@ -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(), @@ -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 @@ -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); @@ -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(); @@ -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); } @@ -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)); } } @@ -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); + } + }