Path: blob/main/contrib/llvm-project/lldb/source/Utility/StringList.cpp
39587 views
//===-- StringList.cpp ----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Utility/StringList.h"910#include "lldb/Utility/Log.h"11#include "lldb/Utility/Stream.h"12#include "lldb/Utility/StreamString.h"13#include "llvm/ADT/ArrayRef.h"1415#include <algorithm>16#include <cstdint>17#include <cstring>1819using namespace lldb_private;2021StringList::StringList() : m_strings() {}2223StringList::StringList(const char *str) : m_strings() {24if (str)25m_strings.push_back(str);26}2728StringList::StringList(const char **strv, int strc) : m_strings() {29for (int i = 0; i < strc; ++i) {30if (strv[i])31m_strings.push_back(strv[i]);32}33}3435StringList::~StringList() = default;3637void StringList::AppendString(const char *str) {38if (str)39m_strings.push_back(str);40}4142void StringList::AppendString(const std::string &s) { m_strings.push_back(s); }4344void StringList::AppendString(std::string &&s) {45m_strings.push_back(std::move(s));46}4748void StringList::AppendString(const char *str, size_t str_len) {49if (str)50m_strings.push_back(std::string(str, str_len));51}5253void StringList::AppendString(llvm::StringRef str) {54m_strings.push_back(str.str());55}5657void StringList::AppendString(const llvm::Twine &str) {58m_strings.push_back(str.str());59}6061void StringList::AppendList(const char **strv, int strc) {62for (int i = 0; i < strc; ++i) {63if (strv[i])64m_strings.push_back(strv[i]);65}66}6768void StringList::AppendList(StringList strings) {69m_strings.reserve(m_strings.size() + strings.GetSize());70m_strings.insert(m_strings.end(), strings.begin(), strings.end());71}7273size_t StringList::GetSize() const { return m_strings.size(); }7475size_t StringList::GetMaxStringLength() const {76size_t max_length = 0;77for (const auto &s : m_strings) {78const size_t len = s.size();79if (max_length < len)80max_length = len;81}82return max_length;83}8485const char *StringList::GetStringAtIndex(size_t idx) const {86if (idx < m_strings.size())87return m_strings[idx].c_str();88return nullptr;89}9091void StringList::Join(const char *separator, Stream &strm) {92size_t size = GetSize();9394if (size == 0)95return;9697for (uint32_t i = 0; i < size; ++i) {98if (i > 0)99strm.PutCString(separator);100strm.PutCString(GetStringAtIndex(i));101}102}103104void StringList::Clear() { m_strings.clear(); }105106std::string StringList::LongestCommonPrefix() {107if (m_strings.empty())108return {};109110auto args = llvm::ArrayRef(m_strings);111llvm::StringRef prefix = args.front();112for (auto arg : args.drop_front()) {113size_t count = 0;114for (count = 0; count < std::min(prefix.size(), arg.size()); ++count) {115if (prefix[count] != arg[count])116break;117}118prefix = prefix.take_front(count);119}120return prefix.str();121}122123void StringList::InsertStringAtIndex(size_t idx, const char *str) {124if (str) {125if (idx < m_strings.size())126m_strings.insert(m_strings.begin() + idx, str);127else128m_strings.push_back(str);129}130}131132void StringList::InsertStringAtIndex(size_t idx, const std::string &str) {133if (idx < m_strings.size())134m_strings.insert(m_strings.begin() + idx, str);135else136m_strings.push_back(str);137}138139void StringList::InsertStringAtIndex(size_t idx, std::string &&str) {140if (idx < m_strings.size())141m_strings.insert(m_strings.begin() + idx, std::move(str));142else143m_strings.push_back(std::move(str));144}145146void StringList::DeleteStringAtIndex(size_t idx) {147if (idx < m_strings.size())148m_strings.erase(m_strings.begin() + idx);149}150151size_t StringList::SplitIntoLines(const std::string &lines) {152return SplitIntoLines(lines.c_str(), lines.size());153}154155size_t StringList::SplitIntoLines(const char *lines, size_t len) {156const size_t orig_size = m_strings.size();157158if (len == 0)159return 0;160161const char *k_newline_chars = "\r\n";162const char *p = lines;163const char *end = lines + len;164while (p < end) {165size_t count = strcspn(p, k_newline_chars);166if (count == 0) {167if (p[count] == '\r' || p[count] == '\n')168m_strings.push_back(std::string());169else170break;171} else {172if (p + count > end)173count = end - p;174m_strings.push_back(std::string(p, count));175}176if (p[count] == '\r' && p[count + 1] == '\n')177count++; // Skip an extra newline char for the DOS newline178count++; // Skip the newline character179p += count;180}181return m_strings.size() - orig_size;182}183184void StringList::RemoveBlankLines() {185if (GetSize() == 0)186return;187188size_t idx = 0;189while (idx < m_strings.size()) {190if (m_strings[idx].empty())191DeleteStringAtIndex(idx);192else193idx++;194}195}196197std::string StringList::CopyList(const char *item_preamble,198const char *items_sep) const {199StreamString strm;200for (size_t i = 0; i < GetSize(); i++) {201if (i && items_sep && items_sep[0])202strm << items_sep;203if (item_preamble)204strm << item_preamble;205strm << GetStringAtIndex(i);206}207return std::string(strm.GetString());208}209210StringList &StringList::operator<<(const char *str) {211AppendString(str);212return *this;213}214215StringList &StringList::operator<<(const std::string &str) {216AppendString(str);217return *this;218}219220StringList &StringList::operator<<(const StringList &strings) {221AppendList(strings);222return *this;223}224225StringList &StringList::operator=(const std::vector<std::string> &rhs) {226m_strings.assign(rhs.begin(), rhs.end());227228return *this;229}230231void StringList::LogDump(Log *log, const char *name) {232if (!log)233return;234235StreamString strm;236if (name)237strm.Printf("Begin %s:\n", name);238for (const auto &s : m_strings) {239strm.Indent();240strm.Printf("%s\n", s.c_str());241}242if (name)243strm.Printf("End %s.\n", name);244245LLDB_LOGV(log, "{0}", strm.GetData());246}247248249