// sol2 // The MIT License (MIT) // Copyright (c) 2013-2017 Rapptz, ThePhD and contributors // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #ifndef SOL_STRING_VIEW_HPP #define SOL_STRING_VIEW_HPP #include "feature_test.hpp" #include #include #ifdef SOL_CXX17_FEATURES #include #endif // C++17 features #include #ifdef SOL_USE_BOOST #include #endif namespace sol { #ifdef SOL_CXX17_FEATURES typedef std::string_view string_view; typedef std::wstring_view wstring_view; typedef std::u16string_view u16string_view; typedef std::u32string_view u32string_view; typedef std::hash string_view_hash; #else template > struct basic_string_view { std::size_t s; const Char* p; basic_string_view(const std::string& r) : basic_string_view(r.data(), r.size()) { } basic_string_view(const Char* ptr) : basic_string_view(ptr, Traits::length(ptr)) { } basic_string_view(const Char* ptr, std::size_t sz) : s(sz), p(ptr) { } static int compare(const Char* lhs_p, std::size_t lhs_sz, const Char* rhs_p, std::size_t rhs_sz) { int result = Traits::compare(lhs_p, rhs_p, lhs_sz < rhs_sz ? lhs_sz : rhs_sz); if (result != 0) return result; if (lhs_sz < rhs_sz) return -1; if (lhs_sz > rhs_sz) return 1; return 0; } const Char* begin() const { return p; } const Char* end() const { return p + s; } const Char* cbegin() const { return p; } const Char* cend() const { return p + s; } const Char* data() const { return p; } std::size_t size() const { return s; } std::size_t length() const { return size(); } operator std::basic_string() const { return std::basic_string(data(), size()); } bool operator==(const basic_string_view& r) const { return compare(p, s, r.data(), r.size()) == 0; } bool operator==(const Char* r) const { return compare(r, Traits::length(r), p, s) == 0; } bool operator==(const std::basic_string& r) const { return compare(r.data(), r.size(), p, s) == 0; } bool operator!=(const basic_string_view& r) const { return !(*this == r); } bool operator!=(const char* r) const { return !(*this == r); } bool operator!=(const std::basic_string& r) const { return !(*this == r); } }; template > struct basic_string_view_hash { typedef basic_string_view argument_type; typedef std::size_t result_type; template result_type operator()(const std::basic_string& r) const { return (*this)(basic_string_view(r.c_str(), r.size())); } result_type operator()(const argument_type& r) const { #ifdef SOL_USE_BOOST return boost::hash_range(r.begin(), r.end()); #else // Modified, from libstdc++ // An implementation attempt at Fowler No Voll, 1a. // Supposedly, used in MSVC, // GCC (libstdc++) uses MurmurHash of some sort for 64-bit though...? // But, well. Can't win them all, right? // This should normally only apply when NOT using boost, // so this should almost never be tapped into... std::size_t hash = 0; const unsigned char* cptr = reinterpret_cast(r.data()); for (std::size_t sz = r.size(); sz != 0; --sz) { hash ^= static_cast(*cptr++); hash *= static_cast(1099511628211ULL); } return hash; #endif } }; } // namespace sol namespace std { template struct hash< ::sol::basic_string_view > : ::sol::basic_string_view_hash {}; } // namespace std namespace sol { using string_view = basic_string_view; using wstring_view = basic_string_view; using u16string_view = basic_string_view; using u32string_view = basic_string_view; using string_view_hash = std::hash; #endif // C++17 Support } // namespace sol #endif // SOL_STRING_VIEW_HPP