Sha256: d5c5eaa77b2e6801a1e7ee38d7516808429f4fb3166507d1cca5725b8aac3a90

Contents?: true

Size: 932 Bytes

Versions: 7

Compression:

Stored size: 932 Bytes

Contents

##
# 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.
module Memcached::PackArray
  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

  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
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
astro-remcached-0.2.0 lib/remcached/pack_array.rb
astro-remcached-0.2.1 lib/remcached/pack_array.rb
astro-remcached-0.2.2 lib/remcached/pack_array.rb
astro-remcached-0.3.0 lib/remcached/pack_array.rb
remcached-0.4.1 lib/remcached/pack_array.rb
remcached-0.4.0 lib/remcached/pack_array.rb
remcached-0.3.1 lib/remcached/pack_array.rb