From 458e0005690ea9d579588a0a12368fc2c2c9a93a Mon Sep 17 00:00:00 2001 From: untodesu Date: Tue, 1 Jul 2025 03:08:39 +0500 Subject: I hyper-focued on refactoring again - I put a cool-sounding "we are number one" remix on repeat and straight up grinded the entire repository to a better state until 03:09 AM. I guess I have something wrong in my brain that makes me do this shit --- core/utils/CMakeLists.txt | 5 +++++ core/utils/epoch.cc | 39 ++++++++++++++++++++++++++++++++++ core/utils/epoch.hh | 19 +++++++++++++++++ core/utils/string.cc | 54 +++++++++++++++++++++++++++++++++++++++++++++++ core/utils/string.hh | 21 ++++++++++++++++++ 5 files changed, 138 insertions(+) create mode 100644 core/utils/CMakeLists.txt create mode 100644 core/utils/epoch.cc create mode 100644 core/utils/epoch.hh create mode 100644 core/utils/string.cc create mode 100644 core/utils/string.hh (limited to 'core/utils') diff --git a/core/utils/CMakeLists.txt b/core/utils/CMakeLists.txt new file mode 100644 index 0000000..4f96261 --- /dev/null +++ b/core/utils/CMakeLists.txt @@ -0,0 +1,5 @@ +target_sources(core PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/epoch.cc" + "${CMAKE_CURRENT_LIST_DIR}/epoch.hh" + "${CMAKE_CURRENT_LIST_DIR}/string.cc" + "${CMAKE_CURRENT_LIST_DIR}/string.hh") diff --git a/core/utils/epoch.cc b/core/utils/epoch.cc new file mode 100644 index 0000000..36bdb79 --- /dev/null +++ b/core/utils/epoch.cc @@ -0,0 +1,39 @@ +#include "core/pch.hh" + +#include "core/utils/epoch.hh" + +std::uint64_t utils::unix_seconds(void) +{ + const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); + return static_cast(std::chrono::duration_cast(elapsed).count()); +} + +std::uint64_t utils::unix_milliseconds(void) +{ + const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); + return static_cast(std::chrono::duration_cast(elapsed).count()); +} + +std::uint64_t utils::unix_microseconds(void) +{ + const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); + return static_cast(std::chrono::duration_cast(elapsed).count()); +} + +std::int64_t utils::signed_unix_seconds(void) +{ + const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); + return static_cast(std::chrono::duration_cast(elapsed).count()); +} + +std::int64_t utils::signed_unix_milliseconds(void) +{ + const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); + return static_cast(std::chrono::duration_cast(elapsed).count()); +} + +std::int64_t utils::signed_unix_microseconds(void) +{ + const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); + return static_cast(std::chrono::duration_cast(elapsed).count()); +} diff --git a/core/utils/epoch.hh b/core/utils/epoch.hh new file mode 100644 index 0000000..1cd7f43 --- /dev/null +++ b/core/utils/epoch.hh @@ -0,0 +1,19 @@ +#ifndef CORE_UTILS_EPOCH_HH +#define CORE_UTILS_EPOCH_HH 1 +#pragma once + +namespace utils +{ +std::uint64_t unix_seconds(void); +std::uint64_t unix_milliseconds(void); +std::uint64_t unix_microseconds(void); +} // namespace utils + +namespace utils +{ +std::int64_t signed_unix_seconds(void); +std::int64_t signed_unix_milliseconds(void); +std::int64_t signed_unix_microseconds(void); +} // namespace utils + +#endif // CORE_UTILS_EPOCH_HH diff --git a/core/utils/string.cc b/core/utils/string.cc new file mode 100644 index 0000000..5497cec --- /dev/null +++ b/core/utils/string.cc @@ -0,0 +1,54 @@ +#include "core/pch.hh" + +#include "core/utils/string.hh" + +constexpr static const char* WHITESPACE_CHARS = " \t\r\n"; + +bool utils::is_whitespace(const std::string& string) +{ + if(string.find_first_not_of(WHITESPACE_CHARS) == std::string::npos) { + return true; + } else if((string.size() == 1) && string[0] == 0x00) { + return true; + } else { + return string.empty(); + } +} + +std::string utils::join(const std::vector& strings, const std::string& separator) +{ + std::ostringstream stream; + for(const std::string& str : strings) + stream << str << separator; + return stream.str(); +} + +std::vector utils::split(const std::string& string, const std::string& separator) +{ + std::size_t pos = 0; + std::size_t prev = 0; + std::vector result; + + while((pos = string.find(separator, prev)) != std::string::npos) { + result.push_back(string.substr(prev, pos - prev)); + prev = pos + separator.length(); + } + + if(prev <= string.length()) { + result.push_back(string.substr(prev, string.length() - prev)); + } + + return result; +} + +std::string utils::trim_whitespace(const std::string& string) +{ + auto su = string.find_first_not_of(WHITESPACE_CHARS); + auto sv = string.find_last_not_of(WHITESPACE_CHARS); + + if(su == std::string::npos) { + return std::string(); + } else { + return string.substr(su, sv - su + 1); + } +} diff --git a/core/utils/string.hh b/core/utils/string.hh new file mode 100644 index 0000000..3ba56a0 --- /dev/null +++ b/core/utils/string.hh @@ -0,0 +1,21 @@ +#ifndef CORE_UTILS_STRING_HH +#define CORE_UTILS_STRING_HH 1 +#pragma once + +namespace utils +{ +bool is_whitespace(const std::string& string); +} // namespace utils + +namespace utils +{ +std::string join(const std::vector& strings, const std::string& separator); +std::vector split(const std::string& string, const std::string& separator); +} // namespace utils + +namespace utils +{ +std::string trim_whitespace(const std::string& string); +} // namespace utils + +#endif // CORE_UTILS_STRING_HH -- cgit