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: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9eab0f31f87d
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a33c0c2cde8e
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Oct 12, 2016

  1. String::Builder: make capacity bigger to hold string header

    Ary Borenszweig committed Oct 12, 2016
    2

    Verified

    This commit was signed with the committer’s verified signature.
    headius Charles Oliver Nutter
    Copy the full SHA
    d1b5ad4 View commit details
  2. File.read: fix for zero-sized files. Fixes #3410. Closes #3411

    Thank you @Val and @RX14
    Ary Borenszweig committed Oct 12, 2016
    2
    Copy the full SHA
    a33c0c2 View commit details
Showing with 26 additions and 3 deletions.
  1. +9 −0 spec/std/file_spec.cr
  2. +6 −0 spec/std/string_spec.cr
  3. +6 −3 src/file.cr
  4. +5 −0 src/string/builder.cr
9 changes: 9 additions & 0 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
@@ -37,6 +37,15 @@ describe "File" do
str.should eq("Hello World\n" * 20)
end

{% if flag?(:linux) %}
it "reads entire file from proc virtual filesystem" do
str1 = File.open "/proc/self/cmdline", &.gets_to_end
str2 = File.read "/proc/self/cmdline"
str2.empty?.should be_false
str2.should eq(str1)
end
{% end %}

it "reads lines from file" do
lines = File.read_lines "#{__DIR__}/data/test_file.txt"
lines.size.should eq(20)
6 changes: 6 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
@@ -1846,6 +1846,12 @@ describe "String" do
end
end

it "raises if capacity too big on new with UInt32::MAX - String::HEADER_SIZE - 1" do
expect_raises(ArgumentError, "capacity too big") do
String.new(UInt32::MAX - String::HEADER_SIZE) { {0, 0} }
end
end

it "raises if capacity too big on new with UInt64::MAX" do
expect_raises(ArgumentError, "capacity too big") do
String.new(UInt64::MAX) { {0, 0} }
9 changes: 6 additions & 3 deletions src/file.cr
Original file line number Diff line number Diff line change
@@ -421,10 +421,13 @@ class File < IO::FileDescriptor
file.set_encoding(encoding, invalid: invalid)
file.gets_to_end
else
# We try to read a string with an initialize capacity
# equal to the file's size, but the size might not be
# correct or even be zero (for example for /proc files)
size = file.size.to_i
String.new(size) do |buffer|
file.read Slice.new(buffer, size)
{size.to_i, 0}
size = 256 if size == 0
String.build(size) do |io|
IO.copy(file, io)
end
end
end
5 changes: 5 additions & 0 deletions src/string/builder.cr
Original file line number Diff line number Diff line change
@@ -13,6 +13,11 @@ class String::Builder
def initialize(capacity : Int = 64)
String.check_capacity_in_bounds(capacity)

# Make sure to also be able to hold
# the header size plus the trailing zero byte
capacity += String::HEADER_SIZE + 1
String.check_capacity_in_bounds(capacity)

@buffer = GC.malloc_atomic(capacity.to_u32).as(UInt8*)
@bytesize = 0
@capacity = capacity.to_i