Skip to content

Commit

Permalink
Allow Bytes[] to construct an empty Bytes (#11897)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Mar 17, 2022
1 parent 03ed280 commit 39efd0c
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion spec/std/io/buffered_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ describe "IO::Buffered" do
it "shouldn't call unbuffered read if reading to an empty slice" do
str = IO::Memory.new("foo")
io = BufferedWrapper.new(str)
io.read(Bytes.new(0))
io.read(Bytes[])
io.called_unbuffered_read.should be_false
end

Expand Down
2 changes: 1 addition & 1 deletion spec/std/io/io_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ describe IO do
it "reads all remaining content as bytes" do
io = SimpleIOMemory.new(Bytes[0, 1, 3, 6, 10, 15])
io.getb_to_end.should eq(Bytes[0, 1, 3, 6, 10, 15])
io.getb_to_end.should eq(Bytes.new(0))
io.getb_to_end.should eq(Bytes[])
io.rewind
bytes = io.getb_to_end
bytes.should eq(Bytes[0, 1, 3, 6, 10, 15])
Expand Down
4 changes: 2 additions & 2 deletions spec/std/io/memory_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ describe IO::Memory do
it "consumes with getb_to_end" do
io = IO::Memory.new(Bytes[0, 1, 3, 6, 10, 15])
io.getb_to_end.should eq(Bytes[0, 1, 3, 6, 10, 15])
io.getb_to_end.should eq(Bytes.new(0))
io.getb_to_end.should eq(Bytes[])
io.seek(3)
bytes = io.getb_to_end
bytes.should eq(Bytes[6, 10, 15])
Expand All @@ -403,7 +403,7 @@ describe IO::Memory do
bytes.should eq(Bytes[6, 10, 15])

io.seek(10)
io.getb_to_end.should eq(Bytes.new(0))
io.getb_to_end.should eq(Bytes[])
end

it "peeks" do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/random_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe "Random" do
rng.random_bytes(1).should eq Bytes[0x3e]
rng.random_bytes(4).should eq Bytes[1, 0, 0, 0]
rng.random_bytes(3).should eq Bytes[0x78, 0x56, 0x34]
rng.random_bytes(0).should eq Bytes.new(0)
rng.random_bytes(0).should eq Bytes[]

rng = TestRNG.new([12u8, 255u8, 11u8, 5u8, 122u8, 200u8, 192u8])
rng.random_bytes(7).should eq Bytes[12, 255, 11, 5, 122, 200, 192]
Expand Down
6 changes: 6 additions & 0 deletions spec/std/slice_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@ describe "Slice" do
slice.to_a.should eq([1, 2, 3])
end

it "does Bytes[]" do
slice = Bytes[]
slice.should be_a(Bytes)
slice.empty?.should be_true
end

it "uses percent vars in [] macro (#2954)" do
slices = itself(Slice[1, 2], Slice[3])
slices[0].to_a.should eq([1, 2])
Expand Down
2 changes: 1 addition & 1 deletion src/io/memory.cr
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class IO::Memory < IO
check_open

if @pos >= @bytesize
Bytes.new(0)
Bytes[]
else
bytes = Slice.new(@buffer + @pos, @bytesize - @pos).dup
@pos = @bytesize
Expand Down
2 changes: 1 addition & 1 deletion src/slice.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Slice(T)
# TODO: there should be a better way to check this, probably
# asking if @type was instantiated or if T is defined
{% if @type.name != "Slice(T)" && T < Number %}
{{T}}.slice({{*args}}, read_only: {{read_only}})
{{T}}.slice({{args.splat(", ")}}read_only: {{read_only}})
{% else %}
%ptr = Pointer(typeof({{*args}})).malloc({{args.size}})
{% for arg, i in args %}
Expand Down

0 comments on commit 39efd0c

Please sign in to comment.