1 //===-- StringList.h --------------------------------------------*- 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 #ifndef LLDB_UTILITY_STRINGLIST_H
10 #define LLDB_UTILITY_STRINGLIST_H
11 
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/Twine.h"
14 
15 #include <cstddef>
16 #include <string>
17 #include <vector>
18 
19 namespace lldb_private {
20 class Log;
21 class Stream;
22 }
23 
24 namespace lldb_private {
25 
26 class StringList {
27   typedef std::vector<std::string> collection;
28 
29 public:
30   StringList();
31 
32   explicit StringList(const char *str);
33 
34   StringList(const char **strv, int strc);
35 
36   virtual ~StringList();
37 
38   void AppendString(const std::string &s);
39 
40   void AppendString(std::string &&s);
41 
42   void AppendString(const char *str);
43 
44   void AppendString(const char *str, size_t str_len);
45 
46   void AppendString(llvm::StringRef str);
47 
48   void AppendString(const llvm::Twine &str);
49 
50   void AppendList(const char **strv, int strc);
51 
52   void AppendList(StringList strings);
53 
54   size_t GetSize() const;
55 
56   void SetSize(size_t n) { m_strings.resize(n); }
57 
58   size_t GetMaxStringLength() const;
59 
60   typedef collection::iterator iterator;
61   typedef collection::const_iterator const_iterator;
62 
63   iterator begin() { return m_strings.begin(); }
64   iterator end() { return m_strings.end(); }
65   const_iterator begin() const { return m_strings.begin(); }
66   const_iterator end() const { return m_strings.end(); }
67 
68   std::string &operator[](size_t idx) {
69     // No bounds checking, verify "idx" is good prior to calling this function
70     return m_strings[idx];
71   }
72 
73   const std::string &operator[](size_t idx) const {
74     // No bounds checking, verify "idx" is good prior to calling this function
75     return m_strings[idx];
76   }
77 
78   void PopBack() { m_strings.pop_back(); }
79   const char *GetStringAtIndex(size_t idx) const;
80 
81   void Join(const char *separator, Stream &strm);
82 
83   void Clear();
84 
85   std::string LongestCommonPrefix();
86 
87   void InsertStringAtIndex(size_t idx, const std::string &str);
88 
89   void InsertStringAtIndex(size_t idx, std::string &&str);
90 
91   void InsertStringAtIndex(size_t id, const char *str);
92 
93   void DeleteStringAtIndex(size_t id);
94 
95   void RemoveBlankLines();
96 
97   size_t SplitIntoLines(const std::string &lines);
98 
99   size_t SplitIntoLines(const char *lines, size_t len);
100 
101   std::string CopyList(const char *item_preamble = nullptr,
102                        const char *items_sep = "\n") const;
103 
104   StringList &operator<<(const char *str);
105 
106   StringList &operator<<(const std::string &s);
107 
108   StringList &operator<<(const StringList &strings);
109 
110   // Copy assignment for a vector of strings
111   StringList &operator=(const std::vector<std::string> &rhs);
112 
113   // Dump the StringList to the given lldb_private::Log, `log`, one item per
114   // line. If given, `name` will be used to identify the start and end of the
115   // list in the output.
116   virtual void LogDump(Log *log, const char *name = nullptr);
117 
118   // Static helper to convert an iterable of strings to a StringList, and then
119   // dump it with the semantics of the `LogDump` method.
120   template <typename T>
121   static void LogDump(Log *log, T s_iterable, const char *name = nullptr) {
122     if (!log)
123       return;
124     // Make a copy of the iterable as a StringList
125     StringList l{};
126     for (const auto &s : s_iterable)
127       l << s;
128 
129     l.LogDump(log, name);
130   }
131 
132 private:
133   collection m_strings;
134 };
135 
136 } // namespace lldb_private
137 
138 #endif // LLDB_UTILITY_STRINGLIST_H
139