geode/loader/include/Geode/utils/conststring.hpp

58 lines
2.2 KiB
C++
Raw Normal View History

2022-07-30 12:24:03 -04:00
#pragma once
#include <array>
#include <string_view>
using namespace std::string_view_literals;
namespace geode::utils {
static constexpr const std::string_view sv_null = "\0";
static constexpr const std::string_view sv_none = "";
static constexpr const std::string_view sv_path = "/";
2022-10-30 14:59:20 -04:00
// straight off
// https://stackoverflow.com/questions/38955940/how-to-concatenate-static-strings-at-compile-time
2022-07-30 12:24:03 -04:00
template <std::string_view const& Separator, std::string_view const&... Strs>
struct ConstStringJoin {
static constexpr auto impl() noexcept {
2022-10-30 14:59:20 -04:00
constexpr std::size_t len =
(Strs.size() + ... + 0) + (sizeof...(Strs) - 1) * Separator.size();
std::array<char, len + 1> arr {};
2022-07-30 12:24:03 -04:00
auto append = [len, i = 0, &arr](auto const& s) mutable {
2022-10-30 14:59:20 -04:00
for (auto c : s)
arr[i++] = c;
2022-07-30 12:24:03 -04:00
if (i < static_cast<int>(len - 1)) {
2022-10-30 14:59:20 -04:00
for (auto c : Separator)
arr[i++] = c;
2022-07-30 12:24:03 -04:00
}
};
(append(Strs), ...);
arr[len] = 0;
return arr;
}
2022-10-30 14:59:20 -04:00
2022-07-30 12:24:03 -04:00
static constexpr auto arr = impl();
2022-10-30 14:59:20 -04:00
static constexpr std::string_view value { arr.data(), arr.size() - 1 };
2022-07-30 12:24:03 -04:00
};
2022-10-30 14:59:20 -04:00
2022-07-30 12:24:03 -04:00
template <std::string_view const& Separator, std::string_view const&... Strs>
static constexpr auto const_join_sep = ConstStringJoin<Separator, Strs...>::value;
template <std::string_view const&... Strs>
static constexpr auto const_join = ConstStringJoin<sv_none, Strs...>::value;
template <std::string_view const&... Strs>
2022-10-30 14:59:20 -04:00
static constexpr auto const_join_path = ConstStringJoin<sv_path, Strs...>::value;
2022-07-30 12:24:03 -04:00
template <std::string_view const& Separator, std::string_view const&... Strs>
2022-10-30 14:59:20 -04:00
static constexpr char const* const_join_sep_c_str =
ConstStringJoin<Separator, Strs...>::value.data();
2022-07-30 12:24:03 -04:00
template <std::string_view const&... Strs>
2022-10-30 14:59:20 -04:00
static constexpr char const* const_join_c_str = ConstStringJoin<sv_none, Strs...>::value.data();
2022-07-30 12:24:03 -04:00
template <std::string_view const&... Strs>
2022-10-30 14:59:20 -04:00
static constexpr char const* const_join_path_c_str =
ConstStringJoin<sv_path, Strs...>::value.data();
2022-07-30 12:24:03 -04:00
}