Merge branch 'master' into 0.10-beta

This commit is contained in:
auria.mg
2019-01-08 21:13:13 -05:00
4 changed files with 108 additions and 33 deletions

View File

@@ -604,8 +604,9 @@ PowerupManager::PowerupType PowerupManager::getRandomPowerup(unsigned int pos,
*n=1;
// Prevents early explosive items
if (stk_config->ticks2Time(World::getWorld()->getTicksSinceStart()) <
stk_config->m_no_explosive_items_timeout)
if (World::getWorld() &&
stk_config->ticks2Time(World::getWorld()->getTicksSinceStart()) <
stk_config->m_no_explosive_items_timeout)
{
if (powerup == POWERUP_CAKE || powerup == POWERUP_RUBBERBALL)
powerup = POWERUP_BOWLING;

View File

@@ -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

View File

@@ -821,6 +821,36 @@ 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.
* Example:
* std::string version_with_suffix = "10-alpha2";
* checkForStringNumber(&version_with-suffix, "alpha", &x)
* will set version_with_suffix to "10", x to 2, and return true.
* \param version_with_suffix The string in which to search for WORD.
* \param word The word to search for.
* \param pre-release An integer pointer in which to store the result.
*/
bool checkForStringNumber(std::string *version_with_suffix, const std::string &word,
int *pre_release)
{
// First check if the word string is contained:
size_t pos = version_with_suffix->find(std::string("-")+word);
if (pos == std::string::npos) return false;
std::string word_string = std::string("-") + word + "%d";
if (sscanf(version_with_suffix->substr(pos).c_str(),
word_string.c_str(), pre_release ) == 1)
{
version_with_suffix->erase(pos); // Erase the suffix (till end)
return true;
}
return false;
} // checkForStringNumber
// ------------------------------------------------------------------------
/** Converts a version string (in the form of 'X.Y.Za-rcU' into an
* integer number.
@@ -832,46 +862,66 @@ 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> version_parts
= StringUtils::split(version_string, '.');
// The string that might contain alpha, etc details
std::string version_with_suffix = version_parts.back();
// Fill up to a 3 digit number
while (version_parts.size() < 3)
version_parts.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(&version_with_suffix, "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(&version_with_suffix, "beta", &pre_release))
{
assert(pre_release <= 9 && pre_release > 0);
pre_release += 10;
}
else if (checkForStringNumber(&version_with_suffix, "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')
if(version_with_suffix.length()>0 &&
version_with_suffix.back() >= 'a' &&
version_with_suffix.back() <= 'z' )
{
very_minor = s[s.size()-1]-'a'+1;
s = s.substr(0, s.size()-1);
very_minor = version_with_suffix[version_with_suffix.size()-1]-'a'+1;
// Remove suffix character
version_with_suffix.erase(version_with_suffix.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;
// This relies on the fact that atoi will stop at a non-digit:
// E.g. if the version is '1.0-rc1', the 'rc1' is converted and
// stored in pre_release, but the version_parts[1] string still
// contains the '-rc1' suffix.
int version = 10000000*atoi(version_parts[0].c_str())
+ 100000*atoi(version_parts[1].c_str())
+ 1000*atoi(version_parts[2].c_str())
+ 100*very_minor
+ pre_release;
if(version <= 0)
Log::error("StringUtils", "Invalid version string '%s'.", s.c_str());
Log::error("StringUtils", "Invalid version string '%s'.", version_with_suffix.c_str());
return version;
} // versionToInt
@@ -1064,6 +1114,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

View File

@@ -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);