Skip to content

Commit

Permalink
Use 8bit bytesize for length
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed May 15, 2011
1 parent a639a9d commit b839f8d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/otnetstring.rb
Expand Up @@ -24,7 +24,7 @@ def self.parse(io)


def self.encode(obj, string_sep = ',') def self.encode(obj, string_sep = ',')
case obj case obj
when String then "#{obj.length}#{string_sep}#{obj}" when String then "#{obj.bytesize}#{string_sep}#{obj}"
when Integer then encode(obj.inspect, '#') when Integer then encode(obj.inspect, '#')
when NilClass then "0~" when NilClass then "0~"
when Array then encode(obj.map { |e| encode(e) }.join, '[') when Array then encode(obj.map { |e| encode(e) }.join, '[')
Expand Down
5 changes: 5 additions & 0 deletions spec/otnetstring_spec.rb
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Based on tnetstring-rb's spec/tnetstring_spec.rb # Based on tnetstring-rb's spec/tnetstring_spec.rb
# #
# Copyright (c) 2011 Matt Yoho # Copyright (c) 2011 Matt Yoho
Expand Down Expand Up @@ -70,6 +71,10 @@
OTNetstring.encode("hello world").should == "11,hello world" OTNetstring.encode("hello world").should == "11,hello world"
end end


it "encodes a multibyte string" do
OTNetstring.encode("☃").should == "3,☃"
end

context "boolean" do context "boolean" do
it "encodes true as 'true'" do it "encodes true as 'true'" do
OTNetstring.encode(true).should == "4!true" OTNetstring.encode(true).should == "4!true"
Expand Down

0 comments on commit b839f8d

Please sign in to comment.