20 #include "pedigree/kernel/utilities/StringView.h" 21 #include "pedigree/kernel/Log.h" 22 #include "pedigree/kernel/utilities/String.h" 23 #include "pedigree/kernel/utilities/assert.h" 24 #include "pedigree/kernel/utilities/utility.h" 26 StringView::StringView() : m_String(nullptr), m_Length(0), m_HashingEnabled(HASH_STRINGVIEWS_BY_DEFAULT)
31 StringView::StringView(
const char *s)
32 : m_String(s),
m_Length(StringLength(s)),
m_Hash(0), m_HashingEnabled(HASH_STRINGVIEWS_BY_DEFAULT)
34 setHashingEnable(m_HashingEnabled);
37 StringView::StringView(
const char *s,
size_t length)
38 : m_String(s),
m_Length(length),
m_Hash(0), m_HashingEnabled(HASH_STRINGVIEWS_BY_DEFAULT)
40 setHashingEnable(m_HashingEnabled);
43 StringView::StringView(
const char *s,
size_t length, uint32_t
hash,
bool hashingEnabled)
44 : m_String(s),
m_Length(length),
m_Hash(hash), m_HashingEnabled(hashingEnabled)
48 StringView::StringView(
const StringView &other)
50 m_HashingEnabled(other.m_HashingEnabled)
58 StringView::~StringView() =
default;
62 m_String = s.m_String;
65 m_HashingEnabled = s.m_HashingEnabled;
69 bool StringView::operator==(
const char *s)
const 71 return compare(s, StringLength(s));
74 bool StringView::operator==(
const String &s)
const 80 else if (!compareHash(s))
85 return compare(static_cast<const char *>(s), s.length());
88 bool StringView::operator==(
const StringView &s)
const 98 else if (!compareHash(s))
103 return !StringMatchN(m_String, s.m_String,
m_Length);
123 return !StringMatchN(s, m_String,
m_Length);
126 size_t StringView::length()
const 143 if ((start >
m_Length) || (start >= end))
148 return StringView(m_String + start, end - start);
151 String StringView::toString()
const 156 char StringView::operator[](
size_t index)
const 158 #ifdef ADDITIONAL_CHECKS 161 ERROR(
"operator[] - index " << index <<
" exceeds length " <<
m_Length);
165 return m_String[index];
168 size_t StringView::nextCharacter(
size_t c)
const 170 return ::nextCharacter(m_String, c);
173 size_t StringView::prevCharacter(
size_t c)
const 175 return ::prevCharacter(m_String, c);
178 uint32_t StringView::hash()
const 190 uint32_t StringView::hash()
193 if (!m_HashingEnabled)
195 setHashingEnable(
true);
200 const char *StringView::str()
const 207 m_HashingEnabled = enabled;
219 bool StringView::compareHash(
const StringView &other)
const 221 if (!(m_HashingEnabled && other.m_HashingEnabled))
227 return hash() == other.hash();
231 bool StringView::compareHash(
const String &other)
const 233 if (!m_HashingEnabled)
243 uint32_t StringView::computeHash()
const 251 return spookyHash(m_String,
m_Length);
255 bool StringView::defaultHashingEnabled()
const 257 return HASH_STRINGVIEWS_BY_DEFAULT;
260 HashedStringView::HashedStringView(
const char *s) :
StringView(s)
262 setHashingEnable(
true);
265 HashedStringView::HashedStringView(
const char *s,
size_t length) :
StringView(s, length)
267 setHashingEnable(
true);
272 setHashingEnable(
true);
277 setHashingEnable(
true);
282 setHashingEnable(
true);
285 bool HashedStringView::defaultHashingEnabled()
const
StringView substring(size_t start, size_t end, bool hashed=HASH_STRINGVIEWS_BY_DEFAULT) const
void setHashingEnable(bool enabled)
bool compare(const char *s, size_t length) const