Permalink
Browse files

add support for writing unicode content strings

Ruby 1.9 changed String#length to report the number of logical
characters instead of the number of bytes, so use String#bytesize.
  • Loading branch information...
1 parent ad3f0c9 commit 67cc05d5a54075a66c9a86ee7bffb76fd15014a9 @sunaku committed Feb 25, 2011
Showing with 32 additions and 10 deletions.
  1. +1 −1 INSTALL
  2. +20 −3 lib/rumai/ixp/message.rb
  3. +3 −3 lib/rumai/ixp/transport.rb
  4. +8 −3 test/rumai/ixp/message_test.rb
View
@@ -2,7 +2,7 @@
=== Prerequisites
-* <<Ruby>> 1.8.6 or newer.
+* <<Ruby>> 1.8.7 or newer.
* <<RubyGems>> 1.3.6 or newer.
View
@@ -234,7 +234,13 @@ def value_from_9p stream
#
module CounterField
def to_9p field_values
- value_to_9p field_values[@countee.name].length
+ value = field_values[@countee.name]
+ count =
+ case value
+ when String then value.bytesize
+ else value.length
+ end
+ value_to_9p count
end
end
@@ -401,7 +407,7 @@ class Fcall
#
def to_9p
data = type.to_9p(1) << super
- size = (data.length + 4).to_9p(4)
+ size = (data.bytesize + 4).to_9p(4)
size << data
end
@@ -683,7 +689,8 @@ class String
# Transforms this object into a string of 9P2000 bytes.
#
def to_9p
- length.to_9p(2) << self[0, Rumai::IXP::BYTE2_MASK]
+ count = [bytesize, Rumai::IXP::BYTE2_MASK].min
+ count.to_9p(2) << byteslice(0, count)
end
##
@@ -693,6 +700,16 @@ def to_9p
def self.from_9p stream
stream.read stream.read_9p(2)
end
+
+ unless method_defined? :byteslice
+ ##
+ # Does the same thing as String#slice but
+ # operates on bytes instead of characters.
+ #
+ def byteslice(*args)
+ unpack('C*').slice(*args).pack('C*')
+ end
+ end
end
class Time
@@ -327,15 +327,15 @@ def write content
raise 'cannot write to a directory' if @stat.directory?
data = content.to_s
- limit = data.length + @pos
+ limit = data.bytesize + @pos
while @pos < limit
- chunk = data[@pos, @msize]
+ chunk = data.byteslice(@pos, @msize)
req = Twrite.new(
:fid => @fid,
:offset => @pos,
- :count => chunk.length,
+ :count => chunk.bytesize,
:data => chunk
)
rsp = @agent.talk(req)
@@ -169,21 +169,22 @@
end
D "write to #{file.inspect}" do
+ message = "\u{266A} hello world \u{266B}"
write_request, write_response = talk(Twrite,
:tag => 0,
:fid => 1,
:offset => 0,
:data => (
require 'rumai/wm'
if Rumai::Barlet::SPLIT_FILE_FORMAT
- "colors #000000 #000000 #000000\nlabel hello world!!!"
+ "colors #000000 #000000 #000000\nlabel #{message}"
else
- "#000000 #000000 #000000 hello world!!!"
+ "#000000 #000000 #000000 #{message}"
end
)
)
T write_response.type == Rwrite.type
- T write_response.count == write_request.data.length
+ T write_response.count == write_request.data.bytesize
D "verify the write" do
read_request, read_response = talk(Tread,
@@ -193,6 +194,10 @@
:count => write_response.count
)
T read_response.type == Rread.type
+
+ # wmii sends the response in ASCII-8BIT whereas we requested in UTF-8
+ read_response.data.force_encoding message.encoding
+
T read_response.data == write_request.data
end
end

0 comments on commit 67cc05d

Please sign in to comment.