Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3f97c7aafb68
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 45fbf17873f2
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Feb 9, 2017

  1. Copy the full SHA
    507d226 View commit details
  2. Copy the full SHA
    45fbf17 View commit details
Showing with 41 additions and 19 deletions.
  1. +15 −19 core/src/main/java/org/jruby/util/Pack.java
  2. +12 −0 spec/ruby/core/string/unpack/a_spec.rb
  3. +6 −0 spec/ruby/core/string/unpack/b_spec.rb
  4. +8 −0 spec/ruby/core/string/unpack/u_spec.rb
34 changes: 15 additions & 19 deletions core/src/main/java/org/jruby/util/Pack.java
Original file line number Diff line number Diff line change
@@ -786,16 +786,15 @@ private static ByteList encodes(Ruby runtime, ByteList io2Append,byte[]charsToEn
* @see RubyArray#pack
**/
public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList formatString) {
Encoding encoding = encodedString.getEncoding();
RubyArray result = runtime.newArray();
// Encoding encoding = encodedString.getEncoding();
final RubyArray result = runtime.newArray();
// FIXME: potentially could just use ByteList here?
ByteBuffer format = ByteBuffer.wrap(formatString.getUnsafeBytes(), formatString.begin(), formatString.length());
ByteBuffer encode = ByteBuffer.wrap(encodedString.getUnsafeBytes(), encodedString.begin(), encodedString.length());
int type = 0;
int next = safeGet(format);

mainLoop: while (next != 0) {
type = next;
int type = next;
next = safeGet(format);
if (UNPACK_IGNORE_NULL_CODES.indexOf(type) != -1 && next == 0) {
next = safeGetIgnoreNull(format);
@@ -813,8 +812,7 @@ public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList fo
if (next == '_' || next == '!') {
int index = NATIVE_CODES.indexOf(type);
if (index == -1) {
throw runtime.newArgumentError("'" + next +
"' allowed only after types " + NATIVE_CODES);
throw runtime.newArgumentError("'" + next + "' allowed only after types " + NATIVE_CODES);
}
type = MAPPED_CODES.charAt(index);

@@ -825,8 +823,7 @@ public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList fo
next = next == '>' ? BE : LE;
int index = ENDIANESS_CODES.indexOf(type + next);
if (index == -1) {
throw runtime.newArgumentError("'" + (char)next +
"' allowed only after types sSiIlLqQ");
throw runtime.newArgumentError("'" + (char)next + "' allowed only after types sSiIlLqQ");
}
type = ENDIANESS_CODES.charAt(index);
next = safeGet(format);
@@ -835,7 +832,7 @@ public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList fo
}

// How many occurrences of 'type' we want
int occurrences = 0;
int occurrences;
if (next == 0) {
occurrences = 1;
} else {
@@ -893,7 +890,7 @@ public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList fo
}
}

result.append(RubyString.newString(runtime, new ByteList(potential, 0, occurrences, encoding, false)));
result.append(RubyString.newString(runtime, new ByteList(potential, 0, occurrences, ASCIIEncoding.INSTANCE, false)));
}
break;
case 'Z' :
@@ -916,7 +913,7 @@ public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList fo
t++;
}

result.append(RubyString.newString(runtime, new ByteList(potential, 0, t, encoding, false)));
result.append(RubyString.newString(runtime, new ByteList(potential, 0, t, ASCIIEncoding.INSTANCE, false)));

// In case when the number of occurences is
// explicitly specified, we have to read up
@@ -943,7 +940,7 @@ public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList fo
}
byte[] potential = new byte[occurrences];
encode.get(potential);
result.append(RubyString.newString(runtime, new ByteList(potential, encoding, false)));
result.append(RubyString.newString(runtime, new ByteList(potential, ASCIIEncoding.INSTANCE, false)));
break;
case 'b' :
{
@@ -960,7 +957,7 @@ public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList fo
}
lElem[lCurByte] = (bits & 1) != 0 ? (byte)'1' : (byte)'0';
}
result.append(RubyString.newString(runtime, new ByteList(lElem, encoding, false)));
result.append(RubyString.newString(runtime, new ByteList(lElem, ASCIIEncoding.INSTANCE, false)));
}
break;
case 'B' :
@@ -979,7 +976,7 @@ public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList fo
lElem[lCurByte] = (bits & 128) != 0 ? (byte)'1' : (byte)'0';
}

