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 
14 #include <stddef.h>
15 #include <string>
16 #include <vector>
17 
18 namespace lldb_private {
19 class Log;
20 class Stream;
21 }
22 
23 namespace lldb_private {
24 
25 class StringList {
26   typedef std::vector<std::string> collection;
27 
28 public:
29   StringList();
30 
31   explicit StringList(const char *str);
32 
33   StringList(const char **strv, int strc);
34 
35   virtual ~StringList();
36 
37   void AppendString(const std::string &s);
38 
39   void AppendString(std::string &&s);
40 
41   void AppendString(const char *str);
42 
43   void AppendString(const char *str, size_t str_len);
44 
45   void AppendString(llvm::StringRef str);
46 
47   void AppendList(const char **strv, int strc);
48 
49   void AppendList(StringList strings);
50 
51   size_t GetSize() const;
52 
53   void SetSize(size_t n) { m_strings.resize(n); }
54 
55   size_t GetMaxStringLength() const;
56 
57   typedef collection::iterator iterator;
58   typedef collection::const_iterator const_iterator;
59 
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(); }
64 
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];
68   }
69 
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];
73   }
74 
75   void PopBack() { m_strings.pop_back(); }
76   const char *GetStringAtIndex(size_t idx) const;
77 
78   void Join(const char *separator, Stream &strm);
79 
80   void Clear();
81 
82   std::string LongestCommonPrefix();
83 
84   void InsertStringAtIndex(size_t idx, const std::string &str);
85 
86   void InsertStringAtIndex(size_t idx, std::string &&str);
87 
88   void InsertStringAtIndex(size_t id, const char *str);
89 
90   void DeleteStringAtIndex(size_t id);
91 
92   void RemoveBlankLines();
93 
94   size_t SplitIntoLines(const std::string &lines);
95 
96   size_t SplitIntoLines(const char *lines, size_t len);
97 
98   std::string CopyList(const char *item_preamble = nullptr,
99                        const char *items_sep = "\n") const;
100 
101   StringList &operator<<(const char *str);
102 
103   StringList &operator<<(const std::string &s);
104 
105   StringList &operator<<(const StringList &strings);
106 
107   // Copy assignment for a vector of strings
108   StringList &operator=(const std::vector<std::string> &rhs);
109 
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);
114 
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) {
119     if (!log)
120       return;
121     // Make a copy of the iterable as a StringList
122     StringList l{};
123     for (const auto &s : s_iterable)
124       l << s;
125 
126     l.LogDump(log, name);
127   }
128 
129 private:
130   collection m_strings;
131 };
132 
133 } // namespace lldb_private
134 
135 #endif // LLDB_UTILITY_STRINGLIST_H
136