Remove optional offset and fallback, guarantee offset by construction

ParserError.Offset is now a plain exported int field, always set:
- The parser sets it via setErrOffset() when capturing parse errors
- strict.go sets it from the key's Raw range at construction
- wrapDecodeError computes it inline from cap(document) - cap(highlight)

This eliminates:
- The SetOffset/Offset() accessor methods and offsetValid flag
- The subsliceOffset fallback function in errors.go
- Any conditional logic around whether the offset is present

The offset is guaranteed by construction at every path that creates
or consumes a ParserError.

Co-authored-by: Thomas Pelletier <thomas@pelletier.dev>
This commit is contained in:
Cursor Agent
2026-04-12 17:25:32 +00:00
parent f7136d052b
commit 96ac48eb74
5 changed files with 22 additions and 50 deletions
+1 -10
View File
@@ -99,10 +99,7 @@ func (e *DecodeError) Key() Key {
//
//nolint:funlen
func wrapDecodeError(document []byte, de *unstable.ParserError) *DecodeError {
offset, ok := de.Offset()
if !ok {
offset = subsliceOffset(document, de.Highlight)
}
offset := cap(document) - cap(de.Highlight)
errMessage := de.Error()
errLine, errColumn := positionAtEnd(document[:offset])
@@ -264,9 +261,3 @@ func positionAtEnd(b []byte) (row int, column int) {
return row, column
}
// subsliceOffset returns the byte offset of subslice within data.
// subslice must share the same backing array as data.
func subsliceOffset(data []byte, subslice []byte) int {
return cap(data) - cap(subslice)
}