LLDB mainline
ConstString.cpp
Go to the documentation of this file.
1//===-- ConstString.cpp ---------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "lldb/Utility/Stream.h"
13
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/iterator.h"
16#include "llvm/Support/FormatProviders.h"
17
18#include <utility>
19
20#include <cinttypes>
21#include <cstdint>
22#include <cstring>
23
24using namespace lldb_private;
25
26ConstString::ConstString(const char *cstr)
27 : m_string(StringPool::GetGlobal().GetConstCString(cstr)) {}
28
29ConstString::ConstString(const char *cstr, size_t cstr_len)
30 : m_string(
31 StringPool::GetGlobal().GetConstCStringWithLength(cstr, cstr_len)) {}
32
33ConstString::ConstString(llvm::StringRef s)
34 : m_string(StringPool::GetGlobal().Intern(s)) {}
35
37 if (m_string == rhs.m_string)
38 return false;
39
40 llvm::StringRef lhs_string_ref(GetStringRef());
41 llvm::StringRef rhs_string_ref(rhs.GetStringRef());
42
43 // If both have valid C strings, then return the comparison
44 if (lhs_string_ref.data() && rhs_string_ref.data())
45 return lhs_string_ref < rhs_string_ref;
46
47 // Else one of them was nullptr, so if LHS is nullptr then it is less than
48 return lhs_string_ref.data() == nullptr;
49}
50
52 const char *cstr = str.GetCString();
53 if (cstr != nullptr)
54 s << cstr;
55
56 return s;
57}
58
62
64 const bool case_sensitive) {
65 if (lhs.m_string == rhs.m_string)
66 return true;
67
68 // Since the pointers weren't equal, and identical ConstStrings always have
69 // identical pointers, the result must be false for case sensitive equality
70 // test.
71 if (case_sensitive)
72 return false;
73
74 // perform case insensitive equality test
75 llvm::StringRef lhs_string_ref(lhs.GetStringRef());
76 llvm::StringRef rhs_string_ref(rhs.GetStringRef());
77 return lhs_string_ref.equals_insensitive(rhs_string_ref);
78}
79
81 const bool case_sensitive) {
82 // If the iterators are the same, this is the same string
83 const char *lhs_cstr = lhs.m_string;
84 const char *rhs_cstr = rhs.m_string;
85 if (lhs_cstr == rhs_cstr)
86 return 0;
87 if (lhs_cstr && rhs_cstr) {
88 llvm::StringRef lhs_string_ref(lhs.GetStringRef());
89 llvm::StringRef rhs_string_ref(rhs.GetStringRef());
90
91 if (case_sensitive) {
92 return lhs_string_ref.compare(rhs_string_ref);
93 } else {
94 return lhs_string_ref.compare_insensitive(rhs_string_ref);
95 }
96 }
97
98 if (lhs_cstr)
99 return +1; // LHS isn't nullptr but RHS is
100 else
101 return -1; // LHS is nullptr but RHS isn't
102}
103
104void ConstString::Dump(Stream *s, const char *fail_value) const {
105 if (s != nullptr) {
106 const char *cstr = AsCString(fail_value);
107 if (cstr != nullptr)
108 s->PutCString(cstr);
109 }
110}
111
113 const char *cstr = GetCString();
114 size_t cstr_len = GetLength();
115 // Only print the parens if we have a non-nullptr string
116 const char *parens = cstr ? "\"" : "";
117 s->Printf("%*p: ConstString, string = %s%s%s, length = %" PRIu64,
118 static_cast<int>(sizeof(void *) * 2),
119 static_cast<const void *>(this), parens, cstr, parens,
120 static_cast<uint64_t>(cstr_len));
121}
122
123void ConstString::SetCString(const char *cstr) {
125}
126
127void ConstString::SetString(llvm::StringRef s) {
129}
130
131void ConstString::SetStringWithMangledCounterpart(llvm::StringRef demangled,
132 ConstString mangled) {
134 demangled, mangled.GetStringRef());
135}
136
138 counterpart.m_string =
140 return (bool)counterpart;
141}
142
143void ConstString::SetCStringWithLength(const char *cstr, size_t cstr_len) {
145}
146
148 size_t cstr_len) {
149 m_string =
151}
152
156
157void llvm::format_provider<ConstString>::format(const ConstString &CS,
158 llvm::raw_ostream &OS,
159 llvm::StringRef Options) {
160 format_provider<StringRef>::format(CS.GetStringRef(), OS, Options);
161}
A uniqued constant string class.
Definition ConstString.h:40
bool GetMangledCounterpart(ConstString &counterpart) const
Retrieve the mangled or demangled counterpart for a mangled or demangled ConstString.
static MemoryStats GetMemoryStats()
void SetCStringWithLength(const char *cstr, size_t cstr_len)
Set the C string value with length.
void SetCString(const char *cstr)
Set the C string value.
static int Compare(ConstString lhs, ConstString rhs, const bool case_sensitive=true)
Compare two string objects.
ConstString()=default
Default constructor.
void Dump(Stream *s, const char *value_if_empty=nullptr) const
Dump the object description to a stream.
static bool Equals(ConstString lhs, ConstString rhs, const bool case_sensitive=true)
Equal to operator.
void DumpDebug(Stream *s) const
Dump the object debug description to a stream.
void SetTrimmedCStringWithLength(const char *cstr, size_t fixed_cstr_len)
Set the C string value with the minimum length between fixed_cstr_len and the actual length of the C ...
size_t GetLength() const
Get the length in bytes of string value.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
void SetString(llvm::StringRef s)
bool operator<(ConstString rhs) const
const char * GetCString() const
Get the string value as a C string.
void SetStringWithMangledCounterpart(llvm::StringRef demangled, ConstString mangled)
Set the C string value and its mangled counterpart.
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
A command line option parsing protocol class.
Definition Options.h:58
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
A thread-safe pool of interned, null-terminated strings.
Definition StringPool.h:25
const char * GetConstCString(const char *cstr)
const char * Intern(llvm::StringRef str)
Returns the pooled copy of str, or nullptr if str has no data.
const char * GetMangledCounterpart(llvm::StringRef str)
ConstString::MemoryStats GetMemoryStats() const
const char * GetConstCStringWithLength(const char *cstr, size_t cstr_len)
static StringPool & GetGlobal()
The pool backing ConstString. It is never destroyed.
static size_t GetConstCStringLength(const char *ccstr)
Length of a string returned by any pool.
const char * GetConstTrimmedCStringWithLength(const char *cstr, size_t cstr_len)
const char * GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled, llvm::StringRef mangled)
Interns demangled and links it with the already interned mangled.
A class that represents a running process on the host machine.
Stream & operator<<(Stream &s, const Mangled &obj)