StringList.h revision 327952
1//===-- StringList.h --------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_StringList_h_
11#define liblldb_StringList_h_
12
13#include "llvm/ADT/StringRef.h"
14
15#include <stddef.h> // for size_t
16#include <string>
17#include <vector>
18
19namespace lldb_private {
20class Log;
21}
22namespace lldb_private {
23class Stream;
24}
25
26namespace lldb_private {
27
28class StringList {
29public:
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 AppendList(const char **strv, int strc);
49
50  void AppendList(StringList strings);
51
52  size_t GetSize() const;
53
54  void SetSize(size_t n) { m_strings.resize(n); }
55
56  size_t GetMaxStringLength() const;
57
58  std::string &operator[](size_t idx) {
59    // No bounds checking, verify "idx" is good prior to calling this function
60    return m_strings[idx];
61  }
62
63  const std::string &operator[](size_t idx) const {
64    // No bounds checking, verify "idx" is good prior to calling this function
65    return m_strings[idx];
66  }
67
68  void PopBack() { m_strings.pop_back(); }
69  const char *GetStringAtIndex(size_t idx) const;
70
71  void Join(const char *separator, Stream &strm);
72
73  void Clear();
74
75  void LongestCommonPrefix(std::string &common_prefix);
76
77  void InsertStringAtIndex(size_t idx, const std::string &str);
78
79  void InsertStringAtIndex(size_t idx, std::string &&str);
80
81  void InsertStringAtIndex(size_t id, const char *str);
82
83  void DeleteStringAtIndex(size_t id);
84
85  void RemoveBlankLines();
86
87  size_t SplitIntoLines(const std::string &lines);
88
89  size_t SplitIntoLines(const char *lines, size_t len);
90
91  std::string CopyList(const char *item_preamble = nullptr,
92                       const char *items_sep = "\n") const;
93
94  StringList &operator<<(const char *str);
95
96  StringList &operator<<(const std::string &s);
97
98  StringList &operator<<(StringList strings);
99
100  // Copy assignment for a vector of strings
101  StringList &operator=(const std::vector<std::string> &rhs);
102
103  // This string list contains a list of valid auto completion
104  // strings, and the "s" is passed in. "matches" is filled in
105  // with zero or more string values that start with "s", and
106  // the first string to exactly match one of the string
107  // values in this collection, will have "exact_matches_idx"
108  // filled in to match the index, or "exact_matches_idx" will
109  // have SIZE_MAX
110  size_t AutoComplete(llvm::StringRef s, StringList &matches,
111                      size_t &exact_matches_idx) const;
112
113  // Dump the StringList to the given lldb_private::Log, `log`, one item per
114  // line.
115  // If given, `name` will be used to identify the start and end of the list in
116  // the output.
117  virtual void LogDump(Log *log, const char *name = nullptr);
118
119  // Static helper to convert an iterable of strings to a StringList, and then
120  // dump it with the semantics of the `LogDump` method.
121  template <typename T>
122  static void LogDump(Log *log, T s_iterable, const char *name = nullptr) {
123    if (!log)
124      return;
125    // Make a copy of the iterable as a StringList
126    StringList l{};
127    for (const auto &s : s_iterable)
128      l << s;
129
130    l.LogDump(log, name);
131  }
132
133private:
134  std::vector<std::string> m_strings;
135};
136
137} // namespace lldb_private
138
139#endif // liblldb_StringList_h_
140