LLDB  mainline
StringList.cpp
Go to the documentation of this file.
1 //===-- StringList.cpp ------------------------------------------*- C++ -*-===//
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  size_t len = strings.GetSize();
65 
66  for (size_t i = 0; i < len; ++i)
67  m_strings.push_back(strings.GetStringAtIndex(i));
68 }
69 
70 size_t StringList::GetSize() const { return m_strings.size(); }
71 
73  size_t max_length = 0;
74  for (const auto &s : m_strings) {
75  const size_t len = s.size();
76  if (max_length < len)
77  max_length = len;
78  }
79  return max_length;
80 }
81 
82 const char *StringList::GetStringAtIndex(size_t idx) const {
83  if (idx < m_strings.size())
84  return m_strings[idx].c_str();
85  return nullptr;
86 }
87 
88 void StringList::Join(const char *separator, Stream &strm) {
89  size_t size = GetSize();
90 
91  if (size == 0)
92  return;
93 
94  for (uint32_t i = 0; i < size; ++i) {
95  if (i > 0)
96  strm.PutCString(separator);
98  }
99 }
100 
101 void StringList::Clear() { m_strings.clear(); }
102 
103 void StringList::LongestCommonPrefix(std::string &common_prefix) {
104  common_prefix.clear();
105  if (m_strings.empty())
106  return;
107 
108  auto args = llvm::makeArrayRef(m_strings);
109  llvm::StringRef prefix = args.front();
110  for (auto arg : args.drop_front()) {
111  size_t count = 0;
112  for (count = 0; count < std::min(prefix.size(), arg.size()); ++count) {
113  if (prefix[count] != arg[count])
114  break;
115  }
116  prefix = prefix.take_front(count);
117  }
118  common_prefix = prefix;
119 }
120 
121 void StringList::InsertStringAtIndex(size_t idx, const char *str) {
122  if (str) {
123  if (idx < m_strings.size())
124  m_strings.insert(m_strings.begin() + idx, str);
125  else
126  m_strings.push_back(str);
127  }
128 }
129 
130 void StringList::InsertStringAtIndex(size_t idx, const std::string &str) {
131  if (idx < m_strings.size())
132  m_strings.insert(m_strings.begin() + idx, str);
133  else
134  m_strings.push_back(str);
135 }
136 
137 void StringList::InsertStringAtIndex(size_t idx, std::string &&str) {
138  if (idx < m_strings.size())
139  m_strings.insert(m_strings.begin() + idx, str);
140  else
141  m_strings.push_back(str);
142 }
143 
145  if (idx < m_strings.size())
146  m_strings.erase(m_strings.begin() + idx);
147 }
148 
149 size_t StringList::SplitIntoLines(const std::string &lines) {
150  return SplitIntoLines(lines.c_str(), lines.size());
151 }
152 
153 size_t StringList::SplitIntoLines(const char *lines, size_t len) {
154  const size_t orig_size = m_strings.size();
155 
156  if (len == 0)
157  return 0;
158 
159  const char *k_newline_chars = "\r\n";
160  const char *p = lines;
161  const char *end = lines + len;
162  while (p < end) {
163  size_t count = strcspn(p, k_newline_chars);
164  if (count == 0) {
165  if (p[count] == '\r' || p[count] == '\n')
166  m_strings.push_back(std::string());
167  else
168  break;
169  } else {
170  if (p + count > end)
171  count = end - p;
172  m_strings.push_back(std::string(p, count));
173  }
174  if (p[count] == '\r' && p[count + 1] == '\n')
175  count++; // Skip an extra newline char for the DOS newline
176  count++; // Skip the newline character
177  p += count;
178  }
179  return m_strings.size() - orig_size;
180 }
181 
183  if (GetSize() == 0)
184  return;
185 
186  size_t idx = 0;
187  while (idx < m_strings.size()) {
188  if (m_strings[idx].empty())
189  DeleteStringAtIndex(idx);
190  else
191  idx++;
192  }
193 }
194 
195 std::string StringList::CopyList(const char *item_preamble,
196  const char *items_sep) const {
197  StreamString strm;
198  for (size_t i = 0; i < GetSize(); i++) {
199  if (i && items_sep && items_sep[0])
200  strm << items_sep;
201  if (item_preamble)
202  strm << item_preamble;
203  strm << GetStringAtIndex(i);
204  }
205  return strm.GetString();
206 }
207 
209  AppendString(str);
210  return *this;
211 }
212 
213 StringList &StringList::operator<<(const std::string &str) {
214  AppendString(str);
215  return *this;
216 }
217 
219  AppendList(strings);
220  return *this;
221 }
222 
223 StringList &StringList::operator=(const std::vector<std::string> &rhs) {
224  m_strings.assign(rhs.begin(), rhs.end());
225 
226  return *this;
227 }
228 
229 size_t StringList::AutoComplete(llvm::StringRef s, StringList &matches,
230  size_t &exact_idx) const {
231  matches.Clear();
232  exact_idx = SIZE_MAX;
233  if (s.empty()) {
234  // No string, so it matches everything
235  matches = *this;
236  return matches.GetSize();
237  }
238 
239  const size_t s_len = s.size();
240  const size_t num_strings = m_strings.size();
241 
242  for (size_t i = 0; i < num_strings; ++i) {
243  if (m_strings[i].find(s) == 0) {
244  if (exact_idx == SIZE_MAX && m_strings[i].size() == s_len)
245  exact_idx = matches.GetSize();
246  matches.AppendString(m_strings[i]);
247  }
248  }
249  return matches.GetSize();
250 }
251 
252 void StringList::LogDump(Log *log, const char *name) {
253  if (!log)
254  return;
255 
256  StreamString strm;
257  if (name)
258  strm.Printf("Begin %s:\n", name);
259  for (const auto &s : m_strings) {
260  strm.Indent();
261  strm.Printf("%s\n", s.c_str());
262  }
263  if (name)
264  strm.Printf("End %s.\n", name);
265 
266  LLDB_LOGV(log, "{0}", strm.GetData());
267 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void LongestCommonPrefix(std::string &common_prefix)
Definition: StringList.cpp:103
const char * GetData() const
Definition: StreamString.h:43
StringList & operator<<(const char *str)
Definition: StringList.cpp:208
size_t GetMaxStringLength() const
Definition: StringList.cpp:72
const char * GetStringAtIndex(size_t idx) const
Definition: StringList.cpp:82
void AppendList(const char **strv, int strc)
Definition: StringList.cpp:56
llvm::StringRef GetString() const
size_t AutoComplete(llvm::StringRef s, StringList &matches, size_t &exact_matches_idx) const
Definition: StringList.cpp:229
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
std::string CopyList(const char *item_preamble=nullptr, const char *items_sep="\) const
Definition: StringList.cpp:195
void DeleteStringAtIndex(size_t id)
Definition: StringList.cpp:144
size_t GetSize() const
Definition: StringList.cpp:70
void InsertStringAtIndex(size_t idx, const std::string &str)
Definition: StringList.cpp:130
virtual void LogDump(Log *log, const char *name=nullptr)
Definition: StringList.cpp:252
size_t Indent(const char *s=nullptr)
Indent the current line in the stream.
Definition: Stream.cpp:131
size_t SplitIntoLines(const std::string &lines)
Definition: StringList.cpp:149
void Join(const char *separator, Stream &strm)
Definition: StringList.cpp:88
StringList & operator=(const std::vector< std::string > &rhs)
Definition: StringList.cpp:223
void AppendString(const std::string &s)
Definition: StringList.cpp:43
#define LLDB_LOGV(log,...)
Definition: Log.h:216