#ifndef SIBS_STRING_VIEW_HPP #define SIBS_STRING_VIEW_HPP #define XXH_FORCE_NATIVE_FORMAT #include "../external/xxhash.h" #include "types.hpp" #include "env.hpp" #include #include #include namespace sibs { class StringView { public: StringView() : data(nullptr), size(0) { } StringView(const StringView &other) : data(other.data), size(other.size) { } StringView(const char *_data) : data(_data), size(strlen(_data)) { } StringView(const char *_data, usize _size) : data(_data), size(_size) { } bool equals(const StringView &other) const { if(size != other.size) return false; return memcmp(data, other.data, size) == 0; } size_t hash() const { #if defined(CISB_ENV_64BIT) return XXH64(data, size, 0xdec05eba); #else return XXH32(data, size, 0xdec05eba); #endif } char operator [] (size_t index) const { assert(index < size); return data[index]; } const char *data; usize size; }; struct StringViewHash { size_t operator()(const StringView &stringView) const { return stringView.hash(); } }; struct StringViewCompare { public: bool operator()(const StringView &lhs, const StringView &rhs) const { return lhs.equals(rhs); } }; template using StringViewMap = std::unordered_map; } #endif // SIBS_STRING_VIEW_HPP