Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/util/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,8 @@ public static IRubyObject unpack1WithBlock(ThreadContext context, RubyString enc
}
}

return unpackInternal(context, encoded, formatString, UNPACK_1, offset, block);
IRubyObject value = unpackInternal(context, encoded, formatString, UNPACK_1, offset, block);
return value == null ? context.nil : value;
}

private static IRubyObject unpackInternal(ThreadContext context, RubyString encoded, ByteList formatString, int mode, long offset, Block block) {
Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/core/string/unpack1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"a".unpack1("C", offset: 1).should == nil
end

it "returns nil if the format unpacks no value" do
"".unpack1("C*").should == nil
"\x80".b.unpack1("w*").should == nil
"abc".unpack1("").should == nil
end

it "raises an ArgumentError when the offset is larger than the string bytesize" do
-> { "a".unpack1("C", offset: 2) }.should raise_error(ArgumentError, "offset outside of string")
end
Expand Down
Loading