Created
March 27, 2016 01:50
-
-
Save xigang/6c2f26caf32e568a03b5 to your computer and use it in GitHub Desktop.
比较两个字符串的版本号大小
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func VersionOrdinal(version string) string { | |
| // ISO/IEC 14651:2011 | |
| const maxByte = 1<<8 - 1 | |
| vo := make([]byte, 0, len(version)+8) | |
| j := -1 | |
| for i := 0; i < len(version); i++ { | |
| b := version[i] | |
| if '0' > b || b > '9' { | |
| vo = append(vo, b) | |
| j = -1 | |
| continue | |
| } | |
| if j == -1 { | |
| vo = append(vo, 0x00) | |
| j = len(vo) - 1 | |
| } | |
| if vo[j] == 1 && vo[j+1] == '0' { | |
| vo[j+1] = b | |
| continue | |
| } | |
| if vo[j]+1 > maxByte { | |
| panic("VersionOrdinal: invalid version") | |
| } | |
| vo = append(vo, b) | |
| vo[j]++ | |
| } | |
| return string(vo) | |
| } | |
| func main() { | |
| versions := []struct{ a, b string }{ | |
| {"1.05.00.0156", "1.0.221.9289"}, | |
| // Go versions | |
| {"1", "1.0.1"}, | |
| {"1.0.1", "1.0.2"}, | |
| {"1.0.2", "1.0.3"}, | |
| {"1.0.3", "1.1"}, | |
| {"1.1", "1.1.1"}, | |
| {"1.1.1", "1.1.2"}, | |
| {"1.1.2", "1.2"}, | |
| } | |
| for _, version := range versions { | |
| a, b := VersionOrdinal(version.a), VersionOrdinal(version.b) | |
| switch { | |
| case a > b: | |
| fmt.Println(version.a, ">", version.b) | |
| case a < b: | |
| fmt.Println(version.a, "<", version.b) | |
| case a == b: | |
| fmt.Println(version.a, "=", version.b) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment