1 //===-- ConstString.h -------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_ConstString_h_
10 #define liblldb_ConstString_h_
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/DenseMapInfo.h"
14 #include "llvm/Support/FormatVariadic.h"
18 namespace lldb_private {
25 namespace lldb_private {
27 /// \class ConstString ConstString.h "lldb/Utility/ConstString.h"
28 /// A uniqued constant string class.
30 /// Provides an efficient way to store strings as uniqued strings. After the
31 /// strings are uniqued, finding strings that are equal to one another is very
32 /// fast as just the pointers need to be compared. It also allows for many
33 /// common strings from many different sources to be shared to keep the memory
36 /// No reference counting is done on strings that are added to the string
37 /// pool, once strings are added they are in the string pool for the life of
41 /// Default constructor
43 /// Initializes the string to an empty string.
44 ConstString() : m_string(nullptr) {}
48 /// Copies the string value in \a rhs into this object.
51 /// Another string object to copy.
52 ConstString(const ConstString &rhs) : m_string(rhs.m_string) {}
54 explicit ConstString(const llvm::StringRef &s);
56 /// Construct with C String value
58 /// Constructs this object with a C string by looking to see if the
59 /// C string already exists in the global string pool. If it doesn't
60 /// exist, it is added to the string pool.
63 /// A NULL terminated C string to add to the string pool.
64 explicit ConstString(const char *cstr);
66 /// Construct with C String value with max length
68 /// Constructs this object with a C string with a length. If \a max_cstr_len
69 /// is greater than the actual length of the string, the string length will
70 /// be truncated. This allows substrings to be created without the need to
71 /// NULL terminate the string as it is passed into this function.
74 /// A pointer to the first character in the C string. The C
75 /// string can be NULL terminated in a buffer that contains
76 /// more characters than the length of the string, or the
77 /// string can be part of another string and a new substring
80 /// \param[in] max_cstr_len
81 /// The max length of \a cstr. If the string length of \a cstr
82 /// is less than \a max_cstr_len, then the string will be
83 /// truncated. If the string length of \a cstr is greater than
84 /// \a max_cstr_len, then only max_cstr_len bytes will be used
86 explicit ConstString(const char *cstr, size_t max_cstr_len);
90 /// Since constant string values are currently not reference counted, there
91 /// isn't much to do here.
92 ~ConstString() = default;
94 /// C string equality binary predicate function object for ConstString
96 struct StringIsEqual {
99 /// Two C strings are equal when they are contained in ConstString objects
100 /// when their pointer values are equal to each other.
103 /// Returns \b true if the C string in \a lhs is equal to
104 /// the C string value in \a rhs, \b false otherwise.
105 bool operator()(const char *lhs, const char *rhs) const {
110 /// Convert to bool operator.
112 /// This allows code to check a ConstString object to see if it contains a
113 /// valid string using code such as:
116 /// ConstString str(...);
122 /// /b True this object contains a valid non-empty C string, \b
124 explicit operator bool() const { return !IsEmpty(); }
126 /// Assignment operator
128 /// Assigns the string in this object with the value from \a rhs.
131 /// Another string object to copy into this object.
134 /// A const reference to this object.
135 ConstString operator=(ConstString rhs) {
136 m_string = rhs.m_string;
140 /// Equal to operator
142 /// Returns true if this string is equal to the string in \a rhs. This
143 /// operation is very fast as it results in a pointer comparison since all
144 /// strings are in a uniqued in a global string pool.
147 /// Another string object to compare this object to.
150 /// true if this object is equal to \a rhs.
151 /// false if this object is not equal to \a rhs.
152 bool operator==(ConstString rhs) const {
153 // We can do a pointer compare to compare these strings since they must
154 // come from the same pool in order to be equal.
155 return m_string == rhs.m_string;
158 /// Equal to operator against a non-ConstString value.
160 /// Returns true if this string is equal to the string in \a rhs. This
161 /// overload is usually slower than comparing against a ConstString value.
162 /// However, if the rhs string not already a ConstString and it is impractical
163 /// to turn it into a non-temporary variable, then this overload is faster.
166 /// Another string object to compare this object to.
169 /// \b true if this object is equal to \a rhs.
170 /// \b false if this object is not equal to \a rhs.
171 bool operator==(const char *rhs) const {
172 // ConstString differentiates between empty strings and nullptr strings, but
173 // StringRef doesn't. Therefore we have to do this check manually now.
174 if (m_string == nullptr && rhs != nullptr)
176 if (m_string != nullptr && rhs == nullptr)
179 return GetStringRef() == rhs;
182 /// Not equal to operator
184 /// Returns true if this string is not equal to the string in \a rhs. This
185 /// operation is very fast as it results in a pointer comparison since all
186 /// strings are in a uniqued in a global string pool.
189 /// Another string object to compare this object to.
192 /// \b true if this object is not equal to \a rhs.
193 /// \b false if this object is equal to \a rhs.
194 bool operator!=(ConstString rhs) const {
195 return m_string != rhs.m_string;
198 /// Not equal to operator against a non-ConstString value.
200 /// Returns true if this string is not equal to the string in \a rhs. This
201 /// overload is usually slower than comparing against a ConstString value.
202 /// However, if the rhs string not already a ConstString and it is impractical
203 /// to turn it into a non-temporary variable, then this overload is faster.
206 /// Another string object to compare this object to.
208 /// \return \b true if this object is not equal to \a rhs, false otherwise.
209 bool operator!=(const char *rhs) const { return !(*this == rhs); }
211 bool operator<(ConstString rhs) const;
213 /// Get the string value as a C string.
215 /// Get the value of the contained string as a NULL terminated C string
218 /// If \a value_if_empty is nullptr, then nullptr will be returned.
220 /// \return Returns \a value_if_empty if the string is empty, otherwise
221 /// the C string value contained in this object.
222 const char *AsCString(const char *value_if_empty = nullptr) const {
223 return (IsEmpty() ? value_if_empty : m_string);
226 /// Get the string value as a llvm::StringRef
229 /// Returns a new llvm::StringRef object filled in with the
231 llvm::StringRef GetStringRef() const {
232 return llvm::StringRef(m_string, GetLength());
235 /// Get the string value as a C string.
237 /// Get the value of the contained string as a NULL terminated C string
238 /// value. Similar to the ConstString::AsCString() function, yet this
239 /// function will always return nullptr if the string is not valid. So this
240 /// function is a direct accessor to the string pointer value.
243 /// Returns nullptr the string is invalid, otherwise the C string
244 /// value contained in this object.
245 const char *GetCString() const { return m_string; }
247 /// Get the length in bytes of string value.
249 /// The string pool stores the length of the string, so we can avoid calling
250 /// strlen() on the pointer value with this function.
253 /// Returns the number of bytes that this string occupies in
254 /// memory, not including the NULL termination byte.
255 size_t GetLength() const;
257 /// Clear this object's state.
259 /// Clear any contained string and reset the value to the empty string
261 void Clear() { m_string = nullptr; }
263 /// Equal to operator
265 /// Returns true if this string is equal to the string in \a rhs. If case
266 /// sensitive equality is tested, this operation is very fast as it results
267 /// in a pointer comparison since all strings are in a uniqued in a global
271 /// The Left Hand Side const ConstString object reference.
274 /// The Right Hand Side const ConstString object reference.
276 /// \param[in] case_sensitive
277 /// Case sensitivity. If true, case sensitive equality
278 /// will be tested, otherwise character case will be ignored
280 /// \return \b true if this object is equal to \a rhs, \b false otherwise.
281 static bool Equals(ConstString lhs, ConstString rhs,
282 const bool case_sensitive = true);
284 /// Compare two string objects.
286 /// Compares the C string values contained in \a lhs and \a rhs and returns
287 /// an integer result.
289 /// NOTE: only call this function when you want a true string
290 /// comparison. If you want string equality use the, use the == operator as
291 /// it is much more efficient. Also if you want string inequality, use the
292 /// != operator for the same reasons.
295 /// The Left Hand Side const ConstString object reference.
298 /// The Right Hand Side const ConstString object reference.
300 /// \param[in] case_sensitive
301 /// Case sensitivity of compare. If true, case sensitive compare
302 /// will be performed, otherwise character case will be ignored
304 /// \return -1 if lhs < rhs, 0 if lhs == rhs, 1 if lhs > rhs
305 static int Compare(ConstString lhs, ConstString rhs,
306 const bool case_sensitive = true);
308 /// Dump the object description to a stream.
310 /// Dump the string value to the stream \a s. If the contained string is
311 /// empty, print \a value_if_empty to the stream instead. If \a
312 /// value_if_empty is nullptr, then nothing will be dumped to the stream.
315 /// The stream that will be used to dump the object description.
317 /// \param[in] value_if_empty
318 /// The value to dump if the string is empty. If nullptr, nothing
319 /// will be output to the stream.
320 void Dump(Stream *s, const char *value_if_empty = nullptr) const;
322 /// Dump the object debug description to a stream.
325 /// The stream that will be used to dump the object description.
326 void DumpDebug(Stream *s) const;
328 /// Test for empty string.
331 /// \b true if the contained string is empty.
332 /// \b false if the contained string is not empty.
333 bool IsEmpty() const { return m_string == nullptr || m_string[0] == '\0'; }
335 /// Test for null string.
338 /// \b true if there is no string associated with this instance.
339 /// \b false if there is a string associated with this instance.
340 bool IsNull() const { return m_string == nullptr; }
342 /// Set the C string value.
344 /// Set the string value in the object by uniquing the \a cstr string value
345 /// in our global string pool.
347 /// If the C string already exists in the global string pool, it finds the
348 /// current entry and returns the existing value. If it doesn't exist, it is
349 /// added to the string pool.
352 /// A NULL terminated C string to add to the string pool.
353 void SetCString(const char *cstr);
355 void SetString(const llvm::StringRef &s);
357 /// Set the C string value and its mangled counterpart.
359 /// Object files and debug symbols often use mangled string to represent the
360 /// linkage name for a symbol, function or global. The string pool can
361 /// efficiently store these values and their counterparts so when we run
362 /// into another instance of a mangled name, we can avoid calling the name
363 /// demangler over and over on the same strings and then trying to unique
366 /// \param[in] demangled
367 /// The demangled string to correlate with the \a mangled name.
369 /// \param[in] mangled
370 /// The already uniqued mangled ConstString to correlate the
371 /// soon to be uniqued version of \a demangled.
372 void SetStringWithMangledCounterpart(llvm::StringRef demangled,
373 ConstString mangled);
375 /// Retrieve the mangled or demangled counterpart for a mangled or demangled
378 /// Object files and debug symbols often use mangled string to represent the
379 /// linkage name for a symbol, function or global. The string pool can
380 /// efficiently store these values and their counterparts so when we run
381 /// into another instance of a mangled name, we can avoid calling the name
382 /// demangler over and over on the same strings and then trying to unique
385 /// \param[in] counterpart
386 /// A reference to a ConstString object that might get filled in
387 /// with the demangled/mangled counterpart.
390 /// /b True if \a counterpart was filled in with the counterpart
391 /// /b false otherwise.
392 bool GetMangledCounterpart(ConstString &counterpart) const;
394 /// Set the C string value with length.
396 /// Set the string value in the object by uniquing \a cstr_len bytes
397 /// starting at the \a cstr string value in our global string pool. If trim
398 /// is true, then \a cstr_len indicates a maximum length of the CString and
399 /// if the actual length of the string is less, then it will be trimmed.
401 /// If the C string already exists in the global string pool, it finds the
402 /// current entry and returns the existing value. If it doesn't exist, it is
403 /// added to the string pool.
406 /// A NULL terminated C string to add to the string pool.
408 /// \param[in] cstr_len
409 /// The maximum length of the C string.
410 void SetCStringWithLength(const char *cstr, size_t cstr_len);
412 /// Set the C string value with the minimum length between \a fixed_cstr_len
413 /// and the actual length of the C string. This can be used for data
414 /// structures that have a fixed length to store a C string where the string
415 /// might not be NULL terminated if the string takes the entire buffer.
416 void SetTrimmedCStringWithLength(const char *cstr, size_t fixed_cstr_len);
418 /// Get the memory cost of this object.
420 /// Return the size in bytes that this object takes in memory. This returns
421 /// the size in bytes of this object, which does not include any the shared
422 /// string values it may refer to.
425 /// The number of bytes that this object occupies in memory.
427 /// \see ConstString::StaticMemorySize ()
428 size_t MemorySize() const { return sizeof(ConstString); }
430 /// Get the size in bytes of the current global string pool.
432 /// Reports the size in bytes of all shared C string values, containers and
433 /// any other values as a byte size for the entire string pool.
436 /// The number of bytes that the global string pool occupies
438 static size_t StaticMemorySize();
441 template <typename T> friend struct ::llvm::DenseMapInfo;
442 /// Only used by DenseMapInfo.
443 static ConstString FromStringPoolPointer(const char *ptr) {
450 const char *m_string;
453 /// Stream the string value \a str to the stream \a s
454 Stream &operator<<(Stream &s, ConstString str);
456 } // namespace lldb_private
459 template <> struct format_provider<lldb_private::ConstString> {
460 static void format(const lldb_private::ConstString &CS, llvm::raw_ostream &OS,
461 llvm::StringRef Options);
464 /// DenseMapInfo implementation.
466 template <> struct DenseMapInfo<lldb_private::ConstString> {
467 static inline lldb_private::ConstString getEmptyKey() {
468 return lldb_private::ConstString::FromStringPoolPointer(
469 DenseMapInfo<const char *>::getEmptyKey());
471 static inline lldb_private::ConstString getTombstoneKey() {
472 return lldb_private::ConstString::FromStringPoolPointer(
473 DenseMapInfo<const char *>::getTombstoneKey());
475 static unsigned getHashValue(lldb_private::ConstString val) {
476 return DenseMapInfo<const char *>::getHashValue(val.m_string);
478 static bool isEqual(lldb_private::ConstString LHS,
479 lldb_private::ConstString RHS) {
486 #endif // liblldb_ConstString_h_