cmd/compile/internal/syntax, types2: move cmpPos to pos.Cmp

Make position comparison generally available.

Change-Id: I94b6f658fa19a15b30574dbb2181879115c131a8
Reviewed-on: https://go-review.googlesource.com/c/go/+/307215
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2021-04-05 15:48:32 -07:00
parent 1395432f23
commit 8f1099b585
5 changed files with 47 additions and 41 deletions

View file

@ -59,6 +59,45 @@ func (pos Pos) RelCol() uint {
return pos.Col()
}
// Cmp compares the positions p and q and returns a result r as follows:
//
// r < 0: p is before q
// r == 0: p and q are the same position (but may not be identical)
// r > 0: p is after q
//
// If p and q are in different files, p is before q if the filename
// of p sorts lexicographically before the filename of q.
func (p Pos) Cmp(q Pos) int {
pname := p.RelFilename()
qname := q.RelFilename()
switch {
case pname < qname:
return -1
case pname > qname:
return +1
}
pline := p.Line()
qline := q.Line()
switch {
case pline < qline:
return -1
case pline > qline:
return +1
}
pcol := p.Col()
qcol := q.Col()
switch {
case pcol < qcol:
return -1
case pcol > qcol:
return +1
}
return 0
}
func (pos Pos) String() string {
rel := position_{pos.RelFilename(), pos.RelLine(), pos.RelCol()}
abs := position_{pos.Base().Pos().RelFilename(), pos.Line(), pos.Col()}