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 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 // 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. // 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. // 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) { if !HasLocation(d) {
return errors.New("pmt does not location descriptor, cannot update") return errors.New("pmt does not location descriptor, cannot update")
} }
gb := LocationStrBytes(s) loc := d[LocationDataIndx : LocationDataIndx+LocationDataSize]
copy(d[LocationDataIndx:LocationDataIndx+LocationDataSize], gb) n := copy(loc, s)
loc = loc[n:]
for i := range loc {
loc[i] = 0
}
updateCrc(d) updateCrc(d)
return nil return nil
} }

View File

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