fce8c48c30
5 funciones Go puras del dominio core: parse_version y compare_versions para parsing y comparacion semantica de versiones, longest_common_prefix para encontrar el prefijo comun mas largo entre strings, rel_or_full para devolver rutas relativas cuando es posible, y split_command_and_arg para separar comandos de sus argumentos. Todas sin dependencias externas.
37 lines
621 B
Go
37 lines
621 B
Go
package core
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// CompareVersions compares two semver strings (e.g. "v1.4.0", "1.2.3").
|
|
// Returns -1 if a < b, 0 if equal, 1 if a > b.
|
|
func CompareVersions(a, b string) int {
|
|
pa := parseVersionParts(a)
|
|
pb := parseVersionParts(b)
|
|
for i := 0; i < 3; i++ {
|
|
if pa[i] < pb[i] {
|
|
return -1
|
|
}
|
|
if pa[i] > pb[i] {
|
|
return 1
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func parseVersionParts(v string) [3]int {
|
|
v = strings.TrimPrefix(v, "v")
|
|
parts := strings.SplitN(v, ".", 3)
|
|
var nums [3]int
|
|
for i, p := range parts {
|
|
if i >= 3 {
|
|
break
|
|
}
|
|
n, _ := strconv.Atoi(p)
|
|
nums[i] = n
|
|
}
|
|
return nums
|
|
}
|