From 5941ac8cfefe31b54200e0397046d30da164cf67 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Tue, 24 Nov 2015 11:40:48 +0100 Subject: [PATCH] Update github.com/skarademir/naturalsort dependency Signed-off-by: David Gageot --- Godeps/Godeps.json | 2 +- .../skarademir/naturalsort/README.md | 16 ++++--- .../skarademir/naturalsort/naturalsort.go | 45 ++++++++++++++----- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 26b0424a44..30121d0f7b 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -95,7 +95,7 @@ }, { "ImportPath": "github.com/skarademir/naturalsort", - "Rev": "983d4d86054d80f91fd04dd62ec52c1d078ce403" + "Rev": "69a5d87bef620f77ee8508db30c846b3b84b111e" }, { "ImportPath": "github.com/smartystreets/go-aws-auth", diff --git a/vendor/github.com/skarademir/naturalsort/README.md b/vendor/github.com/skarademir/naturalsort/README.md index 89523853a3..60307fc049 100644 --- a/vendor/github.com/skarademir/naturalsort/README.md +++ b/vendor/github.com/skarademir/naturalsort/README.md @@ -1,16 +1,22 @@ # naturalsort -A simple natural sorter for Go Strings +A simple natural string sorter for Go. + ##Usage Implements the `sort.Interface` -called by `sort.Sort(NaturalSort([]string)` +called by `sort.Sort(NaturalSort([]string))` ###Example -`SampleStringArray := []string{"z24", "z2", "z15", "z1", + +```go +SampleStringArray := []string{ + "z24", "z2", "z15", "z1", "z3", "z20", "z5", "z11", "z 21", "z22"} - sort.Sort(NaturalSort(SampleStringArray))` +sort.Sort(NaturalSort(SampleStringArray)) +``` + ##Needless Description Inspired by [Jeff Atwood's seminal blog post](http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/) and structured similarly to [Ian Griffiths' C# implementation](http://www.interact-sw.co.uk/iangblog/2007/12/13/natural-sorting). This uses a regex to split the numeric and non-numeric portions of the string into a chunky array. Next, the left and right sides' -chunks are compared either by string comparrison (if either is a non-numeric chunk), or by integer (if both chunks are numeric) +chunks are compared either by string comparrison (if either chunk is a non-numeric), or by ~~integer (if both chunks are numeric)~~ a character-by-character iterative function that compares numerical strings diff --git a/vendor/github.com/skarademir/naturalsort/naturalsort.go b/vendor/github.com/skarademir/naturalsort/naturalsort.go index 68709efc06..23dd16d163 100644 --- a/vendor/github.com/skarademir/naturalsort/naturalsort.go +++ b/vendor/github.com/skarademir/naturalsort/naturalsort.go @@ -2,12 +2,13 @@ package naturalsort import ( "regexp" - "strconv" "strings" ) type NaturalSort []string +var r = regexp.MustCompile(`[^0-9]+|[0-9]+`) + func (s NaturalSort) Len() int { return len(s) } @@ -15,25 +16,45 @@ func (s NaturalSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s NaturalSort) Less(i, j int) bool { - r := regexp.MustCompilePOSIX(`^([^0-9]*)+|[0-9]+`) spliti := r.FindAllString(strings.Replace(s[i], " ", "", -1), -1) splitj := r.FindAllString(strings.Replace(s[j], " ", "", -1), -1) - - splitshortest := len(spliti) - if len(spliti) > len(splitj) { - splitshortest = len(splitj) - } - for index := 0; index < splitshortest; index ++{ + + for index := 0; index < len(spliti) && index < len(splitj); index++ { if spliti[index] != splitj[index] { - inti, ei := strconv.Atoi(spliti[index]) - intj, ej := strconv.Atoi(splitj[index]) - if ei == nil && ej == nil { //if number - return inti < intj + // Both slices are numbers + if isNumber(spliti[index][0]) && isNumber(splitj[index][0]) { + // Remove Leading Zeroes + stringi := strings.TrimLeft(spliti[index], "0") + stringj := strings.TrimLeft(splitj[index], "0") + if len(stringi) == len(stringj) { + for indexchar := 0; indexchar < len(stringi); indexchar++ { + if stringi[indexchar] != stringj[indexchar] { + return stringi[indexchar] < stringj[indexchar] + } + } + return len(spliti[index]) < len(splitj[index]) + } + return len(stringi) < len(stringj) } + // One of the slices is a number (we give precedence to numbers regardless of ASCII table position) + if isNumber(spliti[index][0]) || isNumber(splitj[index][0]) { + return isNumber(spliti[index][0]) + } + // Both slices are not numbers return spliti[index] < splitj[index] } } + // Fall back for cases where space characters have been annihliated by the replacment call + // Here we iterate over the unmolsested string and prioritize numbers over + for index := 0; index < len(s[i]) && index < len(s[j]); index++ { + if isNumber(s[i][index]) || isNumber(s[j][index]) { + return isNumber(s[i][index]) + } + } return s[i] < s[j] } +func isNumber(input uint8) bool { + return input >= '0' && input <= '9' +}