Skip to content

Commit

Permalink
Adds clarifying comments to statements.Less()
Browse files Browse the repository at this point in the history
  • Loading branch information
tomnomnom committed Jun 30, 2017
1 parent 4b59306 commit c463f7e
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions statements.go
Expand Up @@ -155,7 +155,11 @@ func (ss statements) toInterface() (interface{}, error) {
// Implements a natural sort to keep array indexes in order
func (ss statements) Less(a, b int) bool {

diffStart := -1
// ss[a] and ss[b] are both slices of tokens. The first
// thing we need to do is find the first token (if any)
// that differs, then we can use that token to decide
// if ss[a] or ss[b] should come first in the sort.
diffIndex := -1
for i := range ss[a] {

if len(ss[b]) < i+1 {
Expand All @@ -170,19 +174,19 @@ func (ss statements) Less(a, b int) bool {
}

// We've found a difference
diffStart = i
diffIndex = i
break
}

// If diffStart is still -1 then the only difference must be
// that string B is longer than A, so A should come first
if diffStart == -1 {
// If diffIndex is still -1 then the only difference must be
// that ss[b] is longer than ss[a], so ss[a] should come first
if diffIndex == -1 {
return true
}

// Get the tokens that differ
ta := ss[a][diffStart]
tb := ss[b][diffStart]
ta := ss[a][diffIndex]
tb := ss[b][diffIndex]

// An equals always comes first
if ta.typ == typEquals {
Expand Down

0 comments on commit c463f7e

Please sign in to comment.