|
|
|||
File indexing completed on 2026-05-10 08:42:57
0001 //===-- ConstString.h -------------------------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 0009 #ifndef LLDB_UTILITY_CONSTSTRING_H 0010 #define LLDB_UTILITY_CONSTSTRING_H 0011 0012 #include "llvm/ADT/DenseMapInfo.h" 0013 #include "llvm/ADT/StringRef.h" 0014 #include "llvm/Support/FormatVariadic.h" 0015 0016 #include <cstddef> 0017 #include <string_view> 0018 0019 namespace lldb_private { 0020 class Stream; 0021 } 0022 namespace llvm { 0023 class raw_ostream; 0024 } 0025 0026 namespace lldb_private { 0027 0028 /// \class ConstString ConstString.h "lldb/Utility/ConstString.h" 0029 /// A uniqued constant string class. 0030 /// 0031 /// Provides an efficient way to store strings as uniqued strings. After the 0032 /// strings are uniqued, finding strings that are equal to one another is very 0033 /// fast as just the pointers need to be compared. It also allows for many 0034 /// common strings from many different sources to be shared to keep the memory 0035 /// footprint low. 0036 /// 0037 /// No reference counting is done on strings that are added to the string 0038 /// pool, once strings are added they are in the string pool for the life of 0039 /// the program. 0040 class ConstString { 0041 public: 0042 /// Default constructor 0043 /// 0044 /// Initializes the string to an empty string. 0045 ConstString() = default; 0046 0047 explicit ConstString(llvm::StringRef s); 0048 0049 /// Construct with C String value 0050 /// 0051 /// Constructs this object with a C string by looking to see if the 0052 /// C string already exists in the global string pool. If it doesn't 0053 /// exist, it is added to the string pool. 0054 /// 0055 /// \param[in] cstr 0056 /// A NULL terminated C string to add to the string pool. 0057 explicit ConstString(const char *cstr); 0058 0059 /// Construct with C String value with max length 0060 /// 0061 /// Constructs this object with a C string with a length. If \a max_cstr_len 0062 /// is greater than the actual length of the string, the string length will 0063 /// be truncated. This allows substrings to be created without the need to 0064 /// NULL terminate the string as it is passed into this function. 0065 /// 0066 /// \param[in] cstr 0067 /// A pointer to the first character in the C string. The C 0068 /// string can be NULL terminated in a buffer that contains 0069 /// more characters than the length of the string, or the 0070 /// string can be part of another string and a new substring 0071 /// can be created. 0072 /// 0073 /// \param[in] max_cstr_len 0074 /// The max length of \a cstr. If the string length of \a cstr 0075 /// is less than \a max_cstr_len, then the string will be 0076 /// truncated. If the string length of \a cstr is greater than 0077 /// \a max_cstr_len, then only max_cstr_len bytes will be used 0078 /// from \a cstr. 0079 explicit ConstString(const char *cstr, size_t max_cstr_len); 0080 0081 /// Convert to bool operator. 0082 /// 0083 /// This allows code to check a ConstString object to see if it contains a 0084 /// valid string using code such as: 0085 /// 0086 /// \code 0087 /// ConstString str(...); 0088 /// if (str) 0089 /// { ... 0090 /// \endcode 0091 /// 0092 /// \return 0093 /// /b True this object contains a valid non-empty C string, \b 0094 /// false otherwise. 0095 explicit operator bool() const { return !IsEmpty(); } 0096 0097 /// Equal to operator 0098 /// 0099 /// Returns true if this string is equal to the string in \a rhs. This 0100 /// operation is very fast as it results in a pointer comparison since all 0101 /// strings are in a uniqued in a global string pool. 0102 /// 0103 /// \param[in] rhs 0104 /// Another string object to compare this object to. 0105 /// 0106 /// \return 0107 /// true if this object is equal to \a rhs. 0108 /// false if this object is not equal to \a rhs. 0109 bool operator==(ConstString rhs) const { 0110 // We can do a pointer compare to compare these strings since they must 0111 // come from the same pool in order to be equal. 0112 return m_string == rhs.m_string; 0113 } 0114 0115 /// Equal to operator against a non-ConstString value. 0116 /// 0117 /// Returns true if this string is equal to the string in \a rhs. This 0118 /// overload is usually slower than comparing against a ConstString value. 0119 /// However, if the rhs string not already a ConstString and it is impractical 0120 /// to turn it into a non-temporary variable, then this overload is faster. 0121 /// 0122 /// \param[in] rhs 0123 /// Another string object to compare this object to. 0124 /// 0125 /// \return 0126 /// \b true if this object is equal to \a rhs. 0127 /// \b false if this object is not equal to \a rhs. 0128 bool operator==(const char *rhs) const { 0129 // ConstString differentiates between empty strings and nullptr strings, but 0130 // StringRef doesn't. Therefore we have to do this check manually now. 0131 if (m_string == nullptr && rhs != nullptr) 0132 return false; 0133 if (m_string != nullptr && rhs == nullptr) 0134 return false; 0135 0136 return GetStringRef() == rhs; 0137 } 0138 0139 /// Not equal to operator 0140 /// 0141 /// Returns true if this string is not equal to the string in \a rhs. This 0142 /// operation is very fast as it results in a pointer comparison since all 0143 /// strings are in a uniqued in a global string pool. 0144 /// 0145 /// \param[in] rhs 0146 /// Another string object to compare this object to. 0147 /// 0148 /// \return 0149 /// \b true if this object is not equal to \a rhs. 0150 /// \b false if this object is equal to \a rhs. 0151 bool operator!=(ConstString rhs) const { return m_string != rhs.m_string; } 0152 0153 /// Not equal to operator against a non-ConstString value. 0154 /// 0155 /// Returns true if this string is not equal to the string in \a rhs. This 0156 /// overload is usually slower than comparing against a ConstString value. 0157 /// However, if the rhs string not already a ConstString and it is impractical 0158 /// to turn it into a non-temporary variable, then this overload is faster. 0159 /// 0160 /// \param[in] rhs 0161 /// Another string object to compare this object to. 0162 /// 0163 /// \return \b true if this object is not equal to \a rhs, false otherwise. 0164 bool operator!=(const char *rhs) const { return !(*this == rhs); } 0165 0166 bool operator<(ConstString rhs) const; 0167 0168 // Implicitly convert \class ConstString instances to \class StringRef. 0169 operator llvm::StringRef() const { return GetStringRef(); } 0170 0171 // Explicitly convert \class ConstString instances to \class std::string_view. 0172 explicit operator std::string_view() const { 0173 return std::string_view(m_string, GetLength()); 0174 } 0175 0176 // Explicitly convert \class ConstString instances to \class std::string. 0177 explicit operator std::string() const { return GetString(); } 0178 0179 /// Get the string value as a C string. 0180 /// 0181 /// Get the value of the contained string as a NULL terminated C string 0182 /// value. 0183 /// 0184 /// If \a value_if_empty is nullptr, then nullptr will be returned. 0185 /// 0186 /// \return Returns \a value_if_empty if the string is empty, otherwise 0187 /// the C string value contained in this object. 0188 const char *AsCString(const char *value_if_empty = nullptr) const { 0189 return (IsEmpty() ? value_if_empty : m_string); 0190 } 0191 0192 /// Get the string value as a llvm::StringRef 0193 /// 0194 /// \return 0195 /// Returns a new llvm::StringRef object filled in with the 0196 /// needed data. 0197 llvm::StringRef GetStringRef() const { 0198 return llvm::StringRef(m_string, GetLength()); 0199 } 0200 0201 /// Get the string value as a std::string 0202 std::string GetString() const { 0203 return std::string(AsCString(""), GetLength()); 0204 } 0205 0206 /// Get the string value as a C string. 0207 /// 0208 /// Get the value of the contained string as a NULL terminated C string 0209 /// value. Similar to the ConstString::AsCString() function, yet this 0210 /// function will always return nullptr if the string is not valid. So this 0211 /// function is a direct accessor to the string pointer value. 0212 /// 0213 /// \return 0214 /// Returns nullptr the string is invalid, otherwise the C string 0215 /// value contained in this object. 0216 const char *GetCString() const { return m_string; } 0217 0218 /// Get the length in bytes of string value. 0219 /// 0220 /// The string pool stores the length of the string, so we can avoid calling 0221 /// strlen() on the pointer value with this function. 0222 /// 0223 /// \return 0224 /// Returns the number of bytes that this string occupies in 0225 /// memory, not including the NULL termination byte. 0226 size_t GetLength() const; 0227 0228 /// Clear this object's state. 0229 /// 0230 /// Clear any contained string and reset the value to the empty string 0231 /// value. 0232 void Clear() { m_string = nullptr; } 0233 0234 /// Equal to operator 0235 /// 0236 /// Returns true if this string is equal to the string in \a rhs. If case 0237 /// sensitive equality is tested, this operation is very fast as it results 0238 /// in a pointer comparison since all strings are in a uniqued in a global 0239 /// string pool. 0240 /// 0241 /// \param[in] lhs 0242 /// The Left Hand Side const ConstString object reference. 0243 /// 0244 /// \param[in] rhs 0245 /// The Right Hand Side const ConstString object reference. 0246 /// 0247 /// \param[in] case_sensitive 0248 /// Case sensitivity. If true, case sensitive equality 0249 /// will be tested, otherwise character case will be ignored 0250 /// 0251 /// \return \b true if this object is equal to \a rhs, \b false otherwise. 0252 static bool Equals(ConstString lhs, ConstString rhs, 0253 const bool case_sensitive = true); 0254 0255 /// Compare two string objects. 0256 /// 0257 /// Compares the C string values contained in \a lhs and \a rhs and returns 0258 /// an integer result. 0259 /// 0260 /// NOTE: only call this function when you want a true string 0261 /// comparison. If you want string equality use the, use the == operator as 0262 /// it is much more efficient. Also if you want string inequality, use the 0263 /// != operator for the same reasons. 0264 /// 0265 /// \param[in] lhs 0266 /// The Left Hand Side const ConstString object reference. 0267 /// 0268 /// \param[in] rhs 0269 /// The Right Hand Side const ConstString object reference. 0270 /// 0271 /// \param[in] case_sensitive 0272 /// Case sensitivity of compare. If true, case sensitive compare 0273 /// will be performed, otherwise character case will be ignored 0274 /// 0275 /// \return -1 if lhs < rhs, 0 if lhs == rhs, 1 if lhs > rhs 0276 static int Compare(ConstString lhs, ConstString rhs, 0277 const bool case_sensitive = true); 0278 0279 /// Dump the object description to a stream. 0280 /// 0281 /// Dump the string value to the stream \a s. If the contained string is 0282 /// empty, print \a value_if_empty to the stream instead. If \a 0283 /// value_if_empty is nullptr, then nothing will be dumped to the stream. 0284 /// 0285 /// \param[in] s 0286 /// The stream that will be used to dump the object description. 0287 /// 0288 /// \param[in] value_if_empty 0289 /// The value to dump if the string is empty. If nullptr, nothing 0290 /// will be output to the stream. 0291 void Dump(Stream *s, const char *value_if_empty = nullptr) const; 0292 0293 /// Dump the object debug description to a stream. 0294 /// 0295 /// \param[in] s 0296 /// The stream that will be used to dump the object description. 0297 void DumpDebug(Stream *s) const; 0298 0299 /// Test for empty string. 0300 /// 0301 /// \return 0302 /// \b true if the contained string is empty. 0303 /// \b false if the contained string is not empty. 0304 bool IsEmpty() const { return m_string == nullptr || m_string[0] == '\0'; } 0305 0306 /// Test for null string. 0307 /// 0308 /// \return 0309 /// \b true if there is no string associated with this instance. 0310 /// \b false if there is a string associated with this instance. 0311 bool IsNull() const { return m_string == nullptr; } 0312 0313 /// Set the C string value. 0314 /// 0315 /// Set the string value in the object by uniquing the \a cstr string value 0316 /// in our global string pool. 0317 /// 0318 /// If the C string already exists in the global string pool, it finds the 0319 /// current entry and returns the existing value. If it doesn't exist, it is 0320 /// added to the string pool. 0321 /// 0322 /// \param[in] cstr 0323 /// A NULL terminated C string to add to the string pool. 0324 void SetCString(const char *cstr); 0325 0326 void SetString(llvm::StringRef s); 0327 0328 /// Set the C string value and its mangled counterpart. 0329 /// 0330 /// Object files and debug symbols often use mangled string to represent the 0331 /// linkage name for a symbol, function or global. The string pool can 0332 /// efficiently store these values and their counterparts so when we run 0333 /// into another instance of a mangled name, we can avoid calling the name 0334 /// demangler over and over on the same strings and then trying to unique 0335 /// them. 0336 /// 0337 /// \param[in] demangled 0338 /// The demangled string to correlate with the \a mangled name. 0339 /// 0340 /// \param[in] mangled 0341 /// The already uniqued mangled ConstString to correlate the 0342 /// soon to be uniqued version of \a demangled. 0343 void SetStringWithMangledCounterpart(llvm::StringRef demangled, 0344 ConstString mangled); 0345 0346 /// Retrieve the mangled or demangled counterpart for a mangled or demangled 0347 /// ConstString. 0348 /// 0349 /// Object files and debug symbols often use mangled string to represent the 0350 /// linkage name for a symbol, function or global. The string pool can 0351 /// efficiently store these values and their counterparts so when we run 0352 /// into another instance of a mangled name, we can avoid calling the name 0353 /// demangler over and over on the same strings and then trying to unique 0354 /// them. 0355 /// 0356 /// \param[in] counterpart 0357 /// A reference to a ConstString object that might get filled in 0358 /// with the demangled/mangled counterpart. 0359 /// 0360 /// \return 0361 /// /b True if \a counterpart was filled in with the counterpart 0362 /// /b false otherwise. 0363 bool GetMangledCounterpart(ConstString &counterpart) const; 0364 0365 /// Set the C string value with length. 0366 /// 0367 /// Set the string value in the object by uniquing \a cstr_len bytes 0368 /// starting at the \a cstr string value in our global string pool. If trim 0369 /// is true, then \a cstr_len indicates a maximum length of the CString and 0370 /// if the actual length of the string is less, then it will be trimmed. 0371 /// 0372 /// If the C string already exists in the global string pool, it finds the 0373 /// current entry and returns the existing value. If it doesn't exist, it is 0374 /// added to the string pool. 0375 /// 0376 /// \param[in] cstr 0377 /// A NULL terminated C string to add to the string pool. 0378 /// 0379 /// \param[in] cstr_len 0380 /// The maximum length of the C string. 0381 void SetCStringWithLength(const char *cstr, size_t cstr_len); 0382 0383 /// Set the C string value with the minimum length between \a fixed_cstr_len 0384 /// and the actual length of the C string. This can be used for data 0385 /// structures that have a fixed length to store a C string where the string 0386 /// might not be NULL terminated if the string takes the entire buffer. 0387 void SetTrimmedCStringWithLength(const char *cstr, size_t fixed_cstr_len); 0388 0389 /// Get the memory cost of this object. 0390 /// 0391 /// Return the size in bytes that this object takes in memory. This returns 0392 /// the size in bytes of this object, which does not include any the shared 0393 /// string values it may refer to. 0394 /// 0395 /// \return 0396 /// The number of bytes that this object occupies in memory. 0397 size_t MemorySize() const { return sizeof(ConstString); } 0398 0399 struct MemoryStats { 0400 size_t GetBytesTotal() const { return bytes_total; } 0401 size_t GetBytesUsed() const { return bytes_used; } 0402 size_t GetBytesUnused() const { return bytes_total - bytes_used; } 0403 size_t bytes_total = 0; 0404 size_t bytes_used = 0; 0405 }; 0406 0407 static MemoryStats GetMemoryStats(); 0408 0409 protected: 0410 template <typename T, typename Enable> friend struct ::llvm::DenseMapInfo; 0411 /// Only used by DenseMapInfo. 0412 static ConstString FromStringPoolPointer(const char *ptr) { 0413 ConstString s; 0414 s.m_string = ptr; 0415 return s; 0416 }; 0417 0418 const char *m_string = nullptr; 0419 }; 0420 0421 /// Stream the string value \a str to the stream \a s 0422 Stream &operator<<(Stream &s, ConstString str); 0423 0424 } // namespace lldb_private 0425 0426 namespace llvm { 0427 template <> struct format_provider<lldb_private::ConstString> { 0428 static void format(const lldb_private::ConstString &CS, llvm::raw_ostream &OS, 0429 llvm::StringRef Options); 0430 }; 0431 0432 /// DenseMapInfo implementation. 0433 /// \{ 0434 template <> struct DenseMapInfo<lldb_private::ConstString> { 0435 static inline lldb_private::ConstString getEmptyKey() { 0436 return lldb_private::ConstString::FromStringPoolPointer( 0437 DenseMapInfo<const char *>::getEmptyKey()); 0438 } 0439 static inline lldb_private::ConstString getTombstoneKey() { 0440 return lldb_private::ConstString::FromStringPoolPointer( 0441 DenseMapInfo<const char *>::getTombstoneKey()); 0442 } 0443 static unsigned getHashValue(lldb_private::ConstString val) { 0444 return DenseMapInfo<const char *>::getHashValue(val.m_string); 0445 } 0446 static bool isEqual(lldb_private::ConstString LHS, 0447 lldb_private::ConstString RHS) { 0448 return LHS == RHS; 0449 } 0450 }; 0451 /// \} 0452 0453 inline raw_ostream &operator<<(raw_ostream &os, lldb_private::ConstString s) { 0454 os << s.GetStringRef(); 0455 return os; 0456 } 0457 } // namespace llvm 0458 0459 #endif // LLDB_UTILITY_CONSTSTRING_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|