mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-25 22:56:24 +00:00
feat(Core/Config): rework config and delete ACE inherited (#4608)
This commit is contained in:
259
src/common/Utilities/StringConvert.h
Normal file
259
src/common/Utilities/StringConvert.h
Normal file
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
|
||||
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
|
||||
*/
|
||||
|
||||
#ifndef _ACORE_STRINGCONVERT_H_
|
||||
#define _ACORE_STRINGCONVERT_H_
|
||||
|
||||
#include "Define.h"
|
||||
#include "Errors.h"
|
||||
#include "Optional.h"
|
||||
#include "Types.h"
|
||||
#include "Util.h"
|
||||
#include <charconv>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
namespace acore::Impl::StringConvertImpl
|
||||
{
|
||||
template <typename T, typename = void> struct For
|
||||
{
|
||||
static_assert(acore::dependant_false_v<T>, "Unsupported type used for ToString or StringTo");
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct For<T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>>
|
||||
{
|
||||
static Optional<T> FromString(std::string_view str, int base = 10)
|
||||
{
|
||||
if (base == 0)
|
||||
{
|
||||
if (StringEqualI(str.substr(0, 2), "0x"))
|
||||
{
|
||||
base = 16;
|
||||
str.remove_prefix(2);
|
||||
}
|
||||
else if (StringEqualI(str.substr(0, 2), "0b"))
|
||||
{
|
||||
base = 2;
|
||||
str.remove_prefix(2);
|
||||
}
|
||||
else
|
||||
base = 10;
|
||||
|
||||
if (str.empty())
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
char const* const start = str.data();
|
||||
char const* const end = (start + str.length());
|
||||
|
||||
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 */
|
||||
char* const start = buf.data();
|
||||
char* const end = (start + buf.length());
|
||||
std::to_chars_result const res = std::to_chars(start, end, val);
|
||||
ASSERT(res.ec == std::errc());
|
||||
buf.resize(res.ptr - start);
|
||||
return buf;
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef ACORE_NEED_CHARCONV_WORKAROUND
|
||||
/*
|
||||
If this is defined, std::from_chars will cause linkage errors for 64-bit types.
|
||||
(This is a bug in clang-7.)
|
||||
|
||||
If the clang requirement is bumped to >= clang-8, remove this ifdef block and its
|
||||
associated check in cmake/compiler/clang/settings.cmake
|
||||
*/
|
||||
template <>
|
||||
struct For<uint64, void>
|
||||
{
|
||||
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; }
|
||||
}
|
||||
|
||||
static std::string ToString(uint64 val)
|
||||
{
|
||||
return std::to_string(val);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct For<int64, void>
|
||||
{
|
||||
static Optional<int64> FromString(std::string_view str, int base = 10)
|
||||
{
|
||||
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; }
|
||||
}
|
||||
|
||||
static std::string ToString(int64 val)
|
||||
{
|
||||
return std::to_string(val);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
template <>
|
||||
struct For<bool, void>
|
||||
{
|
||||
static Optional<bool> FromString(std::string_view str, int strict = 0) /* this is int to match the signature for "proper" integral types */
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static std::string ToString(bool val)
|
||||
{
|
||||
return (val ? "1" : "0");
|
||||
}
|
||||
};
|
||||
|
||||
#if AC_COMPILER == AC_COMPILER_MICROSOFT
|
||||
template <typename T>
|
||||
struct For<T, std::enable_if_t<std::is_floating_point_v<T>>>
|
||||
{
|
||||
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())
|
||||
{
|
||||
if (StringEqualI(str.substr(0, 2), "0x"))
|
||||
{
|
||||
fmt = std::chars_format::hex;
|
||||
str.remove_prefix(2);
|
||||
}
|
||||
else
|
||||
fmt = std::chars_format::general;
|
||||
|
||||
if (str.empty())
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
char const* const start = str.data();
|
||||
char const* const end = (start + str.length());
|
||||
|
||||
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)
|
||||
{
|
||||
return std::to_string(val);
|
||||
}
|
||||
};
|
||||
#else
|
||||
// @todo replace this once libc++ supports double args to from_chars
|
||||
template <typename T>
|
||||
struct For<T, std::enable_if_t<std::is_floating_point_v<T>>>
|
||||
{
|
||||
static Optional<T> FromString(std::string_view str, int base = 0)
|
||||
{
|
||||
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; }
|
||||
}
|
||||
|
||||
static std::string ToString(T val)
|
||||
{
|
||||
return std::to_string(val);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
}
|
||||
|
||||
namespace acore
|
||||
{
|
||||
template <typename Result, typename... Params>
|
||||
Optional<Result> StringTo(std::string_view str, Params&&... params)
|
||||
{
|
||||
return acore::Impl::StringConvertImpl::For<Result>::FromString(str, std::forward<Params>(params)...);
|
||||
}
|
||||
|
||||
template <typename Type, typename... Params>
|
||||
std::string ToString(Type&& val, Params&&... params)
|
||||
{
|
||||
return acore::Impl::StringConvertImpl::For<std::decay_t<Type>>::ToString(std::forward<Type>(val), std::forward<Params>(params)...);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // _ACORE_STRINGCONVERT_H_
|
||||
40
src/common/Utilities/StringFormat.cpp
Normal file
40
src/common/Utilities/StringFormat.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
|
||||
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
|
||||
*/
|
||||
|
||||
#include "StringFormat.h"
|
||||
|
||||
// Taken from https://stackoverflow.com/a/1798170
|
||||
std::string acore::String::Trim(std::string const& str, std::string_view whitespace /*= " \t"*/)
|
||||
{
|
||||
const auto strBegin = str.find_first_not_of(whitespace);
|
||||
if (strBegin == std::string::npos)
|
||||
return ""; // no content
|
||||
|
||||
auto const strEnd = str.find_last_not_of(whitespace);
|
||||
auto const strRange = strEnd - strBegin + 1;
|
||||
|
||||
return str.substr(strBegin, strRange);
|
||||
}
|
||||
|
||||
std::string acore::String::Reduce(std::string const& str, std::string_view fill /*= " "*/, std::string_view whitespace /*= " \t"*/)
|
||||
{
|
||||
// trim first
|
||||
auto result = Trim(str, whitespace);
|
||||
|
||||
// replace sub ranges
|
||||
auto beginSpace = result.find_first_of(whitespace);
|
||||
while (beginSpace != std::string::npos)
|
||||
{
|
||||
const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
|
||||
const auto range = endSpace - beginSpace;
|
||||
|
||||
result.replace(beginSpace, range, fill);
|
||||
|
||||
const auto newStart = beginSpace + fill.length();
|
||||
beginSpace = result.find_first_of(whitespace, newStart);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
|
||||
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
|
||||
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
|
||||
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
||||
*/
|
||||
|
||||
#ifndef __STRING_FORMAT_H__
|
||||
#define __STRING_FORMAT_H__
|
||||
#ifndef _STRING_FORMAT_H_
|
||||
#define _STRING_FORMAT_H_
|
||||
|
||||
#include "fmt/printf.h"
|
||||
#include <fmt/printf.h>
|
||||
|
||||
namespace acore
|
||||
{
|
||||
@@ -39,4 +40,10 @@ namespace acore
|
||||
}
|
||||
}
|
||||
|
||||
namespace acore::String
|
||||
{
|
||||
std::string Trim(std::string const& str, std::string_view whitespace = " \t");
|
||||
std::string Reduce(std::string const& str, std::string_view fill = " ", std::string_view whitespace = " \t");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
63
src/common/Utilities/Types.h
Normal file
63
src/common/Utilities/Types.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
|
||||
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
|
||||
*/
|
||||
|
||||
#ifndef _TYPES_H_
|
||||
#define _TYPES_H_
|
||||
|
||||
#include "advstd.h"
|
||||
|
||||
namespace acore
|
||||
{
|
||||
// end "iterator" tag for find_type_if
|
||||
struct find_type_end;
|
||||
|
||||
template<template<typename...> typename Check, typename... Ts>
|
||||
struct find_type_if;
|
||||
|
||||
template<template<typename...> typename Check>
|
||||
struct find_type_if<Check>
|
||||
{
|
||||
using type = find_type_end;
|
||||
};
|
||||
|
||||
template<template<typename...> typename Check, typename T1, typename... Ts>
|
||||
struct find_type_if<Check, T1, Ts...> : std::conditional_t<Check<T1>::value, advstd::type_identity<T1>, find_type_if<Check, Ts...>>
|
||||
{
|
||||
};
|
||||
|
||||
/*
|
||||
Utility to find a type matching predicate (Check) in a given type list (Ts)
|
||||
Evaluates to first type matching predicate or find_type_end
|
||||
Check must be a type that contains static bool ::value, _v aliases don't work
|
||||
|
||||
template<typename... Ts>
|
||||
struct Example
|
||||
{
|
||||
using TupleArg = acore::find_type_if_t<acore::is_tuple, Ts...>;
|
||||
|
||||
bool HasTuple()
|
||||
{
|
||||
return !std::is_same_v<TupleArg, acore::find_type_end>;
|
||||
}
|
||||
};
|
||||
|
||||
Example<int, std::string, std::tuple<int, int, int>, char> example;
|
||||
example.HasTuple() == true; // TupleArg is std::tuple<int, int, int>
|
||||
|
||||
Example<int, std::string, char> example2;
|
||||
example2.HasTuple() == false; // TupleArg is acore::find_type_end
|
||||
*/
|
||||
|
||||
template<template<typename...> typename Check, typename... Ts>
|
||||
using find_type_if_t = typename find_type_if<Check, Ts...>::type;
|
||||
|
||||
template <typename T>
|
||||
struct dependant_false { static constexpr bool value = false; };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool dependant_false_v = dependant_false<T>::value;
|
||||
}
|
||||
|
||||
#endif // _TYPES_H_
|
||||
@@ -682,15 +682,13 @@ void HexStrToByteArray(std::string const& str, uint8* out, bool reverse /*= fals
|
||||
}
|
||||
}
|
||||
|
||||
bool StringToBool(std::string const& str)
|
||||
{
|
||||
std::string lowerStr = str;
|
||||
std::transform(str.begin(), str.end(), lowerStr.begin(), ::tolower);
|
||||
return lowerStr == "1" || lowerStr == "true" || lowerStr == "yes";
|
||||
}
|
||||
|
||||
bool StringContainsStringI(std::string const& haystack, std::string const& needle)
|
||||
{
|
||||
return haystack.end() !=
|
||||
std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end(), [](char c1, char c2) { return std::toupper(c1) == std::toupper(c2); });
|
||||
}
|
||||
|
||||
bool StringEqualI(std::string_view a, std::string_view b)
|
||||
{
|
||||
return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char c1, char c2) { return std::tolower(c1) == std::tolower(c2); });
|
||||
}
|
||||
|
||||
@@ -343,9 +343,10 @@ std::string GetAddressString(ACE_INET_Addr const& addr);
|
||||
uint32 CreatePIDFile(const std::string& filename);
|
||||
uint32 GetPID();
|
||||
|
||||
bool StringEqualI(std::string_view str1, std::string_view str2);
|
||||
|
||||
std::string ByteArrayToHexStr(uint8 const* bytes, uint32 length, bool reverse = false);
|
||||
void HexStrToByteArray(std::string const& str, uint8* out, bool reverse = false);
|
||||
bool StringToBool(std::string const& str);
|
||||
|
||||
bool StringContainsStringI(std::string const& haystack, std::string const& needle);
|
||||
template <typename T>
|
||||
|
||||
31
src/common/Utilities/advstd.h
Normal file
31
src/common/Utilities/advstd.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
|
||||
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
|
||||
*/
|
||||
|
||||
#ifndef _ACORE_ADV_STD_H_
|
||||
#define _ACORE_ADV_STD_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
|
||||
// this namespace holds implementations of upcoming stdlib features that our c++ version doesn't have yet
|
||||
namespace advstd
|
||||
{
|
||||
// C++20 advstd::remove_cvref_t
|
||||
template <class T>
|
||||
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||
|
||||
// C++20 std::type_identity
|
||||
template <typename T>
|
||||
struct type_identity
|
||||
{
|
||||
using type = T;
|
||||
};
|
||||
|
||||
// C++20 std::type_identity_t
|
||||
template <typename T>
|
||||
using type_identity_t = typename type_identity<T>::type;
|
||||
}
|
||||
|
||||
#endif // _ADV_STD_H_
|
||||
Reference in New Issue
Block a user