aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/conf
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-05-05 17:38:04 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-05-05 17:38:04 +0200
commit61da208c9571f3348eb0e11400fa54365d49a366 (patch)
treed6384ce455a01a0799d531020d6f7a2532435651 /conf
parentui: attach shortcuts to their windows (diff)
downloadwireguard-windows-61da208c9571f3348eb0e11400fa54365d49a366.tar.xz
wireguard-windows-61da208c9571f3348eb0e11400fa54365d49a366.zip
ui: use natural sorting order
Diffstat (limited to 'conf')
-rw-r--r--conf/name.go61
-rw-r--r--conf/path_windows.go2
2 files changed, 62 insertions, 1 deletions
diff --git a/conf/name.go b/conf/name.go
index 00479c04..87c463af 100644
--- a/conf/name.go
+++ b/conf/name.go
@@ -7,6 +7,7 @@ package conf
import (
"regexp"
+ "strconv"
"strings"
)
@@ -49,3 +50,63 @@ func TunnelNameIsValid(name string) bool {
}
return allowedNameFormat.MatchString(name)
}
+
+type naturalSortToken struct {
+ maybeString string
+ maybeNumber int
+}
+type naturalSortString struct {
+ originalString string
+ tokens []naturalSortToken
+}
+
+var naturalSortDigitFinder = regexp.MustCompile(`\d+|\D+`)
+
+func newNaturalSortString(s string) (t naturalSortString) {
+ t.originalString = s
+ s = strings.ToLower(strings.Join(strings.Fields(s), " "))
+ x := naturalSortDigitFinder.FindAllString(s, -1)
+ t.tokens = make([]naturalSortToken, len(x))
+ for i, s := range x {
+ if n, err := strconv.Atoi(s); err == nil {
+ t.tokens[i].maybeNumber = n
+ } else {
+ t.tokens[i].maybeString = s
+ }
+ }
+ return
+}
+
+func (f1 naturalSortToken) Cmp(f2 naturalSortToken) int {
+ if len(f1.maybeString) == 0 {
+ if len(f2.maybeString) > 0 || f1.maybeNumber < f2.maybeNumber {
+ return -1
+ } else if f1.maybeNumber > f2.maybeNumber {
+ return 1
+ }
+ } else if len(f2.maybeString) == 0 || f1.maybeString > f2.maybeString {
+ return 1
+ } else if f1.maybeString < f2.maybeString {
+ return -1
+ }
+ return 0
+}
+
+func TunnelNameIsLess(a, b string) bool {
+ if a == b {
+ return false
+ }
+ na, nb := newNaturalSortString(a), newNaturalSortString(b)
+ for i, t := range nb.tokens {
+ if i == len(na.tokens) {
+ return true
+ }
+ switch na.tokens[i].Cmp(t) {
+ case -1:
+ return true
+ case 1:
+ return false
+ }
+ }
+ return false
+}
diff --git a/conf/path_windows.go b/conf/path_windows.go
index 96c68738..8b23e399 100644
--- a/conf/path_windows.go
+++ b/conf/path_windows.go
@@ -65,4 +65,4 @@ func RootDirectory() (string, error) {
}
cachedRootDir = c
return cachedRootDir, nil
-} \ No newline at end of file
+}