refactor(Core/Misc): add braces and impove codestyle (#6402)

This commit is contained in:
Kargatum
2021-06-25 00:50:18 +07:00
committed by GitHub
parent 33c271cc7c
commit 3c24b511f2
72 changed files with 1486 additions and 401 deletions

View File

@@ -14,7 +14,8 @@
#include <vector>
template <typename T>
class CircularBuffer {
class CircularBuffer
{
public:
explicit CircularBuffer(size_t size) :
buf_(std::unique_ptr<T[]>(new T[size])),
@@ -77,13 +78,15 @@ public:
// the implementation of this function is simplified by the fact that head_ will never be lower than tail_
// when compared to the original implementation of this class
std::vector<T> content() {
std::vector<T> content()
{
std::lock_guard<std::mutex> lock(mutex_);
return std::vector<T>(buf_.get(), buf_.get() + size());
}
T peak_back() {
T peak_back()
{
std::lock_guard<std::mutex> lock(mutex_);
return empty() ? T() : buf_[tail_];

View File

@@ -55,7 +55,9 @@ namespace Acore
void check() const
{
if (!(_buf < _end))
{
throw std::out_of_range("index");
}
}
};
}
@@ -70,7 +72,9 @@ namespace Acore::Containers
static_assert(std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<typename C::iterator>::iterator_category>::value, "Invalid container passed to Acore::Containers::RandomResize");
if (std::size(container) <= requestedSize)
{
return;
}
auto keepIt = std::begin(container), curIt = std::begin(container);
uint32 elementsToKeep = requestedSize, elementsToProcess = std::size(container);
@@ -81,7 +85,9 @@ namespace Acore::Containers
if (urand(1, elementsToProcess) <= elementsToKeep)
{
if (keepIt != curIt)
{
*keepIt = std::move(*curIt);
}
++keepIt;
--elementsToKeep;
@@ -102,7 +108,9 @@ namespace Acore::Containers
std::copy_if(std::begin(container), std::end(container), std::inserter(containerCopy, std::end(containerCopy)), predicate);
if (requestedSize)
{
RandomResize(containerCopy, requestedSize);
}
container = std::move(containerCopy);
}
@@ -160,7 +168,9 @@ namespace Acore::Containers
}
if (weightSum <= 0.0)
{
weights.assign(std::size(container), 1.0);
}
return SelectRandomWeightedContainerElement(container, weights);
}
@@ -195,9 +205,13 @@ namespace Acore::Containers
for (auto itr = range.first; itr != range.second;)
{
if (itr->second == value)
{
itr = multimap.erase(itr);
}
else
{
++itr;
}
}
}

View File

@@ -30,11 +30,15 @@ public:
{
static_assert(std::is_base_of<Base, T>::value, "T must derive from Base");
if (Container.empty())
{
return nullptr;
}
auto it = Container.find(k);
if (it != Container.end())
{
return dynamic_cast<T*>(it->second.get());
}
return nullptr;
}
@@ -47,7 +51,9 @@ public:
{
static_assert(std::is_base_of<Base, T>::value, "T must derive from Base");
if (T* v = Get<T>(k))
{
return v;
}
T* v = new T();
Container.emplace(k, std::unique_ptr<T>(v));
return v;

View File

@@ -64,18 +64,22 @@ void EventProcessor::KillAllEvents(bool force)
delete i_old->second;
if (!force) // need per-element cleanup
{
m_events.erase (i_old);
}
}
}
// fast clear event list (in force case)
if (force)
{
m_events.clear();
}
}
void EventProcessor::AddEvent(BasicEvent* Event, uint64 e_time, bool set_addtime)
{
if (set_addtime) Event->m_addTime = m_time;
if (set_addtime) { Event->m_addTime = m_time; }
Event->m_execTime = e_time;
m_events.insert(std::pair<uint64, BasicEvent*>(e_time, Event));
}

View File

@@ -28,7 +28,7 @@
[[nodiscard]] inline float getSlopeAngle(float startX, float startY, float startZ, float destX, float destY, float destZ)
{
float floorDist = sqrt(pow(startY - destY, 2.0f) + pow(startX - destX,2.0f));
float floorDist = sqrt(pow(startY - destY, 2.0f) + pow(startX - destX, 2.0f));
return atan(abs(destZ - startZ) / abs(floorDist));
}

