|
| 1 | +#ifndef PROXICT_CONSTEXPR_STRING_HPP_ |
| 2 | +#define PROXICT_CONSTEXPR_STRING_HPP_ |
| 3 | +#include <iostream> |
| 4 | +#include <type_traits> |
| 5 | + |
| 6 | +template <bool TTest, typename TType = void> |
| 7 | +using EnableIf = typename std::enable_if<TTest, TType>::type; |
| 8 | + |
| 9 | +template <int... Indices> |
| 10 | +struct IntSequence {}; |
| 11 | + |
| 12 | +template <typename T> |
| 13 | +struct append; |
| 14 | + |
| 15 | +template <int... Indices> |
| 16 | +struct append<IntSequence<Indices...>> { |
| 17 | + using type = IntSequence<Indices..., sizeof...(Indices)>; |
| 18 | +}; |
| 19 | + |
| 20 | +template <int TSize> |
| 21 | +struct MakeIntSequenceImpl; |
| 22 | + |
| 23 | +template <> |
| 24 | +struct MakeIntSequenceImpl<0> { |
| 25 | + using type = IntSequence<>; |
| 26 | +}; |
| 27 | + |
| 28 | +template <int TSize> |
| 29 | +using MakeIntSequence = typename MakeIntSequenceImpl<TSize>::type; |
| 30 | + |
| 31 | +template <int TSize> |
| 32 | +struct MakeIntSequenceImpl : append<MakeIntSequence<TSize - 1>> { |
| 33 | + static_assert(TSize >= 0, "Integer sequence size must be positive"); |
| 34 | +}; |
| 35 | + |
| 36 | +constexpr int cstrlen(const char* str) { |
| 37 | + return *str ? 1 + cstrlen(str + 1) : 0; |
| 38 | +} |
| 39 | + |
| 40 | +constexpr bool cstreq(const char* a, const char* b) { |
| 41 | + return *a == *b && (*a == 0 || cstreq(a + 1, b + 1)); |
| 42 | +} |
| 43 | + |
| 44 | +template <int TSize> |
| 45 | +class ConstexprString final { |
| 46 | +public: |
| 47 | + static_assert(TSize >= -1, "Invalid string size"); |
| 48 | + |
| 49 | + constexpr explicit ConstexprString(const char* data) |
| 50 | + : ConstexprString(data, MakeIntSequence<TSize>()) {} |
| 51 | + |
| 52 | + constexpr char operator[](const int index) const { return mData[index]; } |
| 53 | + |
| 54 | + template <int TOtherSize, typename = EnableIf<TOtherSize <= TSize>> |
| 55 | + constexpr ConstexprString(const ConstexprString<TOtherSize>& s1, |
| 56 | + const ConstexprString<TSize - TOtherSize>& s2) |
| 57 | + : ConstexprString{ s1, s2, MakeIntSequence<TOtherSize>{}, MakeIntSequence<TSize - TOtherSize>{} } {} |
| 58 | + |
| 59 | + constexpr const char* cStr() const noexcept { return mData; } |
| 60 | + |
| 61 | + constexpr int size() const { return TSize; } |
| 62 | + |
| 63 | + template <typename TPredicate> |
| 64 | + constexpr bool allOf(TPredicate&& p) const { |
| 65 | + return allOfInternal(0, p); |
| 66 | + } |
| 67 | + |
| 68 | + template <int TOtherSize> |
| 69 | + constexpr ConstexprString<TSize + TOtherSize> operator+(const ConstexprString<TOtherSize>& rhs) const { |
| 70 | + return ConstexprString<TSize + TOtherSize>(*this, rhs); |
| 71 | + } |
| 72 | + |
| 73 | + template <int TFrom, int TTo = TSize, typename = EnableIf<TFrom <= TTo>> |
| 74 | + constexpr ConstexprString<TTo - TFrom> substr() const { |
| 75 | + return ConstexprString<TTo - TFrom>(mData + TFrom, MakeIntSequence<TTo - TFrom>{}); |
| 76 | + } |
| 77 | + |
| 78 | + template <int TOtherSize> |
| 79 | + constexpr bool operator==(const ConstexprString<TOtherSize>& rhs) const { |
| 80 | + return size() == rhs.size() && cstreq(cStr(), rhs.cStr()); |
| 81 | + } |
| 82 | + |
| 83 | + template <int TOtherSize> |
| 84 | + constexpr bool operator!=(const ConstexprString<TOtherSize>& rhs) const { |
| 85 | + return !(*this == rhs); |
| 86 | + } |
| 87 | + |
| 88 | + template <int TOtherSize, typename = EnableIf<TOtherSize - 1 <= TSize>> |
| 89 | + constexpr int find(char const (&str)[TOtherSize], const int pos = 0) const { |
| 90 | + return findInternal(pos, 0, str); |
| 91 | + } |
| 92 | + |
| 93 | + constexpr int find(const char c, const int pos = 0) const { return findInternal(pos, c); } |
| 94 | + |
| 95 | + constexpr const char* begin() const { |
| 96 | + return mData; |
| 97 | + } |
| 98 | + |
| 99 | + constexpr const char* end() const { |
| 100 | + return mData + TSize; |
| 101 | + } |
| 102 | + |
| 103 | +private: |
| 104 | + constexpr int findInternal(int index, const char c) const { |
| 105 | + return index < TSize ? (mData[index] == c ? index : findInternal(index + 1, c)) : -1; |
| 106 | + } |
| 107 | + |
| 108 | + template <int TOtherSize> |
| 109 | + constexpr int findInternal(int index, int index2, char const (&str)[TOtherSize]) const { |
| 110 | + // clang-format off |
| 111 | + return index2 == TOtherSize - 1 |
| 112 | + ? index - TOtherSize + 1 |
| 113 | + : (index < TSize ? (mData[index] == str[index2] ? findInternal(index + 1, index2 + 1, str) : findInternal(index + 1 - index2, 0, str)) : -1); |
| 114 | + // clang-format on |
| 115 | + } |
| 116 | + |
| 117 | + template <int TNewSize> |
| 118 | + friend class ConstexprString; |
| 119 | + |
| 120 | + template <typename TPredicate> |
| 121 | + constexpr bool allOfInternal(const int index, TPredicate&& p) const { |
| 122 | + return index < TSize ? (p(mData[index]) ? allOfInternal(index + 1, p) : false) : true; |
| 123 | + } |
| 124 | + |
| 125 | + template <int... TPack> |
| 126 | + constexpr ConstexprString(const char* str, IntSequence<TPack...>) |
| 127 | + : mData{ str[TPack]..., '\0' } {} |
| 128 | + |
| 129 | + template <int TOtherSize, int... TPack1, int... TPack2> |
| 130 | + constexpr ConstexprString(const ConstexprString<TOtherSize>& lhs, |
| 131 | + const ConstexprString<TSize - TOtherSize>& rhs, |
| 132 | + IntSequence<TPack1...>, |
| 133 | + IntSequence<TPack2...>) |
| 134 | + : mData{ lhs[TPack1]..., rhs[TPack2]..., '\0' } {} |
| 135 | + |
| 136 | + char mData[TSize + 1]; |
| 137 | +}; |
| 138 | + |
| 139 | +template <int TSize> |
| 140 | +constexpr ConstexprString<TSize - 1> String(char const (&str)[TSize]) { |
| 141 | + return ConstexprString<TSize - 1>(str); |
| 142 | +} |
| 143 | + |
| 144 | +#endif // PROXICT_CONSTEXPR_STRING_HPP_ |
0 commit comments