LLDB  mainline
StringList.cpp
Go to the documentation of this file.
1 //===-- StringList.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/Log.h"
12 #include "lldb/Utility/Stream.h"
14 #include "llvm/ADT/ArrayRef.h"
15 
16 #include <algorithm>
17 #include <stdint.h>
18 #include <string.h>
19 
20 using namespace lldb_private;
21 
22 StringList::StringList() : m_strings() {}
23 
24 StringList::StringList(const char *str) : m_strings() {
25  if (str)
26  m_strings.push_back(str);
27 }
28 
29 StringList::StringList(const char **strv, int strc) : m_strings() {
30  for (int i = 0; i < strc; ++i) {
31  if (strv[i])
32  m_strings.push_back(strv[i]);
33  }
34 }
35 
37 
38 void StringList::AppendString(const char *str) {
39  if (str)
40  m_strings.push_back(str);
41 }
42 
43 void StringList::AppendString(const std::string &s) { m_strings.push_back(s); }
44 
45 void StringList::AppendString(std::string &&s) { m_strings.push_back(s); }
46 
47 void StringList::AppendString(const char *str, size_t str_len) {
48  if (str)
49  m_strings.push_back(std::string(str, str_len));
50 }
51 
52 void StringList::AppendString(llvm::StringRef str) {
53  m_strings.push_back(str.str());
54 }
55 
56 void StringList::AppendList(const char **strv, int strc) {
57  for (int i = 0; i < strc; ++i) {
58  if (strv[i])
59  m_strings.push_back(strv[i]);
60  }
61 }
62 
64  m_strings.reserve(m_strings.size() + strings.GetSize());
65  m_strings.insert(m_strings.end(), strings.begin(), strings.end());
66 }
67 
68 size_t StringList::GetSize() const { return m_strings.size(); }
69 
71  size_t max_length = 0;
72  for (const auto &s : m_strings) {
73  const size_t len = s.size();
74  if (max_length < len)
75  max_length = len;
76  }
77  return max_length;
78 }
79 
80 const char *StringList::GetStringAtIndex(size_t idx) const {
81  if (idx < m_strings.size())
82  return m_strings[idx].c_str();
83  return nullptr;
84 }
85 
86 void StringList::Join(const char *separator, Stream &strm) {
87  size_t size = GetSize();
88 
89  if (size == 0)
90  return;
91 
92  for (uint32_t i = 0; i < size; ++i) {
93  if (i > 0)
94  strm.PutCString(separator);
96  }
97 }
98 
99 void StringList::Clear() { m_strings.clear(); }
100 
102  if (m_strings.empty())
103  return {};
104 
105  auto args = llvm::makeArrayRef(m_strings);
106  llvm::StringRef prefix = args.front();
107  for (auto arg : args.drop_front()) {
108  size_t count = 0;
109  for (count = 0; count < std::min(prefix.size(), arg.size()); ++count) {
110  if (prefix[count] != arg[count])
111  break;
112  }
113  prefix = prefix.take_front(count);
114  }
115  return prefix.str();
116 }
117 
118 void StringList::InsertStringAtIndex(size_t idx, const char *str) {
119  if (str) {
120  if (idx < m_strings.size())
121  m_strings.insert(m_strings.begin() + idx, str);
122  else
123  m_strings.push_back(str);
124  }
125 }
126 
127 void StringList::InsertStringAtIndex(size_t idx, const std::string &str) {
128  if (idx < m_strings.size())
129  m_strings.insert(m_strings.begin() + idx, str);
130  else
131  m_strings.push_back(str);
132 }
133 
135  if (idx < m_strings.size())
136  m_strings.insert(m_strings.begin() + idx, str);
137  else
138  m_strings.push_back(str);
139 }
140 
142  if (idx < m_strings.size())
143  m_strings.erase(m_strings.begin() + idx);
144 }
145 
147  return SplitIntoLines(lines.c_str(), lines.size());
148 }
149 
150 size_t StringList::SplitIntoLines(const char *lines, size_t len) {
151  const size_t orig_size = m_strings.size();
152 
153  if (len == 0)
154  return 0;
155 
156  const char *k_newline_chars = "\r\n";
157  const char *p = lines;
158  const char *end = lines + len;
159  while (p < end) {
160  size_t count = strcspn(p, k_newline_chars);
161  if (count == 0) {
162  if (p[count] == '\r' || p[count] == '\n')
163  m_strings.push_back(std::string());
164  else
165  break;
166  } else {
167  if (p + count > end)
168  count = end - p;
169  m_strings.push_back(std::string(p, count));
170  }
171  if (p[count] == '\r' && p[count + 1] == '\n')
172  count++; // Skip an extra newline char for the DOS newline
173  count++; // Skip the newline character
174  p += count;
175  }
176  return m_strings.size() - orig_size;
177 }
178 
180  if (GetSize() == 0)
181  return;
182 
183  size_t idx = 0;
184  while (idx < m_strings.size()) {
185  if (m_strings[idx].empty())
186  DeleteStringAtIndex(idx);
187  else
188  idx++;
189  }
190 }
191 
192 std::string StringList::CopyList(const char *item_preamble,
193  const char *items_sep) const {
194  StreamString strm;
195  for (size_t i = 0; i < GetSize(); i++) {
196  if (i && items_sep && items_sep[0])
197  strm << items_sep;
198  if (item_preamble)
199  strm << item_preamble;
200  strm << GetStringAtIndex(i);
201  }
202  return std::string(strm.GetString());
203 }
204 
206  AppendString(str);
207  return *this;
208 }
209 
211  AppendString(str);
212  return *this;
213 }
214 
216  AppendList(strings);
217  return *this;
218 }
219 
220 StringList &StringList::operator=(const std::vector<std::string> &rhs) {
221  m_strings.assign(rhs.begin(), rhs.end());
222 
223  return *this;
224 }
225 
226 void StringList::LogDump(Log *log, const char *name) {
227  if (!log)
228  return;
229 
230  StreamString strm;
231  if (name)
232  strm.Printf("Begin %s:\n", name);
233  for (const auto &s : m_strings) {
234  strm.Indent();
235  strm.Printf("%s\n", s.c_str());
236  }
237  if (name)
238  strm.Printf("End %s.\n", name);
239 
240  LLDB_LOGV(log, "{0}", strm.GetData());
241 }
lldb_private::StringList::Clear
void Clear()
Definition: StringList.cpp:99
lldb_private::StringList::begin
iterator begin()
Definition: StringList.h:60
lldb_private::StringList::AppendString
void AppendString(const std::string &s)
Definition: StringList.cpp:43
lldb_private::StringList::SplitIntoLines
size_t SplitIntoLines(const std::string &lines)
Definition: StringList.cpp:146
lldb_private::StringList::InsertStringAtIndex
void InsertStringAtIndex(size_t idx, const std::string &str)
Definition: StringList.cpp:127
lldb_private::Stream
Definition: Stream.h:28
LLDB_LOGV
#define LLDB_LOGV(log,...)
Definition: Log.h:256
lldb_private::StreamString::GetString
llvm::StringRef GetString() const
Definition: StreamString.cpp:51
lldb_private::StringList::m_strings
collection m_strings
Definition: StringList.h:130
lldb_private::StringList::GetSize
size_t GetSize() const
Definition: StringList.cpp:68
lldb_private::StringList::end
iterator end()
Definition: StringList.h:61
lldb_private::StringList::StringList
StringList()
Definition: StringList.cpp:22
lldb_private::StringList::GetStringAtIndex
const char * GetStringAtIndex(size_t idx) const
Definition: StringList.cpp:80
lldb_private::Stream::Indent
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:130
Log.h
lldb_private::StringList::GetMaxStringLength
size_t GetMaxStringLength() const
Definition: StringList.cpp:70
lldb_private::StreamString::GetData
const char * GetData() const
Definition: StreamString.h:43
StreamString.h
lldb_private::StringList
Definition: StringList.h:25
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::StringList::LongestCommonPrefix
std::string LongestCommonPrefix()
Definition: StringList.cpp:101
lldb_private::StringList::RemoveBlankLines
void RemoveBlankLines()
Definition: StringList.cpp:179
lldb_private::StringList::operator<<
StringList & operator<<(const char *str)
Definition: StringList.cpp:205
lldb_private::StringList::~StringList
virtual ~StringList()
Definition: StringList.cpp:36
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
StringList.h
lldb_private::StringList::AppendList
void AppendList(const char **strv, int strc)
Definition: StringList.cpp:56
lldb_private::StringList::operator=
StringList & operator=(const std::vector< std::string > &rhs)
Definition: StringList.cpp:220
lldb_private::StringList::CopyList
std::string CopyList(const char *item_preamble=nullptr, const char *items_sep="\n") const
Definition: StringList.cpp:192
uint32_t
lldb_private::StringList::DeleteStringAtIndex
void DeleteStringAtIndex(size_t id)
Definition: StringList.cpp:141
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::StringList::LogDump
virtual void LogDump(Log *log, const char *name=nullptr)
Definition: StringList.cpp:226
Stream.h
lldb_private::Log
Definition: Log.h:49
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb_private::StringList::Join
void Join(const char *separator, Stream &strm)
Definition: StringList.cpp:86