LLDB  mainline
StringList.h
Go to the documentation of this file.
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 
18 namespace lldb_private {
19 class Log;
20 }
21 namespace lldb_private {
22 class Stream;
23 }
24 
25 namespace lldb_private {
26 
27 class StringList {
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  std::string &operator[](size_t idx) {
58  // No bounds checking, verify "idx" is good prior to calling this function
59  return m_strings[idx];
60  }
61 
62  const std::string &operator[](size_t idx) const {
63  // No bounds checking, verify "idx" is good prior to calling this function
64  return m_strings[idx];
65  }
66 
67  void PopBack() { m_strings.pop_back(); }
68  const char *GetStringAtIndex(size_t idx) const;
69 
70  void Join(const char *separator, Stream &strm);
71 
72  void Clear();
73 
74  void LongestCommonPrefix(std::string &common_prefix);
75 
76  void InsertStringAtIndex(size_t idx, const std::string &str);
77 
78  void InsertStringAtIndex(size_t idx, std::string &&str);
79 
80  void InsertStringAtIndex(size_t id, const char *str);
81 
82  void DeleteStringAtIndex(size_t id);
83 
84  void RemoveBlankLines();
85 
86  size_t SplitIntoLines(const std::string &lines);
87 
88  size_t SplitIntoLines(const char *lines, size_t len);
89 
90  std::string CopyList(const char *item_preamble = nullptr,
91  const char *items_sep = "\n") const;
92 
93  StringList &operator<<(const char *str);
94 
95  StringList &operator<<(const std::string &s);
96 
98 
99  // Copy assignment for a vector of strings
100  StringList &operator=(const std::vector<std::string> &rhs);
101 
102  // This string list contains a list of valid auto completion strings, and the
103  // "s" is passed in. "matches" is filled in with zero or more string values
104  // that start with "s", and the first string to exactly match one of the
105  // string values in this collection, will have "exact_matches_idx" filled in
106  // to match the index, or "exact_matches_idx" will have SIZE_MAX
107  size_t AutoComplete(llvm::StringRef s, StringList &matches,
108  size_t &exact_matches_idx) const;
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  std::vector<std::string> m_strings;
131 };
132 
133 } // namespace lldb_private
134 
135 #endif // liblldb_StringList_h_
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void LongestCommonPrefix(std::string &common_prefix)
Definition: StringList.cpp:103
StringList & operator<<(const char *str)
Definition: StringList.cpp:208
const std::string & operator[](size_t idx) const
Definition: StringList.h:62
static void LogDump(Log *log, T s_iterable, const char *name=nullptr)
Definition: StringList.h:118
size_t GetMaxStringLength() const
Definition: StringList.cpp:72
const char * GetStringAtIndex(size_t idx) const
Definition: StringList.cpp:82
void AppendList(const char **strv, int strc)
Definition: StringList.cpp:56
size_t AutoComplete(llvm::StringRef s, StringList &matches, size_t &exact_matches_idx) const
Definition: StringList.cpp:229
std::string CopyList(const char *item_preamble=nullptr, const char *items_sep="\) const
Definition: StringList.cpp:195
void DeleteStringAtIndex(size_t id)
Definition: StringList.cpp:144
size_t GetSize() const
Definition: StringList.cpp:70
void InsertStringAtIndex(size_t idx, const std::string &str)
Definition: StringList.cpp:130
virtual void LogDump(Log *log, const char *name=nullptr)
Definition: StringList.cpp:252
size_t SplitIntoLines(const std::string &lines)
Definition: StringList.cpp:149
void Join(const char *separator, Stream &strm)
Definition: StringList.cpp:88
StringList & operator=(const std::vector< std::string > &rhs)
Definition: StringList.cpp:223
void AppendString(const std::string &s)
Definition: StringList.cpp:43
void SetSize(size_t n)
Definition: StringList.h:53
std::string & operator[](size_t idx)
Definition: StringList.h:57