View File

@@ -20,11 +20,15 @@ inline T standard_deviation(Container&& c)
auto mean = sum / size;
if (size == 1)
{
return (T) 0;
}
T accum = T();
for (const auto d : c)
{
accum += (d - mean) * (d - mean);
}
return std::sqrt(accum / (size - 1));
}
@@ -43,7 +47,8 @@ inline T median(std::vector<T> a)
{
size_t n = a.size();
// If size of the arr[] is even
if (n % 2 == 0) {
if (n % 2 == 0)
{
// Applying nth_element
// on n/2th index
@@ -65,7 +70,8 @@ inline T median(std::vector<T> a)
}
// If size of the arr[] is odd
else {
else
{
// Applying nth_element
// on n/2

View File

@@ -15,7 +15,9 @@ static RandomEngine engine;
static SFMTRand* GetRng()
{
if (!sfmtRand)
{
sfmtRand = std::make_unique<SFMTRand>();
}
return sfmtRand.get();
}
@@ -51,7 +53,7 @@ Milliseconds randtime(Milliseconds min, Milliseconds max)
{
long long diff = max.count() - min.count();
ASSERT(diff >= 0);
ASSERT(diff <= (uint32)-1);
ASSERT(diff <= (uint32) - 1);
return min + Milliseconds(urand(0, diff));
}

View File

@@ -26,7 +26,9 @@ namespace Acore
std::lock_guard lock(_mutex);
if (_handled.find(sig) != _handled.end())
{
return false;
}
_handled.insert(sig);
signal(sig, func);
@@ -36,7 +38,9 @@ namespace Acore
~SignalHandler()
{
for (auto const& sig : _handled)
{
signal(sig, nullptr);
}
}
};
}

View File

@@ -34,87 +34,88 @@ namespace Acore::Impl::EnumUtilsImpl
class EnumUtils
{
public:
template <typename Enum>
static size_t Count() { return Acore::Impl::EnumUtilsImpl::EnumUtils<Enum>::Count(); }
template <typename Enum>
static EnumText ToString(Enum value) { return Acore::Impl::EnumUtilsImpl::EnumUtils<Enum>::ToString(value); }
template <typename Enum>
static Enum FromIndex(size_t index) { return Acore::Impl::EnumUtilsImpl::EnumUtils<Enum>::FromIndex(index); }
template <typename Enum>
static uint32 ToIndex(Enum value) { return Acore::Impl::EnumUtilsImpl::EnumUtils<Enum>::ToIndex(value);}
public:
template <typename Enum>
static size_t Count() { return Acore::Impl::EnumUtilsImpl::EnumUtils<Enum>::Count(); }
template <typename Enum>
static EnumText ToString(Enum value) { return Acore::Impl::EnumUtilsImpl::EnumUtils<Enum>::ToString(value); }
template <typename Enum>
static Enum FromIndex(size_t index) { return Acore::Impl::EnumUtilsImpl::EnumUtils<Enum>::FromIndex(index); }
template <typename Enum>
static uint32 ToIndex(Enum value) { return Acore::Impl::EnumUtilsImpl::EnumUtils<Enum>::ToIndex(value);}
template<typename Enum>
static bool IsValid(Enum value)
template<typename Enum>
static bool IsValid(Enum value)
{
try
{
try
{
Acore::Impl::EnumUtilsImpl::EnumUtils<Enum>::ToIndex(value);
return true;
} catch (...)
{
return false;
}
Acore::Impl::EnumUtilsImpl::EnumUtils<Enum>::ToIndex(value);
return true;
}
template<typename Enum>
static bool IsValid(std::underlying_type_t<Enum> value) { return IsValid(static_cast<Enum>(value)); }
template <typename Enum>
class Iterator
catch (...)
{
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = Enum;
using pointer = Enum*;
using reference = Enum&;
using difference_type = std::ptrdiff_t;
return false;
}
}
Iterator() : _index(EnumUtils::Count<Enum>()) {}
explicit Iterator(size_t index) : _index(index) { }
template<typename Enum>
static bool IsValid(std::underlying_type_t<Enum> value) { return IsValid(static_cast<Enum>(value)); }
bool operator==(const Iterator& other) const { return other._index == _index; }
bool operator!=(const Iterator& other) const { return !operator==(other); }
difference_type operator-(Iterator const& other) const { return _index - other._index; }
bool operator<(const Iterator& other) const { return _index < other._index; }
bool operator<=(const Iterator& other) const { return _index <= other._index; }
bool operator>(const Iterator& other) const { return _index > other._index; }
bool operator>=(const Iterator& other) const { return _index >= other._index; }
template <typename Enum>
class Iterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = Enum;
using pointer = Enum*;
using reference = Enum&;
using difference_type = std::ptrdiff_t;
value_type operator[](difference_type d) const { return FromIndex<Enum>(_index + d); }
value_type operator*() const { return operator[](0); }
Iterator() : _index(EnumUtils::Count<Enum>()) {}
explicit Iterator(size_t index) : _index(index) { }
Iterator& operator+=(difference_type d) { _index += d; return *this; }
Iterator& operator++() { return operator+=(1); }
Iterator operator++(int) { Iterator i = *this; operator++(); return i; }
Iterator operator+(difference_type d) const { Iterator i = *this; i += d; return i; }
bool operator==(const Iterator& other) const { return other._index == _index; }
bool operator!=(const Iterator& other) const { return !operator==(other); }
difference_type operator-(Iterator const& other) const { return _index - other._index; }
bool operator<(const Iterator& other) const { return _index < other._index; }
bool operator<=(const Iterator& other) const { return _index <= other._index; }
bool operator>(const Iterator& other) const { return _index > other._index; }
bool operator>=(const Iterator& other) const { return _index >= other._index; }
Iterator& operator-=(difference_type d) { _index -= d; return *this; }
Iterator& operator--() { return operator-=(1); }
Iterator operator--(int) { Iterator i = *this; operator--(); return i; }
Iterator operator-(difference_type d) const { Iterator i = *this; i -= d; return i; }
value_type operator[](difference_type d) const { return FromIndex<Enum>(_index + d); }
value_type operator*() const { return operator[](0); }
private:
difference_type _index;
};
Iterator& operator+=(difference_type d) { _index += d; return *this; }
Iterator& operator++() { return operator+=(1); }
Iterator operator++(int) { Iterator i = *this; operator++(); return i; }
Iterator operator+(difference_type d) const { Iterator i = *this; i += d; return i; }
template <typename Enum>
static Iterator<Enum> Begin() { return Iterator<Enum>(0); }
Iterator& operator-=(difference_type d) { _index -= d; return *this; }
Iterator& operator--() { return operator-=(1); }
Iterator operator--(int) { Iterator i = *this; operator--(); return i; }
Iterator operator-(difference_type d) const { Iterator i = *this; i -= d; return i; }
template <typename Enum>
static Iterator<Enum> End() { return Iterator<Enum>(); }
private:
difference_type _index;
};
template <typename Enum>
static Acore::IteratorPair<Iterator<Enum>> Iterate() { return { Begin<Enum>(), End<Enum>() }; }
template <typename Enum>
static Iterator<Enum> Begin() { return Iterator<Enum>(0); }
template <typename Enum>
static char const* ToConstant(Enum value) { return ToString(value).Constant; }
template <typename Enum>
static Iterator<Enum> End() { return Iterator<Enum>(); }
template <typename Enum>
static char const* ToTitle(Enum value) { return ToString(value).Title; }
template <typename Enum>
static Acore::IteratorPair<Iterator<Enum>> Iterate() { return { Begin<Enum>(), End<Enum>() }; }
template <typename Enum>
static char const* ToDescription(Enum value) { return ToString(value).Description; }
template <typename Enum>
static char const* ToConstant(Enum value) { return ToString(value).Constant; }
template <typename Enum>
static char const* ToTitle(Enum value) { return ToString(value).Title; }
template <typename Enum>
static char const* ToDescription(Enum value) { return ToString(value).Description; }
};
#endif

