1 //===-- StringList.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/StringList.h"
12 #include "lldb/Core/StreamString.h"
13 #include "lldb/Host/FileSpec.h"
17 using namespace lldb_private;
19 StringList::StringList () :
24 StringList::StringList (const char *str) :
28 m_strings.push_back (str);
31 StringList::StringList (const char **strv, int strc) :
34 for (int i = 0; i < strc; ++i)
37 m_strings.push_back (strv[i]);
41 StringList::~StringList ()
46 StringList::AppendString (const char *str)
49 m_strings.push_back (str);
53 StringList::AppendString (const std::string &s)
55 m_strings.push_back (s);
59 StringList::AppendString (const char *str, size_t str_len)
62 m_strings.push_back (std::string (str, str_len));
66 StringList::AppendList (const char **strv, int strc)
68 for (int i = 0; i < strc; ++i)
71 m_strings.push_back (strv[i]);
76 StringList::AppendList (StringList strings)
78 size_t len = strings.GetSize();
80 for (size_t i = 0; i < len; ++i)
81 m_strings.push_back (strings.GetStringAtIndex(i));
85 StringList::ReadFileLines (FileSpec &input_file)
87 return input_file.ReadFileLines (m_strings);
91 StringList::GetSize () const
93 return m_strings.size();
97 StringList::GetStringAtIndex (size_t idx) const
99 if (idx < m_strings.size())
100 return m_strings[idx].c_str();
105 StringList::Join (const char *separator, Stream &strm)
107 size_t size = GetSize();
112 for (uint32_t i = 0; i < size; ++i)
115 strm.PutCString(separator);
116 strm.PutCString(GetStringAtIndex(i));
127 StringList::LongestCommonPrefix (std::string &common_prefix)
129 //arg_sstr_collection::iterator pos, end = m_args.end();
131 size_t end = m_strings.size();
134 common_prefix.clear();
136 common_prefix = m_strings[pos];
138 for (++pos; pos != end; ++pos)
140 size_t new_size = strlen (m_strings[pos].c_str());
142 // First trim common_prefix if it is longer than the current element:
143 if (common_prefix.size() > new_size)
144 common_prefix.erase (new_size);
146 // Then trim it at the first disparity:
148 for (size_t i = 0; i < common_prefix.size(); i++)
150 if (m_strings[pos][i] != common_prefix[i])
152 common_prefix.erase(i);
157 // If we've emptied the common prefix, we're done.
158 if (common_prefix.empty())
164 StringList::InsertStringAtIndex (size_t idx, const char *str)
168 if (idx < m_strings.size())
169 m_strings.insert (m_strings.begin() + idx, str);
171 m_strings.push_back (str);
176 StringList::DeleteStringAtIndex (size_t idx)
178 if (idx < m_strings.size())
179 m_strings.erase (m_strings.begin() + idx);
183 StringList::SplitIntoLines (const char *lines, size_t len)
185 const size_t orig_size = m_strings.size();
190 const char *k_newline_chars = "\r\n";
191 const char *p = lines;
192 const char *end = lines + len;
195 size_t count = strcspn (p, k_newline_chars);
198 if (p[count] == '\r' || p[count] == '\n')
199 m_strings.push_back(std::string());
207 m_strings.push_back(std::string(p, count));
209 if (p[count] == '\r' && p[count+1] == '\n')
210 count++; // Skip an extra newline char for the DOS newline
211 count++; // Skip the newline character
214 return m_strings.size() - orig_size;
218 StringList::RemoveBlankLines ()
224 while (idx < m_strings.size())
226 if (m_strings[idx].empty())
227 DeleteStringAtIndex(idx);
234 StringList::CopyList(const char* item_preamble,
235 const char* items_sep)
238 for (size_t i = 0; i < GetSize(); i++)
240 if (i && items_sep && items_sep[0])
243 strm << item_preamble;
244 strm << GetStringAtIndex(i);
246 return std::string(strm.GetData());
250 StringList::operator << (const char* str)
257 StringList::operator << (StringList strings)
264 StringList::AutoComplete (const char *s, StringList &matches, size_t &exact_idx) const
267 exact_idx = SIZE_MAX;
270 const size_t s_len = strlen (s);
271 const size_t num_strings = m_strings.size();
273 for (size_t i=0; i<num_strings; ++i)
275 if (m_strings[i].find(s) == 0)
277 if (exact_idx == SIZE_MAX && m_strings[i].size() == s_len)
278 exact_idx = matches.GetSize();
279 matches.AppendString (m_strings[i]);
285 // No string, so it matches everything
288 return matches.GetSize();