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