1 //===-- StringList.cpp ------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Utility/StringList.h"
11 
12 #include "lldb/Utility/Log.h"
13 #include "lldb/Utility/StreamString.h"
14 
15 #include <string>
16 
17 using namespace lldb_private;
18 
19 StringList::StringList() : m_strings() {}
20 
21 StringList::StringList(const char *str) : m_strings() {
22   if (str)
23     m_strings.push_back(str);
24 }
25 
26 StringList::StringList(const char **strv, int strc) : m_strings() {
27   for (int i = 0; i < strc; ++i) {
28     if (strv[i])
29       m_strings.push_back(strv[i]);
30   }
31 }
32 
33 StringList::~StringList() {}
34 
35 void StringList::AppendString(const char *str) {
36   if (str)
37     m_strings.push_back(str);
38 }
39 
40 void StringList::AppendString(const std::string &s) { m_strings.push_back(s); }
41 
42 void StringList::AppendString(std::string &&s) { m_strings.push_back(s); }
43 
44 void StringList::AppendString(const char *str, size_t str_len) {
45   if (str)
46     m_strings.push_back(std::string(str, str_len));
47 }
48 
49 void StringList::AppendString(llvm::StringRef str) {
50   m_strings.push_back(str.str());
51 }
52 
53 void StringList::AppendList(const char **strv, int strc) {
54   for (int i = 0; i < strc; ++i) {
55     if (strv[i])
56       m_strings.push_back(strv[i]);
57   }
58 }
59 
60 void StringList::AppendList(StringList strings) {
61   size_t len = strings.GetSize();
62 
63   for (size_t i = 0; i < len; ++i)
64     m_strings.push_back(strings.GetStringAtIndex(i));
65 }
66 
67 size_t StringList::GetSize() const { return m_strings.size(); }
68 
69 size_t StringList::GetMaxStringLength() const {
70   size_t max_length = 0;
71   for (const auto &s : m_strings) {
72     const size_t len = s.size();
73     if (max_length < len)
74       max_length = len;
75   }
76   return max_length;
77 }
78 
79 const char *StringList::GetStringAtIndex(size_t idx) const {
80   if (idx < m_strings.size())
81     return m_strings[idx].c_str();
82   return NULL;
83 }
84 
85 void StringList::Join(const char *separator, Stream &strm) {
86   size_t size = GetSize();
87 
88   if (size == 0)
89     return;
90 
91   for (uint32_t i = 0; i < size; ++i) {
92     if (i > 0)
93       strm.PutCString(separator);
94     strm.PutCString(GetStringAtIndex(i));
95   }
96 }
97 
98 void StringList::Clear() { m_strings.clear(); }
99 
100 void StringList::LongestCommonPrefix(std::string &common_prefix) {
101   common_prefix.clear();
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   common_prefix = prefix;
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 
134 void StringList::InsertStringAtIndex(size_t idx, std::string &&str) {
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 
141 void StringList::DeleteStringAtIndex(size_t idx) {
142   if (idx < m_strings.size())
143     m_strings.erase(m_strings.begin() + idx);
144 }
145 
146 size_t StringList::SplitIntoLines(const std::string &lines) {
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 
179 void StringList::RemoveBlankLines() {
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 strm.GetString();
203 }
204 
205 StringList &StringList::operator<<(const char *str) {
206   AppendString(str);
207   return *this;
208 }
209 
210 StringList &StringList::operator<<(const std::string &str) {
211   AppendString(str);
212   return *this;
213 }
214 
215 StringList &StringList::operator<<(StringList strings) {
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 size_t StringList::AutoComplete(llvm::StringRef s, StringList &matches,
227                                 size_t &exact_idx) const {
228   matches.Clear();
229   exact_idx = SIZE_MAX;
230   if (s.empty()) {
231     // No string, so it matches everything
232     matches = *this;
233     return matches.GetSize();
234   }
235 
236   const size_t s_len = s.size();
237   const size_t num_strings = m_strings.size();
238 
239   for (size_t i = 0; i < num_strings; ++i) {
240     if (m_strings[i].find(s) == 0) {
241       if (exact_idx == SIZE_MAX && m_strings[i].size() == s_len)
242         exact_idx = matches.GetSize();
243       matches.AppendString(m_strings[i]);
244     }
245   }
246   return matches.GetSize();
247 }
248 
249 void StringList::LogDump(Log *log, const char *name) {
250   if (!log)
251     return;
252 
253   StreamString strm;
254   if (name)
255     strm.Printf("Begin %s:\n", name);
256   for (const auto &s : m_strings) {
257     strm.Indent();
258     strm.Printf("%s\n", s.c_str());
259   }
260   if (name)
261     strm.Printf("End %s.\n", name);
262 
263   LLDB_LOGV(log, "{0}", strm.GetData());
264 }
265