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 
32 class String;
33 
35 {
36  friend class String;
37 
38  public:
39  StringView();
40  explicit StringView(const char *s);
41  StringView(const char *s, size_t length);
42  StringView(const StringView &other);
43  explicit StringView(const String &other);
44  virtual ~StringView();
45 
46  StringView &operator=(const StringView &s);
47 
48  bool operator==(const char *s) const;
49  bool operator==(const String &s) const;
50  bool operator==(const StringView &s) const;
51 
56  bool compare(const char *s, size_t length) const;
57 
58  size_t length() const;
59 
61  StringView substring(size_t start, size_t end, bool hashed = HASH_STRINGVIEWS_BY_DEFAULT) const;
62 
63  String toString() const;
64 
65  char operator[](size_t index) const;
66 
67  size_t nextCharacter(size_t i) const;
68  size_t prevCharacter(size_t i) const;
69 
70  uint32_t hash() const;
71  uint32_t hash();
72 
73  const char *str() const;
74 
80  void setHashingEnable(bool enabled);
81 
82  private:
83  bool compareHash(const StringView &other) const;
84  bool compareHash(const String &other) const;
85 
86  uint32_t computeHash() const;
87 
88  // String::view() can set m_Hash to avoid recalculating.
89  StringView(const char *s, size_t length, uint32_t hash, bool hashingEnabled);
90 
91  const char *m_String;
92  size_t m_Length;
93  uint32_t m_Hash;
94  bool m_HashingEnabled;
95 
96  protected:
97  virtual bool defaultHashingEnabled() const;
98 };
99 
107 {
108  public:
109  HashedStringView() = default;
110  explicit HashedStringView(const char *s);
111  HashedStringView(const char *s, size_t length);
112  HashedStringView(const StringView &other);
113  HashedStringView(const HashedStringView &other);
114  HashedStringView(const String &other);
115 
116  private:
117  bool defaultHashingEnabled() const override;
118 };
119 
122 #endif // KERNEL_UTILITIES_STRINGVIEW_H
uint32_t hash() const
Definition: String.cc:218
Definition: String.h:49
size_t nextCharacter(size_t c) const
Definition: String.cc:238
size_t m_Length
Definition: String.h:208
size_t prevCharacter(size_t c) const
Definition: String.cc:244
uint32_t m_Hash
Definition: String.h:219
void computeHash()
Definition: String.cc:849
bool operator==(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T1 > &x1, const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x2)
Definition: Iterator.h:326