diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/Common.h | 25 | ||||
| -rw-r--r-- | src/common/IPLocation/IPLocation.cpp | 15 | ||||
| -rw-r--r-- | src/common/Utilities/StringFormat.h | 13 | ||||
| -rw-r--r-- | src/common/Utilities/Util.cpp | 17 |
4 files changed, 31 insertions, 39 deletions
diff --git a/src/common/Common.h b/src/common/Common.h index 141b163a63e..0b33af12cec 100644 --- a/src/common/Common.h +++ b/src/common/Common.h @@ -21,22 +21,6 @@ #include "Define.h" #include <array> #include <string> -#include <cstdlib> - -#if TRINITY_COMPILER == TRINITY_COMPILER_MICROSOFT - -#define atoll _atoi64 -#define llabs _abs64 - -#else - -#define stricmp strcasecmp -#define strnicmp strncasecmp - -#endif - -inline unsigned long atoul(char const* str) { return strtoul(str, nullptr, 10); } -inline unsigned long long atoull(char const* str) { return strtoull(str, nullptr, 10); } #define STRINGIZE(a) #a @@ -84,15 +68,6 @@ TC_COMMON_API extern char const* localeNames[TOTAL_LOCALES]; TC_COMMON_API LocaleConstant GetLocaleByName(std::string const& name); -// we always use stdlib std::max/std::min, undefine some not C++ standard defines (Win API and some other platforms) -#ifdef max -#undef max -#endif - -#ifdef min -#undef min -#endif - #ifndef M_PI #define M_PI 3.14159265358979323846 #endif diff --git a/src/common/IPLocation/IPLocation.cpp b/src/common/IPLocation/IPLocation.cpp index a1717420eb4..2dfd025fc7e 100644 --- a/src/common/IPLocation/IPLocation.cpp +++ b/src/common/IPLocation/IPLocation.cpp @@ -16,11 +16,12 @@ */ #include "IPLocation.h" -#include "Common.h" #include "Config.h" #include "Errors.h" #include "IpAddress.h" #include "Log.h" +#include "StringConvert.h" +#include "Util.h" #include <fstream> #include <iostream> @@ -83,9 +84,17 @@ void IpLocationStore::Load() countryName.erase(std::remove(countryName.begin(), countryName.end(), '"'), countryName.end()); // Convert country code to lowercase - std::transform(countryCode.begin(), countryCode.end(), countryCode.begin(), ::tolower); + strToLower(countryCode); - _ipLocationStore.emplace_back(uint32(atoul(ipFrom.c_str())), uint32(atoul(ipTo.c_str())), std::move(countryCode), std::move(countryName)); + Optional<uint32> from = Trinity::StringTo<uint32>(ipFrom); + if (!from) + continue; + + Optional<uint32> to = Trinity::StringTo<uint32>(ipTo); + if (!to) + continue; + + _ipLocationStore.emplace_back(*from, *to, std::move(countryCode), std::move(countryName)); } std::sort(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.IpFrom < b.IpFrom; }); diff --git a/src/common/Utilities/StringFormat.h b/src/common/Utilities/StringFormat.h index c95039efea4..9bef5516139 100644 --- a/src/common/Utilities/StringFormat.h +++ b/src/common/Utilities/StringFormat.h @@ -39,6 +39,19 @@ namespace Trinity } } + template<typename OutputIt, typename... Args> + inline OutputIt StringFormatTo(OutputIt out, FormatString<Args...> fmt, Args&&... args) + { + try + { + return fmt::format_to(out, fmt, std::forward<Args>(args)...); + } + catch (std::exception const& formatError) + { + return fmt::format_to(out, "An error occurred formatting string \"{}\" : {}", fmt, formatError.what()); + } + } + /// Returns true if the given char pointer is null. inline bool IsFormatEmptyOrNull(char const* fmt) { diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index 6763685ba70..9c77e9f9821 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -658,15 +658,13 @@ std::string Trinity::Impl::ByteArrayToHexStr(uint8 const* bytes, size_t arrayLen op = -1; } - std::ostringstream ss; + std::string result; + result.reserve(arrayLen * 2); + auto inserter = std::back_inserter(result); for (int32 i = init; i != end; i += op) - { - char buffer[4]; - sprintf(buffer, "%02X", bytes[i]); - ss << buffer; - } + Trinity::StringFormatTo(inserter, "{:02X}", bytes[i]); - return ss.str(); + return result; } void Trinity::Impl::HexStrToByteArray(std::string_view str, uint8* out, size_t outlen, bool reverse /*= false*/) @@ -686,10 +684,7 @@ void Trinity::Impl::HexStrToByteArray(std::string_view str, uint8* out, size_t o uint32 j = 0; for (int32 i = init; i != end; i += 2 * op) - { - char buffer[3] = { str[i], str[i + 1], '\0' }; - out[j++] = uint8(strtoul(buffer, nullptr, 16)); - } + out[j++] = Trinity::StringTo<uint8>(str.substr(i, 2), 16).value_or(0); } bool StringEqualI(std::string_view a, std::string_view b) |
