Skip to content

Commit dccd802

Browse files
committed
Move pack methods shared between JRuby and Truffle into a separate class.
1 parent db41f16 commit dccd802

File tree

3 files changed

+103
-60
lines changed

3 files changed

+103
-60
lines changed

core/src/main/java/org/jruby/util/Pack.java

+1-58
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public class Pack {
7676
private static final String PACK_IGNORE_NULL_CODES = "cCiIlLnNqQsSvV";
7777
private static final String PACK_IGNORE_NULL_CODES_WITH_MODIFIERS = "lLsS";
7878
private static final String sTooFew = "too few arguments";
79-
private static final byte[] hex_table;
8079
private static final byte[] uu_table;
8180
private static final byte[] b64_table;
8281
private static final byte[] sHexDigits;
@@ -103,7 +102,6 @@ private static double obj2dbl(Ruby runtime, IRubyObject o) {
103102
}
104103

105104
static {
106-
hex_table = ByteList.plain("0123456789ABCDEF");
107105
uu_table =
108106
ByteList.plain("`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_");
109107
b64_table =
@@ -553,61 +551,6 @@ private static ByteList encodes(Ruby runtime, ByteList io2Append,byte[]charsToEn
553551
return io2Append;
554552
}
555553

556-
/**
557-
* encodes a String with the Quoted printable, MIME encoding (see RFC2045).
558-
* appends the result of the encoding in a StringBuffer
559-
* @param io2Append The StringBuffer which should receive the result
560-
* @param i2Encode The String to encode
561-
* @param iLength The max number of characters to encode
562-
* @return the io2Append buffer
563-
**/
564-
public static ByteList qpencode(ByteList io2Append, ByteList i2Encode, int iLength) {
565-
io2Append.ensure(1024);
566-
int lCurLineLength = 0;
567-
int lPrevChar = -1;
568-
byte[] l2Encode = i2Encode.getUnsafeBytes();
569-
try {
570-
int end = i2Encode.getBegin() + i2Encode.getRealSize();
571-
for (int i = i2Encode.getBegin(); i < end; i++) {
572-
int lCurChar = l2Encode[i] & 0xff;
573-
if (lCurChar > 126 || (lCurChar < 32 && lCurChar != '\n' && lCurChar != '\t') || lCurChar == '=') {
574-
io2Append.append('=');
575-
io2Append.append(hex_table[lCurChar >>> 4]);
576-
io2Append.append(hex_table[lCurChar & 0x0f]);
577-
lCurLineLength += 3;
578-
lPrevChar = -1;
579-
} else if (lCurChar == '\n') {
580-
if (lPrevChar == ' ' || lPrevChar == '\t') {
581-
io2Append.append('=');
582-
io2Append.append(lCurChar);
583-
}
584-
io2Append.append(lCurChar);
585-
lCurLineLength = 0;
586-
lPrevChar = lCurChar;
587-
} else {
588-
io2Append.append(lCurChar);
589-
lCurLineLength++;
590-
lPrevChar = lCurChar;
591-
}
592-
if (lCurLineLength > iLength) {
593-
io2Append.append('=');
594-
io2Append.append('\n');
595-
lCurLineLength = 0;
596-
lPrevChar = '\n';
597-
}
598-
}
599-
} catch (ArrayIndexOutOfBoundsException e) {
600-
//normal exit, this should be faster than a test at each iterations for string with more than
601-
//about 40 char
602-
}
603-
604-
if (lCurLineLength > 0) {
605-
io2Append.append('=');
606-
io2Append.append('\n');
607-
}
608-
return io2Append;
609-
}
610-
611554
/**
612555
* Decodes <i>str</i> (which may contain binary data) according to the format
613556
* string, returning an array of each value extracted.
@@ -2027,7 +1970,7 @@ private static RubyString packCommon(Ruby runtime, RubyArray list, ByteList form
20271970
occurrences = 72;
20281971
}
20291972

2030-
qpencode(result, lCurElemString, occurrences);
1973+
PackUtils.qpencode(result, lCurElemString, occurrences);
20311974
}
20321975
break;
20331976
case 'U' :
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/***** BEGIN LICENSE BLOCK *****
2+
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
3+
*
4+
* The contents of this file are subject to the Eclipse Public
5+
* License Version 1.0 (the "License"); you may not use this file
6+
* except in compliance with the License. You may obtain a copy of
7+
* the License at http://www.eclipse.org/legal/epl-v10.html
8+
*
9+
* Software distributed under the License is distributed on an "AS
10+
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11+
* implied. See the License for the specific language governing
12+
* rights and limitations under the License.
13+
*
14+
* Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
15+
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
16+
* Copyright (C) 2003-2004 Thomas E Enebo <enebo@acm.org>
17+
* Copyright (C) 2004 Charles O Nutter <headius@headius.com>
18+
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
19+
* Copyright (C) 2005 Derek Berner <derek.berner@state.nm.us>
20+
* Copyright (C) 2006 Evan Buswell <ebuswell@gmail.com>
21+
* Copyright (C) 2007 Nick Sieger <nicksieger@gmail.com>
22+
* Copyright (C) 2009 Joseph LaFata <joe@quibb.org>
23+
*
24+
* Alternatively, the contents of this file may be used under the terms of
25+
* either of the GNU General Public License Version 2 or later (the "GPL"),
26+
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27+
* in which case the provisions of the GPL or the LGPL are applicable instead
28+
* of those above. If you wish to allow use of your version of this file only
29+
* under the terms of either the GPL or the LGPL, and not to allow others to
30+
* use your version of this file under the terms of the EPL, indicate your
31+
* decision by deleting the provisions above and replace them with the notice
32+
* and other provisions required by the GPL or the LGPL. If you do not delete
33+
* the provisions above, a recipient may use your version of this file under
34+
* the terms of any one of the EPL, the GPL or the LGPL.
35+
***** END LICENSE BLOCK *****/
36+
package org.jruby.util;
37+
38+
public class PackUtils {
39+
40+
private static final byte[] hex_table;
41+
42+
static {
43+
hex_table = ByteList.plain("0123456789ABCDEF");
44+
}
45+
46+
/**
47+
* encodes a String with the Quoted printable, MIME encoding (see RFC2045).
48+
* appends the result of the encoding in a StringBuffer
49+
* @param io2Append The StringBuffer which should receive the result
50+
* @param i2Encode The String to encode
51+
* @param iLength The max number of characters to encode
52+
* @return the io2Append buffer
53+
**/
54+
public static ByteList qpencode(ByteList io2Append, ByteList i2Encode, int iLength) {
55+
io2Append.ensure(1024);
56+
int lCurLineLength = 0;
57+
int lPrevChar = -1;
58+
byte[] l2Encode = i2Encode.getUnsafeBytes();
59+
try {
60+
int end = i2Encode.getBegin() + i2Encode.getRealSize();
61+
for (int i = i2Encode.getBegin(); i < end; i++) {
62+
int lCurChar = l2Encode[i] & 0xff;
63+
if (lCurChar > 126 || (lCurChar < 32 && lCurChar != '\n' && lCurChar != '\t') || lCurChar == '=') {
64+
io2Append.append('=');
65+
io2Append.append(hex_table[lCurChar >>> 4]);
66+
io2Append.append(hex_table[lCurChar & 0x0f]);
67+
lCurLineLength += 3;
68+
lPrevChar = -1;
69+
} else if (lCurChar == '\n') {
70+
if (lPrevChar == ' ' || lPrevChar == '\t') {
71+
io2Append.append('=');
72+
io2Append.append(lCurChar);
73+
}
74+
io2Append.append(lCurChar);
75+
lCurLineLength = 0;
76+
lPrevChar = lCurChar;
77+
} else {
78+
io2Append.append(lCurChar);
79+
lCurLineLength++;
80+
lPrevChar = lCurChar;
81+
}
82+
if (lCurLineLength > iLength) {
83+
io2Append.append('=');
84+
io2Append.append('\n');
85+
lCurLineLength = 0;
86+
lPrevChar = '\n';
87+
}
88+
}
89+
} catch (ArrayIndexOutOfBoundsException e) {
90+
//normal exit, this should be faster than a test at each iterations for string with more than
91+
//about 40 char
92+
}
93+
94+
if (lCurLineLength > 0) {
95+
io2Append.append('=');
96+
io2Append.append('\n');
97+
}
98+
return io2Append;
99+
}
100+
}

truffle/src/main/java/org/jruby/truffle/pack/nodes/write/WriteMIMEStringNode.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.jruby.truffle.pack.nodes.PackNode;
1818
import org.jruby.truffle.runtime.core.RubyNilClass;
1919
import org.jruby.util.ByteList;
20-
import org.jruby.util.Pack;
20+
import org.jruby.util.PackUtils;
2121

2222
/**
2323
* Read a string that contains MIME encoded data and write as actual binary
@@ -49,7 +49,7 @@ public Object write(VirtualFrame frame, ByteList bytes) {
4949
private ByteList encode(ByteList bytes) {
5050
// TODO CS 30-Mar-15 should write our own optimizable version of MIME
5151
final ByteList output = new ByteList();
52-
Pack.qpencode(output, bytes, length);
52+
PackUtils.qpencode(output, bytes, length);
5353
return output;
5454
}
5555

0 commit comments

Comments
 (0)