Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit 6b95269

Browse files
committed
Clean up tag key/value parsing
Addresses adrianmo#59 (comment)
1 parent 12c2343 commit 6b95269

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

tagblock.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type TagBlock struct {
2525
}
2626

2727
func parseInt64(raw string) (int64, error) {
28-
i, err := strconv.ParseInt(raw[2:], 10, 64)
28+
i, err := strconv.ParseInt(raw, 10, 64)
2929
if err != nil {
3030
return 0, fmt.Errorf("nmea: tagblock unable to parse uint64 [%s]", raw)
3131
}
@@ -64,33 +64,35 @@ func parseTagBlock(raw string) (TagBlock, string, error) {
6464

6565
items := strings.Split(tags[:sumSepIndex], ",")
6666
for _, item := range items {
67-
if len(item) == 0 {
67+
parts := strings.SplitN(item, ":", 2)
68+
if len(parts) != 2 {
6869
continue
6970
}
70-
switch item[:1] {
71+
key, value := parts[0], parts[1]
72+
switch key {
7173
case "c": // UNIX timestamp
72-
tagBlock.Time, err = parseInt64(item)
74+
tagBlock.Time, err = parseInt64(value)
7375
if err != nil {
7476
return tagBlock, raw, err
7577
}
7678
case "d": // Destination ID
77-
tagBlock.Destination = item[2:]
79+
tagBlock.Destination = value
7880
case "g": // Grouping
79-
tagBlock.Grouping = item[2:]
81+
tagBlock.Grouping = value
8082
case "n": // Line count
81-
tagBlock.LineCount, err = parseInt64(item)
83+
tagBlock.LineCount, err = parseInt64(value)
8284
if err != nil {
8385
return tagBlock, raw, err
8486
}
8587
case "r": // Relative time
86-
tagBlock.RelativeTime, err = parseInt64(item)
88+
tagBlock.RelativeTime, err = parseInt64(value)
8789
if err != nil {
8890
return tagBlock, raw, err
8991
}
9092
case "s": // Source ID
91-
tagBlock.Source = item[2:]
93+
tagBlock.Source = value
9294
case "t": // Text string
93-
tagBlock.Text = item[2:]
95+
tagBlock.Text = value
9496
}
9597
}
9698
return tagBlock, raw, nil

tagblock_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,19 @@ var tagblocktests = []struct {
107107

108108
name: "Test invalid timestamp",
109109
raw: "UdPbC?\\s:satelite,c:gjadslkg*30\\!AIVDM,1,1,,A,19NSRaP02A0fo91kwnaMKbjR08:J,0*15",
110-
err: "nmea: tagblock unable to parse uint64 [c:gjadslkg]",
110+
err: "nmea: tagblock unable to parse uint64 [gjadslkg]",
111111
},
112112
{
113113

114114
name: "Test invalid linecount",
115115
raw: "UdPbC?\\s:satelite,n:gjadslkg*3D\\!AIVDM,1,1,,A,19NSRaP02A0fo91kwnaMKbjR08:J,0*15",
116-
err: "nmea: tagblock unable to parse uint64 [n:gjadslkg]",
116+
err: "nmea: tagblock unable to parse uint64 [gjadslkg]",
117117
},
118118
{
119119

120120
name: "Test invalid relative time",
121121
raw: "UdPbC?\\s:satelite,r:gjadslkg*21\\!AIVDM,1,1,,A,19NSRaP02A0fo91kwnaMKbjR08:J,0*15",
122-
err: "nmea: tagblock unable to parse uint64 [r:gjadslkg]",
122+
err: "nmea: tagblock unable to parse uint64 [gjadslkg]",
123123
},
124124
}
125125

0 commit comments

Comments
 (0)