The Pedigree Project 0.1
String.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_STRING_H
21#define KERNEL_UTILITIES_STRING_H
22
26#include "pedigree/kernel/compiler.h"
27#include "pedigree/kernel/processor/types.h"
28#include "pedigree/kernel/utilities/StringView.h"
29#include "pedigree/kernel/utilities/Vector.h"
30#include "pedigree/kernel/utilities/template.h" // IWYU pragma: keep
31
32// If non-zero, disable expensive forms of copy construction.
33#ifdef IN_STRING_TESTSUITE
34#define STRING_DISABLE_EXPENSIVE_COPY_CONSTRUCTION 0
35#else
36#define STRING_DISABLE_EXPENSIVE_COPY_CONSTRUCTION 1
37#endif
38
39class Cord;
40
43class EXPORTED_PUBLIC String {
44 public:
46 String();
47 // The constructors marked explicit are slow, but sometimes the only way
48 // to instantiate a String object. The intention to these being explicit
49 // is that they will not be used accidentally.
50 explicit String(const char* s);
51 explicit String(const char* s, size_t length);
52 explicit String(const char* s, size_t length, bool unsafe);
53 explicit String(const Cord& x);
54 String(const String& x);
55 explicit String(const StringView& x);
56 String(String&& x) noexcept;
57 virtual ~String();
58
59 String& operator=(String&& x) noexcept;
60 String& operator=(const Cord& x);
61 String& operator=(const String& x);
62#if STRING_DISABLE_EXPENSIVE_COPY_CONSTRUCTION
63 String& operator=(const char* s) = delete;
64#else
65 String& operator=(const char* s);
66#endif
71 explicit operator const char*() const {
72 return cstr();
73 }
76 operator StringView() const {
77 return view();
78 }
79 String& operator+=(const String& x);
80 String& operator+=(const char* s);
81
82 bool operator==(const String& s) const;
83 bool operator==(const StringView& s) const;
84 bool operator==(const char* s) const;
85
87 bool compare(const char* s, size_t len) const;
88
95 template <size_t N>
96 bool compare(const char (&s)[N]) const {
97 // ignore null in c-string for comparison
98 return compare(s, N - 1);
99 }
100
101 char operator[](size_t i) const;
102
103 const char* cstr() const {
104 return extract();
105 }
106
107 size_t length() const {
108 return m_Length;
109 }
110
111 size_t size() const {
112 return m_Size;
113 }
114
119 uint32_t hash() const;
120
122 uint32_t hash();
123
126 size_t nextCharacter(size_t c) const;
127
130 size_t prevCharacter(size_t c) const;
131
133 void lchomp();
134
136 void chomp();
137
139 void strip();
140
142 void lstrip();
143
145 void rstrip();
146
148 void ltrim(size_t n);
149
151 void rtrim(size_t n);
152
156 String split(size_t offset);
157 void split(size_t offset, String& back);
158
159 Vector<String> tokenise(char token);
160 void tokenise(char token, Vector<String>& output) const;
162 void tokenise(char token, Vector<StringView>& output) const;
163
168 static size_t Utf32ToUtf8(uint32_t utf32, char* utf8);
169
170 void Format(const char* format, ...) FORMAT(printf, 2, 3);
171
172 template <size_t N>
173 void assign(const char (&s)[N]) {
174 assign(s, N, true);
175 }
176
177 void assign(const String& x);
178 void assign(const Cord& x);
185 void assign(const char* s, size_t len = 0, bool unsafe = false);
186 void reserve(size_t size);
187 virtual void clear() noexcept;
188
190 void downsize();
191
193 bool endswith(const char c) const;
194 bool endswith(const String& s) const;
195 bool endswith(const char* s, size_t len = 0) const;
196
198 bool startswith(const char c) const;
199 bool startswith(const String& s) const;
200 bool startswith(const char* s, size_t len = 0) const;
201
203 ssize_t find(const char c) const;
204 ssize_t rfind(const char c) const;
205
207 String copy() const;
208
212 StringView view() const;
213
214 protected:
216 bool iswhitespace(const char c) const;
218 void reserve(size_t size, bool zero);
220 void computeHash();
222 uint32_t computeHash() const;
223
224 private:
226 uint32_t maybeHash() const;
228 virtual char* extract() const;
230 void move(String&& other) noexcept;
232 char* m_Data;
234 size_t m_Length;
236 size_t m_Size;
238 uint32_t m_Hash;
240 virtual bool resizable() const;
242 virtual bool assignable() const;
243
244 protected:
246 void setLength(size_t n);
248 void setSize(size_t n);
249};
250
256template <size_t N>
257class ConstantString : public String {
258 public:
259 ConstantString(const char str[N]) {
260 setLength(N - 1);
261 setSize(N);
262 MemoryCopy(m_Data, str, N);
263 computeHash();
264 }
265
266 virtual void clear() noexcept override {}
267
268 private:
269 virtual char* extract() const override {
270 return const_cast<char*>(m_Data);
271 }
272
273 virtual bool resizable() const override {
274 return false;
275 }
276
277 virtual bool assignable() const override {
278 return false;
279 }
280
281 char m_Data[N];
282};
283
284template <size_t N>
285ConstantString<N> MakeConstantString(const char (&str)[N]) {
286 return ConstantString<N>(str);
287}
288
291#endif
virtual char * extract() const override
Definition String.h:269
virtual bool assignable() const override
Definition String.h:277
virtual bool resizable() const override
Definition String.h:273
Definition Cord.h:33
bool compare(const char(&s)[N]) const
Definition String.h:96
A vector / dynamic array.
Definition Vector.h:33
#define FORMAT(type, idx, first)
bool operator==(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T1 > &x1, const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x2)
Definition Iterator.h:256