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>
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>
The subsliceOffset method incorrectly computed offset as
len(p.data) - len(b), which only works when b is a suffix (tail) of
p.data. However, error highlights (ParserError.Highlight) are arbitrary
subslices from the middle of the input (e.g., b[0:1] from parseSimpleKey),
so their len has no relationship to their position.
This was a regression introduced in commit 3aaf147 (Remove unsafe package
usage) which replaced danger.SubsliceOffset (pointer arithmetic) with the
incorrect len-based approach.
Fix by using reflect.ValueOf().Pointer() to compute the actual byte
offset between slice data pointers, matching the approach already used
in errors.go:subsliceOffset.
Fixes#1047
Co-authored-by: Thomas Pelletier <thomas@pelletier.dev>
When parsing a key without '=' at EOF (e.g., "a = 1\nb = 2\nc"), the
error highlight was an empty slice, causing subsliceOffset to return 0
and the error to point at line 1 instead of line 3. Pass the consumed
key bytes as the highlight instead of the empty remainder.
Fixes#1032https://claude.ai/code/session_01UWv8pyc8P1ktAPfHpveixj
Co-authored-by: Claude <noreply@anthropic.com>
Removes all unsafe operations from go-toml, making the codebase
fully safe Go code. The internal/danger package that contained
unsafe operations has been deleted.
Changes:
- Replace pointer-based node navigation with index-based navigation
- Node.next and Node.child now store absolute indices into the
backing nodes slice instead of relative offsets
- Add nodes pointer to Node and Iterator for safe navigation
- Replace danger.TypeID with reflect.Type for cache keys
- Delete internal/danger package entirely
Performance overhead is under 10% compared to the unsafe version,
which is acceptable for the safety and maintainability benefits.
[Cursor][claude-sonnet-4-20250514]
As recommended, an `internal/assert` package was added with a reduced set of assertions. All tests were then refactored to use the internal assertions. When more complex assertions were used, they have been rewritten using logic and the simplified assertions.
Fancy formatting for failures was omitted. The `internal/assert/assertions.diff` function could be overwritten for better formatting. That is where diff libraries are used in other test suites.
Refs: #872
Co-authored-by: Alex Mikitik <alex.mikitik@oracle.com>
* Benchmark script
* Rewrite unmarshaler using the stack
Instead of tracking the build chain using `target`s, use the stack
instead.
Working and most benchmarks look good, but regression on structs unmarshalling.
~60% slower on ReferenceFile/struct.
* Shortcut to check if last node of iterator
* Remove unecessary pointer allocation
* Skip over unused keys without marking them as seen
* Add some tests
* Fix mktemp on macos