-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhisuru.go
68 lines (55 loc) · 1.18 KB
/
hisuru.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Copyright 2015 Adrian Stanescu. All rights reserved.
Use of this source code is governed by the MIT License (MIT) that can be found in the LICENSE file.
*/
/*
hisuru
Go program that compares software versions in the x.y.z format
Usage:
x := "1"
y := "1.0.1"
z := "1.0"
fmt.Println(hisuru.Compare(x, y)) // 1 = y
fmt.Println(hisuru.Compare(x, z)) // 0 = equal
fmt.Println(hisuru.Compare(x, a)) // -1 = x
*/
package hisuru
import (
// "os"
"strconv"
"strings"
)
/*
compare two versions in x.y.z form
@param {string} a version string
@param {string} b version string
@return {int} -1 = a is higher, 0 = equal, 1 = b is higher
*/
func Compare(a, b string) (ret int) {
as := strings.Split(a, ".")
bs := strings.Split(b, ".")
loopMax := len(bs)
if len(as) > len(bs) {
loopMax = len(as)
}
for i := 0; i < loopMax; i++ {
var x, y string
if len(as) > i {
x = as[i]
}
if len(bs) > i {
y = bs[i]
}
xi,_ := strconv.Atoi(x)
yi,_ := strconv.Atoi(y)
if xi > yi {
ret = -1
} else if xi < yi {
ret = 1
}
if ret != 0 {
break
}
}
return
}