Skip to content

Commit

Permalink
Fix multibyte content truncation and close #22
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jul 3, 2016
1 parent 6338a08 commit 70bcfcb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/tty/command/truncator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def write(content)
content = append(content, @prefix)

if (over = (content.bytesize - @max_size)) > 0
content = content[over..-1]
content = content.byteslice(over..-1)
@skipped += over
end

Expand Down Expand Up @@ -81,10 +81,10 @@ def copy(value, dest)
bytes = value.bytesize
n = bytes < dest.bytesize ? bytes : dest.bytesize

head, tail = dest[0...n], dest[n..-1]
head, tail = dest.byteslice(0...n), dest.byteslice(n..-1)
dest.replace("#{tail}#{value[0...n]}")
@skipped += head.bytesize
value[n..-1]
value.byteslice(n..-1)
end

# Append value to destination
Expand All @@ -99,10 +99,10 @@ def append(value, dst)
if remain > 0
value_bytes = value.to_s.bytesize
offset = value_bytes < remain ? value_bytes : remain
dst << value[0...offset]
value = value[offset..-1]
dst << value.byteslice(0...offset)
value = value.byteslice(offset..-1)
end
value.to_s
value
end
end # Truncator
end # Command
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/truncator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

it "writes more bytes letter" do
truncator = described_class.new(max_size: 1000)

multibytes_string = "’test’"

expect { truncator.write(multibytes_string) }.to_not raise_error
truncator.write(multibytes_string)

expect(truncator.read).to eq(multibytes_string)
end

Expand Down

0 comments on commit 70bcfcb

Please sign in to comment.