Don't use Array#pack when the result is constant.
[ruby-eet.git] / lib / eet.rb
index 8a9400f9c28efa31b86702ab3e7b119e27368e2d..266a9e3b86572224c9d18d5f7713b43172a00b92 100644 (file)
@@ -1,5 +1,5 @@
 #--
-# $Id: eet.rb 3 2005-03-26 19:59:05Z tilman $
+# $Id: eet.rb 40 2005-05-18 19:04:52Z tilman $
 #
 # Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
 #
@@ -46,7 +46,7 @@ class Object
 
                props.each_pair do |tag, arg|
                        unless arg.is_a?(Array)
-                               raise(Eet::PropertyError, "hash value not an array")
+                               raise(Eet::PropertyError, "hash value is not an array")
                        end
 
                        value, type = arg
@@ -124,13 +124,13 @@ end
 
 class TrueClass # :nodoc:
        def to_eet_chunks(tag, type = nil)
-               [Eet::Chunk.new(tag, [1].pack("c"))]
+               [Eet::Chunk.new(tag, "\1")]
        end
 end
 
 class FalseClass # :nodoc:
        def to_eet_chunks(tag, type = nil)
-               [Eet::Chunk.new(tag, [0].pack("c"))]
+               [Eet::Chunk.new(tag, "\0")]
        end
 end
 
@@ -154,22 +154,18 @@ class Hash # :nodoc:
 end
 
 module Eet
-       VERSION = "0.1.0"
+       VERSION = "0.1.2"
 
        class EetError < StandardError; end
        class NameError < EetError; end
        class PropertyError < EetError; end
        class ChunkError < EetError; end
 
-       class Stream < Array # :nodoc:
+       class Stream # :nodoc:
                def initialize(chunk = nil)
                        super(chunk.nil? ? 0 : 1, chunk)
                end
 
-               def serialize
-                       inject("") { |a, c| a << c.serialize }
-               end
-
                def Stream.deserialize(data)
                        data = data.to_str.dup
                        s = Stream.new
@@ -194,21 +190,13 @@ module Eet
                        @tag = tag.to_str.dup.freeze
                        @data = data.to_str.dup.freeze
 
-                       @size = @tag.length + 1 + @data.length
-
                        # libeet uses a signed 32bit integer to store the
                        # chunk size, so make sure we don't overflow it
-                       if @size >= (1 << 31)
+                       if (@tag.length + 1 + @data.length) >= (1 << 31)
                                raise(ArgumentError, "tag or data too long")
                        end
                end
 
-               def serialize
-                       buf = "CHnK"
-                       buf << [@size].pack("V")
-                       buf << @tag << "\0" << @data
-               end
-
                def Chunk.deserialize(data)
                        if data.length < 8 || data[0, 4] != "CHnK"
                                raise(ChunkError, "invalid data")