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 9 #include "lldb/Utility/StringList.h" 10 11 #include "lldb/Utility/Log.h" 12 #include "lldb/Utility/Stream.h" 13 #include "lldb/Utility/StreamString.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 36 StringList::~StringList() {} 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 63 void StringList::AppendList(StringList strings) { 64 m_strings.insert(m_strings.end(), strings.begin(), strings.end()); 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 nullptr; 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 std::string StringList::LongestCommonPrefix() { 101 if (m_strings.empty()) 102 return {}; 103 104 auto args = llvm::makeArrayRef(m_strings); 105 llvm::StringRef prefix = args.front(); 106 for (auto arg : args.drop_front()) { 107 size_t count = 0; 108 for (count = 0; count < std::min(prefix.size(), arg.size()); ++count) { 109 if (prefix[count] != arg[count]) 110 break; 111 } 112 prefix = prefix.take_front(count); 113 } 114 return prefix.str(); 115 } 116 117 void StringList::InsertStringAtIndex(size_t idx, const char *str) { 118 if (str) { 119 if (idx < m_strings.size()) 120 m_strings.insert(m_strings.begin() + idx, str); 121 else 122 m_strings.push_back(str); 123 } 124 } 125 126 void StringList::InsertStringAtIndex(size_t idx, const std::string &str) { 127 if (idx < m_strings.size()) 128 m_strings.insert(m_strings.begin() + idx, str); 129 else 130 m_strings.push_back(str); 131 } 132 133 void StringList::InsertStringAtIndex(size_t idx, std::string &&str) { 134 if (idx < m_strings.size()) 135 m_strings.insert(m_strings.begin() + idx, str); 136 else 137 m_strings.push_back(str); 138 } 139 140 void StringList::DeleteStringAtIndex(size_t idx) { 141 if (idx < m_strings.size()) 142 m_strings.erase(m_strings.begin() + idx); 143 } 144 145 size_t StringList::SplitIntoLines(const std::string &lines) { 146 return SplitIntoLines(lines.c_str(), lines.size()); 147 } 148 149 size_t StringList::SplitIntoLines(const char *lines, size_t len) { 150 const size_t orig_size = m_strings.size(); 151 152 if (len == 0) 153 return 0; 154 155 const char *k_newline_chars = "\r\n"; 156 const char *p = lines; 157 const char *end = lines + len; 158 while (p < end) { 159 size_t count = strcspn(p, k_newline_chars); 160 if (count == 0) { 161 if (p[count] == '\r' || p[count] == '\n') 162 m_strings.push_back(std::string()); 163 else 164 break; 165 } else { 166 if (p + count > end) 167 count = end - p; 168 m_strings.push_back(std::string(p, count)); 169 } 170 if (p[count] == '\r' && p[count + 1] == '\n') 171 count++; // Skip an extra newline char for the DOS newline 172 count++; // Skip the newline character 173 p += count; 174 } 175 return m_strings.size() - orig_size; 176 } 177 178 void StringList::RemoveBlankLines() { 179 if (GetSize() == 0) 180 return; 181 182 size_t idx = 0; 183 while (idx < m_strings.size()) { 184 if (m_strings[idx].empty()) 185 DeleteStringAtIndex(idx); 186 else 187 idx++; 188 } 189 } 190 191 std::string StringList::CopyList(const char *item_preamble, 192 const char *items_sep) const { 193 StreamString strm; 194 for (size_t i = 0; i < GetSize(); i++) { 195 if (i && items_sep && items_sep[0]) 196 strm << items_sep; 197 if (item_preamble) 198 strm << item_preamble; 199 strm << GetStringAtIndex(i); 200 } 201 return strm.GetString(); 202 } 203 204 StringList &StringList::operator<<(const char *str) { 205 AppendString(str); 206 return *this; 207 } 208 209 StringList &StringList::operator<<(const std::string &str) { 210 AppendString(str); 211 return *this; 212 } 213 214 StringList &StringList::operator<<(StringList strings) { 215 AppendList(strings); 216 return *this; 217 } 218 219 StringList &StringList::operator=(const std::vector<std::string> &rhs) { 220 m_strings.assign(rhs.begin(), rhs.end()); 221 222 return *this; 223 } 224 225 size_t StringList::AutoComplete(llvm::StringRef s, StringList &matches, 226 size_t &exact_idx) const { 227 matches.Clear(); 228 exact_idx = SIZE_MAX; 229 if (s.empty()) { 230 // No string, so it matches everything 231 matches = *this; 232 return matches.GetSize(); 233 } 234 235 const size_t s_len = s.size(); 236 const size_t num_strings = m_strings.size(); 237 238 for (size_t i = 0; i < num_strings; ++i) { 239 if (m_strings[i].find(s) == 0) { 240 if (exact_idx == SIZE_MAX && m_strings[i].size() == s_len) 241 exact_idx = matches.GetSize(); 242 matches.AppendString(m_strings[i]); 243 } 244 } 245 return matches.GetSize(); 246 } 247 248 void StringList::LogDump(Log *log, const char *name) { 249 if (!log) 250 return; 251 252 StreamString strm; 253 if (name) 254 strm.Printf("Begin %s:\n", name); 255 for (const auto &s : m_strings) { 256 strm.Indent(); 257 strm.Printf("%s\n", s.c_str()); 258 } 259 if (name) 260 strm.Printf("End %s.\n", name); 261 262 LLDB_LOGV(log, "{0}", strm.GetData()); 263 } 264