aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/utility/StringUtils.hpp
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2024-01-03 15:30:23 +0100
committer hop311 <hop3114@gmail.com>2024-01-03 15:43:35 +0100
commitf0dd758b6c7f35ffb1f6b237805bcb8d39c20cc5 (patch)
tree21e54285c4f3927ecd3b1b621587d75b875ac3d3 /src/openvic-simulation/utility/StringUtils.hpp
parent461ec160448373f8d9492b9c586ff53a35edef18 (diff)
Added case insensitive ordered set and map and IdentifierRegistry
Diffstat (limited to 'src/openvic-simulation/utility/StringUtils.hpp')
-rw-r--r--src/openvic-simulation/utility/StringUtils.hpp39
1 files changed, 34 insertions, 5 deletions
diff --git a/src/openvic-simulation/utility/StringUtils.hpp b/src/openvic-simulation/utility/StringUtils.hpp
index ede1d6b..c5a0b71 100644
--- a/src/openvic-simulation/utility/StringUtils.hpp
+++ b/src/openvic-simulation/utility/StringUtils.hpp
@@ -1,5 +1,6 @@
#pragma once
+#include <cctype>
#include <cstdint>
#include <cstring>
#include <limits>
@@ -97,7 +98,7 @@ namespace OpenVic::StringUtils {
return string_to_uint64(str, str + length, successful, base);
}
- inline uint64_t string_to_uint64(std::string_view str, bool* successful = nullptr, int base = 10) {
+ inline constexpr uint64_t string_to_uint64(std::string_view str, bool* successful = nullptr, int base = 10) {
return string_to_uint64(str.data(), str.length(), successful, base);
}
@@ -142,10 +143,20 @@ namespace OpenVic::StringUtils {
return string_to_int64(str, str + length, successful, base);
}
- inline int64_t string_to_int64(std::string_view str, bool* successful = nullptr, int base = 10) {
+ inline constexpr int64_t string_to_int64(std::string_view str, bool* successful = nullptr, int base = 10) {
return string_to_int64(str.data(), str.length(), successful, base);
}
+ inline constexpr bool strings_equal_case_insensitive(std::string_view const& lhs, std::string_view const& rhs) {
+ if (lhs.size() != rhs.size()) {
+ return false;
+ }
+ constexpr auto ichar_equals = [](unsigned char l, unsigned char r) {
+ return std::tolower(l) == std::tolower(r);
+ };
+ return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), ichar_equals);
+ }
+
inline constexpr std::string_view get_filename(std::string_view path) {
size_t pos = path.size();
while (pos > 0 && path[pos - 1] != '/' && path[pos - 1] != '\\') {
@@ -198,11 +209,29 @@ namespace OpenVic::StringUtils {
return _append_string_views(std::string_view { args }...);
}
- inline constexpr std::string_view remove_extension(std::string_view path) {
+ inline constexpr size_t get_extension_pos(std::string_view const& path) {
size_t pos = path.size();
while (pos > 0 && path[--pos] != '.') {}
- if (path[pos] == '.') {
- path.remove_suffix(path.size() - pos);
+ return pos;
+ }
+
+ inline constexpr std::string_view get_extension(std::string_view path) {
+ if (!path.empty()) {
+ const size_t pos = get_extension_pos(path);
+ if (path[pos] == '.') {
+ path.remove_prefix(pos);
+ return path;
+ }
+ }
+ return {};
+ }
+
+ inline constexpr std::string_view remove_extension(std::string_view path) {
+ if (!path.empty()) {
+ const size_t pos = get_extension_pos(path);
+ if (path[pos] == '.') {
+ path.remove_suffix(path.size() - pos);
+ }
}
return path;
}