1 //===-- ConstString.h -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ConstString_h_
11 #define liblldb_ConstString_h_
15 // Other libraries and framework includes
16 #include "llvm/ADT/StringRef.h"
19 #include "lldb/lldb-private.h"
21 namespace lldb_private {
23 //----------------------------------------------------------------------
24 /// @class ConstString ConstString.h "lldb/Core/ConstString.h"
25 /// @brief A uniqued constant string class.
27 /// Provides an efficient way to store strings as uniqued strings. After
28 /// the strings are uniqued, finding strings that are equal to one
29 /// another is very fast as just the pointers need to be compared. It
30 /// also allows for many common strings from many different sources to
31 /// be shared to keep the memory footprint low.
33 /// No reference counting is done on strings that are added to the
34 /// string pool, once strings are added they are in the string pool for
35 /// the life of the program.
36 //----------------------------------------------------------------------
40 //------------------------------------------------------------------
41 /// Default constructor
43 /// Initializes the string to an empty string.
44 //------------------------------------------------------------------
50 //------------------------------------------------------------------
53 /// Copies the string value in \a rhs into this object.
56 /// Another string object to copy.
57 //------------------------------------------------------------------
58 ConstString (const ConstString& rhs) :
59 m_string (rhs.m_string)
63 explicit ConstString (const llvm::StringRef &s);
65 //------------------------------------------------------------------
66 /// Construct with C String value
68 /// Constructs this object with a C string by looking to see if the
69 /// C string already exists in the global string pool. If it doesn't
70 /// exist, it is added to the string pool.
73 /// A NULL terminated C string to add to the string pool.
74 //------------------------------------------------------------------
75 explicit ConstString (const char *cstr);
77 //------------------------------------------------------------------
78 /// Construct with C String value with max length
80 /// Constructs this object with a C string with a length. If
81 /// \a max_cstr_len is greater than the actual length of the string,
82 /// the string length will be truncated. This allows substrings to
83 /// be created without the need to NULL terminate the string as it
84 /// is passed into this function.
87 /// A pointer to the first character in the C string. The C
88 /// string can be NULL terminated in a buffer that contains
89 /// more characters than the length of the string, or the
90 /// string can be part of another string and a new substring
93 /// @param[in] max_cstr_len
94 /// The max length of \a cstr. If the string length of \a cstr
95 /// is less than \a max_cstr_len, then the string will be
96 /// truncated. If the string length of \a cstr is greater than
97 /// \a max_cstr_len, then only max_cstr_len bytes will be used
99 //------------------------------------------------------------------
100 explicit ConstString (const char *cstr, size_t max_cstr_len);
102 //------------------------------------------------------------------
105 /// Since constant string values are currently not reference counted,
106 /// there isn't much to do here.
107 //------------------------------------------------------------------
108 ~ConstString() = default;
110 //----------------------------------------------------------------------
111 /// C string equality binary predicate function object for ConstString
113 //----------------------------------------------------------------------
116 //--------------------------------------------------------------
119 /// Two C strings are equal when they are contained in ConstString
120 /// objects when their pointer values are equal to each other.
123 /// Returns \b true if the C string in \a lhs is equal to
124 /// the C string value in \a rhs, \b false otherwise.
125 //--------------------------------------------------------------
126 bool operator()(const char* lhs, const char* rhs) const
132 //------------------------------------------------------------------
133 /// Convert to bool operator.
135 /// This allows code to check a ConstString object to see if it
136 /// contains a valid string using code such as:
139 /// ConstString str(...);
145 /// /b True this object contains a valid non-empty C string, \b
147 //------------------------------------------------------------------
148 explicit operator bool() const
150 return m_string && m_string[0];
153 //------------------------------------------------------------------
154 /// Assignment operator
156 /// Assigns the string in this object with the value from \a rhs.
159 /// Another string object to copy into this object.
162 /// A const reference to this object.
163 //------------------------------------------------------------------
165 operator = (const ConstString& rhs)
167 m_string = rhs.m_string;
171 //------------------------------------------------------------------
172 /// Equal to operator
174 /// Returns true if this string is equal to the string in \a rhs.
175 /// This operation is very fast as it results in a pointer
176 /// comparison since all strings are in a uniqued in a global string
180 /// Another string object to compare this object to.
183 /// @li \b true if this object is equal to \a rhs.
184 /// @li \b false if this object is not equal to \a rhs.
185 //------------------------------------------------------------------
187 operator == (const ConstString& rhs) const
189 // We can do a pointer compare to compare these strings since they
190 // must come from the same pool in order to be equal.
191 return m_string == rhs.m_string;
194 //------------------------------------------------------------------
195 /// Not equal to operator
197 /// Returns true if this string is not equal to the string in \a rhs.
198 /// This operation is very fast as it results in a pointer
199 /// comparison since all strings are in a uniqued in a global string
203 /// Another string object to compare this object to.
206 /// @li \b true if this object is not equal to \a rhs.
207 /// @li \b false if this object is equal to \a rhs.
208 //------------------------------------------------------------------
210 operator != (const ConstString& rhs) const
212 return m_string != rhs.m_string;
216 operator < (const ConstString& rhs) const;
218 //------------------------------------------------------------------
219 /// Get the string value as a C string.
221 /// Get the value of the contained string as a NULL terminated C
224 /// If \a value_if_empty is nullptr, then nullptr will be returned.
227 /// Returns \a value_if_empty if the string is empty, otherwise
228 /// the C string value contained in this object.
229 //------------------------------------------------------------------
231 AsCString(const char *value_if_empty = nullptr) const
233 return (IsEmpty() ? value_if_empty : m_string);
236 //------------------------------------------------------------------
237 /// Get the string value as a llvm::StringRef
240 /// Returns a new llvm::StringRef object filled in with the
242 //------------------------------------------------------------------
244 GetStringRef () const
246 return llvm::StringRef (m_string, GetLength());
249 //------------------------------------------------------------------
250 /// Get the string value as a C string.
252 /// Get the value of the contained string as a NULL terminated C
253 /// string value. Similar to the ConstString::AsCString() function,
254 /// yet this function will always return nullptr if the string is not
255 /// valid. So this function is a direct accessor to the string
259 /// Returns nullptr the string is invalid, otherwise the C string
260 /// value contained in this object.
261 //------------------------------------------------------------------
268 //------------------------------------------------------------------
269 /// Get the length in bytes of string value.
271 /// The string pool stores the length of the string, so we can avoid
272 /// calling strlen() on the pointer value with this function.
275 /// Returns the number of bytes that this string occupies in
276 /// memory, not including the NULL termination byte.
277 //------------------------------------------------------------------
281 //------------------------------------------------------------------
282 /// Clear this object's state.
284 /// Clear any contained string and reset the value to the an empty
286 //------------------------------------------------------------------
293 //------------------------------------------------------------------
294 /// Compare two string objects.
296 /// Compares the C string values contained in \a lhs and \a rhs and
297 /// returns an integer result.
299 /// NOTE: only call this function when you want a true string
300 /// comparison. If you want string equality use the, use the ==
301 /// operator as it is much more efficient. Also if you want string
302 /// inequality, use the != operator for the same reasons.
305 /// The Left Hand Side const ConstString object reference.
308 /// The Right Hand Side const ConstString object reference.
311 /// @li -1 if lhs < rhs
312 /// @li 0 if lhs == rhs
313 /// @li 1 if lhs > rhs
314 //------------------------------------------------------------------
316 Compare (const ConstString& lhs, const ConstString& rhs);
318 //------------------------------------------------------------------
319 /// Dump the object description to a stream.
321 /// Dump the string value to the stream \a s. If the contained string
322 /// is empty, print \a value_if_empty to the stream instead. If
323 /// \a value_if_empty is nullptr, then nothing will be dumped to the
327 /// The stream that will be used to dump the object description.
329 /// @param[in] value_if_empty
330 /// The value to dump if the string is empty. If nullptr, nothing
331 /// will be output to the stream.
332 //------------------------------------------------------------------
334 Dump(Stream *s, const char *value_if_empty = nullptr) const;
336 //------------------------------------------------------------------
337 /// Dump the object debug description to a stream.
340 /// The stream that will be used to dump the object description.
341 //------------------------------------------------------------------
343 DumpDebug (Stream *s) const;
345 //------------------------------------------------------------------
346 /// Test for empty string.
349 /// @li \b true if the contained string is empty.
350 /// @li \b false if the contained string is not empty.
351 //------------------------------------------------------------------
355 return m_string == nullptr || m_string[0] == '\0';
358 //------------------------------------------------------------------
359 /// Set the C string value.
361 /// Set the string value in the object by uniquing the \a cstr
362 /// string value in our global string pool.
364 /// If the C string already exists in the global string pool, it
365 /// finds the current entry and returns the existing value. If it
366 /// doesn't exist, it is added to the string pool.
369 /// A NULL terminated C string to add to the string pool.
370 //------------------------------------------------------------------
372 SetCString (const char *cstr);
375 SetString (const llvm::StringRef &s);
377 //------------------------------------------------------------------
378 /// Set the C string value and its mangled counterpart.
380 /// Object files and debug symbols often use mangled string to
381 /// represent the linkage name for a symbol, function or global.
382 /// The string pool can efficiently store these values and their
383 /// counterparts so when we run into another instance of a mangled
384 /// name, we can avoid calling the name demangler over and over on
385 /// the same strings and then trying to unique them.
387 /// @param[in] demangled
388 /// The demangled C string to correlate with the \a mangled
391 /// @param[in] mangled
392 /// The already uniqued mangled ConstString to correlate the
393 /// soon to be uniqued version of \a demangled.
394 //------------------------------------------------------------------
396 SetCStringWithMangledCounterpart (const char *demangled,
397 const ConstString &mangled);
399 //------------------------------------------------------------------
400 /// Retrieve the mangled or demangled counterpart for a mangled
401 /// or demangled ConstString.
403 /// Object files and debug symbols often use mangled string to
404 /// represent the linkage name for a symbol, function or global.
405 /// The string pool can efficiently store these values and their
406 /// counterparts so when we run into another instance of a mangled
407 /// name, we can avoid calling the name demangler over and over on
408 /// the same strings and then trying to unique them.
410 /// @param[in] counterpart
411 /// A reference to a ConstString object that might get filled in
412 /// with the demangled/mangled counterpart.
415 /// /b True if \a counterpart was filled in with the counterpart
416 /// /b false otherwise.
417 //------------------------------------------------------------------
419 GetMangledCounterpart (ConstString &counterpart) const;
421 //------------------------------------------------------------------
422 /// Set the C string value with length.
424 /// Set the string value in the object by uniquing \a cstr_len bytes
425 /// starting at the \a cstr string value in our global string pool.
426 /// If trim is true, then \a cstr_len indicates a maximum length of
427 /// the CString and if the actual length of the string is less, then
428 /// it will be trimmed.
430 /// If the C string already exists in the global string pool, it
431 /// finds the current entry and returns the existing value. If it
432 /// doesn't exist, it is added to the string pool.
435 /// A NULL terminated C string to add to the string pool.
437 /// @param[in] cstr_len
438 /// The maximum length of the C string.
439 //------------------------------------------------------------------
441 SetCStringWithLength (const char *cstr, size_t cstr_len);
443 //------------------------------------------------------------------
444 /// Set the C string value with the minimum length between
445 /// \a fixed_cstr_len and the actual length of the C string. This
446 /// can be used for data structures that have a fixed length to
447 /// store a C string where the string might not be NULL terminated
448 /// if the string takes the entire buffer.
449 //------------------------------------------------------------------
451 SetTrimmedCStringWithLength (const char *cstr, size_t fixed_cstr_len);
453 //------------------------------------------------------------------
454 /// Get the memory cost of this object.
456 /// Return the size in bytes that this object takes in memory. This
457 /// returns the size in bytes of this object, which does not include
458 /// any the shared string values it may refer to.
461 /// The number of bytes that this object occupies in memory.
463 /// @see ConstString::StaticMemorySize ()
464 //------------------------------------------------------------------
468 return sizeof(ConstString);
471 //------------------------------------------------------------------
472 /// Get the size in bytes of the current global string pool.
474 /// Reports the size in bytes of all shared C string values,
475 /// containers and any other values as a byte size for the
476 /// entire string pool.
479 /// The number of bytes that the global string pool occupies
481 //------------------------------------------------------------------
486 //------------------------------------------------------------------
488 //------------------------------------------------------------------
489 const char *m_string;
492 //------------------------------------------------------------------
493 /// Stream the string value \a str to the stream \a s
494 //------------------------------------------------------------------
495 Stream& operator << (Stream& s, const ConstString& str);
497 } // namespace lldb_private
499 #endif // liblldb_ConstString_h_