From 641d7fa017b2c3ef7538c16fb57a3d62580b7666 Mon Sep 17 00:00:00 2001 From: George Dietrich Date: Tue, 2 Feb 2021 19:10:06 -0500 Subject: [PATCH 1/4] Allow Int64 values within `IO#read_at` --- spec/std/file_spec.cr | 5 +++++ src/file/preader.cr | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/std/file_spec.cr b/spec/std/file_spec.cr index 50192d20f402..fdf071bf0cb0 100644 --- a/spec/std/file_spec.cr +++ b/spec/std/file_spec.cr @@ -946,9 +946,14 @@ describe "File" do file.read_at(6, 100) do |io| io.gets_to_end.should eq("World\nHello World\nHello World\nHello World\nHello World\nHello World\nHello World\nHello World\nHello Worl") end + file.read_at(0, 240) do |io| io.gets_to_end.should eq(File.read(filename)) end + + file.read_at(6_i64, 5_i64) do |io| + io.gets_to_end.should eq("World") + end end end diff --git a/src/file/preader.cr b/src/file/preader.cr index fa39b48ffa8f..9cfa805cc3e9 100644 --- a/src/file/preader.cr +++ b/src/file/preader.cr @@ -4,7 +4,7 @@ class File::PReader < IO getter? closed = false - def initialize(@file : File, @offset : Int32, @bytesize : Int32) + def initialize(@file : File, @offset : Int64, @bytesize : Int64) @pos = 0 end From 4c665ebdd3cbe2bed3377c91f34fb38852661edd Mon Sep 17 00:00:00 2001 From: George Dietrich Date: Tue, 2 Feb 2021 19:27:16 -0500 Subject: [PATCH 2/4] Add type hints to `read_at` args --- src/file.cr | 2 +- src/io.cr | 2 +- src/io/memory.cr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/file.cr b/src/file.cr index 27313753ef56..ab4f36bbd5fc 100644 --- a/src/file.cr +++ b/src/file.cr @@ -804,7 +804,7 @@ class File < IO::FileDescriptor # Yields an `IO` to read a section inside this file. # Multiple sections can be read concurrently. - def read_at(offset, bytesize, &block) + def read_at(offset : Int64, bytesize : Int64, & : IO ->) self_bytesize = self.size unless 0 <= offset <= self_bytesize diff --git a/src/io.cr b/src/io.cr index d17f9c670651..131c856938e5 100644 --- a/src/io.cr +++ b/src/io.cr @@ -1099,7 +1099,7 @@ abstract class IO # `File` and `IO::Memory` implement it. # # Multiple sections can be read concurrently. - def read_at(offset, bytesize, &block) + def read_at(offset : Int64, bytesize : Int64, & : IO ->) raise Error.new "Unable to read_at" end diff --git a/src/io/memory.cr b/src/io/memory.cr index e447c9f5da93..fd73ddbc8ff2 100644 --- a/src/io/memory.cr +++ b/src/io/memory.cr @@ -338,7 +338,7 @@ class IO::Memory < IO # # During the block duration `self` becomes read-only, # so multiple concurrent open are allowed. - def read_at(offset, bytesize) + def read_at(offset : Int64, bytesize : Int64, & : IO ->) unless 0 <= offset <= @bytesize raise ArgumentError.new("Offset out of bounds") end From 37b486c21c1a076548ec0ffe00c2de01112c9404 Mon Sep 17 00:00:00 2001 From: George Dietrich Date: Wed, 3 Feb 2021 08:59:41 -0500 Subject: [PATCH 3/4] Be less strict on `read_at` arg types Add overload to accept Int32s --- src/file.cr | 2 +- src/file/preader.cr | 4 ++++ src/io.cr | 2 +- src/io/memory.cr | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/file.cr b/src/file.cr index ab4f36bbd5fc..06bea69f11f5 100644 --- a/src/file.cr +++ b/src/file.cr @@ -804,7 +804,7 @@ class File < IO::FileDescriptor # Yields an `IO` to read a section inside this file. # Multiple sections can be read concurrently. - def read_at(offset : Int64, bytesize : Int64, & : IO ->) + def read_at(offset, bytesize, & : IO ->) self_bytesize = self.size unless 0 <= offset <= self_bytesize diff --git a/src/file/preader.cr b/src/file/preader.cr index 9cfa805cc3e9..65c099df395f 100644 --- a/src/file/preader.cr +++ b/src/file/preader.cr @@ -4,6 +4,10 @@ class File::PReader < IO getter? closed = false + def self.new(file : File, offset : Int32, bytesize : Int32) + new file, offset.to_i64, bytesize.to_i64 + end + def initialize(@file : File, @offset : Int64, @bytesize : Int64) @pos = 0 end diff --git a/src/io.cr b/src/io.cr index 131c856938e5..37abcdea3722 100644 --- a/src/io.cr +++ b/src/io.cr @@ -1099,7 +1099,7 @@ abstract class IO # `File` and `IO::Memory` implement it. # # Multiple sections can be read concurrently. - def read_at(offset : Int64, bytesize : Int64, & : IO ->) + def read_at(offset, bytesize, & : IO ->) raise Error.new "Unable to read_at" end diff --git a/src/io/memory.cr b/src/io/memory.cr index fd73ddbc8ff2..082e16477ece 100644 --- a/src/io/memory.cr +++ b/src/io/memory.cr @@ -338,7 +338,7 @@ class IO::Memory < IO # # During the block duration `self` becomes read-only, # so multiple concurrent open are allowed. - def read_at(offset : Int64, bytesize : Int64, & : IO ->) + def read_at(offset, bytesize, & : IO ->) unless 0 <= offset <= @bytesize raise ArgumentError.new("Offset out of bounds") end From 9a8004cb44b5eebcfb077c13659f76607dc09b1d Mon Sep 17 00:00:00 2001 From: George Dietrich Date: Thu, 4 Feb 2021 08:46:57 -0500 Subject: [PATCH 4/4] Use `.to_i64` versus separate overload --- src/file/preader.cr | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/file/preader.cr b/src/file/preader.cr index 65c099df395f..e8b91a8d6b55 100644 --- a/src/file/preader.cr +++ b/src/file/preader.cr @@ -4,11 +4,12 @@ class File::PReader < IO getter? closed = false - def self.new(file : File, offset : Int32, bytesize : Int32) - new file, offset.to_i64, bytesize.to_i64 - end + @offset : Int64 + @bytesize : Int64 - def initialize(@file : File, @offset : Int64, @bytesize : Int64) + def initialize(@file : File, offset : Int, bytesize : Int) + @offset = offset.to_i64 + @bytesize = bytesize.to_i64 @pos = 0 end