Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #14 from romuloceccon/encoding_bug_fix

RangesIO#write corrupts output if passed a UTF8 string
  • Loading branch information...
commit 5d4652fde0fd2f4ebc443d140fae234d87918dd0 2 parents dde760e + e687b26
@aquasync authored
Showing with 26 additions and 0 deletions.
  1. +5 −0 lib/ole/ranges_io.rb
  2. +21 −0 test/test_storage.rb
View
5 lib/ole/ranges_io.rb
@@ -195,6 +195,11 @@ def size= size
end
def write data
+ # duplicates object to avoid side effects for the caller, but do so only if
+ # encoding isn't already ASCII-8BIT (slight optimization)
+ unless data.encoding == Encoding::ASCII_8BIT
+ data = data.dup.force_encoding(Encoding::ASCII_8BIT)
+ end
return 0 if data.empty?
data_pos = 0
# if we don't have room, we can use the truncate hook to make more space.
View
21 test/test_storage.rb
@@ -1,4 +1,5 @@
#! /usr/bin/ruby
+# coding: utf-8
$: << File.dirname(__FILE__) + '/../lib'
#require 'rubygems'
@@ -216,5 +217,25 @@ def test_create_dirent
assert_raises(ArgumentError) { Ole::Storage::Dirent.new ole, :type => :bogus }
end
end
+
+ def test_write_utf8_string
+ tmp_file = "#{TEST_DIR}/encoding.tmp"
+ begin
+ Ole::Storage.open File.open(tmp_file, 'wb+') do |ole|
+ ole.file.open '1', 'w' do |writer|
+ writer.write("programação ")
+ writer.write("ruby")
+ end
+ end
+
+ Ole::Storage.open File.open(tmp_file, 'rb') do |ole|
+ ole.file.open '1', 'r' do |reader|
+ assert_equal("programação ruby", reader.read.force_encoding('UTF-8'))
+ end
+ end
+ ensure
+ FileUtils.rm(tmp_file) if File.exist?(tmp_file)
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.