StringList.h revision 341825
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 strings, and the
104  // "s" is passed in. "matches" is filled in with zero or more string values
105  // that start with "s", and the first string to exactly match one of the
106  // string values in this collection, will have "exact_matches_idx" filled in
107  // to match the index, or "exact_matches_idx" will have SIZE_MAX
108  size_t AutoComplete(llvm::StringRef s, StringList &matches,
109                      size_t &exact_matches_idx) const;
110
111  // Dump the StringList to the given lldb_private::Log, `log`, one item per
112  // line. If given, `name` will be used to identify the start and end of the
113  // list in the output.
114  virtual void LogDump(Log *log, const char *name = nullptr);
115
116  // Static helper to convert an iterable of strings to a StringList, and then
117  // dump it with the semantics of the `LogDump` method.
118  template <typename T>
119  static void LogDump(Log *log, T s_iterable, const char *name = nullptr) {
120    if (!log)
121      return;
122    // Make a copy of the iterable as a StringList
123    StringList l{};
124    for (const auto &s : s_iterable)
125      l << s;
126
127    l.LogDump(log, name);
128  }
129
130private:
131  std::vector<std::string> m_strings;
132};
133
134} // namespace lldb_private
135
136#endif // liblldb_StringList_h_
137