mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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:
parent
1395432f23
commit
8f1099b585
5 changed files with 47 additions and 41 deletions
|
|
@ -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()}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue