Remove cap tricks, use address comparison for subslice offset
Replace cap(parent) - cap(subslice) with a straightforward scan that compares element addresses: &data[i] == &subslice[0]. This is well-defined Go pointer comparison on elements of the same backing array, with no dependency on capacity semantics, reflect, or unsafe. The scan is O(n) but only runs on error paths, and TOML documents are small per the project's design constraints. Also remove the Offset field from ParserError and the setErrOffset machinery — the offset is computed at the point of consumption (wrapDecodeError, Parser.Range) rather than cached on the error. Co-authored-by: Thomas Pelletier <thomas@pelletier.dev>
This commit is contained in:
@@ -766,50 +766,6 @@ func TestErrorHighlightPositions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParserError_Offset(t *testing.T) {
|
||||
examples := []struct {
|
||||
desc string
|
||||
input string
|
||||
wantOffset int
|
||||
}{
|
||||
{
|
||||
desc: "error after comment",
|
||||
input: "# comment\n= \"value\"",
|
||||
wantOffset: 10,
|
||||
},
|
||||
{
|
||||
desc: "error on first line",
|
||||
input: "= \"value\"",
|
||||
wantOffset: 0,
|
||||
},
|
||||
{
|
||||
desc: "error after two lines",
|
||||
input: "a = 1\n= \"value\"",
|
||||
wantOffset: 6,
|
||||
},
|
||||
}
|
||||
|
||||
for _, e := range examples {
|
||||
t.Run(e.desc, func(t *testing.T) {
|
||||
p := Parser{}
|
||||
p.Reset([]byte(e.input))
|
||||
for p.NextExpression() {
|
||||
}
|
||||
err := p.Error()
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
var perr *ParserError
|
||||
if !errors.As(err, &perr) {
|
||||
t.Fatalf("expected ParserError, got %T", err)
|
||||
}
|
||||
if perr.Offset != e.wantOffset {
|
||||
t.Errorf("offset: got %d, want %d", perr.Offset, e.wantOffset)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleParser() {
|
||||
doc := `
|
||||
hello = "world"
|
||||
|
||||
Reference in New Issue
Block a user