Module: Memcached::PackArray
- Defined in:
- lib/remcached/pack_array.rb
Overview
Works exactly like Array#pack and String#unpack, except that it inverts ‘q’ & ‘Q’ prior packing/after unpacking. This is done to achieve network byte order for these values on a little-endian machine.
FIXME: implement check for big-endian machines.
Class Method Summary collapse
Class Method Details
.pack(ary, fmt1) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/remcached/pack_array.rb', line 8 def self.pack(ary, fmt1) fmt2 = '' values = [] fmt1.each_char do |c| if c == 'Q' || c == 'q' fmt2 += 'a8' values << [ary.shift].pack(c).reverse else fmt2 += c values << ary.shift end end values.pack(fmt2) end |
.unpack(buf, fmt1) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/remcached/pack_array.rb', line 24 def self.unpack(buf, fmt1) fmt2 = '' reverse = [] i = 0 fmt1.each_char do |c| if c == 'Q' || c == 'q' fmt2 += 'a8' reverse << [i, c] else fmt2 += c end i += 1 end ary = buf.unpack(fmt2) reverse.each do |i, c| ary[i], = ary[i].reverse.unpack(c) end ary end |