Refactor unsafe pointer usage to use reflect.Type and pointers

Remove internal/danger package and replace unsafe pointer arithmetic with direct pointer manipulation. Update AST node references to use pointers instead of integer offsets. This improves code safety and maintainability.

Co-authored-by: thomas.pelletier <thomas.pelletier@bedrockrobotics.com>
This commit is contained in:
Cursor Agent
2026-01-04 03:11:48 +00:00
parent 9702fae9b8
commit f09f77ab06
10 changed files with 44 additions and 306 deletions
+6 -21
View File
@@ -2,9 +2,6 @@ package unstable
import (
"fmt"
"unsafe"
"github.com/pelletier/go-toml/v2/internal/danger"
)
// Iterator over a sequence of nodes.
@@ -37,7 +34,7 @@ func (c *Iterator) Next() bool {
// IsLast returns true if the current node of the iterator is the last
// one. Subsequent calls to Next() will return false.
func (c *Iterator) IsLast() bool {
return c.node.next == 0
return c.node.next == nil
}
// Node returns a pointer to the node pointed at by the iterator.
@@ -65,11 +62,9 @@ type Node struct {
Raw Range // Raw bytes from the input.
Data []byte // Node value (either allocated or referencing the input).
// References to other nodes, as offsets in the backing array
// from this node. References can go backward, so those can be
// negative.
next int // 0 if last element
child int // 0 if no child
// References to other nodes.
next *Node // nil if last element
child *Node // nil if no child
}
// Range of bytes in the document.
@@ -80,24 +75,14 @@ type Range struct {
// Next returns a pointer to the next node, or nil if there is no next node.
func (n *Node) Next() *Node {
if n.next == 0 {
return nil
}
ptr := unsafe.Pointer(n)
size := unsafe.Sizeof(Node{})
return (*Node)(danger.Stride(ptr, size, n.next))
return n.next
}
// Child returns a pointer to the first child node of this node. Other children
// can be accessed calling Next on the first child. Returns nil if this Node
// has no child.
func (n *Node) Child() *Node {
if n.child == 0 {
return nil
}
ptr := unsafe.Pointer(n)
size := unsafe.Sizeof(Node{})
return (*Node)(danger.Stride(ptr, size, n.child))
return n.child
}
// Valid returns true if the node's kind is set (not to Invalid).
+25 -5
View File
@@ -29,8 +29,10 @@ func (r reference) Valid() bool {
}
type builder struct {
tree root
lastIdx int
tree root
lastIdx int
nextOffsets []int
childOffsets []int
}
func (b *builder) Tree() *root {
@@ -43,29 +45,47 @@ func (b *builder) NodeAt(ref reference) *Node {
func (b *builder) Reset() {
b.tree.nodes = b.tree.nodes[:0]
b.nextOffsets = b.nextOffsets[:0]
b.childOffsets = b.childOffsets[:0]
b.lastIdx = 0
}
func (b *builder) Push(n Node) reference {
b.lastIdx = len(b.tree.nodes)
b.tree.nodes = append(b.tree.nodes, n)
b.nextOffsets = append(b.nextOffsets, 0)
b.childOffsets = append(b.childOffsets, 0)
return reference(b.lastIdx)
}
func (b *builder) PushAndChain(n Node) reference {
newIdx := len(b.tree.nodes)
b.tree.nodes = append(b.tree.nodes, n)
b.nextOffsets = append(b.nextOffsets, 0)
b.childOffsets = append(b.childOffsets, 0)
if b.lastIdx >= 0 {
b.tree.nodes[b.lastIdx].next = newIdx - b.lastIdx
b.nextOffsets[b.lastIdx] = newIdx - b.lastIdx
}
b.lastIdx = newIdx
return reference(b.lastIdx)
}
func (b *builder) AttachChild(parent reference, child reference) {
b.tree.nodes[parent].child = int(child) - int(parent)
b.childOffsets[parent] = int(child) - int(parent)
}
func (b *builder) Chain(from reference, to reference) {
b.tree.nodes[from].next = int(to) - int(from)
b.nextOffsets[from] = int(to) - int(from)
}
func (b *builder) Link() {
for i := range b.tree.nodes {
if next := b.nextOffsets[i]; next != 0 {
b.tree.nodes[i].next = &b.tree.nodes[i+next]
}
if child := b.childOffsets[i]; child != 0 {
b.tree.nodes[i].child = &b.tree.nodes[i+child]
}
}
}
+3 -3
View File
@@ -6,7 +6,6 @@ import (
"unicode"
"github.com/pelletier/go-toml/v2/internal/characters"
"github.com/pelletier/go-toml/v2/internal/danger"
)
// ParserError describes an error relative to the content of the document.
@@ -70,7 +69,7 @@ func (p *Parser) Data() []byte {
// panics.
func (p *Parser) Range(b []byte) Range {
return Range{
Offset: uint32(danger.SubsliceOffset(p.data, b)),
Offset: uint32(cap(p.data) - cap(b)),
Length: uint32(len(b)),
}
}
@@ -126,6 +125,7 @@ func (p *Parser) NextExpression() bool {
p.first = false
if p.ref.Valid() {
p.builder.Link()
return true
}
}
@@ -159,7 +159,7 @@ type Shape struct {
}
func (p *Parser) position(b []byte) Position {
offset := danger.SubsliceOffset(p.data, b)
offset := cap(p.data) - cap(b)
lead := p.data[:offset]