1 //===-- StringList.cpp ----------------------------------------------------===// 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 <cstdint> 18 #include <cstring> 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() = default; 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) { 46 m_strings.push_back(std::move(s)); 47 } 48 49 void StringList::AppendString(const char *str, size_t str_len) { 50 if (str) 51 m_strings.push_back(std::string(str, str_len)); 52 } 53 54 void StringList::AppendString(llvm::StringRef str) { 55 m_strings.push_back(str.str()); 56 } 57 58 void StringList::AppendList(const char **strv, int strc) { 59 for (int i = 0; i < strc; ++i) { 60 if (strv[i]) 61 m_strings.push_back(strv[i]); 62 } 63 } 64 65 void StringList::AppendList(StringList strings) { 66 m_strings.reserve(m_strings.size() + strings.GetSize()); 67 m_strings.insert(m_strings.end(), strings.begin(), strings.end()); 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, std::move(str)); 139 else 140 m_strings.push_back(std::move(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 std::string(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<<(const 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 void StringList::LogDump(Log *log, const char *name) { 229 if (!log) 230 return; 231 232 StreamString strm; 233 if (name) 234 strm.Printf("Begin %s:\n", name); 235 for (const auto &s : m_strings) { 236 strm.Indent(); 237 strm.Printf("%s\n", s.c_str()); 238 } 239 if (name) 240 strm.Printf("End %s.\n", name); 241 242 LLDB_LOGV(log, "{0}", strm.GetData()); 243 } 244