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 package util
// Tells if bit is set in given byte // 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 return n&byte(1<<bitN-1) != 0
} }

6
util/bits_test.go

@ -2,13 +2,13 @@ package util
import "testing" import "testing"
func TestIsSet(t *testing.T) { func TestGetBit(t *testing.T) {
testBytes := []byte{1 << 0, 1 << 1, 1 << 2, 1 << 3} testBytes := []byte{1 << 0, 1 << 1, 1 << 2, 1 << 3}
for index, testByte := range testBytes { 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", 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 header.Size = framesize
// Flags // Flags
frameFlagsByte1 := fHeaderbytes[8] frameFlags1 := fHeaderbytes[8]
frameFlagsByte2 := fHeaderbytes[9] 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 var flags FrameFlags
if flagsByte1Bits[0] == 1 { if util.GetBit(frameFlags1, 1) {
flags.TagAlterPreservation = true flags.TagAlterPreservation = true
} else { } else {
flags.TagAlterPreservation = false flags.TagAlterPreservation = false
} }
if flagsByte1Bits[1] == 1 { if util.GetBit(frameFlags1, 2) {
flags.FileAlterPreservation = true flags.FileAlterPreservation = true
} else { } else {
flags.FileAlterPreservation = false flags.FileAlterPreservation = false
} }
if flagsByte1Bits[2] == 1 { if util.GetBit(frameFlags1, 3) {
flags.ReadOnly = true flags.ReadOnly = true
} else { } else {
flags.ReadOnly = false flags.ReadOnly = false
} }
if flagsByte2Bits[0] == 1 { if util.GetBit(frameFlags2, 1) {
flags.Compressed = true flags.Compressed = true
} else { } else {
flags.Compressed = false flags.Compressed = false
} }
if flagsByte2Bits[1] == 1 { if util.GetBit(frameFlags2, 1) {
flags.Encrypted = true flags.Encrypted = true
} else { } else {
flags.Encrypted = false flags.Encrypted = false
} }
if flagsByte2Bits[2] == 1 { if util.GetBit(frameFlags2, 1) {
flags.InGroup = true flags.InGroup = true
} else { } else {
flags.InGroup = false flags.InGroup = false
@ -179,7 +175,7 @@ func (f *Frame) Text() string {
} }
// Returns bytes of the frame that can be // Returns bytes of the frame that can be
// written in a file. // written into a file.
// func (f *Frame) Bytes() ([]byte, error) { // func (f *Frame) Bytes() ([]byte, error) {
// header := f.Header // header := f.Header
// contents := f.Contents // contents := f.Contents

32
v2/header.go

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

Loading…
Cancel
Save