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