20#include "pedigree/kernel/Log.h"
21#include "pedigree/kernel/utilities/String.h"
22#include "pedigree/kernel/utilities/StringView.h"
23#include "pedigree/kernel/utilities/assert.h"
24#include "pedigree/kernel/utilities/utility.h"
26StringView::StringView()
27 : m_String(nullptr), m_Length(0), m_HashingEnabled(HASH_STRINGVIEWS_BY_DEFAULT) {
31StringView::StringView(
const char* s)
33 m_Length(StringLength(s)),
35 m_HashingEnabled(HASH_STRINGVIEWS_BY_DEFAULT) {
36 setHashingEnable(m_HashingEnabled);
39StringView::StringView(
const char* s,
size_t length)
40 : m_String(s), m_Length(length), m_Hash(0), m_HashingEnabled(HASH_STRINGVIEWS_BY_DEFAULT) {
41 setHashingEnable(m_HashingEnabled);
44StringView::StringView(
const char* s,
size_t length, uint32_t hash,
bool hashingEnabled)
45 : m_String(s), m_Length(length), m_Hash(hash), m_HashingEnabled(hashingEnabled) {}
48 : m_String(other.m_String),
49 m_Length(other.m_Length),
51 m_HashingEnabled(other.m_HashingEnabled) {}
55StringView::~StringView() =
default;
58 m_String = s.m_String;
59 m_Length = s.m_Length;
61 m_HashingEnabled = s.m_HashingEnabled;
65bool StringView::operator==(
const char* s)
const {
66 return compare(s, StringLength(s));
69bool StringView::operator==(
const String& s)
const {
70 if (m_Length != s.length()) {
72 }
else if (!compareHash(s)) {
76 return compare(
static_cast<const char*
>(s), s.length());
79bool StringView::operator==(
const StringView& s)
const {
80 if (m_Length != s.m_Length) {
82 }
else if (!(m_Length && s.m_Length)) {
84 }
else if (!compareHash(s)) {
88 return !StringMatchN(m_String, s.m_String, m_Length);
100 if (length != m_Length) {
106 return !StringMatchN(s, m_String, m_Length);
109size_t StringView::length()
const {
114 if (start == 0 && end == m_Length) {
115 return StringView(m_String, m_Length, hashed ? hash() : 0, hashed);
118 if (end > m_Length) {
122 if ((start > m_Length) || (start >= end)) {
126 StringView result(m_String + start, end - start);
131String StringView::toString()
const {
132 return String(m_String, m_Length,
true);
135char StringView::operator[](
size_t index)
const {
138 ERROR(
"operator[] - index " << index <<
" exceeds length " << m_Length);
139 assert(index < m_Length);
142 return m_String[index];
145size_t StringView::nextCharacter(
size_t c)
const {
146 return ::nextCharacter(m_String, c);
149size_t StringView::prevCharacter(
size_t c)
const {
150 return ::prevCharacter(m_String, c);
153uint32_t StringView::hash()
const {
157 return computeHash();
161uint32_t StringView::hash() {
163 if (!m_HashingEnabled) {
169const char* StringView::str()
const {
174 m_HashingEnabled = enabled;
178 }
else if (!m_Hash) {
179 m_Hash = computeHash();
183bool StringView::compareHash(
const StringView& other)
const {
184 if (!(m_HashingEnabled && other.m_HashingEnabled)) {
187 return hash() == other.hash();
191bool StringView::compareHash(
const String& other)
const {
192 if (!m_HashingEnabled) {
195 return hash() == other.hash();
199uint32_t StringView::computeHash()
const {
203 return spookyHash(m_String, m_Length);
207bool StringView::defaultHashingEnabled()
const {
208 return HASH_STRINGVIEWS_BY_DEFAULT;
211HashedStringView::HashedStringView(
const char* s) :
StringView(s) {
215HashedStringView::HashedStringView(
const char* s,
size_t length) :
StringView(s, length) {
231bool HashedStringView::defaultHashingEnabled()
const {
void setHashingEnable(bool enabled)
bool compare(const char *s, size_t length) const
StringView substring(size_t start, size_t end, bool hashed=HASH_STRINGVIEWS_BY_DEFAULT) const