The Pedigree Project 0.1
StringView.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef KERNEL_UTILITIES_STRINGVIEW_H
21#define KERNEL_UTILITIES_STRINGVIEW_H
22
26#define HASH_STRINGVIEWS_BY_DEFAULT false
27
28#include "pedigree/kernel/compiler.h"
29#include "pedigree/kernel/processor/types.h"
30#include "pedigree/kernel/utilities/template.h" // IWYU pragma: keep
31
32class String;
33
34class EXPORTED_PUBLIC StringView {
35 friend class String;
36
37 public:
38 StringView();
39 explicit StringView(const char* s);
40 StringView(const char* s, size_t length);
41 StringView(const StringView& other);
42 explicit StringView(const String& other);
43 virtual ~StringView();
44
45 StringView& operator=(const StringView& s);
46
47 bool operator==(const char* s) const;
48 bool operator==(const String& s) const;
49 bool operator==(const StringView& s) const;
50
55 bool compare(const char* s, size_t length) const;
56
57 size_t length() const;
58
60 StringView substring(size_t start, size_t end, bool hashed = HASH_STRINGVIEWS_BY_DEFAULT) const;
61
62 String toString() const;
63
64 char operator[](size_t index) const;
65
66 size_t nextCharacter(size_t i) const;
67 size_t prevCharacter(size_t i) const;
68
69 uint32_t hash() const;
70 uint32_t hash();
71
72 const char* str() const;
73
79 void setHashingEnable(bool enabled);
80
81 private:
82 bool compareHash(const StringView& other) const;
83 bool compareHash(const String& other) const;
84
85 uint32_t computeHash() const;
86
87 // String::view() can set m_Hash to avoid recalculating.
88 StringView(const char* s, size_t length, uint32_t hash, bool hashingEnabled);
89
90 const char* m_String;
91 size_t m_Length;
92 uint32_t m_Hash;
93 bool m_HashingEnabled;
94
95 protected:
96 virtual bool defaultHashingEnabled() const;
97};
98
105class EXPORTED_PUBLIC HashedStringView : public StringView {
106 public:
107 HashedStringView() = default;
108 explicit HashedStringView(const char* s);
109 HashedStringView(const char* s, size_t length);
110 HashedStringView(const StringView& other);
112 HashedStringView(const String& other);
113
114 private:
115 bool defaultHashingEnabled() const override;
116};
117
120#endif // KERNEL_UTILITIES_STRINGVIEW_H
bool operator==(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T1 > &x1, const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x2)
Definition Iterator.h:256