I really wonder why people strive for such complicated solutions when there is sscanf
in C. Here is a very simple solution to that problem that will work for 99% of all use cases:
int compVersions ( const char * version1, const char * version2 ) {
unsigned major1 = 0, minor1 = 0, bugfix1 = 0;
unsigned major2 = 0, minor2 = 0, bugfix2 = 0;
sscanf(version1, "%u.%u.%u", &major1, &minor1, &bugfix1);
sscanf(version2, "%u.%u.%u", &major2, &minor2, &bugfix2);
if (major1 < major2) return -1;
if (major1 > major2) return 1;
if (minor1 < minor2) return -1;
if (minor1 > minor2) return 1;
if (bugfix1 < bugfix2) return -1;
if (bugfix1 > bugfix2) return 1;
return 0;
}
Here, give it a try:
https://ideone.com/bxCjsb
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…