#ifndef wasm_support_istring_h
#define wasm_support_istring_h
#include <set>
#include <string_view>
#include <unordered_set>
#include <assert.h>
#include "threads.h"
#include "utilities.h"
namespace wasm {
struct IString {
private:
static std::string_view interned(std::string_view s, bool reuse = true);
public:
const std::string_view str;
IString() = default;
IString(std::string_view str, bool reuse = true)
: str(interned(str, reuse)) {}
IString(const char* str) : str(interned(str, false)) {}
IString(const std::string& str) : str(interned(str, false)) {}
IString(const IString& other) = default;
IString& operator=(const IString& other) {
return *(new (this) IString(other));
}
bool operator==(const IString& other) const {
return str.data() == other.str.data();
}
bool operator!=(const IString& other) const { return !(*this == other); }
bool operator<(const IString& other) const { return str < other.str; }
bool operator<=(const IString& other) const { return str <= other.str; }
bool operator>(const IString& other) const { return str > other.str; }
bool operator>=(const IString& other) const { return str >= other.str; }
char operator[](int x) const { return str[x]; }
explicit operator bool() const { return str.data() != nullptr; }
bool is() const { return bool(*this); }
bool isNull() const { return !bool(*this); }
std::string toString() const { return {str.data(), str.size()}; }
bool equals(std::string_view other) const { return str == other; }
bool startsWith(std::string_view prefix) const {
return str.substr(0, prefix.size()) == prefix;
}
bool startsWith(IString str) const { return startsWith(str.str); }
template<int N> bool startsWith(const char (&str)[N]) {
return startsWith(std::string_view(str));
}
size_t size() const { return str.size(); }
};
}
namespace std {
template<> struct hash<wasm::IString> {
size_t operator()(const wasm::IString& str) const {
return std::hash<size_t>{}(uintptr_t(str.str.data()));
}
};
inline std::ostream& operator<<(std::ostream& os, const wasm::IString& str) {
return os << str.str;
}
}
#endif