From 9f39b215746f9b9cd820c75604d601b8b96e1754 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 9 Feb 2022 19:58:44 +0100 Subject: [PATCH] Moved Date from trantor to core/math. Also moved the methods from and Funcs.h to core. --- .../utils/Date.cc => core/math/date.cpp | 12 ++-- .../trantor/utils/Date.h => core/math/date.h | 0 core/math/math.h | 21 ++++++- core/utilities.cpp | 2 + core/utilities.h | 36 ++++++++++++ .../drogon/drogon/lib/inc/drogon/drogon.h | 2 +- .../drogon/lib/inc/drogon/utils/Utilities.h | 6 +- .../drogon/drogon/lib/inc/http/Cookie.h | 2 +- .../drogon/drogon/lib/inc/http/HttpRequest.h | 2 +- .../drogon/lib/inc/http/HttpResponseImpl.h | 2 +- web_backends/drogon/trantor/net/EventLoop.h | 2 +- web_backends/drogon/trantor/net/InetAddress.h | 1 - .../drogon/trantor/unittests/DateUnittest.cc | 2 +- .../trantor/unittests/splitStringUnittest.cc | 3 +- .../drogon/trantor/utils/AsyncFileLogger.h | 2 +- web_backends/drogon/trantor/utils/Funcs.h | 56 ------------------- web_backends/drogon/trantor/utils/Logger.h | 2 +- .../drogon/trantor/utils/MsgBuffer.cc | 8 +-- 18 files changed, 80 insertions(+), 81 deletions(-) rename web_backends/drogon/trantor/utils/Date.cc => core/math/date.cpp (96%) rename web_backends/drogon/trantor/utils/Date.h => core/math/date.h (100%) create mode 100644 core/utilities.cpp create mode 100644 core/utilities.h delete mode 100644 web_backends/drogon/trantor/utils/Funcs.h diff --git a/web_backends/drogon/trantor/utils/Date.cc b/core/math/date.cpp similarity index 96% rename from web_backends/drogon/trantor/utils/Date.cc rename to core/math/date.cpp index f025dec..54a098c 100644 --- a/web_backends/drogon/trantor/utils/Date.cc +++ b/core/math/date.cpp @@ -12,8 +12,8 @@ * */ -#include "Date.h" -#include "Funcs.h" +#include "date.h" +#include "core/utilities.h" #ifndef _WIN32 #include #endif @@ -253,19 +253,19 @@ std::string Date::toDbStringLocal() const { Date Date::fromDbStringLocal(const std::string &datetime) { unsigned int year = { 0 }, month = { 0 }, day = { 0 }, hour = { 0 }, minute = { 0 }, second = { 0 }, microSecond = { 0 }; - std::vector &&v = splitString(datetime, " "); + std::vector &&v = Utilities::splitString(datetime, " "); if (2 == v.size()) { // date - std::vector date = splitString(v[0], "-"); + std::vector date = Utilities::splitString(v[0], "-"); if (3 == date.size()) { year = std::stol(date[0]); month = std::stol(date[1]); day = std::stol(date[2]); - std::vector time = splitString(v[1], ":"); + std::vector time = Utilities::splitString(v[1], ":"); if (2 < time.size()) { hour = std::stol(time[0]); minute = std::stol(time[1]); - auto seconds = splitString(time[2], "."); + auto seconds = Utilities::splitString(time[2], "."); second = std::stol(seconds[0]); if (1 < seconds.size()) { if (seconds[1].length() > 6) { diff --git a/web_backends/drogon/trantor/utils/Date.h b/core/math/date.h similarity index 100% rename from web_backends/drogon/trantor/utils/Date.h rename to core/math/date.h diff --git a/core/math/math.h b/core/math/math.h index ae4ae45..9be0956 100644 --- a/core/math/math.h +++ b/core/math/math.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #define MATH_PI 3.1415926535897932384626433833 @@ -95,7 +96,7 @@ public: static float is_equal_approx(const float a, const float b); static float is_zero_approx(const float a); - //can save typing static_cast + // can save typing static_cast inline static float divf(const float a, const float b) { return a / b; } // Taken from the Godot Engine (MIT License) @@ -322,6 +323,24 @@ public: return hf; } + + // Taken from trantor (MIT License) - Funcs.h + // Copyright (c) 2018 An Tao + static _ALWAYS_INLINE_ uint64_t hton64(uint64_t n) { + static const int one = 1; + static const char sig = *(char *)&one; + if (sig == 0) + return n; // for big endian machine just return the input + char *ptr = reinterpret_cast(&n); + std::reverse(ptr, ptr + sizeof(uint64_t)); + return n; + } + + // Taken from trantor (MIT License) - Funcs.h + // Copyright (c) 2018 An Tao + static _ALWAYS_INLINE_ uint64_t ntoh64(uint64_t n) { + return hton64(n); + } }; #ifndef ABS diff --git a/core/utilities.cpp b/core/utilities.cpp new file mode 100644 index 0000000..901ca29 --- /dev/null +++ b/core/utilities.cpp @@ -0,0 +1,2 @@ +#include "utilities.h" + diff --git a/core/utilities.h b/core/utilities.h new file mode 100644 index 0000000..6f5b461 --- /dev/null +++ b/core/utilities.h @@ -0,0 +1,36 @@ +#ifndef UTILS_H +#define UTILS_H + +#include "core/containers/vector.h" +#include "core/string.h" +#include "core/typedefs.h" + +#include +#include + +class Utilities { +public: + // Taken from trantor (MIT License) - Funcs.h + // Copyright (c) 2018 An Tao + static _ALWAYS_INLINE_ std::vector splitString(const std::string &s, + const std::string &delimiter, + bool acceptEmptyString = false) { + if (delimiter.empty()) + return std::vector{}; + std::vector v; + size_t last = 0; + size_t next = 0; + while ((next = s.find(delimiter, last)) != std::string::npos) { + if (next > last || acceptEmptyString) + v.push_back(s.substr(last, next - last)); + last = next + delimiter.length(); + } + if (s.length() > last || acceptEmptyString) + v.push_back(s.substr(last)); + return v; + } + +protected: +}; + +#endif \ No newline at end of file diff --git a/web_backends/drogon/drogon/lib/inc/drogon/drogon.h b/web_backends/drogon/drogon/lib/inc/drogon/drogon.h index 5fff366..49cedc8 100644 --- a/web_backends/drogon/drogon/lib/inc/drogon/drogon.h +++ b/web_backends/drogon/drogon/lib/inc/drogon/drogon.h @@ -16,7 +16,7 @@ #include #include -#include +#include "core/math/date.h" #include #include diff --git a/web_backends/drogon/drogon/lib/inc/drogon/utils/Utilities.h b/web_backends/drogon/drogon/lib/inc/drogon/utils/Utilities.h index 22bebe8..6d3f3ab 100644 --- a/web_backends/drogon/drogon/lib/inc/drogon/utils/Utilities.h +++ b/web_backends/drogon/drogon/lib/inc/drogon/utils/Utilities.h @@ -16,8 +16,8 @@ #include -#include -#include +#include "core/math/date.h" +#include "core/utilities.h" #include #include #include @@ -60,7 +60,7 @@ std::vector hexToBinaryVector(const char *ptr, inline std::vector splitString(const std::string &str, const std::string &separator, bool acceptEmptyString = false) { - return trantor::splitString(str, separator, acceptEmptyString); + return Utilities::splitString(str, separator, acceptEmptyString); } std::set splitStringToSet( diff --git a/web_backends/drogon/drogon/lib/inc/http/Cookie.h b/web_backends/drogon/drogon/lib/inc/http/Cookie.h index 4e82e4f..40a5d7c 100644 --- a/web_backends/drogon/drogon/lib/inc/http/Cookie.h +++ b/web_backends/drogon/drogon/lib/inc/http/Cookie.h @@ -14,7 +14,7 @@ #pragma once -#include +#include "core/math/date.h" #include #include diff --git a/web_backends/drogon/drogon/lib/inc/http/HttpRequest.h b/web_backends/drogon/drogon/lib/inc/http/HttpRequest.h index b336ac5..fd84680 100644 --- a/web_backends/drogon/drogon/lib/inc/http/HttpRequest.h +++ b/web_backends/drogon/drogon/lib/inc/http/HttpRequest.h @@ -22,7 +22,7 @@ #include #include #include -#include +#include "core/math/date.h" #include #include #include diff --git a/web_backends/drogon/drogon/lib/inc/http/HttpResponseImpl.h b/web_backends/drogon/drogon/lib/inc/http/HttpResponseImpl.h index f4bedc9..ff70228 100644 --- a/web_backends/drogon/drogon/lib/inc/http/HttpResponseImpl.h +++ b/web_backends/drogon/drogon/lib/inc/http/HttpResponseImpl.h @@ -20,7 +20,7 @@ #include #include -#include +#include "core/math/date.h" #include #include #include diff --git a/web_backends/drogon/trantor/net/EventLoop.h b/web_backends/drogon/trantor/net/EventLoop.h index d28f6b8..1a78d01 100644 --- a/web_backends/drogon/trantor/net/EventLoop.h +++ b/web_backends/drogon/trantor/net/EventLoop.h @@ -17,7 +17,7 @@ #pragma once #include -#include +#include "core/math/date.h" #include #include #include diff --git a/web_backends/drogon/trantor/net/InetAddress.h b/web_backends/drogon/trantor/net/InetAddress.h index 1750bb5..a752199 100644 --- a/web_backends/drogon/trantor/net/InetAddress.h +++ b/web_backends/drogon/trantor/net/InetAddress.h @@ -20,7 +20,6 @@ #ifndef MUDUO_NET_INETADDRESS_H #define MUDUO_NET_INETADDRESS_H -#include #include #ifdef _WIN32 diff --git a/web_backends/drogon/trantor/unittests/DateUnittest.cc b/web_backends/drogon/trantor/unittests/DateUnittest.cc index 255ce28..e3aaafb 100644 --- a/web_backends/drogon/trantor/unittests/DateUnittest.cc +++ b/web_backends/drogon/trantor/unittests/DateUnittest.cc @@ -1,4 +1,4 @@ -#include +#include "core/math/date.h" #include #include #include diff --git a/web_backends/drogon/trantor/unittests/splitStringUnittest.cc b/web_backends/drogon/trantor/unittests/splitStringUnittest.cc index efa64af..39d0191 100644 --- a/web_backends/drogon/trantor/unittests/splitStringUnittest.cc +++ b/web_backends/drogon/trantor/unittests/splitStringUnittest.cc @@ -1,6 +1,5 @@ -#include +#include "core/math/date.h" #include -#include #include using namespace trantor; TEST(splitString, ACCEPT_EMPTY_STRING1) diff --git a/web_backends/drogon/trantor/utils/AsyncFileLogger.h b/web_backends/drogon/trantor/utils/AsyncFileLogger.h index 0f54c67..f04647b 100644 --- a/web_backends/drogon/trantor/utils/AsyncFileLogger.h +++ b/web_backends/drogon/trantor/utils/AsyncFileLogger.h @@ -15,7 +15,7 @@ #pragma once #include -#include +#include "core/math/date.h" #include #include #include diff --git a/web_backends/drogon/trantor/utils/Funcs.h b/web_backends/drogon/trantor/utils/Funcs.h deleted file mode 100644 index 444fe27..0000000 --- a/web_backends/drogon/trantor/utils/Funcs.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * - * Funcs.h - * An Tao - * - * Public header file in trantor lib. - * - * Copyright 2018, An Tao. All rights reserved. - * Use of this source code is governed by a BSD-style license - * that can be found in the License file. - * - * - */ - -#pragma once -#include -#include -#include -#include - -namespace trantor -{ -inline uint64_t hton64(uint64_t n) -{ - static const int one = 1; - static const char sig = *(char *)&one; - if (sig == 0) - return n; // for big endian machine just return the input - char *ptr = reinterpret_cast(&n); - std::reverse(ptr, ptr + sizeof(uint64_t)); - return n; -} -inline uint64_t ntoh64(uint64_t n) -{ - return hton64(n); -} -inline std::vector splitString(const std::string &s, - const std::string &delimiter, - bool acceptEmptyString = false) -{ - if (delimiter.empty()) - return std::vector{}; - std::vector v; - size_t last = 0; - size_t next = 0; - while ((next = s.find(delimiter, last)) != std::string::npos) - { - if (next > last || acceptEmptyString) - v.push_back(s.substr(last, next - last)); - last = next + delimiter.length(); - } - if (s.length() > last || acceptEmptyString) - v.push_back(s.substr(last)); - return v; -} -} // namespace trantor diff --git a/web_backends/drogon/trantor/utils/Logger.h b/web_backends/drogon/trantor/utils/Logger.h index ddb6100..70ef8a5 100644 --- a/web_backends/drogon/trantor/utils/Logger.h +++ b/web_backends/drogon/trantor/utils/Logger.h @@ -15,7 +15,7 @@ #pragma once #include -#include +#include "core/math/date.h" #include #include #include diff --git a/web_backends/drogon/trantor/utils/MsgBuffer.cc b/web_backends/drogon/trantor/utils/MsgBuffer.cc index c23535d..0ae068b 100644 --- a/web_backends/drogon/trantor/utils/MsgBuffer.cc +++ b/web_backends/drogon/trantor/utils/MsgBuffer.cc @@ -13,8 +13,8 @@ */ #include -#include #include +#include "core/math/math.h" #ifndef _WIN32 #include #include @@ -80,7 +80,7 @@ void MsgBuffer::appendInt32(const uint32_t i) { append(static_cast((void *)&ii), 4); } void MsgBuffer::appendInt64(const uint64_t l) { - uint64_t ll = hton64(l); + uint64_t ll = Math::hton64(l); append(static_cast((void *)&ll), 8); } @@ -93,7 +93,7 @@ void MsgBuffer::addInFrontInt32(const uint32_t i) { addInFront(static_cast((void *)&ii), 4); } void MsgBuffer::addInFrontInt64(const uint64_t l) { - uint64_t ll = hton64(l); + uint64_t ll = Math::hton64(l); addInFront(static_cast((void *)&ll), 8); } @@ -110,7 +110,7 @@ uint32_t MsgBuffer::peekInt32() const { uint64_t MsgBuffer::peekInt64() const { assert(readableBytes() >= 8); uint64_t rll = *(static_cast((void *)peek())); - return ntoh64(rll); + return Math::ntoh64(rll); } void MsgBuffer::retrieve(size_t len) {