From 8db7d0c620be2b4284588693e1b0471f1317968f Mon Sep 17 00:00:00 2001 From: Adam Shelly Date: Fri, 29 Feb 2008 03:51:58 -0500 Subject: [PATCH] Updating Array#pack to pass new specs * added 'Q' and 'q' * used :to_int instead of :to_i where needed --- kernel/core/array.rb | 27 ++++++++++++++++++++++----- 1 files changed, 22 insertions(+), 5 deletions(-) diff --git a/kernel/core/array.rb b/kernel/core/array.rb index d3f092e..46c9375 100644 --- a/kernel/core/array.rb +++ b/kernel/core/array.rb @@ -1060,7 +1060,7 @@ class Array arr_idx += 1 # BER compressed integer elsif kind == "w" - item = Type.coerce_to(item, Integer, :to_i) + item = Type.coerce_to(item, Integer, :to_int) raise ArgumentError, "can't compress negative numbers" if item < 0 ret << (item & 0x7f) while (item >>= 7) > 0 @@ -1098,7 +1098,7 @@ class Array ret << lines.join arr_idx += 1 elsif kind =~ /i|s|l/i - item = Type.coerce_to(item, Integer, :to_i) + item = Type.coerce_to(item, Integer, :to_int) # Either convert to short, integer, or long # If _ is passed (like s_) then we use the native representation. # Otherwise, use the platform independent version @@ -1171,7 +1171,7 @@ class Array raise ArgumentError, "too few array elements" if arr_idx + count > self.length count.times do - item = Type.coerce_to(self[arr_idx], Integer, :to_i) + item = Type.coerce_to(self[arr_idx], Integer, :to_int) raise RangeError, "pack(U): value out of range" if item < 0 #handle the simple case and move on if item < 0x80 @@ -1202,8 +1202,25 @@ class Array end #catch the highest bits - the mask depends on the byte count ret[-bytes] = (item | ((0x3F00>>bytes)) & 0xFC) - end - arr_idx += 1 + end + arr_idx += 1 + end + elsif kind =~ /Q|q/ + #64 bits + #converts the number passed, or all for * or 1 if missing + count = !t ? 1 : (t == "*" ? self.size-arr_idx : t.to_i) + raise ArgumentError, "too few array elements" if arr_idx + count > self.length + + count.times do |i| + item = Type.coerce_to(self[arr_idx], Integer, :to_int) + #store lsB first + 8.times do + ret<< (item & 0xFF) + item >>= 8 + end + #if there are still bits set, number was too big. + raise RangeError, "bignum too big to convert into `quad int'" if item>0 + arr_idx += 1 end else raise ArgumentError, "Unknown kind #{kind}" -- 1.5.2.5