2010-06-09 00:52:24 +08:00
|
|
|
//===-- StringList.cpp ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-03-22 02:25:04 +08:00
|
|
|
#include "lldb/Utility/StringList.h"
|
2011-07-15 10:26:42 +08:00
|
|
|
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "lldb/Utility/Log.h"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "lldb/Utility/Stream.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/StreamString.h"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
StringList::StringList() : m_strings() {}
|
|
|
|
|
|
|
|
StringList::StringList(const char *str) : m_strings() {
|
|
|
|
if (str)
|
|
|
|
m_strings.push_back(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringList::StringList(const char **strv, int strc) : m_strings() {
|
|
|
|
for (int i = 0; i < strc; ++i) {
|
|
|
|
if (strv[i])
|
|
|
|
m_strings.push_back(strv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringList::~StringList() {}
|
|
|
|
|
|
|
|
void StringList::AppendString(const char *str) {
|
|
|
|
if (str)
|
|
|
|
m_strings.push_back(str);
|
|
|
|
}
|
|
|
|
|
2012-08-23 01:17:09 +08:00
|
|
|
void StringList::AppendString(const std::string &s) { m_strings.push_back(s); }
|
|
|
|
|
2014-01-28 07:43:24 +08:00
|
|
|
void StringList::AppendString(std::string &&s) { m_strings.push_back(s); }
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
void StringList::AppendString(const char *str, size_t str_len) {
|
|
|
|
if (str)
|
|
|
|
m_strings.push_back(std::string(str, str_len));
|
|
|
|
}
|
|
|
|
|
2015-05-23 03:34:17 +08:00
|
|
|
void StringList::AppendString(llvm::StringRef str) {
|
|
|
|
m_strings.push_back(str.str());
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
void StringList::AppendList(const char **strv, int strc) {
|
|
|
|
for (int i = 0; i < strc; ++i) {
|
|
|
|
if (strv[i])
|
|
|
|
m_strings.push_back(strv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringList::AppendList(StringList strings) {
|
2019-08-19 15:22:19 +08:00
|
|
|
m_strings.reserve(m_strings.size() + strings.GetSize());
|
2019-08-16 22:27:35 +08:00
|
|
|
m_strings.insert(m_strings.end(), strings.begin(), strings.end());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2013-01-26 02:06:21 +08:00
|
|
|
size_t StringList::GetSize() const { return m_strings.size(); }
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-03-11 09:48:52 +08:00
|
|
|
size_t StringList::GetMaxStringLength() const {
|
2014-01-28 07:43:24 +08:00
|
|
|
size_t max_length = 0;
|
2010-06-09 00:52:24 +08:00
|
|
|
for (const auto &s : m_strings) {
|
|
|
|
const size_t len = s.size();
|
2014-01-28 07:43:24 +08:00
|
|
|
if (max_length < len)
|
|
|
|
max_length = len;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
return max_length;
|
|
|
|
}
|
|
|
|
|
2014-01-28 07:43:24 +08:00
|
|
|
const char *StringList::GetStringAtIndex(size_t idx) const {
|
|
|
|
if (idx < m_strings.size())
|
|
|
|
return m_strings[idx].c_str();
|
2018-12-13 08:15:17 +08:00
|
|
|
return nullptr;
|
2014-01-28 07:43:24 +08:00
|
|
|
}
|
|
|
|
|
2012-05-27 01:21:14 +08:00
|
|
|
void StringList::Join(const char *separator, Stream &strm) {
|
2013-01-26 02:06:21 +08:00
|
|
|
size_t size = GetSize();
|
2014-01-28 07:43:24 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
2012-05-26 08:32:39 +08:00
|
|
|
for (uint32_t i = 0; i < size; ++i) {
|
|
|
|
if (i > 0)
|
2012-05-27 01:21:14 +08:00
|
|
|
strm.PutCString(separator);
|
|
|
|
strm.PutCString(GetStringAtIndex(i));
|
2012-05-26 08:32:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
void StringList::Clear() { m_strings.clear(); }
|
|
|
|
|
2019-07-31 11:26:10 +08:00
|
|
|
std::string StringList::LongestCommonPrefix() {
|
2016-10-04 06:51:09 +08:00
|
|
|
if (m_strings.empty())
|
2019-07-31 11:26:10 +08:00
|
|
|
return {};
|
2016-10-04 06:51:09 +08:00
|
|
|
|
|
|
|
auto args = llvm::makeArrayRef(m_strings);
|
|
|
|
llvm::StringRef prefix = args.front();
|
|
|
|
for (auto arg : args.drop_front()) {
|
|
|
|
size_t count = 0;
|
|
|
|
for (count = 0; count < std::min(prefix.size(), arg.size()); ++count) {
|
|
|
|
if (prefix[count] != arg[count])
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-04 06:51:09 +08:00
|
|
|
prefix = prefix.take_front(count);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2019-07-31 11:26:10 +08:00
|
|
|
return prefix.str();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2014-01-28 07:43:24 +08:00
|
|
|
void StringList::InsertStringAtIndex(size_t idx, const char *str) {
|
|
|
|
if (str) {
|
|
|
|
if (idx < m_strings.size())
|
|
|
|
m_strings.insert(m_strings.begin() + idx, str);
|
|
|
|
else
|
|
|
|
m_strings.push_back(str);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-01-28 07:43:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringList::InsertStringAtIndex(size_t idx, const std::string &str) {
|
|
|
|
if (idx < m_strings.size())
|
|
|
|
m_strings.insert(m_strings.begin() + idx, str);
|
|
|
|
else
|
|
|
|
m_strings.push_back(str);
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
void StringList::InsertStringAtIndex(size_t idx, std::string &&str) {
|
|
|
|
if (idx < m_strings.size())
|
|
|
|
m_strings.insert(m_strings.begin() + idx, str);
|
|
|
|
else
|
|
|
|
m_strings.push_back(str);
|
|
|
|
}
|
|
|
|
|
2014-01-28 07:43:24 +08:00
|
|
|
void StringList::DeleteStringAtIndex(size_t idx) {
|
|
|
|
if (idx < m_strings.size())
|
|
|
|
m_strings.erase(m_strings.begin() + idx);
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
size_t StringList::SplitIntoLines(const std::string &lines) {
|
|
|
|
return SplitIntoLines(lines.c_str(), lines.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t StringList::SplitIntoLines(const char *lines, size_t len) {
|
|
|
|
const size_t orig_size = m_strings.size();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
const char *k_newline_chars = "\r\n";
|
|
|
|
const char *p = lines;
|
|
|
|
const char *end = lines + len;
|
|
|
|
while (p < end) {
|
|
|
|
size_t count = strcspn(p, k_newline_chars);
|
|
|
|
if (count == 0) {
|
|
|
|
if (p[count] == '\r' || p[count] == '\n')
|
|
|
|
m_strings.push_back(std::string());
|
2016-09-07 04:57:50 +08:00
|
|
|
else
|
|
|
|
break;
|
2010-06-09 00:52:24 +08:00
|
|
|
} else {
|
|
|
|
if (p + count > end)
|
|
|
|
count = end - p;
|
|
|
|
m_strings.push_back(std::string(p, count));
|
|
|
|
}
|
|
|
|
if (p[count] == '\r' && p[count + 1] == '\n')
|
|
|
|
count++; // Skip an extra newline char for the DOS newline
|
|
|
|
count++; // Skip the newline character
|
|
|
|
p += count;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
return m_strings.size() - orig_size;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2014-01-28 07:43:24 +08:00
|
|
|
void StringList::RemoveBlankLines() {
|
2010-06-09 00:52:24 +08:00
|
|
|
if (GetSize() == 0)
|
2016-09-07 04:57:50 +08:00
|
|
|
return;
|
|
|
|
|
2010-07-10 04:39:50 +08:00
|
|
|
size_t idx = 0;
|
2014-01-28 07:43:24 +08:00
|
|
|
while (idx < m_strings.size()) {
|
2012-08-23 01:17:09 +08:00
|
|
|
if (m_strings[idx].empty())
|
2011-07-15 10:26:42 +08:00
|
|
|
DeleteStringAtIndex(idx);
|
2016-09-07 04:57:50 +08:00
|
|
|
else
|
|
|
|
idx++;
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2011-07-15 10:26:42 +08:00
|
|
|
|
2014-01-28 07:43:24 +08:00
|
|
|
std::string StringList::CopyList(const char *item_preamble,
|
|
|
|
const char *items_sep) const {
|
2011-07-15 10:26:42 +08:00
|
|
|
StreamString strm;
|
2013-06-20 03:04:53 +08:00
|
|
|
for (size_t i = 0; i < GetSize(); i++) {
|
2011-07-15 10:26:42 +08:00
|
|
|
if (i && items_sep && items_sep[0])
|
|
|
|
strm << items_sep;
|
|
|
|
if (item_preamble)
|
|
|
|
strm << item_preamble;
|
|
|
|
strm << GetStringAtIndex(i);
|
|
|
|
}
|
2016-11-17 05:15:24 +08:00
|
|
|
return strm.GetString();
|
2011-07-15 10:26:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StringList &StringList::operator<<(const char *str) {
|
|
|
|
AppendString(str);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-01-06 19:06:30 +08:00
|
|
|
StringList &StringList::operator<<(const std::string &str) {
|
|
|
|
AppendString(str);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-07-15 10:26:42 +08:00
|
|
|
StringList &StringList::operator<<(StringList strings) {
|
|
|
|
AppendList(strings);
|
|
|
|
return *this;
|
2011-08-10 10:10:13 +08:00
|
|
|
}
|
2012-08-23 01:17:09 +08:00
|
|
|
|
2016-01-06 19:06:30 +08:00
|
|
|
StringList &StringList::operator=(const std::vector<std::string> &rhs) {
|
2017-03-22 02:25:04 +08:00
|
|
|
m_strings.assign(rhs.begin(), rhs.end());
|
2016-01-06 19:06:30 +08:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringList::LogDump(Log *log, const char *name) {
|
|
|
|
if (!log)
|
2016-09-07 04:57:50 +08:00
|
|
|
return;
|
|
|
|
|
2016-01-06 19:06:30 +08:00
|
|
|
StreamString strm;
|
|
|
|
if (name)
|
|
|
|
strm.Printf("Begin %s:\n", name);
|
2014-01-28 07:43:24 +08:00
|
|
|
for (const auto &s : m_strings) {
|
2016-09-07 04:57:50 +08:00
|
|
|
strm.Indent();
|
2016-01-06 19:06:30 +08:00
|
|
|
strm.Printf("%s\n", s.c_str());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2016-01-06 19:06:30 +08:00
|
|
|
if (name)
|
|
|
|
strm.Printf("End %s.\n", name);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-02-13 19:03:17 +08:00
|
|
|
LLDB_LOGV(log, "{0}", strm.GetData());
|
2016-01-06 19:06:30 +08:00
|
|
|
}
|