result.append(RubyString.newString(runtime, new ByteList(lElem, encoding, false)));
result.append(RubyString.newString(runtime, new ByteList(lElem, ASCIIEncoding.INSTANCE, false)));
}
break;
case 'h' :
@@ -997,7 +994,7 @@ public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList fo
}
lElem[lCurByte] = sHexDigits[bits & 15];
}
result.append(RubyString.newString(runtime, new ByteList(lElem, encoding, false)));
result.append(RubyString.newString(runtime, new ByteList(lElem, ASCIIEncoding.INSTANCE, false)));
}
break;
case 'H' :
@@ -1015,7 +1012,7 @@ public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList fo
}
lElem[lCurByte] = sHexDigits[(bits >>> 4) & 15];
}
result.append(RubyString.newString(runtime, new ByteList(lElem, encoding, false)));
result.append(RubyString.newString(runtime, new ByteList(lElem, ASCIIEncoding.INSTANCE, false)));
}
break;

@@ -1083,7 +1080,7 @@ else if (encode.hasRemaining()) {
}
}
}
result.append(RubyString.newString(runtime, new ByteList(lElem, 0, index, encoding, false)));
result.append(RubyString.newString(runtime, new ByteList(lElem, 0, index, ASCIIEncoding.INSTANCE, false)));
}
break;

@@ -1261,8 +1258,7 @@ else if (encode.hasRemaining()) {
// but should use UTF8Encoding facilities
// from Joni, once it starts prefroming
// UTF-8 content validation.
result.append(
runtime.newFixnum(utf8Decode(encode)));
result.append(runtime.newFixnum(utf8Decode(encode)));
} catch (IllegalArgumentException e) {
throw runtime.newArgumentError(e.getMessage());
}
12 changes: 12 additions & 0 deletions spec/ruby/core/string/unpack/a_spec.rb
Original file line number Diff line number Diff line change
@@ -27,6 +27,12 @@
["a\x00 b\x00\v", ["a\x00 b\x00\v"]],
].should be_computed_by(:unpack, "A*")
end

it "decodes into raw (ascii) string values" do
str = "str".force_encoding('UTF-8').unpack("A*")[0]
str.encoding.name.should == 'ASCII-8BIT'
end

end

describe "String#unpack with format 'a'" do
@@ -48,4 +54,10 @@
["a\x00 b\v", ["a\x00 b\v"]]
].should be_computed_by(:unpack, "a*")
end

it "decodes into raw (ascii) string values" do
str = "".unpack("a*")[0]
str.encoding.name.should == 'ASCII-8BIT'
end

end
6 changes: 6 additions & 0 deletions spec/ruby/core/string/unpack/b_spec.rb
Original file line number Diff line number Diff line change
@@ -181,4 +181,10 @@
it "ignores spaces between directives" do
"\x01\x00".unpack("b b").should == ["1", "0"]
end

it "decodes into raw (ascii) string values" do
str = "s".force_encoding('UTF-8').unpack("b*")[0]
str.encoding.name.should == 'ASCII-8BIT'
end

end
8 changes: 8 additions & 0 deletions spec/ruby/core/string/unpack/u_spec.rb
Original file line number Diff line number Diff line change
@@ -26,6 +26,14 @@
"".unpack("u").should == [""]
end

it "decodes into raw (ascii) string values" do
str = "".unpack("u")[0]
str.encoding.name.should == 'ASCII-8BIT'

str = "1".force_encoding('UTF-8').unpack("u")[0]
str.encoding.name.should == 'ASCII-8BIT'
end

it "decodes the complete string ignoring newlines when given a single directive" do
"#86)C\n#1$5&\n".unpack("u").should == ["abcDEF"]
end