Browse Source

ៜmall code-readability improvements

main
Unbewohnte 3 years ago
parent
commit
2a1b44de6c
  1. 2
      util/bits.go
  2. 6
      util/bits_test.go
  3. 22
      v2/frame.go
  4. 32
      v2/header.go

2
util/bits.go

@ -1,6 +1,6 @@
package util
// Tells if bit is set in given byte
func IsSet(n byte, bitN int) bool {
func GetBit(n byte, bitN int) bool {
return n&byte(1<<bitN-1) != 0
}

6
util/bits_test.go

@ -2,13 +2,13 @@ package util
import "testing"
func TestIsSet(t *testing.T) {
func TestGetBit(t *testing.T) {
testBytes := []byte{1 << 0, 1 << 1, 1 << 2, 1 << 3}
for index, testByte := range testBytes {
if !IsSet(testByte, index+1) {
if !GetBit(testByte, index+1) {
t.Errorf("IsSet failed: expected %dth bit of %d to be %v; got %v",
index+1, testByte, true, IsSet(testByte, index+1))
index+1, testByte, true, GetBit(testByte, index+1))
}
}
}

22
v2/frame.go

@ -87,41 +87,37 @@ func getFrameHeader(fHeaderbytes []byte, version string) (FrameHeader, error) {
header.Size = framesize
// Flags
frameFlagsByte1 := fHeaderbytes[8]
frameFlagsByte2 := fHeaderbytes[9]
frameFlags1 := fHeaderbytes[8]
frameFlags2 := fHeaderbytes[9]
// I don`t have enough knowledge to handle this more elegantly
flagsByte1Bits := fmt.Sprintf("%08b", frameFlagsByte1)
flagsByte2Bits := fmt.Sprintf("%08b", frameFlagsByte2)
var flags FrameFlags
if flagsByte1Bits[0] == 1 {
if util.GetBit(frameFlags1, 1) {
flags.TagAlterPreservation = true
} else {
flags.TagAlterPreservation = false
}
if flagsByte1Bits[1] == 1 {
if util.GetBit(frameFlags1, 2) {
flags.FileAlterPreservation = true
} else {
flags.FileAlterPreservation = false
}
if flagsByte1Bits[2] == 1 {
if util.GetBit(frameFlags1, 3) {
flags.ReadOnly = true
} else {
flags.ReadOnly = false
}
if flagsByte2Bits[0] == 1 {
if util.GetBit(frameFlags2, 1) {
flags.Compressed = true
} else {
flags.Compressed = false
}
if flagsByte2Bits[1] == 1 {
if util.GetBit(frameFlags2, 1) {
flags.Encrypted = true
} else {
flags.Encrypted = false
}
if flagsByte2Bits[2] == 1 {
if util.GetBit(frameFlags2, 1) {
flags.InGroup = true
} else {
flags.InGroup = false
@ -179,7 +175,7 @@ func (f *Frame) Text() string {
}
// Returns bytes of the frame that can be
// written in a file.
// written into a file.
// func (f *Frame) Bytes() ([]byte, error) {
// header := f.Header
// contents := f.Contents

32
v2/header.go

@ -72,8 +72,7 @@ func (h *Header) readExtendedHeader(r io.Reader) error {
if err != nil {
return fmt.Errorf("could not read from reader: %s", err)
}
flagbits := fmt.Sprintf("%08b", extendedFlag[0])
if flagbits[0] == 1 {
if util.GetBit(extendedFlag[0], 1) {
extended.Flags.CRCpresent = true
} else {
extended.Flags.CRCpresent = false
@ -90,20 +89,19 @@ func (h *Header) readExtendedHeader(r io.Reader) error {
return fmt.Errorf("could not read from reader: %s", err)
}
flagBits := fmt.Sprintf("%08b", flagByte[0])
if flagBits[1] == 1 {
if util.GetBit(flagByte[0], 2) {
extended.Flags.UpdateTag = true
} else {
extended.Flags.UpdateTag = false
}
if flagBits[2] == 1 {
if util.GetBit(flagByte[0], 3) {
extended.Flags.CRCpresent = true
} else {
extended.Flags.CRCpresent = false
}
if flagBits[3] == 1 {
if util.GetBit(flagByte[0], 4) {
extended.Flags.HasRestrictions = true
} else {
extended.Flags.HasRestrictions = false
@ -195,7 +193,7 @@ func readHeader(rs io.ReadSeeker) (Header, error) {
identifier := hBytes[0:3]
// check if has identifier ID3v2
// check if has ID3v2 identifier
if !bytes.Equal([]byte(HEADERIDENTIFIER), identifier) {
return Header{}, ErrDoesNotUseID3v2
}
@ -219,33 +217,31 @@ func readHeader(rs io.ReadSeeker) (Header, error) {
// flags
flags := hBytes[5]
flagBits := fmt.Sprintf("%08b", flags) // 1 byte is 8 bits
// v3.0 and v4.0 have different amount of flags
switch header.Version {
case V2_2:
if flagBits[0] == 1 {
if util.GetBit(flags, 1) {
header.Flags.Unsynchronisated = true
} else {
header.Flags.Unsynchronisated = false
}
if flagBits[1] == 1 {
if util.GetBit(flags, 2) {
header.Flags.Compressed = true
} else {
header.Flags.Compressed = false
}
case V2_3:
if flagBits[0] == 1 {
if util.GetBit(flags, 1) {
header.Flags.Unsynchronisated = true
} else {
header.Flags.Unsynchronisated = false
}
if flagBits[1] == 1 {
if util.GetBit(flags, 2) {
header.Flags.HasExtendedHeader = true
} else {
header.Flags.HasExtendedHeader = false
}
if flagBits[2] == 1 {
if util.GetBit(flags, 3) {
header.Flags.Experimental = true
} else {
header.Flags.Experimental = false
@ -254,22 +250,22 @@ func readHeader(rs io.ReadSeeker) (Header, error) {
header.Flags.FooterPresent = false
case V2_4:
if flagBits[0] == 1 {
if util.GetBit(flags, 1) {
header.Flags.Unsynchronisated = true
} else {
header.Flags.Unsynchronisated = false
}
if flagBits[1] == 1 {
if util.GetBit(flags, 2) {
header.Flags.HasExtendedHeader = true
} else {
header.Flags.HasExtendedHeader = false
}
if flagBits[2] == 1 {
if util.GetBit(flags, 3) {
header.Flags.Experimental = true
} else {
header.Flags.Experimental = false
}
if flagBits[3] == 1 {
if util.GetBit(flags, 4) {
header.Flags.FooterPresent = true
} else {
header.Flags.FooterPresent = false

Loading…
Cancel
Save