go/token: simplify fixOffset

Each time I go to definition of this I am deeply confused
at what I am looking, so let's clean this a bit with modern Go.

Change-Id: I8f44e78f0cdde9b970388f9b98a2720e6a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/738341
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Mateusz Poliwczak 2026-01-23 16:12:27 +01:00 committed by Gopher Robot
parent 5593ea4634
commit 48788436b8

View file

@ -276,26 +276,12 @@ func (f *File) AddLineColumnInfo(offset int, filename string, line, column int)
// fixOffset fixes an out-of-bounds offset such that 0 <= offset <= f.size.
func (f *File) fixOffset(offset int) int {
switch {
case offset < 0:
if !debug {
return 0
}
case offset > f.size:
if !debug {
return f.size
}
default:
return offset
}
// only generate this code if needed
if debug {
if debug && !(0 <= offset && offset <= f.size) {
panic(fmt.Sprintf("offset %d out of bounds [%d, %d] (position %d out of bounds [%d, %d])",
0 /* for symmetry */, offset, f.size,
f.base+offset, f.base, f.base+f.size))
}
return 0
return max(min(f.size, offset), 0)
}
// Pos returns the Pos value for the given file offset.