1 //===-- StringList.h --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_StringList_h_
10 #define liblldb_StringList_h_
12 #include "llvm/ADT/StringRef.h"
18 namespace lldb_private {
23 namespace lldb_private {
26 typedef std::vector<std::string> collection;
31 explicit StringList(const char *str);
33 StringList(const char **strv, int strc);
35 virtual ~StringList();
37 void AppendString(const std::string &s);
39 void AppendString(std::string &&s);
41 void AppendString(const char *str);
43 void AppendString(const char *str, size_t str_len);
45 void AppendString(llvm::StringRef str);
47 void AppendList(const char **strv, int strc);
49 void AppendList(StringList strings);
51 size_t GetSize() const;
53 void SetSize(size_t n) { m_strings.resize(n); }
55 size_t GetMaxStringLength() const;
57 typedef collection::iterator iterator;
58 typedef collection::const_iterator const_iterator;
60 iterator begin() { return m_strings.begin(); }
61 iterator end() { return m_strings.end(); }
62 const_iterator begin() const { return m_strings.begin(); }
63 const_iterator end() const { return m_strings.end(); }
65 std::string &operator[](size_t idx) {
66 // No bounds checking, verify "idx" is good prior to calling this function
67 return m_strings[idx];
70 const std::string &operator[](size_t idx) const {
71 // No bounds checking, verify "idx" is good prior to calling this function
72 return m_strings[idx];
75 void PopBack() { m_strings.pop_back(); }
76 const char *GetStringAtIndex(size_t idx) const;
78 void Join(const char *separator, Stream &strm);
82 std::string LongestCommonPrefix();
84 void InsertStringAtIndex(size_t idx, const std::string &str);
86 void InsertStringAtIndex(size_t idx, std::string &&str);
88 void InsertStringAtIndex(size_t id, const char *str);
90 void DeleteStringAtIndex(size_t id);
92 void RemoveBlankLines();
94 size_t SplitIntoLines(const std::string &lines);
96 size_t SplitIntoLines(const char *lines, size_t len);
98 std::string CopyList(const char *item_preamble = nullptr,
99 const char *items_sep = "\n") const;
101 StringList &operator<<(const char *str);
103 StringList &operator<<(const std::string &s);
105 StringList &operator<<(StringList strings);
107 // Copy assignment for a vector of strings
108 StringList &operator=(const std::vector<std::string> &rhs);
110 // Dump the StringList to the given lldb_private::Log, `log`, one item per
111 // line. If given, `name` will be used to identify the start and end of the
112 // list in the output.
113 virtual void LogDump(Log *log, const char *name = nullptr);
115 // Static helper to convert an iterable of strings to a StringList, and then
116 // dump it with the semantics of the `LogDump` method.
117 template <typename T>
118 static void LogDump(Log *log, T s_iterable, const char *name = nullptr) {
121 // Make a copy of the iterable as a StringList
123 for (const auto &s : s_iterable)
126 l.LogDump(log, name);
130 collection m_strings;
133 } // namespace lldb_private
135 #endif // liblldb_StringList_h_