• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ghettovoice / abnf / 18412738443

10 Oct 2025 04:41PM UTC coverage: 70.728% (-16.9%) from 87.642%
18412738443

push

github

ghettovoice
Fix test action to use new coverage file

1428 of 2019 relevant lines covered (70.73%)

50.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.53
/node.go
1
package abnf
2

3
import (
4
        "bytes"
5
        "sync"
6
)
7

8
// Node represents a single node in a tree generated by [Operator].
9
type Node struct {
10
        Key      string
11
        Pos      uint
12
        Value    []byte
13
        Children Nodes
14
}
15

16
// String returns the node's value as string.
17
func (n *Node) String() string {
×
18
        if n == nil {
×
19
                return ""
×
20
        }
×
21
        return string(n.Value)
×
22
}
23

24
// Len returns length of the node's value.
25
func (n *Node) Len() int {
42✔
26
        if n == nil {
42✔
27
                return 0
×
28
        }
×
29
        return len(n.Value)
42✔
30
}
31

32
// IsEmpty returns true if the node's value length = 0.
33
func (n *Node) IsEmpty() bool { return n == nil || len(n.Value) == 0 }
×
34

35
// Contains returns whether the subtree contains the given key.
36
func (n *Node) Contains(key string) bool {
×
37
        if n == nil {
×
38
                return false
×
39
        }
×
40
        return n.GetNode(key) != nil
×
41
}
42

43
// GetNode recursively searches a node with the given key starting from itself.
44
// Returns found node or nil if not found.
45
func (n *Node) GetNode(key string) *Node {
3✔
46
        if n.Key == key {
4✔
47
                return n
1✔
48
        }
1✔
49
        return n.Children.Get(key)
2✔
50
}
51

52
// GetNodes recursively searches all nodes with the given key starting from itself.
53
func (n *Node) GetNodes(key string) Nodes {
×
54
        if n == nil {
×
55
                return nil
×
56
        }
×
57

58
        var ns Nodes
×
59
        if n.Key == key {
×
60
                ns = append(ns, n)
×
61
        }
×
62
        ns = append(ns, n.Children.GetAll(key)...)
×
63
        return ns
×
64
}
65

66
// Compare compares node values via [bytes.Compare].
67
// The result will be 0 if n.Value == other.Value, -1 if n.Value < other.Value, and +1 if n.Value > other.Value.
68
func (n *Node) Compare(other *Node) int {
32✔
69
        if n == other {
32✔
70
                return 0
×
71
        } else if n == nil {
32✔
72
                return -1
×
73
        } else if other == nil {
32✔
74
                return 1
×
75
        }
×
76
        return bytes.Compare(n.Value, other.Value)
32✔
77
}
78

79
// Nodes represents a list of nodes.
80
type Nodes []*Node
81

82
// Contains returns whether the subtree contains the given key.
83
func (ns Nodes) Contains(key string) bool {
×
84
        for _, n := range ns {
×
85
                if n.Key == key || n.Children.Contains(key) {
×
86
                        return true
×
87
                }
×
88
        }
89
        return false
×
90
}
91

92
// Get recursively searches a node with the given key.
93
func (ns Nodes) Get(key string) *Node {
6✔
94
        for _, n := range ns {
11✔
95
                if n.Key == key {
6✔
96
                        return n
1✔
97
                }
1✔
98
                if n := n.Children.Get(key); n != nil {
4✔
99
                        return n
×
100
                }
×
101
        }
102
        return nil
5✔
103
}
104

105
// GetAll recursively searches all nodes with the given key.
106
func (ns Nodes) GetAll(key string) Nodes {
×
107
        var nodes Nodes
×
108
        for _, n := range ns {
×
109
                if n.Key == key {
×
110
                        nodes = append(nodes, n)
×
111
                }
×
112
                nodes = append(nodes, n.Children.GetAll(key)...)
×
113
        }
114
        return nodes
×
115
}
116

117
// Best returns a node with the longest value or nil if the list is empty.
118
func (ns Nodes) Best() *Node {
70✔
119
        if len(ns) == 0 {
70✔
120
                return nil
×
121
        }
×
122
        best := ns[0]
70✔
123
        for _, n := range ns[1:] {
91✔
124
                if n.Len() > best.Len() {
22✔
125
                        best = n
1✔
126
                }
1✔
127
        }
128
        return best
70✔
129
}
130

131
// Compare compares two best nodes.
132
// The result will be 0 if a == b, -1 if a < b, and +1 if a > b where a - self best node, b - other best node.
133
func (ns Nodes) Compare(other Nodes) int {
32✔
134
        return ns.Best().Compare(other.Best())
32✔
135
}
32✔
136

137
var nodesPool = sync.Pool{
138
        New: func() any {
63✔
139
                ns := make(Nodes, 0, 10)
63✔
140
                return &ns
63✔
141
        },
63✔
142
}
143

144
func newNodes() Nodes {
283✔
145
        ns := nodesPool.Get().(*Nodes)
283✔
146
        ns.clear()
283✔
147
        return *ns
283✔
148
}
283✔
149

150
func (ns *Nodes) clear() {
822✔
151
        clear(*ns)
822✔
152
        *ns = (*ns)[:0]
822✔
153
}
822✔
154

155
func (ns *Nodes) free() {
224✔
156
        ns.clear()
224✔
157
        if cap(*ns) > 1000 {
224✔
158
                return
×
159
        }
×
160
        nodesPool.Put(ns)
224✔
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc