Browse Source

ᛜ Added some core util functions ᛜ

main
Unbewohnte 3 years ago
parent
commit
f1cc35635c
  1. 6
      util/bits.go
  2. 14
      util/bits_test.go
  3. 36
      util/conversion.go
  4. 26
      util/conversion_test.go

6
util/bits.go

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

14
util/bits_test.go

@ -0,0 +1,14 @@
package util
import "testing"
func TestIsSet(t *testing.T) {
testBytes := []byte{1 << 0, 1 << 1, 1 << 2, 1 << 3}
for index, testByte := range testBytes {
if !IsSet(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))
}
}
}

36
util/conversion.go

@ -7,8 +7,7 @@ import (
) )
// got the logic from: https://github.com/bogem/id3v2 , thank you very much. // got the logic from: https://github.com/bogem/id3v2 , thank you very much.
const first7BitsMask = uint32(254 << 24) // shifting 11111110 to the end of uint32
const first7BitsMask = uint32(254) << 24 // shifting 11111110 to the end of uint32
// Converts given bytes into integer // Converts given bytes into integer
func BytesToInt(gBytes []byte) uint32 { func BytesToInt(gBytes []byte) uint32 {
@ -32,19 +31,30 @@ func BytesToIntSynchsafe(gBytes []byte) uint32 {
return integer return integer
} }
// The exact opposite of what `BytesToIntIgnoreFirstBit` does // The exact opposite of what `BytesToIntSynchsafe` does
func SynchsafeIntToBytes(gInt uint32) []byte { // Finally understood with the help of: https://github.com/bogem/id3v2/blob/master/size.go ,
bytes := make([]byte, 32) // thank you very much !
func IntToBytesSynchsafe(gInt uint32) []byte {
synchsafeIBytes := make([]byte, 4)
// looping 4 times (32 bits / 8 bits (4 bytes in int32)) // skip 4 0`ed bits
for i := 0; i < 32; i += 8 { gInt = gInt << 4
gIntCopy := gInt //11010101 11001011 00100000 10111111
first7 := gIntCopy & first7BitsMask //11010100 00000000 00000000 00000000 // int32 == 4 bytes
shifted := first7 >> 25 //00000000 00000000 00000000 01101010 for i := 0; i < 32/8; i++ {
bytes = append(bytes, byte(shifted)) // get first 7 bits
} first7Bits := gInt & first7BitsMask
return bytes // shift captured bits to the beginning
first7Bits = first7Bits >> (3*8 + 1)
b := byte(first7Bits)
synchsafeIBytes = append(synchsafeIBytes, b)
// prepare next 7 bits for the next iteration
gInt = gInt << 7
}
return synchsafeIBytes
} }
// Converts given bytes into string, ignoring the first 31 non-printable ASCII characters. // Converts given bytes into string, ignoring the first 31 non-printable ASCII characters.

26
util/conversion_test.go

@ -25,13 +25,23 @@ func TestDecodeText(t *testing.T) {
} }
} }
// func TestIntToBytesFirstBitZeroed(t *testing.T) { func TestIntToBytesSynchsafe(t *testing.T) {
// var testint uint32 = 123456 testInts := []uint32{
1234,
12,
1,
0,
99999,
87654321,
}
for _, testInt := range testInts {
synchSafeBytes := IntToBytesSynchsafe(testInt)
// intbytes := IntToBytesFirstBitZeroed(testint) synchsafeInt := BytesToIntSynchsafe(synchSafeBytes)
// if BytesToIntIgnoreFirstBit(intbytes) != testint { if synchsafeInt != testInt {
// t.Errorf("IntToBytesFirstBitZeroed failed: expected to get %v; got %v", t.Errorf("BytesToIntSynchsafe failed: expected to get %d; got %d", testInt, synchsafeInt)
// testint, BytesToIntIgnoreFirstBit(intbytes)) }
// } }
// } }

Loading…
Cancel
Save