Support more version numbers: -alphaX, -betaX, and -rcX
now all work, and result in properly sorted integer version numbers. Added tests.
This commit is contained in:
@@ -251,6 +251,7 @@
|
||||
#include "utils/log.hpp"
|
||||
#include "utils/mini_glm.hpp"
|
||||
#include "utils/profiler.hpp"
|
||||
#include "utils/string_utils.hpp"
|
||||
#include "utils/translation.hpp"
|
||||
|
||||
static void cleanSuperTuxKart();
|
||||
@@ -2421,6 +2422,8 @@ void runUnitTests()
|
||||
NetworkString::unitTesting();
|
||||
Log::info("UnitTest", "TransportAddress");
|
||||
TransportAddress::unitTesting();
|
||||
Log::info("UnitTest", "StringUtils::versionToInt");
|
||||
StringUtils::unitTesting();
|
||||
|
||||
Log::info("UnitTest", "Easter detection");
|
||||
// Test easter mode: in 2015 Easter is 5th of April - check with 0 days
|
||||
|
||||
@@ -821,6 +821,32 @@ namespace StringUtils
|
||||
return utf8ToWide(input.c_str());
|
||||
} // utf8ToWide
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
/** This functions tests if the string s contains "-WORDX", where
|
||||
* word is the parameter, and X is a one digit integer number. If
|
||||
* the string is found, it is removed from s, pre-release gets the
|
||||
* value of X, and the function returns true. If the string is not
|
||||
* found, the return value is false, and nothing is changed.
|
||||
* \param s The string in which to search for WORD. Example:
|
||||
* checkForStringNumber("10-alpha2", "alpha", &x) will return true,
|
||||
* set s to "10", and x to 2.
|
||||
* \param word The word to search for.
|
||||
* \param pre-release An integer pointer in which to store the result.
|
||||
*/
|
||||
bool checkForStringNumber(std::string *s, const std::string &word,
|
||||
int *pre_release)
|
||||
{
|
||||
size_t n = s->find(std::string("-")+word);
|
||||
if (n == std::string::npos) return false;
|
||||
|
||||
std::string word_string = std::string("-") + word + "%d";
|
||||
if (sscanf(s->substr(n).c_str(), word_string.c_str(), pre_release) == 1)
|
||||
{
|
||||
s->erase(n);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} // checkForStringNumber
|
||||
// ------------------------------------------------------------------------
|
||||
/** Converts a version string (in the form of 'X.Y.Za-rcU' into an
|
||||
* integer number.
|
||||
@@ -832,43 +858,50 @@ namespace StringUtils
|
||||
if(version_string=="GIT" || version_string=="git")
|
||||
{
|
||||
// GIT version will be version 99.99.99i-rcJ
|
||||
return 1000000*99
|
||||
+ 10000*99
|
||||
+ 100*99
|
||||
+ 10* 9
|
||||
+ 9;
|
||||
return 10000000*99
|
||||
+ 100000*99
|
||||
+ 1000*99
|
||||
+ 100* 9
|
||||
+ 99;
|
||||
}
|
||||
|
||||
std::string s=version_string;
|
||||
std::vector<std::string> l = StringUtils::split(version_string, '.');
|
||||
std::string s = l.back(); // the string that might contain alpha,...
|
||||
while (l.size() < 3)
|
||||
l.push_back("0");
|
||||
|
||||
// To guarantee that a release gets a higher version number than
|
||||
// a release candidate, we assign a 'release_candidate' number
|
||||
// of 9 to versions which are not a RC. We assert that any RC
|
||||
// is less than 9 to guarantee the ordering.
|
||||
int release_candidate=9;
|
||||
if(s.length()>4 && sscanf(s.substr(s.length()-4, 4).c_str(), "-rc%d",
|
||||
&release_candidate)==1)
|
||||
// an alpha, beta or release candidate, we assign a 'pre_release' number
|
||||
// of 99 to versions which are not alpha/beta/RC. An alpha version
|
||||
// gets the number 01 till 09; beta 11 till 19; and RC 21 till 29
|
||||
int pre_release=99;
|
||||
if(checkForStringNumber(&s, "alpha", &pre_release))
|
||||
{
|
||||
s = s.substr(0, s.length()-4);
|
||||
// Otherwise a RC can get a higher version number than
|
||||
// the corresponding release! If this should ever get
|
||||
// triggered, multiply all scaling factors above and
|
||||
// below by 10, to get two digits for RC numbers.
|
||||
assert(release_candidate<9);
|
||||
assert(pre_release <= 9 && pre_release >0);
|
||||
// Nothing to do, pre_release is between 1 and 9
|
||||
}
|
||||
else if(checkForStringNumber(&s, "beta", &pre_release))
|
||||
{
|
||||
assert(pre_release <= 9 && pre_release > 0);
|
||||
pre_release += 10;
|
||||
}
|
||||
else if (checkForStringNumber(&s, "rc", &pre_release))
|
||||
{
|
||||
assert(pre_release <= 9 && pre_release > 0);
|
||||
pre_release += 20;
|
||||
}
|
||||
|
||||
int very_minor=0;
|
||||
if(s.length()>0 && s[s.size()-1]>='a' && s[s.size()-1]<='z')
|
||||
{
|
||||
very_minor = s[s.size()-1]-'a'+1;
|
||||
s = s.substr(0, s.size()-1);
|
||||
}
|
||||
std::vector<std::string> l = StringUtils::split(s, '.');
|
||||
while(l.size()<3)
|
||||
l.push_back("0");
|
||||
int version = 1000000*atoi(l[0].c_str())
|
||||
+ 10000*atoi(l[1].c_str())
|
||||
+ 100*atoi(l[2].c_str())
|
||||
+ 10*very_minor
|
||||
+ release_candidate;
|
||||
int version = 10000000*atoi(l[0].c_str())
|
||||
+ 100000*atoi(l[1].c_str())
|
||||
+ 1000*atoi(l[2].c_str())
|
||||
+ 100*very_minor
|
||||
+ pre_release;
|
||||
|
||||
if(version <= 0)
|
||||
Log::error("StringUtils", "Invalid version string '%s'.", s.c_str());
|
||||
@@ -1064,6 +1097,26 @@ namespace StringUtils
|
||||
#endif
|
||||
} // partOfLongUnicodeChar
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
/** At the moment only versionToInt is tested.
|
||||
*/
|
||||
void unitTesting()
|
||||
{
|
||||
assert(versionToInt("git" ) == 999999999);
|
||||
assert(versionToInt("12.34.56-alpha1" ) == 123456001); // alphaX = 0X
|
||||
assert(versionToInt("12.34.56-beta2" ) == 123456012); // betaX = 1X
|
||||
assert(versionToInt("12.34.56-rc3" ) == 123456023); // rcX = 2X
|
||||
assert(versionToInt("12.34.56" ) == 123456099); // release = 99
|
||||
assert(versionToInt("12.34.56a-alpha4") == 123456104);
|
||||
assert(versionToInt("12.34.56b-beta5" ) == 123456215);
|
||||
assert(versionToInt("12.34.56c-rc6" ) == 123456326);
|
||||
assert(versionToInt("12.34.56d" ) == 123456499);
|
||||
assert(versionToInt("1-alpha7" ) == 10000007);
|
||||
assert(versionToInt("1-beta8" ) == 10000018);
|
||||
assert(versionToInt("1-rc9" ) == 10000029);
|
||||
assert(versionToInt("1.0-rc1" ) == 10000021); // same as 1-rc1
|
||||
} // unitTesting
|
||||
|
||||
} // namespace StringUtils
|
||||
|
||||
|
||||
|
||||
@@ -33,7 +33,8 @@
|
||||
|
||||
namespace StringUtils
|
||||
{
|
||||
int versionToInt(const std::string &s);
|
||||
void unitTesting();
|
||||
int versionToInt(const std::string &s);
|
||||
|
||||
bool hasSuffix(const std::string& lhs, const std::string &rhs);
|
||||
bool startsWith(const std::string& str, const std::string& prefix);
|
||||
|
||||
Reference in New Issue
Block a user