zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

bit_string.ex (1325B)


      1 defmodule Postgrex.Extensions.BitString do
      2   @moduledoc false
      3   import Postgrex.BinaryUtils, warn: false
      4   use Postgrex.BinaryExtension, send: "bit_send", send: "varbit_send"
      5 
      6   def init(opts), do: Keyword.fetch!(opts, :decode_binary)
      7 
      8   def encode(_) do
      9     quote location: :keep, generated: true do
     10       val when is_binary(val) ->
     11         [<<byte_size(val) + 4::int32(), bit_size(val)::uint32()>> | val]
     12 
     13       val when is_bitstring(val) ->
     14         bin_size = byte_size(val)
     15         last_pos = bin_size - 1
     16         <<binary::binary-size(last_pos), last::bits>> = val
     17         pad = 8 - bit_size(last)
     18         bit_count = bit_size(val)
     19 
     20         [
     21           <<bin_size + 4::int32(), bit_count::uint32()>>,
     22           binary
     23           | <<last::bits, 0::size(pad)>>
     24         ]
     25 
     26       val ->
     27         raise DBConnection.EncodeError, Postgrex.Utils.encode_msg(val, "a bitstring")
     28     end
     29   end
     30 
     31   def decode(:copy) do
     32     quote location: :keep do
     33       <<len::int32(), value::binary-size(len)>> ->
     34         copy = :binary.copy(value)
     35         <<len::unsigned-32, bits::bits-size(len), _::bits>> = copy
     36         bits
     37     end
     38   end
     39 
     40   def decode(:reference) do
     41     quote location: :keep do
     42       <<len::int32(), value::binary-size(len)>> ->
     43         <<len::int32(), bits::bits-size(len), _::bits>> = value
     44         bits
     45     end
     46   end
     47 end