View File

@@ -41,10 +41,14 @@ namespace Acore::Impl::StringConvertImpl
str.remove_prefix(2);
}
else
{
base = 10;
}
if (str.empty())
{
return std::nullopt;
}
}
char const* const start = str.data();
@@ -53,14 +57,18 @@ namespace Acore::Impl::StringConvertImpl
T val;
std::from_chars_result const res = std::from_chars(start, end, val, base);
if ((res.ptr == end) && (res.ec == std::errc()))
{
return val;
}
else
{
return std::nullopt;
}
}
static std::string ToString(T val)
{
std::string buf(20,'\0'); /* 2^64 is 20 decimal characters, -(2^63) is 20 including the sign */
std::string buf(20, '\0'); /* 2^64 is 20 decimal characters, -(2^63) is 20 including the sign */
char* const start = buf.data();
char* const end = (start + buf.length());
std::to_chars_result const res = std::to_chars(start, end, val);
@@ -84,13 +92,17 @@ namespace Acore::Impl::StringConvertImpl
static Optional<uint64> FromString(std::string_view str, int base = 10)
{
if (str.empty())
{
return std::nullopt;
}
try
{
size_t n;
uint64 val = std::stoull(std::string(str), &n, base);
if (n != str.length())
{
return std::nullopt;
}
return val;
}
catch (...) { return std::nullopt; }
@@ -107,13 +119,18 @@ namespace Acore::Impl::StringConvertImpl
{
static Optional<int64> FromString(std::string_view str, int base = 10)
{
try {
try
{
if (str.empty())
{
return std::nullopt;
}
size_t n;
int64 val = std::stoll(std::string(str), &n, base);
if (n != str.length())
{
return std::nullopt;
}
return val;
}
catch (...) { return std::nullopt; }
@@ -134,17 +151,25 @@ namespace Acore::Impl::StringConvertImpl
if (strict)
{
if (str == "1")
{
return true;
}
if (str == "0")
{
return false;
}
return std::nullopt;
}
else
{
if ((str == "1") || StringEqualI(str, "y") || StringEqualI(str, "on") || StringEqualI(str, "yes") || StringEqualI(str, "true"))
{
return true;
}
if ((str == "0") || StringEqualI(str, "n") || StringEqualI(str, "off") || StringEqualI(str, "no") || StringEqualI(str, "false"))
{
return false;
}
return std::nullopt;
}
}
@@ -162,7 +187,9 @@ namespace Acore::Impl::StringConvertImpl
static Optional<T> FromString(std::string_view str, std::chars_format fmt = std::chars_format())
{
if (str.empty())
{
return std::nullopt;
}
if (fmt == std::chars_format())
{
@@ -172,10 +199,14 @@ namespace Acore::Impl::StringConvertImpl
str.remove_prefix(2);
}
else
{
fmt = std::chars_format::general;
}
if (str.empty())
{
return std::nullopt;
}
}
char const* const start = str.data();
@@ -184,20 +215,30 @@ namespace Acore::Impl::StringConvertImpl
T val;
std::from_chars_result const res = std::from_chars(start, end, val, fmt);
if ((res.ptr == end) && (res.ec == std::errc()))
{
return val;
}
else
{
return std::nullopt;
}
}
// this allows generic converters for all numeric types (easier templating!)
static Optional<T> FromString(std::string_view str, int base)
{
if (base == 16)
{
return FromString(str, std::chars_format::hex);
}
else if (base == 10)
{
return FromString(str, std::chars_format::general);
}
else
{
return FromString(str, std::chars_format());
}
}
static std::string ToString(T val)
@@ -212,22 +253,31 @@ namespace Acore::Impl::StringConvertImpl
{
static Optional<T> FromString(std::string_view str, int base = 0)
{
try {
try
{
if (str.empty())
{
return std::nullopt;
}
if ((base == 10) && StringEqualI(str.substr(0, 2), "0x"))
{
return std::nullopt;
}
std::string tmp;
if (base == 16)
{
tmp.append("0x");
}
tmp.append(str);
size_t n;
T val = static_cast<T>(std::stold(tmp, &n));
if (n != tmp.length())
{
return std::nullopt;
}
return val;
}
catch (...) { return std::nullopt; }

View File

@@ -13,19 +13,26 @@ Str Acore::String::Trim(const Str& s, const std::locale& loc /*= std::locale()*/
typename Str::const_iterator end = s.end();
while (first != end && std::isspace(*first, loc))
{
++first;
}
if (first == end)
{
return Str();
}
typename Str::const_iterator last = end;
do
{
--last;
while (std::isspace(*last, loc));
} while (std::isspace(*last, loc));
if (first != s.begin() || last + 1 != end)
{
return Str(first, last + 1);
}
return s;
}

View File

@@ -65,7 +65,9 @@ void TaskScheduler::Dispatch(success_t const& callback)
{
// If the validation failed abort the dispatching here.
if (!_predicate())
{
return;
}
// Process all asyncs
while (!_asyncHolder.empty())
@@ -75,13 +77,17 @@ void TaskScheduler::Dispatch(success_t const& callback)
// If the validation failed abort the dispatching here.
if (!_predicate())
{
return;
}
}
while (!_task_holder.IsEmpty())
{
if (_task_holder.First()->_end > _now)
{
break;
}
// Perfect forward the context to the handler
// Use weak references to catch destruction before callbacks.
@@ -92,7 +98,9 @@ void TaskScheduler::Dispatch(success_t const& callback)
// If the validation failed abort the dispatching here.
if (!_predicate())
{
return;
}
}
// On finish call the final callback
@@ -125,9 +133,13 @@ void TaskScheduler::TaskQueue::RemoveIf(std::function<bool(TaskContainer const&)
{
for (auto itr = container.begin(); itr != container.end();)
if (filter(*itr))
{
itr = container.erase(itr);
}
else
{
++itr;
}
}
void TaskScheduler::TaskQueue::ModifyIf(std::function<bool(TaskContainer const&)> const& filter)
@@ -140,7 +152,9 @@ void TaskScheduler::TaskQueue::ModifyIf(std::function<bool(TaskContainer const&)
itr = container.erase(itr);
}
else
{
++itr;
}
container.insert(cache.begin(), cache.end());
}
@@ -153,7 +167,9 @@ bool TaskScheduler::TaskQueue::IsEmpty() const
TaskContext& TaskContext::Dispatch(std::function<TaskScheduler&(TaskScheduler&)> const& apply)
{
if (auto const owner = _owner.lock())
{
apply(*owner);
}
return *this;
}

View File

@@ -292,7 +292,9 @@ public:
return true;
}
else
{
return false;
}
});
return *this;
}
@@ -340,7 +342,9 @@ public:
return true;
}
else
{
return false;
}
});
return *this;
}

