Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 978c8b5

Browse files
committed
SetTags - Replace SplitN calls with explicit tokenization
1 parent 16444cb commit 978c8b5

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

api/models/series.go

+34-7
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,46 @@ type Series struct {
2626
}
2727

2828
func (s *Series) SetTags() {
29-
tagSplits := strings.Split(s.Target, ";")
29+
numTags := strings.Count(s.Target, ";")
30+
31+
if s.Tags == nil {
32+
// +1 for the name tag
33+
s.Tags = make(map[string]string, numTags)
34+
} else {
35+
for k := range s.Tags {
36+
delete(s.Tags, k)
37+
}
38+
}
39+
40+
if numTags == 0 {
41+
s.Tags["name"] = s.Target
42+
return
43+
}
44+
45+
index := strings.IndexByte(s.Target, ';')
46+
name := s.Target[0:index]
3047

31-
s.Tags = make(map[string]string, len(tagSplits))
48+
remainder := s.Target
49+
for index > 0 {
50+
remainder = remainder[index+1:]
51+
index = strings.IndexByte(remainder, ';')
3252

33-
for _, tagPair := range tagSplits[1:] {
34-
parts := strings.SplitN(tagPair, "=", 2)
35-
if len(parts) != 2 {
53+
tagPair := remainder
54+
if index > 0 {
55+
tagPair = remainder[:index]
56+
}
57+
58+
equalsPos := strings.IndexByte(tagPair, '=')
59+
if equalsPos < 1 {
3660
// Shouldn't happen
3761
continue
3862
}
39-
s.Tags[parts[0]] = parts[1]
63+
64+
s.Tags[tagPair[0:equalsPos]] = tagPair[equalsPos+1:]
4065
}
41-
s.Tags["name"] = tagSplits[0]
66+
67+
// Do this last to overwrite any invalid "name" tag that might be preset
68+
s.Tags["name"] = name
4269
}
4370

4471
func (s Series) Copy(emptyDatapoints []schema.Point) Series {

0 commit comments

Comments
 (0)