]> Cypherpunks repositories - gostls13.git/commitdiff
go/token: simplify fixOffset
authorMateusz Poliwczak <mpoliwczak34@gmail.com>
Fri, 23 Jan 2026 15:12:27 +0000 (16:12 +0100)
committerGopher Robot <gobot@golang.org>
Thu, 29 Jan 2026 21:17:22 +0000 (13:17 -0800)
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>
src/go/token/position.go

index 37017d43742b9414b6411c212cfff84913197419..37a468012dc8383f5691918a2652e26675adaa79 100644 (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.