View File

@@ -30,9 +30,13 @@ inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
{
// getMSTime() have limited data range and this is case when it overflow in this tick
if (oldMSTime > newMSTime)
{
return (0xFFFFFFFF - oldMSTime) + newMSTime;
}
else
{
return newMSTime - oldMSTime;
}
}
inline uint32 getMSTimeDiff(uint32 oldMSTime, TimePoint newTime)
@@ -60,7 +64,9 @@ public:
{
_current += diff;
if (_current < 0)
{
_current = 0;
}
}
bool Passed()
@@ -71,7 +77,9 @@ public:
void Reset()
{
if (_current >= _interval)
{
_current %= _interval;
}
}
void SetCurrent(time_t current)
@@ -174,7 +182,9 @@ public:
bool Update(const uint32 diff)
{
if ((i_expireTime -= diff) > 0)
{
return false;
}
i_expireTime += i_period > int32(diff) ? i_period : diff;
return true;

View File

@@ -13,13 +13,17 @@ std::vector<std::string_view> Acore::Tokenize(std::string_view str, char sep, bo
for (size_t end = str.find(sep); end != std::string_view::npos; end = str.find(sep, start))
{
if (keepEmpty || (start < end))
{
tokens.push_back(str.substr(start, end - start));
}
start = end + 1;
}
if (keepEmpty || (start < str.length()))
{
tokens.push_back(str.substr(start));
}
return tokens;
}

View File

@@ -26,7 +26,9 @@ Tokenizer::Tokenizer(const std::string& src, const char sep, uint32 vectorReserv
memcpy(m_str, src.c_str(), src.length() + 1);
if (vectorReserve)
{
m_storage.reserve(vectorReserve);
}
char* posold = m_str;
char* posnew = m_str;
@@ -45,7 +47,9 @@ Tokenizer::Tokenizer(const std::string& src, const char sep, uint32 vectorReserv
// Hack like, but the old code accepted these kind of broken strings,
// so changing it would break other things
if (posold != posnew)
{
m_storage.push_back(posold);
}
break;
}
@@ -88,7 +92,9 @@ time_t GetLocalHourTimestamp(time_t time, uint8 hour, bool onlyAfterTime)
time_t hourLocal = midnightLocal + hour * HOUR;
if (onlyAfterTime && hourLocal <= time)
{
hourLocal += DAY;
}
return hourLocal;
}
@@ -113,17 +119,25 @@ void stripLineInvisibleChars(std::string& str)
else
{
if (wpos != pos)
{
str[wpos++] = str[pos];
}
else
{
++wpos;
}
space = false;
}
}
if (wpos < str.size())
{
str.erase(wpos, str.size());
}
if (str.find("|TInterface") != std::string::npos)
{
str.clear();
}
}
std::string secsToTimeString(uint64 timeInSecs, bool shortText)
@@ -135,18 +149,28 @@ std::string secsToTimeString(uint64 timeInSecs, bool shortText)
std::ostringstream ss;
if (days)
{
ss << days << (shortText ? "d" : " day(s) ");
}
if (hours)
{
ss << hours << (shortText ? "h" : " hour(s) ");
}
if (minutes)
{
ss << minutes << (shortText ? "m" : " minute(s) ");
}
if (secs || (!days && !hours && !minutes) )
{
ss << secs << (shortText ? "s" : " second(s) ");
}
std::string str = ss.str();
if (!shortText && !str.empty() && str[str.size() - 1] == ' ')
{
str.resize(str.size() - 1);
}
return str;
}
@@ -158,7 +182,9 @@ int32 MoneyStringToMoney(const std::string& moneyString)
if (!(std::count(moneyString.begin(), moneyString.end(), 'g') == 1 ||
std::count(moneyString.begin(), moneyString.end(), 's') == 1 ||
std::count(moneyString.begin(), moneyString.end(), 'c') == 1))
return 0; // Bad format
{
return 0; // Bad format
}
Tokenizer tokens(moneyString, ' ');
for (Tokenizer::const_iterator itr = tokens.begin(); itr != tokens.end(); ++itr)
@@ -168,15 +194,23 @@ int32 MoneyStringToMoney(const std::string& moneyString)
size_t sCount = std::count(tokenString.begin(), tokenString.end(), 's');
size_t cCount = std::count(tokenString.begin(), tokenString.end(), 'c');
if (gCount + sCount + cCount != 1)
{
return 0;
}
uint32 amount = atoi(*itr);
if (gCount == 1)
{
money += amount * 100 * 100;
}
else if (sCount == 1)
{
money += amount * 100;
}
else if (cCount == 1)
{
money += amount;
}
}
return money;
@@ -257,7 +291,9 @@ std::string TimeToHumanReadable(time_t t)
bool IsIPAddress(char const* ipaddress)
{
if (!ipaddress)
{
return false;
}
// Let the big boys do it.
// Drawback: all valid ip address formats are recognized e.g.: 12.23, 121234, 0xABCD)
@@ -275,7 +311,9 @@ bool IsIPAddrInNetwork(ACE_INET_Addr const& net, ACE_INET_Addr const& addr, ACE_
{
uint32 mask = subnetMask.get_ip_address();
if ((net.get_ip_address() & mask) == (addr.get_ip_address() & mask))
{
return true;
}
return false;
}
@@ -284,7 +322,9 @@ uint32 CreatePIDFile(std::string const& filename)
{
FILE* pid_file = fopen(filename.c_str(), "w");
if (pid_file == nullptr)
{
return 0;
}
uint32 pid = GetPID();
@@ -324,7 +364,9 @@ void utf8truncate(std::string& utf8str, size_t len)
{
size_t wlen = utf8::distance(utf8str.c_str(), utf8str.c_str() + utf8str.size());
if (wlen <= len)
{
return;
}
std::wstring wstr;
wstr.resize(wlen);
@@ -365,7 +407,9 @@ bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize)
wsize = 0;
}
else
{
wsize = 0;
}
return false;
}
@@ -451,7 +495,9 @@ std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension)
{
// supported only Cyrillic cases
if (wname.empty() || !isCyrillicCharacter(wname[0]) || declension > 5)
{
return wname;
}
// Important: end length must be <= MAX_INTERNAL_PLAYER_NAME-MAX_PLAYER_NAME (3 currently)
static std::wstring const a_End = { wchar_t(0x0430), wchar_t(0x0000) };
@@ -488,10 +534,14 @@ std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension)
std::wstring const& ending = **itr;
std::size_t const endLen = ending.length();
if (!(endLen <= thisLen))
{
continue;
}
if (wname.substr(thisLen - endLen, thisLen) == ending)
{
return wname.substr(0, thisLen - endLen);
}
}
return wname;
@@ -502,7 +552,9 @@ bool utf8ToConsole(const std::string& utf8str, std::string& conStr)
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
std::wstring wstr;
if (!Utf8toWStr(utf8str, wstr))
{
return false;
}
conStr.resize(wstr.size());
CharToOemBuffW(&wstr[0], &conStr[0], wstr.size());
@@ -534,13 +586,17 @@ bool Utf8FitTo(const std::string& str, std::wstring const& search)
std::wstring temp;
if (!Utf8toWStr(str, temp))
{
return false;
}
// converting to lower case
wstrToLower(temp);
if (temp.find(search) == std::wstring::npos)
{
return false;
}
return true;
}
@@ -562,7 +618,9 @@ void vutf8printf(FILE* out, const char* str, va_list* ap)
size_t temp_len = vsnprintf(temp_buf, 32 * 1024, str, *ap);
//vsnprintf returns -1 if the buffer is too small
if (temp_len == size_t(-1))
{
temp_len = 32 * 1024 - 1;
}
size_t wtemp_len = 32 * 1024 - 1;
Utf8toWStr(temp_buf, temp_len, wtemp_buf, wtemp_len);
@@ -578,7 +636,9 @@ bool Utf8ToUpperOnlyLatin(std::string& utf8String)
{
std::wstring wstr;
if (!Utf8toWStr(utf8String, wstr))
{
return false;
}
std::transform(wstr.begin(), wstr.end(), wstr.begin(), wcharToUpperOnlyLatin);

View File

@@ -74,7 +74,9 @@ std::string TimeToHumanReadable(time_t t);
inline void ApplyPercentModFloatVar(float& var, float val, bool apply)
{
if (val == -100.0f) // prevent set var to zero
{
val = -99.99f;
}
var *= (apply ? (100.0f + val) / 100.0f : 100.0f / (100.0f + val));
}
@@ -125,60 +127,100 @@ void utf8truncate(std::string& utf8str, size_t len);
inline bool isBasicLatinCharacter(wchar_t wchar)
{
if (wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
{
return true;
}
if (wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
{
return true;
}
return false;
}
inline bool isExtendedLatinCharacter(wchar_t wchar)
{
if (isBasicLatinCharacter(wchar))
{
return true;
}
if (wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
{
return true;
}
if (wchar >= 0x00D8 && wchar <= 0x00DE) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
{
return true;
}
if (wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
{
return true;
}
if (wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
{
return true;
}
if (wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
{
return true;
}
if (wchar >= 0x0100 && wchar <= 0x012F) // LATIN CAPITAL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK
{
return true;
}
if (wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
{
return true;
}
return false;
}
inline bool isCyrillicCharacter(wchar_t wchar)
{
if (wchar >= 0x0410 && wchar <= 0x044F) // CYRILLIC CAPITAL LETTER A - CYRILLIC SMALL LETTER YA
{
return true;
}
if (wchar == 0x0401 || wchar == 0x0451) // CYRILLIC CAPITAL LETTER IO, CYRILLIC SMALL LETTER IO
{
return true;
}
return false;
}
inline bool isEastAsianCharacter(wchar_t wchar)
{
if (wchar >= 0x1100 && wchar <= 0x11F9) // Hangul Jamo
{
return true;
}
if (wchar >= 0x3041 && wchar <= 0x30FF) // Hiragana + Katakana
{
return true;
}
if (wchar >= 0x3131 && wchar <= 0x318E) // Hangul Compatibility Jamo
{
return true;
}
if (wchar >= 0x31F0 && wchar <= 0x31FF) // Katakana Phonetic Ext.
{
return true;
}
if (wchar >= 0x3400 && wchar <= 0x4DB5) // CJK Ideographs Ext. A
{
return true;
}
if (wchar >= 0x4E00 && wchar <= 0x9FC3) // Unified CJK Ideographs
{
return true;
}
if (wchar >= 0xAC00 && wchar <= 0xD7A3) // Hangul Syllables
{
return true;
}
if (wchar >= 0xFF01 && wchar <= 0xFFEE) // Halfwidth forms
{
return true;
}
return false;
}
@@ -196,7 +238,9 @@ inline bool isNumeric(char const* str)
{
for (char const* c = str; *c; ++c)
if (!isNumeric(*c))
{
return false;
}
return true;
}
@@ -210,7 +254,9 @@ inline bool isBasicLatinString(const std::wstring& wstr, bool numericOrSpace)
{
for (wchar_t i : wstr)
if (!isBasicLatinCharacter(i) && (!numericOrSpace || !isNumericOrSpace(i)))
{
return false;
}
return true;
}
@@ -218,7 +264,9 @@ inline bool isExtendedLatinString(const std::wstring& wstr, bool numericOrSpace)
{
for (wchar_t i : wstr)
if (!isExtendedLatinCharacter(i) && (!numericOrSpace || !isNumericOrSpace(i)))
{
return false;
}
return true;
}
@@ -226,7 +274,9 @@ inline bool isCyrillicString(const std::wstring& wstr, bool numericOrSpace)
{
for (wchar_t i : wstr)
if (!isCyrillicCharacter(i) && (!numericOrSpace || !isNumericOrSpace(i)))
{
return false;
}
return true;
}
@@ -234,29 +284,45 @@ inline bool isEastAsianString(const std::wstring& wstr, bool numericOrSpace)
{
for (wchar_t i : wstr)
if (!isEastAsianCharacter(i) && (!numericOrSpace || !isNumericOrSpace(i)))
{
return false;
}
return true;
}
inline wchar_t wcharToUpper(wchar_t wchar)
{
if (wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
{
return wchar_t(uint16(wchar) - 0x0020);
}
if (wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
{
return wchar_t(0x1E9E);
}
if (wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
{
return wchar_t(uint16(wchar) - 0x0020);
}
if (wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
{
return wchar_t(uint16(wchar) - 0x0020);
}
if (wchar >= 0x0101 && wchar <= 0x012F) // LATIN SMALL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK (only %2=1)
{
if (wchar % 2 == 1)
{
return wchar_t(uint16(wchar) - 0x0001);
}
}
if (wchar >= 0x0430 && wchar <= 0x044F) // CYRILLIC SMALL LETTER A - CYRILLIC SMALL LETTER YA
{
return wchar_t(uint16(wchar) - 0x0020);
}
if (wchar == 0x0451) // CYRILLIC SMALL LETTER IO
{
return wchar_t(0x0401);
}
return wchar;
}
@@ -269,22 +335,36 @@ inline wchar_t wcharToUpperOnlyLatin(wchar_t wchar)
inline wchar_t wcharToLower(wchar_t wchar)
{
if (wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
{
return wchar_t(uint16(wchar) + 0x0020);
}
if (wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
{
return wchar_t(uint16(wchar) + 0x0020);
}
if (wchar >= 0x00D8 && wchar <= 0x00DE) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
{
return wchar_t(uint16(wchar) + 0x0020);
}
if (wchar >= 0x0100 && wchar <= 0x012E) // LATIN CAPITAL LETTER A WITH MACRON - LATIN CAPITAL LETTER I WITH OGONEK (only %2=0)
{
if (wchar % 2 == 0)
{
return wchar_t(uint16(wchar) + 0x0001);
}
}
if (wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
{
return wchar_t(0x00DF);
}
if (wchar == 0x0401) // CYRILLIC CAPITAL LETTER IO
{
return wchar_t(0x0451);
}
if (wchar >= 0x0410 && wchar <= 0x042F) // CYRILLIC CAPITAL LETTER A - CYRILLIC CAPITAL LETTER YA
{
return wchar_t(uint16(wchar) + 0x0020);
}
return wchar;
}
@@ -418,9 +498,13 @@ public:
for (uint8 i = 3; i > 0; --i)
{
if (part[i - 1] < right.part[i - 1])
{
return true;
}
else if (part[i - 1] > right.part[i - 1])
{
return false;
}
}
return false;
}