Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-compute String size after #chomp() if possible #15153

Merged
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
18 changes: 18 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,24 @@ describe "String" do
it { "hello\n\n\n\n".chomp("").should eq("hello\n\n\n\n") }

it { "hello\r\n".chomp("\n").should eq("hello") }

it "pre-computes string size if possible" do
{"!hello!", "\u{1f602}hello\u{1f602}", "\xFEhello\xFF"}.each do |str|
{"", "\n", "\r", "\r\n"}.each do |newline|
x = str + newline
x.size_known?.should be_true
y = x.chomp
[email protected] eq(7)
end
end
end

it "does not pre-compute string size if not possible" do
x = String.build &.<< "abc\n"
x.size_known?.should be_false
y = x.chomp
y.size_known?.should be_false
end
end

describe "lchop" do
Expand Down
14 changes: 7 additions & 7 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1661,12 +1661,12 @@ class String
case to_unsafe[bytesize - 1]
when '\n'
if bytesize > 1 && to_unsafe[bytesize - 2] === '\r'
unsafe_byte_slice_string(0, bytesize - 2)
unsafe_byte_slice_string(0, bytesize - 2, @length > 0 ? @length - 2 : 0)
else
unsafe_byte_slice_string(0, bytesize - 1)
unsafe_byte_slice_string(0, bytesize - 1, @length > 0 ? @length - 1 : 0)
end
when '\r'
unsafe_byte_slice_string(0, bytesize - 1)
unsafe_byte_slice_string(0, bytesize - 1, @length > 0 ? @length - 1 : 0)
else
self
end
Expand Down Expand Up @@ -5552,12 +5552,12 @@ class String
Slice.new(to_unsafe + byte_offset, bytesize - byte_offset, read_only: true)
end

protected def unsafe_byte_slice_string(byte_offset)
String.new(unsafe_byte_slice(byte_offset))
protected def unsafe_byte_slice_string(byte_offset, *, size = 0)
String.new(to_unsafe + byte_offset, bytesize - byte_offset, size)
end

protected def unsafe_byte_slice_string(byte_offset, count)
String.new(unsafe_byte_slice(byte_offset, count))
protected def unsafe_byte_slice_string(byte_offset, count, size = 0)
String.new(to_unsafe + byte_offset, count, size)
end

protected def self.char_bytes_and_bytesize(char : Char)
Expand Down
Loading