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, a constexpr constructor will be available in String. It is an
33 // experimental change that needs some more work to be stable.
34 #define STRING_WITH_CONSTEXPR_CONSTRUCTOR 0
35 
36 // If non-zero, all copy-construction will be disabled. An explicit call to
37 // copy() is needed to actively copy a String object in that case. This can be
38 // helpful for removing copies that are not necessary.
39 #define STRING_DISABLE_COPY_CONSTRUCTION 0
40 
41 // Disable just-in-time hashing on all string objects, which causes String
42 // creation (including from substrings and copies) to be much slower, but can
43 // avoid many re-hashes on const String objects that would otherwise be unable
44 // to store the hash.
45 #define STRING_DISABLE_JIT_HASHING 0
46 
50 {
51  public:
53  String();
54  String(const char *s);
55  String(const char *s, size_t length);
56 #if STRING_DISABLE_COPY_CONSTRUCTION
57  String(const String &x) = delete;
58 #else
59  String(const String &x);
60 #endif
61  String(String &&x);
62  ~String();
63 
64 #if STRING_WITH_CONSTEXPR_CONSTRUCTOR
65  template <size_t N>
66  constexpr String(const char (&s)[N]);
67 #endif
68 
69  String &operator=(String &&x);
70 #if !STRING_DISABLE_COPY_CONSTRUCTION
71  String &operator=(const String &x);
72  String &operator=(const char *s);
73 #endif
74  operator const char *() const
75  {
76  if (m_Size == StaticSize)
77  return m_Static;
78  else if (m_Data == 0)
79  return "";
80  else
81  return m_Data;
82  }
85  operator StringView() const
86  {
87  return view();
88  }
89  String &operator+=(const String &x);
90  String &operator+=(const char *s);
91 
92  bool operator==(const String &s) const;
93  bool operator==(const StringView &s) const;
94  bool operator==(const char *s) const;
95 
96  size_t length() const
97  {
98  return m_Length;
99  }
100 
101  size_t size() const
102  {
103  return m_Size;
104  }
105 
110  uint32_t hash() const;
111 
113  uint32_t hash();
114 
117  size_t nextCharacter(size_t c) const;
118 
121  size_t prevCharacter(size_t c) const;
122 
124  void lchomp();
125 
127  void chomp();
128 
130  void strip();
131 
133  void lstrip();
134 
136  void rstrip();
137 
141  String split(size_t offset);
142  void split(size_t offset, String &back);
143 
144  Vector<String> tokenise(char token);
145  void tokenise(char token, Vector<String> &output) const;
147  void tokenise(char token, Vector<StringView> &output) const;
148 
153  static size_t Utf32ToUtf8(uint32_t utf32, char *utf8);
154 
155  void Format(const char *format, ...) FORMAT(printf, 2, 3);
156 
157  void assign(const String &x);
164  void assign(const char *s, size_t len = 0, bool unsafe = false);
165  void reserve(size_t size);
166  void free();
167 
169  bool endswith(const char c) const;
170  bool endswith(const String &s) const;
171  bool endswith(const char *s, size_t len = 0) const;
172 
174  bool startswith(const char c) const;
175  bool startswith(const String &s) const;
176  bool startswith(const char *s, size_t len = 0) const;
177 
179  ssize_t find(const char c) const;
180  ssize_t rfind(const char c) const;
181 
183  String copy() const;
184 
188  StringView view() const;
189 
190  private:
192  void reserve(size_t size, bool zero);
194  char *extract() const;
196  void computeHash();
198  uint32_t computeHash() const;
200  void move(String &&other);
202  static constexpr const size_t StaticSize = 64;
204  char *m_Data;
206  const char *m_ConstData;
208  size_t m_Length;
210  size_t m_Size;
212  char m_Static[StaticSize];
216  bool iswhitespace(const char c) const;
217 
219  uint32_t m_Hash;
220 };
221 
222 #if STRING_WITH_CONSTEXPR_CONSTRUCTOR
223 template <size_t N>
224 constexpr String::String(const char (&s)[N])
225  : m_Data(nullptr), m_ConstData(s), m_Length(N ? N - 1 : 0), m_Size(N),
226  m_HeapData(false)
227 {
228 }
229 #endif
230 
233 #endif
const char * m_ConstData
Definition: String.h:206
A vector / dynamic array.
Definition: String.h:49
bool m_HeapData
Definition: String.h:214
size_t m_Length
Definition: String.h:208
char * m_Data
Definition: String.h:204
uint32_t m_Hash
Definition: String.h:219
String()
Definition: String.cc:27
size_t m_Size
Definition: String.h:210
#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:326