diff options
| author | untodesu <kirill@untode.su> | 2025-07-01 03:08:39 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-07-01 03:08:39 +0500 |
| commit | 458e0005690ea9d579588a0a12368fc2c2c9a93a (patch) | |
| tree | 588a9ca6cb3c76d9193b5bd4601d64f0e50e8c8c /core/utils | |
| parent | c7b0c8e0286a1b2bb7ec55e579137dfc3b22eeb9 (diff) | |
| download | voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.tar.bz2 voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.zip | |
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
Diffstat (limited to 'core/utils')
| -rw-r--r-- | core/utils/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | core/utils/epoch.cc | 39 | ||||
| -rw-r--r-- | core/utils/epoch.hh | 19 | ||||
| -rw-r--r-- | core/utils/string.cc | 54 | ||||
| -rw-r--r-- | core/utils/string.hh | 21 |
5 files changed, 138 insertions, 0 deletions
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::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(elapsed).count()); +} + +std::uint64_t utils::unix_milliseconds(void) +{ + const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); + return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count()); +} + +std::uint64_t utils::unix_microseconds(void) +{ + const auto elapsed = std::chrono::system_clock::now().time_since_epoch(); + return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(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::int64_t>(std::chrono::duration_cast<std::chrono::seconds>(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::int64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(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::int64_t>(std::chrono::duration_cast<std::chrono::microseconds>(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<std::string>& strings, const std::string& separator) +{ + std::ostringstream stream; + for(const std::string& str : strings) + stream << str << separator; + return stream.str(); +} + +std::vector<std::string> utils::split(const std::string& string, const std::string& separator) +{ + std::size_t pos = 0; + std::size_t prev = 0; + std::vector<std::string> 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<std::string>& strings, const std::string& separator); +std::vector<std::string> 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 |
