|
| 1 | +#ifndef CTTI_DETAIL_CSTRING_HPP |
| 2 | +#define CTTI_DETAIL_CSTRING_HPP |
| 3 | + |
| 4 | +#include "hash.hpp" |
| 5 | +#include "algorithm.hpp" |
| 6 | +#include <ostream> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +namespace ctti |
| 10 | +{ |
| 11 | + |
| 12 | +namespace detail |
| 13 | +{ |
| 14 | + |
| 15 | +class cstring |
| 16 | +{ |
| 17 | +public: |
| 18 | + template<std::size_t N> |
| 19 | + constexpr cstring(const char (&str)[N]) : |
| 20 | + cstring{&str[0], N - 1} |
| 21 | + {} |
| 22 | + |
| 23 | + constexpr cstring(const char* begin, std::size_t length) : |
| 24 | + _str{begin}, |
| 25 | + _length{length} |
| 26 | + {} |
| 27 | + |
| 28 | + constexpr cstring(const char* begin, const char* end) : |
| 29 | + cstring{begin, static_cast<std::size_t>(end - begin)} |
| 30 | + {} |
| 31 | + |
| 32 | + constexpr cstring(const char* begin) : |
| 33 | + cstring{begin, length(begin)} |
| 34 | + {} |
| 35 | + |
| 36 | + static constexpr std::size_t length(const char* str) |
| 37 | + { |
| 38 | + return *str ? 1 + length(str + 1) : 0; |
| 39 | + } |
| 40 | + |
| 41 | + constexpr std::size_t length() const |
| 42 | + { |
| 43 | + return _length; |
| 44 | + } |
| 45 | + |
| 46 | + constexpr std::size_t size() const |
| 47 | + { |
| 48 | + return length(); |
| 49 | + } |
| 50 | + |
| 51 | + constexpr hash_t hash() const |
| 52 | + { |
| 53 | + return fnv1a_hash(length(), begin()); |
| 54 | + } |
| 55 | + |
| 56 | + std::string cppstring() const |
| 57 | + { |
| 58 | + return {begin(), end()}; |
| 59 | + } |
| 60 | + |
| 61 | + std::string str() const |
| 62 | + { |
| 63 | + return cppstring(); |
| 64 | + } |
| 65 | + |
| 66 | + constexpr const char* begin() const |
| 67 | + { |
| 68 | + return _str; |
| 69 | + } |
| 70 | + |
| 71 | + constexpr const char* end() const |
| 72 | + { |
| 73 | + return _str + _length; |
| 74 | + } |
| 75 | + |
| 76 | + constexpr char operator[](std::size_t i) const |
| 77 | + { |
| 78 | + return _str[i]; |
| 79 | + } |
| 80 | + |
| 81 | + constexpr const char* operator()(std::size_t i) const |
| 82 | + { |
| 83 | + return _str + i; |
| 84 | + } |
| 85 | + |
| 86 | + constexpr cstring operator()(std::size_t begin, std::size_t end) const |
| 87 | + { |
| 88 | + return {_str + begin, _str + end}; |
| 89 | + } |
| 90 | + |
| 91 | + constexpr cstring pad(std::size_t begin_offset, std::size_t end_offset) const |
| 92 | + { |
| 93 | + return operator()(begin_offset, size() - end_offset); |
| 94 | + } |
| 95 | + |
| 96 | + friend std::ostream& operator<<(std::ostream& os, const cstring& str) |
| 97 | + { |
| 98 | + for(const char c : str) |
| 99 | + { |
| 100 | + os << c; |
| 101 | + } |
| 102 | + |
| 103 | + return os; |
| 104 | + } |
| 105 | + |
| 106 | +private: |
| 107 | + const char* _str; |
| 108 | + std::size_t _length; |
| 109 | +}; |
| 110 | + |
| 111 | +constexpr bool operator==(const cstring& lhs, const cstring& rhs) |
| 112 | +{ |
| 113 | + return ctti::detail::equal_range(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); |
| 114 | +} |
| 115 | + |
| 116 | +constexpr bool operator!=(const cstring& lhs, const cstring& rhs) |
| 117 | +{ |
| 118 | + return !(lhs == rhs); |
| 119 | +} |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +#endif // CTTI_DETAIL_CSTRING_HPP |
0 commit comments