summaryrefslogtreecommitdiffstats
path: root/termui/linegraph.go
diff options
context:
space:
mode:
Diffstat (limited to 'termui/linegraph.go')
-rw-r--r--termui/linegraph.go82
1 files changed, 44 insertions, 38 deletions
diff --git a/termui/linegraph.go b/termui/linegraph.go
index 941183f..17ec1ae 100644
--- a/termui/linegraph.go
+++ b/termui/linegraph.go
@@ -171,57 +171,63 @@ type numbered []string
func (n numbered) Len() int { return len(n) }
func (n numbered) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
+
func (n numbered) Less(i, j int) bool {
- a := n[i]
- b := n[j]
- for i := 0; i < len(a); i++ {
- ac := a[i]
- if unicode.IsDigit(rune(ac)) {
- j := i + 1
- for ; j < len(a); j++ {
- if !unicode.IsDigit(rune(a[j])) {
- break
- }
- if j >= len(b) {
- return false
- }
- if !unicode.IsDigit(rune(b[j])) {
- return false
- }
- }
- an, err := strconv.Atoi(a[i:j])
- if err != nil {
+ ars := n[i]
+ brs := n[j]
+ // iterate over the runes in string A
+ var ai int
+ for ai = 0; ai < len(ars); ai++ {
+ ar := ars[ai]
+ // if brs has been equal to ars so far, but is shorter, than brs is less
+ if ai >= len(brs) {
+ return false
+ }
+ br := brs[ai]
+ // handle numbers if we find them
+ if unicode.IsDigit(rune(ar)) {
+ // if ar is a digit and br isn't, then ars is less
+ if !unicode.IsDigit(rune(brs[ai])) {
return true
}
- if j > len(b) {
- return false
+ // now get the range for the full numbers, which is the sequence of consecutive digits from each
+ ae := ai + 1
+ be := ae
+ for ; ae < len(ars) && unicode.IsDigit(rune(ars[ae])); ae++ {
}
- for ; j < len(b); j++ {
- if !unicode.IsDigit(rune(b[j])) {
- break
- }
+ for ; be < len(brs) && unicode.IsDigit(rune(brs[be])); be++ {
}
- bn, err := strconv.Atoi(b[i:j])
+ if be < ae {
+ return false
+ }
+ adigs, err := strconv.Atoi(string(ars[ai:ae]))
if err != nil {
return true
}
- if an < bn {
+ bdigs, err := strconv.Atoi(string(brs[ai:be]))
+ if err != nil {
return true
- } else if bn < an {
- return false
}
- i = j
- }
- if i >= len(a) {
- return true
- } else if i >= len(b) {
+ // The numbers are equal; continue with the rest of the string
+ if adigs == bdigs {
+ ai = ae - 1
+ continue
+ }
+ return adigs < bdigs
+ } else if unicode.IsDigit(rune(br)) {
return false
}
- if ac < b[i] {
+ if ar == br {
+ continue
+ }
+ // No digits, so compare letters
+ if ar < br {
return true
- } else if b[i] < ac {
- return false
}
+ return false
+ }
+ if ai <= len(brs) {
+ return true
}
- return true
+ return false
}