Fix PiRngWrapper, fix starts_with and ends_with like methods.

master
Webster Sheets 2021-04-21 15:24:17 -04:00 committed by Webster Sheets
parent f08f00d334
commit c6adff8a29
2 changed files with 18 additions and 9 deletions

View File

@ -13,11 +13,20 @@
#include "scenegraph/SceneGraph.h"
#include <algorithm>
struct PiRngWrapper {
unsigned int operator()(unsigned int n)
class PiRngWrapper {
public:
PiRngWrapper(size_t maxValue) :
maxVal(maxValue) {}
typedef unsigned int result_type;
static unsigned int min() { return 0; }
static unsigned int max() { return std::numeric_limits<uint32_t>::max(); }
unsigned int operator()()
{
return Pi::rng.Int32(n);
return Pi::rng.Int32(maxVal);
}
private:
const int32_t maxVal;
};
Intro::Intro(Graphics::Renderer *r, int width, int height) :
@ -61,8 +70,7 @@ Intro::Intro(Graphics::Renderer *r, int width, int height) :
m_models.push_back(model);
}
PiRngWrapper rng;
std::random_shuffle(m_models.begin(), m_models.end(), rng);
std::shuffle(m_models.begin(), m_models.end(), PiRngWrapper(m_models.size()));
m_modelIndex = 0;

View File

@ -98,14 +98,15 @@ inline bool starts_with(const std::string_view s, const std::string_view t)
{
if (s.size() < t.size())
return false;
return memcmp(s.begin(), t.begin(), t.size()) == 0;
return memcmp(s.data(), t.data(), t.size()) == 0;
}
inline bool ends_with(const std::string_view s, const std::string_view t)
{
if (s.size() < t.size())
return false;
return memcmp(s.end() - t.size(), t.begin(), t.size()) == 0;
return memcmp(s.data() + (s.size() - t.size()), t.data(), t.size()) == 0;
}
inline bool starts_with_ci(const std::string_view s, const std::string_view t)
@ -125,8 +126,8 @@ inline bool ends_with_ci(const std::string_view s, const std::string_view t)
if (s.size() < t.size())
return false;
for (size_t i = t.size(); i > 0; i--)
if (tolower(s.end()[-i]) != tolower(t.end()[-i]))
for (int64_t i = t.size(); i > 0; i--)
if (tolower(s.data()[s.size() - i]) != tolower(t.data()[t.size() - i]))
return false;
return true;