Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 11 | |
| 12 | #include "lldb/Core/StreamString.h" |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 13 | #include "lldb/Host/FileSpec.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | |
| 15 | #include <string> |
| 16 | |
| 17 | using namespace lldb_private; |
| 18 | |
| 19 | StringList::StringList () : |
| 20 | m_strings () |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | StringList::StringList (const char *str) : |
| 25 | m_strings () |
| 26 | { |
| 27 | if (str) |
| 28 | m_strings.push_back (str); |
| 29 | } |
| 30 | |
| 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 | |
| 41 | StringList::~StringList () |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | StringList::AppendString (const char *str) |
| 47 | { |
| 48 | if (str) |
| 49 | m_strings.push_back (str); |
| 50 | } |
| 51 | |
| 52 | void |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 53 | StringList::AppendString (const std::string &s) |
| 54 | { |
| 55 | m_strings.push_back (s); |
| 56 | } |
| 57 | |
| 58 | void |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | StringList::AppendString (const char *str, size_t str_len) |
| 60 | { |
| 61 | if (str) |
| 62 | m_strings.push_back (std::string (str, str_len)); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | StringList::AppendList (const char **strv, int strc) |
| 67 | { |
| 68 | for (int i = 0; i < strc; ++i) |
| 69 | { |
| 70 | if (strv[i]) |
| 71 | m_strings.push_back (strv[i]); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | StringList::AppendList (StringList strings) |
| 77 | { |
| 78 | uint32_t len = strings.GetSize(); |
| 79 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 80 | for (uint32_t i = 0; i < len; ++i) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | m_strings.push_back (strings.GetStringAtIndex(i)); |
| 82 | } |
| 83 | |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 84 | bool |
| 85 | StringList::ReadFileLines (FileSpec &input_file) |
| 86 | { |
| 87 | return input_file.ReadFileLines (m_strings); |
| 88 | } |
| 89 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | uint32_t |
Jim Ingham | 8053904 | 2011-03-11 01:48:52 +0000 | [diff] [blame] | 91 | StringList::GetSize () const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | { |
| 93 | return m_strings.size(); |
| 94 | } |
| 95 | |
| 96 | const char * |
Jim Ingham | 8053904 | 2011-03-11 01:48:52 +0000 | [diff] [blame] | 97 | StringList::GetStringAtIndex (size_t idx) const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | { |
| 99 | if (idx < m_strings.size()) |
| 100 | return m_strings[idx].c_str(); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
Greg Clayton | dc43bbf | 2012-05-26 17:21:14 +0000 | [diff] [blame] | 104 | void |
| 105 | StringList::Join (const char *separator, Stream &strm) |
Johnny Chen | 746323a | 2012-05-26 00:32:39 +0000 | [diff] [blame] | 106 | { |
| 107 | uint32_t size = GetSize(); |
Greg Clayton | dc43bbf | 2012-05-26 17:21:14 +0000 | [diff] [blame] | 108 | |
Johnny Chen | 746323a | 2012-05-26 00:32:39 +0000 | [diff] [blame] | 109 | if (size == 0) |
Greg Clayton | dc43bbf | 2012-05-26 17:21:14 +0000 | [diff] [blame] | 110 | return; |
| 111 | |
Johnny Chen | 746323a | 2012-05-26 00:32:39 +0000 | [diff] [blame] | 112 | for (uint32_t i = 0; i < size; ++i) |
| 113 | { |
| 114 | if (i > 0) |
Greg Clayton | dc43bbf | 2012-05-26 17:21:14 +0000 | [diff] [blame] | 115 | strm.PutCString(separator); |
| 116 | strm.PutCString(GetStringAtIndex(i)); |
Johnny Chen | 746323a | 2012-05-26 00:32:39 +0000 | [diff] [blame] | 117 | } |
Johnny Chen | 746323a | 2012-05-26 00:32:39 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | void |
| 121 | StringList::Clear () |
| 122 | { |
| 123 | m_strings.clear(); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | StringList::LongestCommonPrefix (std::string &common_prefix) |
| 128 | { |
| 129 | //arg_sstr_collection::iterator pos, end = m_args.end(); |
| 130 | int pos = 0; |
| 131 | int end = m_strings.size(); |
| 132 | |
| 133 | if (pos == end) |
| 134 | common_prefix.clear(); |
| 135 | else |
| 136 | common_prefix = m_strings[pos]; |
| 137 | |
| 138 | for (++pos; pos != end; ++pos) |
| 139 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 140 | size_t new_size = strlen (m_strings[pos].c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 141 | |
| 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); |
| 145 | |
| 146 | // Then trim it at the first disparity: |
| 147 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 148 | for (size_t i = 0; i < common_prefix.size(); i++) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | { |
| 150 | if (m_strings[pos][i] != common_prefix[i]) |
| 151 | { |
| 152 | common_prefix.erase(i); |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // If we've emptied the common prefix, we're done. |
| 158 | if (common_prefix.empty()) |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | StringList::InsertStringAtIndex (size_t idx, const char *str) |
| 165 | { |
| 166 | if (str) |
| 167 | { |
| 168 | if (idx < m_strings.size()) |
| 169 | m_strings.insert (m_strings.begin() + idx, str); |
| 170 | else |
| 171 | m_strings.push_back (str); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | StringList::DeleteStringAtIndex (size_t idx) |
| 177 | { |
| 178 | if (idx < m_strings.size()) |
| 179 | m_strings.erase (m_strings.begin() + idx); |
| 180 | } |
| 181 | |
| 182 | size_t |
| 183 | StringList::SplitIntoLines (const char *lines, size_t len) |
| 184 | { |
| 185 | const size_t orig_size = m_strings.size(); |
| 186 | |
| 187 | if (len == 0) |
| 188 | return 0; |
| 189 | |
| 190 | const char *k_newline_chars = "\r\n"; |
| 191 | const char *p = lines; |
| 192 | const char *end = lines + len; |
| 193 | while (p < end) |
| 194 | { |
| 195 | size_t count = strcspn (p, k_newline_chars); |
| 196 | if (count == 0) |
| 197 | { |
| 198 | if (p[count] == '\r' || p[count] == '\n') |
| 199 | m_strings.push_back(std::string()); |
| 200 | else |
| 201 | break; |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | if (p + count > end) |
| 206 | count = end - p; |
| 207 | m_strings.push_back(std::string(p, count)); |
| 208 | } |
| 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 |
| 212 | p += count; |
| 213 | } |
| 214 | return m_strings.size() - orig_size; |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | StringList::RemoveBlankLines () |
| 219 | { |
| 220 | if (GetSize() == 0) |
| 221 | return; |
| 222 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 223 | size_t idx = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 224 | while (idx < m_strings.size()) |
| 225 | { |
| 226 | if (m_strings[idx].empty()) |
| 227 | DeleteStringAtIndex(idx); |
| 228 | else |
| 229 | idx++; |
| 230 | } |
| 231 | } |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 232 | |
| 233 | std::string |
| 234 | StringList::CopyList(const char* item_preamble, |
| 235 | const char* items_sep) |
| 236 | { |
| 237 | StreamString strm; |
| 238 | for (int i = 0; i < GetSize(); i++) |
| 239 | { |
| 240 | if (i && items_sep && items_sep[0]) |
| 241 | strm << items_sep; |
| 242 | if (item_preamble) |
| 243 | strm << item_preamble; |
| 244 | strm << GetStringAtIndex(i); |
| 245 | } |
| 246 | return std::string(strm.GetData()); |
| 247 | } |
| 248 | |
| 249 | StringList& |
| 250 | StringList::operator << (const char* str) |
| 251 | { |
| 252 | AppendString(str); |
| 253 | return *this; |
| 254 | } |
| 255 | |
| 256 | StringList& |
| 257 | StringList::operator << (StringList strings) |
| 258 | { |
| 259 | AppendList(strings); |
| 260 | return *this; |
Greg Clayton | 153ccd7 | 2011-08-10 02:10:13 +0000 | [diff] [blame] | 261 | } |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 262 | |
| 263 | size_t |
| 264 | StringList::AutoComplete (const char *s, StringList &matches, size_t &exact_idx) const |
| 265 | { |
| 266 | matches.Clear(); |
| 267 | exact_idx = SIZE_MAX; |
| 268 | if (s && s[0]) |
| 269 | { |
| 270 | const size_t s_len = strlen (s); |
| 271 | const size_t num_strings = m_strings.size(); |
| 272 | |
| 273 | for (size_t i=0; i<num_strings; ++i) |
| 274 | { |
| 275 | if (m_strings[i].find(s) == 0) |
| 276 | { |
| 277 | if (exact_idx == SIZE_MAX && m_strings[i].size() == s_len) |
| 278 | exact_idx = matches.GetSize(); |
| 279 | matches.AppendString (m_strings[i]); |
| 280 | } |
| 281 | } |
| 282 | } |
Greg Clayton | 9f28285 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 283 | else |
| 284 | { |
| 285 | // No string, so it matches everything |
| 286 | matches = *this; |
| 287 | } |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 288 | return matches.GetSize(); |
| 289 | } |
| 290 | |