Moved Date from trantor to core/math. Also moved the methods from and Funcs.h to core.

This commit is contained in:
Relintai 2022-02-09 19:58:44 +01:00
parent 4fd6ce10ea
commit 9f39b21574
18 changed files with 80 additions and 81 deletions

View File

@ -12,8 +12,8 @@
*
*/
#include "Date.h"
#include "Funcs.h"
#include "date.h"
#include "core/utilities.h"
#ifndef _WIN32
#include <sys/time.h>
#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<std::string> &&v = splitString(datetime, " ");
std::vector<std::string> &&v = Utilities::splitString(datetime, " ");
if (2 == v.size()) {
// date
std::vector<std::string> date = splitString(v[0], "-");
std::vector<std::string> 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<std::string> time = splitString(v[1], ":");
std::vector<std::string> 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) {

View File

@ -6,6 +6,7 @@
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <cstdint>
#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<float>
// can save typing static_cast<float>
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<char *>(&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

2
core/utilities.cpp Normal file
View File

@ -0,0 +1,2 @@
#include "utilities.h"

36
core/utilities.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef UTILS_H
#define UTILS_H
#include "core/containers/vector.h"
#include "core/string.h"
#include "core/typedefs.h"
#include <string>
#include <vector>
class Utilities {
public:
// Taken from trantor (MIT License) - Funcs.h
// Copyright (c) 2018 An Tao
static _ALWAYS_INLINE_ std::vector<std::string> splitString(const std::string &s,
const std::string &delimiter,
bool acceptEmptyString = false) {
if (delimiter.empty())
return std::vector<std::string>{};
std::vector<std::string> 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

View File

@ -16,7 +16,7 @@
#include <trantor/net/EventLoop.h>
#include <trantor/net/InetAddress.h>
#include <trantor/utils/Date.h>
#include "core/math/date.h"
#include <trantor/utils/Logger.h>
#include <http/CacheMap.h>

View File

@ -16,8 +16,8 @@
#include <drogon/utils/string_view.h>
#include <trantor/utils/Date.h>
#include <trantor/utils/Funcs.h>
#include "core/math/date.h"
#include "core/utilities.h"
#include <limits>
#include <memory>
#include <set>
@ -60,7 +60,7 @@ std::vector<char> hexToBinaryVector(const char *ptr,
inline std::vector<std::string> 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<std::string> splitStringToSet(

View File

@ -14,7 +14,7 @@
#pragma once
#include <trantor/utils/Date.h>
#include "core/math/date.h"
#include <limits>
#include <string>

View File

@ -22,7 +22,7 @@
#include <drogon/utils/string_view.h>
#include <json/json.h>
#include <trantor/net/InetAddress.h>
#include <trantor/utils/Date.h>
#include "core/math/date.h"
#include <memory>
#include <string>
#include <unordered_map>

View File

@ -20,7 +20,7 @@
#include <drogon/utils/Utilities.h>
#include <trantor/net/InetAddress.h>
#include <trantor/utils/Date.h>
#include "core/math/date.h"
#include <trantor/utils/MsgBuffer.h>
#include <atomic>
#include <memory>

View File

@ -17,7 +17,7 @@
#pragma once
#include <trantor/utils/NonCopyable.h>
#include <trantor/utils/Date.h>
#include "core/math/date.h"
#include <trantor/utils/LockFreeQueue.h>
#include <trantor/exports.h>
#include <thread>

View File

@ -20,7 +20,6 @@
#ifndef MUDUO_NET_INETADDRESS_H
#define MUDUO_NET_INETADDRESS_H
#include <trantor/utils/Date.h>
#include <trantor/exports.h>
#ifdef _WIN32

View File

@ -1,4 +1,4 @@
#include <trantor/utils/Date.h>
#include "core/math/date.h"
#include <gtest/gtest.h>
#include <string>
#include <iostream>

View File

@ -1,6 +1,5 @@
#include <trantor/utils/Date.h>
#include "core/math/date.h"
#include <gtest/gtest.h>
#include <trantor/utils/Funcs.h>
#include <iostream>
using namespace trantor;
TEST(splitString, ACCEPT_EMPTY_STRING1)

View File

@ -15,7 +15,7 @@
#pragma once
#include <trantor/utils/NonCopyable.h>
#include <trantor/utils/Date.h>
#include "core/math/date.h"
#include <trantor/exports.h>
#include <thread>
#include <mutex>

View File

@ -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 <algorithm>
#include <vector>
#include <cstdint>
#include <string>
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<char *>(&n);
std::reverse(ptr, ptr + sizeof(uint64_t));
return n;
}
inline uint64_t ntoh64(uint64_t n)
{
return hton64(n);
}
inline std::vector<std::string> splitString(const std::string &s,
const std::string &delimiter,
bool acceptEmptyString = false)
{
if (delimiter.empty())
return std::vector<std::string>{};
std::vector<std::string> 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

View File

@ -15,7 +15,7 @@
#pragma once
#include <trantor/utils/NonCopyable.h>
#include <trantor/utils/Date.h>
#include "core/math/date.h"
#include <trantor/utils/LogStream.h>
#include <trantor/exports.h>
#include <cstring>

View File

@ -13,8 +13,8 @@
*/
#include <string.h>
#include <trantor/utils/Funcs.h>
#include <trantor/utils/MsgBuffer.h>
#include "core/math/math.h"
#ifndef _WIN32
#include <netinet/in.h>
#include <sys/uio.h>
@ -80,7 +80,7 @@ void MsgBuffer::appendInt32(const uint32_t i) {
append(static_cast<const char *>((void *)&ii), 4);
}
void MsgBuffer::appendInt64(const uint64_t l) {
uint64_t ll = hton64(l);
uint64_t ll = Math::hton64(l);
append(static_cast<const char *>((void *)&ll), 8);
}
@ -93,7 +93,7 @@ void MsgBuffer::addInFrontInt32(const uint32_t i) {
addInFront(static_cast<const char *>((void *)&ii), 4);
}
void MsgBuffer::addInFrontInt64(const uint64_t l) {
uint64_t ll = hton64(l);
uint64_t ll = Math::hton64(l);
addInFront(static_cast<const char *>((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<const uint64_t *>((void *)peek()));
return ntoh64(rll);
return Math::ntoh64(rll);
}
void MsgBuffer::retrieve(size_t len) {