Skip to content

Commit

Permalink
Merge pull request #61 from cuixin/master
Browse files Browse the repository at this point in the history
add support customize binaryOrder & base64.StdEncoding
  • Loading branch information
Will Fitzgerald authored Jul 17, 2018
2 parents 8ce1146 + 8504b65 commit 95e7f39
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
24 changes: 18 additions & 6 deletions bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ const log2WordSize = uint(6)
// allBits has every bit set
const allBits uint64 = 0xffffffffffffffff

// default binary BigEndian
var binaryOrder binary.ByteOrder = binary.BigEndian

// default json encoding base64.URLEncoding
var base64Encoding *base64.Encoding = base64.URLEncoding

// Marshal/Unmarshal BitSet with base64.StdEncoding(Default: base64.URLEncoding)
func Base64StdEncoding() { base64Encoding = base64.StdEncoding }

// Marshal/Unmarshal Binary as Little Endian(Default: binary.BigEndian)
func LittleEndian() { binaryOrder = binary.LittleEndian }

// A BitSet is a set of bits. The zero value of a BitSet is an empty set of length 0.
type BitSet struct {
length uint
Expand Down Expand Up @@ -667,13 +679,13 @@ func (b *BitSet) WriteTo(stream io.Writer) (int64, error) {
length := uint64(b.length)

// Write length
err := binary.Write(stream, binary.BigEndian, length)
err := binary.Write(stream, binaryOrder, length)
if err != nil {
return 0, err
}

// Write set
err = binary.Write(stream, binary.BigEndian, b.set)
err = binary.Write(stream, binaryOrder, b.set)
return int64(b.BinaryStorageSize()), err
}

Expand All @@ -682,7 +694,7 @@ func (b *BitSet) ReadFrom(stream io.Reader) (int64, error) {
var length uint64

// Read length first
err := binary.Read(stream, binary.BigEndian, &length)
err := binary.Read(stream, binaryOrder, &length)
if err != nil {
return 0, err
}
Expand All @@ -693,7 +705,7 @@ func (b *BitSet) ReadFrom(stream io.Reader) (int64, error) {
}

// Read remaining bytes as set
err = binary.Read(stream, binary.BigEndian, newset.set)
err = binary.Read(stream, binaryOrder, newset.set)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -736,7 +748,7 @@ func (b *BitSet) MarshalJSON() ([]byte, error) {
}

// URLEncode all bytes
return json.Marshal(base64.URLEncoding.EncodeToString(buffer.Bytes()))
return json.Marshal(base64Encoding.EncodeToString(buffer.Bytes()))
}

// UnmarshalJSON unmarshals a BitSet from JSON created using MarshalJSON
Expand All @@ -749,7 +761,7 @@ func (b *BitSet) UnmarshalJSON(data []byte) error {
}

// URLDecode string
buf, err := base64.URLEncoding.DecodeString(s)
buf, err := base64Encoding.DecodeString(s)
if err != nil {
return err
}
Expand Down
37 changes: 37 additions & 0 deletions bitset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,20 @@ func TestMarshalUnmarshalBinary(t *testing.T) {
}
}

func TestMarshalUnmarshalBinaryByLittleEndian(t *testing.T) {
LittleEndian()
a := New(1010).Set(10).Set(1001)
b := new(BitSet)

copyBinary(t, a, b)

// BitSets must be equal after marshalling and unmarshalling
if !a.Equal(b) {
t.Error("Bitsets are not equal:\n\t", a.DumpAsBits(), "\n\t", b.DumpAsBits())
return
}
}

func copyBinary(t *testing.T, from encoding.BinaryMarshaler, to encoding.BinaryUnmarshaler) {
data, err := from.MarshalBinary()
if err != nil {
Expand Down Expand Up @@ -919,6 +933,29 @@ func TestMarshalUnmarshalJSON(t *testing.T) {
}
}

func TestMarshalUnmarshalJSONByStdEncoding(t *testing.T) {
Base64StdEncoding()
a := New(1010).Set(10).Set(1001)
data, err := json.Marshal(a)
if err != nil {
t.Errorf(err.Error())
return
}

b := new(BitSet)
err = json.Unmarshal(data, b)
if err != nil {
t.Errorf(err.Error())
return
}

// Bitsets must be equal after marshalling and unmarshalling
if !a.Equal(b) {
t.Error("Bitsets are not equal:\n\t", a.DumpAsBits(), "\n\t", b.DumpAsBits())
return
}
}

func TestSafeSet(t *testing.T) {
b := new(BitSet)
c := b.safeSet()
Expand Down

0 comments on commit 95e7f39

Please sign in to comment.