diff options
author | dec05eba <dec05eba@protonmail.com> | 2017-12-09 01:14:41 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2017-12-09 01:14:47 +0100 |
commit | fb2072deb3e50afdb062570a3a80ec1afb5bfb56 (patch) | |
tree | 44baddd61a9aac3ead96ac8a6fab95d087d0bdd7 /include/StringView.hpp | |
parent | bf24f6fc48b4eebb06cdcd7029d1d31d4c6028dd (diff) |
Finished project config file parsing
Diffstat (limited to 'include/StringView.hpp')
-rw-r--r-- | include/StringView.hpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/include/StringView.hpp b/include/StringView.hpp new file mode 100644 index 0000000..adfa237 --- /dev/null +++ b/include/StringView.hpp @@ -0,0 +1,76 @@ +#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 <cstring> +#include <unordered_map> + +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 + } + + 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 <typename ValueType> + using StringViewMap = std::unordered_map<const StringView, ValueType, StringViewHash, StringViewCompare>; +} + +#endif // SIBS_STRING_VIEW_HPP |