2017-12-20 17:58:32 +08:00
|
|
|
// sol2
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
// The MIT License (MIT)
|
2017-09-06 08:29:21 +08:00
|
|
|
|
|
|
|
// 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 <cstddef>
|
|
|
|
#include <string>
|
|
|
|
#ifdef SOL_CXX17_FEATURES
|
|
|
|
#include <string_view>
|
|
|
|
#endif // C++17 features
|
2018-01-29 11:21:13 +08:00
|
|
|
#include <functional>
|
|
|
|
#ifdef SOL_USE_BOOST
|
|
|
|
#include <boost/functional/hash.hpp>
|
|
|
|
#endif
|
2017-09-06 08:29:21 +08:00
|
|
|
|
|
|
|
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;
|
2018-01-29 11:21:13 +08:00
|
|
|
typedef std::hash<std::string_view> string_view_hash;
|
2017-09-06 08:29:21 +08:00
|
|
|
#else
|
|
|
|
template <typename Char, typename Traits = std::char_traits<Char>>
|
|
|
|
struct basic_string_view {
|
|
|
|
std::size_t s;
|
|
|
|
const Char* p;
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
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) {
|
|
|
|
}
|
2017-09-06 08:29:21 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-01-29 11:21:13 +08:00
|
|
|
operator std::basic_string<Char, Traits>() const {
|
2018-01-29 11:58:56 +08:00
|
|
|
return std::basic_string<Char, Traits>(data(), size());
|
2018-01-29 11:21:13 +08:00
|
|
|
}
|
|
|
|
|
2017-09-06 08:29:21 +08:00
|
|
|
bool operator==(const basic_string_view& r) const {
|
|
|
|
return compare(p, s, r.data(), r.size()) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Char* r) const {
|
2018-01-29 11:21:13 +08:00
|
|
|
return compare(r, Traits::length(r), p, s) == 0;
|
2017-09-06 08:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const std::basic_string<Char, Traits>& 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<Char, Traits>& r) const {
|
|
|
|
return !(*this == r);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-29 11:58:56 +08:00
|
|
|
template <typename Ch, typename Tr = std::char_traits<Ch>>
|
2018-01-29 11:21:13 +08:00
|
|
|
struct basic_string_view_hash {
|
|
|
|
typedef basic_string_view<Ch, Tr> argument_type;
|
|
|
|
typedef std::size_t result_type;
|
|
|
|
|
|
|
|
template <typename Al>
|
|
|
|
result_type operator()(const std::basic_string<Ch, Tr, Al>& r) const {
|
2018-01-31 00:02:46 +08:00
|
|
|
return (*this)(argument_type(r.c_str(), r.size()));
|
2018-01-29 11:21:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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<const unsigned char*>(r.data());
|
|
|
|
for (std::size_t sz = r.size(); sz != 0; --sz) {
|
|
|
|
hash ^= static_cast<size_t>(*cptr++);
|
|
|
|
hash *= static_cast<size_t>(1099511628211ULL);
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace sol
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template <typename Ch, typename Tr>
|
|
|
|
struct hash< ::sol::basic_string_view<Ch, Tr> > : ::sol::basic_string_view_hash<Ch, Tr> {};
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
namespace sol {
|
2017-09-06 08:29:21 +08:00
|
|
|
using string_view = basic_string_view<char>;
|
|
|
|
using wstring_view = basic_string_view<wchar_t>;
|
|
|
|
using u16string_view = basic_string_view<char16_t>;
|
|
|
|
using u32string_view = basic_string_view<char32_t>;
|
2018-01-29 11:21:13 +08:00
|
|
|
using string_view_hash = std::hash<string_view>;
|
2017-09-06 08:29:21 +08:00
|
|
|
#endif // C++17 Support
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace sol
|
2017-09-06 08:29:21 +08:00
|
|
|
|
|
|
|
#endif // SOL_STRING_VIEW_HPP
|