Merge pull request #14 from jwetzell/pointers

return pointers from byte parsing
This commit is contained in:
Joel Wetzell
2026-01-31 15:39:51 -06:00
committed by GitHub
4 changed files with 37 additions and 37 deletions
+13 -13
View File
@@ -21,25 +21,25 @@ func (b *OSCBundle) ToBytes() []byte {
return bytes
}
func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
if len(bytes) < 20 {
return OSCBundle{}, bytes, errors.New("bundle has to be at least 20 bytes")
return nil, bytes, errors.New("bundle has to be at least 20 bytes")
}
if bytes[0] != 35 {
return OSCBundle{}, bytes, errors.New("bundle must start with a #")
return nil, bytes, errors.New("bundle must start with a #")
}
bundleHeader, bytesAfterBundleHeader := readOSCString(bytes)
if bundleHeader != "#bundle" {
return OSCBundle{}, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string")
return nil, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string")
}
timeTag, bytesAfterTimeTag, err := readOSCTimeTag(bytesAfterBundleHeader)
if err != nil {
return OSCBundle{}, bytesAfterBundleHeader, err
return nil, bytesAfterBundleHeader, err
}
bundleContents := []OSCPacket{}
@@ -52,13 +52,13 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
contentSize, bytesAfterContentSize, err := readOSCInt32(remainingBytes)
if err != nil {
return OSCBundle{}, remainingBytes, err
return nil, remainingBytes, err
}
remainingBytes = bytesAfterContentSize
if len(remainingBytes) < int(contentSize) {
return OSCBundle{}, remainingBytes, errors.New("bundle doesn't have enough bytes for the content size it specifies")
return nil, remainingBytes, errors.New("bundle doesn't have enough bytes for the content size it specifies")
}
bundleContentBytes := remainingBytes[0:contentSize]
@@ -66,17 +66,17 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
if bundleContentBytes[0] == 35 {
content, _, err := BundleFromBytes(bundleContentBytes)
if err != nil {
return OSCBundle{}, remainingBytes, err
return nil, remainingBytes, err
}
bundleContents = append(bundleContents, &content)
bundleContents = append(bundleContents, content)
} else if bundleContentBytes[0] == 47 {
content, err := MessageFromBytes(bundleContentBytes)
if err != nil {
return OSCBundle{}, remainingBytes, err
return nil, remainingBytes, err
}
bundleContents = append(bundleContents, &content)
bundleContents = append(bundleContents, content)
} else {
return OSCBundle{}, remainingBytes, errors.New("bundle contents does not look a bundle or message")
return nil, remainingBytes, errors.New("bundle contents does not look a bundle or message")
}
remainingBytes = bytesAfterContentSize[contentSize:]
if len(remainingBytes) == 0 {
@@ -85,7 +85,7 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
}
return OSCBundle{
return &OSCBundle{
TimeTag: timeTag,
Contents: bundleContents,
},
+4 -4
View File
@@ -10,12 +10,12 @@ func TestOSCBundleEncoding(t *testing.T) {
testCases := []struct {
description string
bundle OSCBundle
bundle *OSCBundle
expected []byte
}{
{
"simple contents single message",
OSCBundle{
&OSCBundle{
TimeTag: OSCTimeTag{
seconds: 32,
fractionalSeconds: 0,
@@ -46,12 +46,12 @@ func TestOSCBundleEncoding(t *testing.T) {
func TestOSCBundleDecoding(t *testing.T) {
testCases := []struct {
description string
expected OSCBundle
expected *OSCBundle
bytes []byte
}{
{
"simple contents single message",
OSCBundle{
&OSCBundle{
TimeTag: OSCTimeTag{
seconds: 32,
fractionalSeconds: 0,
+6 -6
View File
@@ -25,15 +25,15 @@ func (m *OSCMessage) ToBytes() []byte {
return oscBuffer
}
func MessageFromBytes(bytes []byte) (OSCMessage, error) {
func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
if len(bytes) == 0 {
return OSCMessage{}, errors.New("cannot create OSC Message from empty byte array")
return nil, errors.New("cannot create OSC Message from empty byte array")
}
address, typeAndArgBytes := readOSCString(bytes)
if address[0] != 47 {
return OSCMessage{}, errors.New("OSC Message address must start with /")
return nil, errors.New("OSC Message address must start with /")
}
oscMessage := OSCMessage{
@@ -46,17 +46,17 @@ func MessageFromBytes(bytes []byte) (OSCMessage, error) {
for index, oscType := range typeString {
if index == 0 {
if oscType != ',' {
return OSCMessage{}, errors.New("type string is malformed")
return nil, errors.New("type string is malformed")
}
} else {
oscArg, remainingBytes, error := readOSCArg(argBytes, string(oscType))
if error != nil {
return oscMessage, error
return nil, error
}
argBytes = remainingBytes
oscMessage.Args = append(oscMessage.Args, oscArg)
}
}
return oscMessage, nil
return &oscMessage, nil
}
+14 -14
View File
@@ -11,12 +11,12 @@ func TestOSCMessageEncoding(t *testing.T) {
testCases := []struct {
description string
message OSCMessage
message *OSCMessage
expected []byte
}{
{
"simple hello",
OSCMessage{
&OSCMessage{
Address: "/hello",
Args: []OSCArg{},
},
@@ -24,7 +24,7 @@ func TestOSCMessageEncoding(t *testing.T) {
},
{
"simple address string arg",
OSCMessage{
&OSCMessage{
Address: "/hello",
Args: []OSCArg{
{
@@ -37,47 +37,47 @@ func TestOSCMessageEncoding(t *testing.T) {
},
{
description: "simple address integer arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: 35}}},
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: 35}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35},
},
{
description: "simple address float arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: 34.5}}},
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: 34.5}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0},
},
{
description: "simple address blob arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}},
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98},
},
{
description: "simple address True arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{OSCArg{Type: "T", Value: true}}},
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0},
},
{
description: "simple address False arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}},
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0},
},
{
description: "simple address color arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}},
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10},
},
{
description: "simple address nil arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}},
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0},
},
{
description: "simple address int64 arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}},
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255},
},
{
description: "simple address float64 arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}},
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}},
expected: []byte{
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
},
@@ -101,7 +101,7 @@ func TestOSCMessageEncoding(t *testing.T) {
// },
{
description: "osc 1.0 spec example 1",
message: OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}},
message: &OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}},
expected: []byte{
47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44,
102, 0, 0, 67, 220, 0, 0,
@@ -109,7 +109,7 @@ func TestOSCMessageEncoding(t *testing.T) {
},
{
description: "osc 1.0 spec example 2",
message: OSCMessage{
message: &OSCMessage{
Address: "/foo",
Args: []OSCArg{
{Type: "i", Value: 1000},