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/Core/StringList.h"
11
12 #include "lldb/Core/StreamString.h"
13 #include "lldb/Host/FileSpec.h"
14
15 #include <string>
16
17 using namespace lldb_private;
18
StringList()19 StringList::StringList () :
20 m_strings ()
21 {
22 }
23
StringList(const char * str)24 StringList::StringList (const char *str) :
25 m_strings ()
26 {
27 if (str)
28 m_strings.push_back (str);
29 }
30
StringList(const char ** strv,int strc)31 StringList::StringList (const char **strv, int strc) :
32 m_strings ()
33 {
34 for (int i = 0; i < strc; ++i)
35 {
36 if (strv[i])
37 m_strings.push_back (strv[i]);
38 }
39 }
40
~StringList()41 StringList::~StringList ()
42 {
43 }
44
45 void
AppendString(const char * str)46 StringList::AppendString (const char *str)
47 {
48 if (str)
49 m_strings.push_back (str);
50 }
51
52 void
AppendString(const std::string & s)53 StringList::AppendString (const std::string &s)
54 {
55 m_strings.push_back (s);
56 }
57
58 void
AppendString(std::string && s)59 StringList::AppendString (std::string &&s)
60 {
61 m_strings.push_back (s);
62 }
63
64 void
AppendString(const char * str,size_t str_len)65 StringList::AppendString (const char *str, size_t str_len)
66 {
67 if (str)
68 m_strings.push_back (std::string (str, str_len));
69 }
70
71 void
AppendString(llvm::StringRef str)72 StringList::AppendString(llvm::StringRef str)
73 {
74 m_strings.push_back(str.str());
75 }
76
77 void
AppendList(const char ** strv,int strc)78 StringList::AppendList (const char **strv, int strc)
79 {
80 for (int i = 0; i < strc; ++i)
81 {
82 if (strv[i])
83 m_strings.push_back (strv[i]);
84 }
85 }
86
87 void
AppendList(StringList strings)88 StringList::AppendList (StringList strings)
89 {
90 size_t len = strings.GetSize();
91
92 for (size_t i = 0; i < len; ++i)
93 m_strings.push_back (strings.GetStringAtIndex(i));
94 }
95
96 bool
ReadFileLines(FileSpec & input_file)97 StringList::ReadFileLines (FileSpec &input_file)
98 {
99 return input_file.ReadFileLines (m_strings);
100 }
101
102 size_t
GetSize() const103 StringList::GetSize () const
104 {
105 return m_strings.size();
106 }
107
108 size_t
GetMaxStringLength() const109 StringList::GetMaxStringLength () const
110 {
111 size_t max_length = 0;
112 for (const auto &s : m_strings)
113 {
114 const size_t len = s.size();
115 if (max_length < len)
116 max_length = len;
117 }
118 return max_length;
119 }
120
121
122 const char *
GetStringAtIndex(size_t idx) const123 StringList::GetStringAtIndex (size_t idx) const
124 {
125 if (idx < m_strings.size())
126 return m_strings[idx].c_str();
127 return NULL;
128 }
129
130 void
Join(const char * separator,Stream & strm)131 StringList::Join (const char *separator, Stream &strm)
132 {
133 size_t size = GetSize();
134
135 if (size == 0)
136 return;
137
138 for (uint32_t i = 0; i < size; ++i)
139 {
140 if (i > 0)
141 strm.PutCString(separator);
142 strm.PutCString(GetStringAtIndex(i));
143 }
144 }
145
146 void
Clear()147 StringList::Clear ()
148 {
149 m_strings.clear();
150 }
151
152 void
LongestCommonPrefix(std::string & common_prefix)153 StringList::LongestCommonPrefix (std::string &common_prefix)
154 {
155 const size_t num_strings = m_strings.size();
156
157 if (num_strings == 0)
158 {
159 common_prefix.clear();
160 }
161 else
162 {
163 common_prefix = m_strings.front();
164
165 for (size_t idx = 1; idx < num_strings; ++idx)
166 {
167 std::string &curr_string = m_strings[idx];
168 size_t new_size = curr_string.size();
169
170 // First trim common_prefix if it is longer than the current element:
171 if (common_prefix.size() > new_size)
172 common_prefix.erase (new_size);
173
174 // Then trim it at the first disparity:
175 for (size_t i = 0; i < common_prefix.size(); i++)
176 {
177 if (curr_string[i] != common_prefix[i])
178 {
179 common_prefix.erase(i);
180 break;
181 }
182 }
183
184 // If we've emptied the common prefix, we're done.
185 if (common_prefix.empty())
186 break;
187 }
188 }
189 }
190
191 void
InsertStringAtIndex(size_t idx,const char * str)192 StringList::InsertStringAtIndex (size_t idx, const char *str)
193 {
194 if (str)
195 {
196 if (idx < m_strings.size())
197 m_strings.insert (m_strings.begin() + idx, str);
198 else
199 m_strings.push_back (str);
200 }
201 }
202
203 void
InsertStringAtIndex(size_t idx,const std::string & str)204 StringList::InsertStringAtIndex (size_t idx, const std::string &str)
205 {
206 if (idx < m_strings.size())
207 m_strings.insert (m_strings.begin() + idx, str);
208 else
209 m_strings.push_back (str);
210 }
211
212 void
InsertStringAtIndex(size_t idx,std::string && str)213 StringList::InsertStringAtIndex (size_t idx, std::string &&str)
214 {
215 if (idx < m_strings.size())
216 m_strings.insert (m_strings.begin() + idx, str);
217 else
218 m_strings.push_back (str);
219 }
220
221 void
DeleteStringAtIndex(size_t idx)222 StringList::DeleteStringAtIndex (size_t idx)
223 {
224 if (idx < m_strings.size())
225 m_strings.erase (m_strings.begin() + idx);
226 }
227
228 size_t
SplitIntoLines(const std::string & lines)229 StringList::SplitIntoLines (const std::string &lines)
230 {
231 return SplitIntoLines (lines.c_str(), lines.size());
232 }
233
234 size_t
SplitIntoLines(const char * lines,size_t len)235 StringList::SplitIntoLines (const char *lines, size_t len)
236 {
237 const size_t orig_size = m_strings.size();
238
239 if (len == 0)
240 return 0;
241
242 const char *k_newline_chars = "\r\n";
243 const char *p = lines;
244 const char *end = lines + len;
245 while (p < end)
246 {
247 size_t count = strcspn (p, k_newline_chars);
248 if (count == 0)
249 {
250 if (p[count] == '\r' || p[count] == '\n')
251 m_strings.push_back(std::string());
252 else
253 break;
254 }
255 else
256 {
257 if (p + count > end)
258 count = end - p;
259 m_strings.push_back(std::string(p, count));
260 }
261 if (p[count] == '\r' && p[count+1] == '\n')
262 count++; // Skip an extra newline char for the DOS newline
263 count++; // Skip the newline character
264 p += count;
265 }
266 return m_strings.size() - orig_size;
267 }
268
269 void
RemoveBlankLines()270 StringList::RemoveBlankLines ()
271 {
272 if (GetSize() == 0)
273 return;
274
275 size_t idx = 0;
276 while (idx < m_strings.size())
277 {
278 if (m_strings[idx].empty())
279 DeleteStringAtIndex(idx);
280 else
281 idx++;
282 }
283 }
284
285 std::string
CopyList(const char * item_preamble,const char * items_sep) const286 StringList::CopyList(const char* item_preamble, const char* items_sep) const
287 {
288 StreamString strm;
289 for (size_t i = 0; i < GetSize(); i++)
290 {
291 if (i && items_sep && items_sep[0])
292 strm << items_sep;
293 if (item_preamble)
294 strm << item_preamble;
295 strm << GetStringAtIndex(i);
296 }
297 return std::string(strm.GetData());
298 }
299
300 StringList&
operator <<(const char * str)301 StringList::operator << (const char* str)
302 {
303 AppendString(str);
304 return *this;
305 }
306
307 StringList&
operator <<(StringList strings)308 StringList::operator << (StringList strings)
309 {
310 AppendList(strings);
311 return *this;
312 }
313
314 size_t
AutoComplete(const char * s,StringList & matches,size_t & exact_idx) const315 StringList::AutoComplete (const char *s, StringList &matches, size_t &exact_idx) const
316 {
317 matches.Clear();
318 exact_idx = SIZE_MAX;
319 if (s && s[0])
320 {
321 const size_t s_len = strlen (s);
322 const size_t num_strings = m_strings.size();
323
324 for (size_t i=0; i<num_strings; ++i)
325 {
326 if (m_strings[i].find(s) == 0)
327 {
328 if (exact_idx == SIZE_MAX && m_strings[i].size() == s_len)
329 exact_idx = matches.GetSize();
330 matches.AppendString (m_strings[i]);
331 }
332 }
333 }
334 else
335 {
336 // No string, so it matches everything
337 matches = *this;
338 }
339 return matches.GetSize();
340 }
341
342