Merged in remove-LocationStrBytes (pull request #118)

Get rid of LocationStrBytes func

Approved-by: kortschak <dan@kortschak.io>
This commit is contained in:
Saxon Milton 2019-01-21 11:40:02 +00:00
commit 1ec23badcc
2 changed files with 25 additions and 28 deletions

View File

@ -99,14 +99,6 @@ func LocationFrom(p []byte) (g string, err error) {
return g, nil
}
// LocationStrBytes take a string of location data and converts to a 32 byte slice -
// easy update of slice representation of a pmt with location descriptor
func LocationStrBytes(s string) []byte {
var b [LocationDataSize]byte
copy(b[:], s)
return b[:]
}
// UpdateLocation takes a byte slice representation of a psi-pmt containing a location
// descriptor and attempts to update the location data value with the passed string.
// If the psi does not contain a location descriptor, and error is returned.
@ -114,8 +106,12 @@ func UpdateLocation(d []byte, s string) error {
if !HasLocation(d) {
return errors.New("pmt does not location descriptor, cannot update")
}
gb := LocationStrBytes(s)
copy(d[LocationDataIndx:LocationDataIndx+LocationDataSize], gb)
loc := d[LocationDataIndx : LocationDataIndx+LocationDataSize]
n := copy(loc, s)
loc = loc[n:]
for i := range loc {
loc[i] = 0
}
updateCrc(d)
return nil
}

View File

@ -79,7 +79,7 @@ var (
// standardPmtTimeLocation is a standard PMT with time and location
// descriptors, but time and location fields zeroed out.
standardPmtTimeLocation = PSI{
standardPmtWithMeta = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
@ -142,7 +142,7 @@ var (
// Parts to construct bytes of pmt with time and bytes
var (
pmtTimeLocationBytesPart1 = []byte{
pmtWithMetaHead = []byte{
0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a,
TimeDescTag, // Descriptor tag for timestamp
TimeDataSize, // Length of bytes to follow
@ -150,7 +150,7 @@ var (
LocationDescTag, // Descriptor tag for location
LocationDataSize, // Length of bytes to follow
}
pmtTimeLocationBytesPart2 = []byte{
pmtWithMetaTail = []byte{
0x1b, 0xe1, 0x00, 0xf0, 0x00,
}
)
@ -175,10 +175,10 @@ var (
}
// Bytes representing pmt with time1 and location1
pmtTimeLocationBytes1 = buildPmtTimeLocationBytes(locationTstStr1)
pmtWithMetaTst1 = buildPmtWithMeta(locationTstStr1)
// bytes representing pmt with with time1 and location 2
pmtTimeLocationBytes2 = buildPmtTimeLocationBytes(locationTstStr2)
pmtWithMetaTst2 = buildPmtWithMeta(locationTstStr2)
)
// bytesTests contains data for testing the Bytes() funcs for the PSI data struct
@ -262,7 +262,7 @@ var bytesTests = []struct {
{
Dt: LocationDescTag,
Dl: LocationDataSize,
Dd: LocationStrBytes(locationTstStr1),
Dd: []byte(locationTstStr1),
},
},
Essd: &ESSD{
@ -273,7 +273,7 @@ var bytesTests = []struct {
},
},
},
want: buildPmtTimeLocationBytes(locationTstStr1),
want: buildPmtWithMeta(locationTstStr1),
},
}
@ -325,7 +325,7 @@ func TestTimeGet(t *testing.T) {
// TestLocationGet checks that we can correctly get location data from a pmt table
func TestLocationGet(t *testing.T) {
pb := standardPmtTimeLocation.Bytes()
pb := standardPmtWithMeta.Bytes()
err := UpdateLocation(pb, locationTstStr1)
if err != nil {
t.Errorf("Error for TestLocationGet UpdateLocation(pb, locationTstStr1): %v", err)
@ -341,16 +341,16 @@ func TestLocationGet(t *testing.T) {
// TestLocationUpdate checks to see if we can update the location string in a pmt correctly
func TestLocationUpdate(t *testing.T) {
cpy := make([]byte, len(pmtTimeLocationBytes1))
copy(cpy, pmtTimeLocationBytes1)
cpy := make([]byte, len(pmtWithMetaTst1))
copy(cpy, pmtWithMetaTst1)
cpy = addCrc(cpy)
err := UpdateLocation(cpy, locationTstStr2)
cpy = cpy[:len(cpy)-4]
if err != nil {
t.Errorf("Update time returned err: %v", err)
}
if !bytes.Equal(pmtTimeLocationBytes2, cpy) {
t.Errorf(errCmp, "TestLocationUpdate", pmtTimeLocationBytes2, cpy)
if !bytes.Equal(pmtWithMetaTst2, cpy) {
t.Errorf(errCmp, "TestLocationUpdate", pmtWithMetaTst2, cpy)
}
}
@ -363,10 +363,11 @@ func TestTrim(t *testing.T) {
}
}
// buildPmtTimeLocationBytes is a helper function to help construct the byte slices
// for pmts with time and location, as the location data field is 32 bytes, i.e. quite large
// to type out
func buildPmtTimeLocationBytes(tstStr string) []byte {
return append(append(append(make([]byte, 0), pmtTimeLocationBytesPart1...),
LocationStrBytes(tstStr)...), pmtTimeLocationBytesPart2...)
// buildPmtTimeLocationBytes returns a PMT with time and location from s.
func buildPmtWithMeta(tstStr string) []byte {
dst := make([]byte, len(pmtWithMetaHead)+32+len(pmtWithMetaTail))
copy(dst, pmtWithMetaHead)
copy(dst[len(pmtWithMetaHead):], tstStr)
copy(dst[len(pmtWithMetaHead)+32:], pmtWithMetaTail)
return dst
}