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 LLDB_UTILITY_STRINGLIST_H
10 #define LLDB_UTILITY_STRINGLIST_H
11 
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/Twine.h"
14 
15 #include <cstddef>
16 #include <string>
17 #include <vector>
18 
19 namespace lldb_private {
20 class Log;
21 class Stream;
22 }
23 
24 namespace lldb_private {
25 
26 class StringList {
27  typedef std::vector<std::string> collection;
28 
29 public:
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 AppendString(const llvm::Twine &str);
49 
50  void AppendList(const char **strv, int strc);
51 
52  void AppendList(StringList strings);
53 
54  size_t GetSize() const;
55 
56  void SetSize(size_t n) { m_strings.resize(n); }
57 
58  size_t GetMaxStringLength() const;
59 
60  typedef collection::iterator iterator;
61  typedef collection::const_iterator const_iterator;
62 
63  iterator begin() { return m_strings.begin(); }
64  iterator end() { return m_strings.end(); }
65  const_iterator begin() const { return m_strings.begin(); }
66  const_iterator end() const { return m_strings.end(); }
67 
68  std::string &operator[](size_t idx) {
69  // No bounds checking, verify "idx" is good prior to calling this function
70  return m_strings[idx];
71  }
72 
73  const std::string &operator[](size_t idx) const {
74  // No bounds checking, verify "idx" is good prior to calling this function
75  return m_strings[idx];
76  }
77 
78  void PopBack() { m_strings.pop_back(); }
79  const char *GetStringAtIndex(size_t idx) const;
80 
81  void Join(const char *separator, Stream &strm);
82 
83  void Clear();
84 
86 
87  void InsertStringAtIndex(size_t idx, const std::string &str);
88 
89  void InsertStringAtIndex(size_t idx, std::string &&str);
90 
91  void InsertStringAtIndex(size_t id, const char *str);
92 
93  void DeleteStringAtIndex(size_t id);
94 
95  void RemoveBlankLines();
96 
97  size_t SplitIntoLines(const std::string &lines);
98 
99  size_t SplitIntoLines(const char *lines, size_t len);
100 
101  std::string CopyList(const char *item_preamble = nullptr,
102  const char *items_sep = "\n") const;
103 
104  StringList &operator<<(const char *str);
105 
106  StringList &operator<<(const std::string &s);
107 
108  StringList &operator<<(const StringList &strings);
109 
110  // Copy assignment for a vector of strings
111  StringList &operator=(const std::vector<std::string> &rhs);
112 
113  // Dump the StringList to the given lldb_private::Log, `log`, one item per
114  // line. If given, `name` will be used to identify the start and end of the
115  // list in the output.
116  virtual void LogDump(Log *log, const char *name = nullptr);
117 
118  // Static helper to convert an iterable of strings to a StringList, and then
119  // dump it with the semantics of the `LogDump` method.
120  template <typename T>
121  static void LogDump(Log *log, T s_iterable, const char *name = nullptr) {
122  if (!log)
123  return;
124  // Make a copy of the iterable as a StringList
125  StringList l{};
126  for (const auto &s : s_iterable)
127  l << s;
128 
129  l.LogDump(log, name);
130  }
131 
132 private:
134 };
135 
136 } // namespace lldb_private
137 
138 #endif // LLDB_UTILITY_STRINGLIST_H
lldb_private::StringList::SetSize
void SetSize(size_t n)
Definition: StringList.h:56
lldb_private::StringList::Clear
void Clear()
Definition: StringList.cpp:105
lldb_private::StringList::begin
iterator begin()
Definition: StringList.h:63
lldb_private::StringList::AppendString
void AppendString(const std::string &s)
Definition: StringList.cpp:43
lldb_private::StringList::operator[]
std::string & operator[](size_t idx)
Definition: StringList.h:68
lldb_private::StringList::SplitIntoLines
size_t SplitIntoLines(const std::string &lines)
Definition: StringList.cpp:152
lldb_private::StringList::InsertStringAtIndex
void InsertStringAtIndex(size_t idx, const std::string &str)
Definition: StringList.cpp:133
lldb_private::Stream
Definition: Stream.h:28
lldb_private::StringList::m_strings
collection m_strings
Definition: StringList.h:133
lldb_private::StringList::GetSize
size_t GetSize() const
Definition: StringList.cpp:74
lldb_private::StringList::end
iterator end()
Definition: StringList.h:64
lldb_private::StringList::StringList
StringList()
Definition: StringList.cpp:22
lldb_private::StringList::GetStringAtIndex
const char * GetStringAtIndex(size_t idx) const
Definition: StringList.cpp:86
lldb_private::StringList::collection
std::vector< std::string > collection
Definition: StringList.h:27
lldb_private::StringList::GetMaxStringLength
size_t GetMaxStringLength() const
Definition: StringList.cpp:76
lldb_private::StringList::end
const_iterator end() const
Definition: StringList.h:66
lldb_private::StringList::begin
const_iterator begin() const
Definition: StringList.h:65
lldb_private::StringList
Definition: StringList.h:26
lldb_private::StringList::iterator
collection::iterator iterator
Definition: StringList.h:60
lldb_private::StringList::LongestCommonPrefix
std::string LongestCommonPrefix()
Definition: StringList.cpp:107
lldb_private::StringList::RemoveBlankLines
void RemoveBlankLines()
Definition: StringList.cpp:185
lldb_private::StringList::operator<<
StringList & operator<<(const char *str)
Definition: StringList.cpp:211
lldb_private::StringList::~StringList
virtual ~StringList()
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::StringList::const_iterator
collection::const_iterator const_iterator
Definition: StringList.h:61
lldb_private::StringList::AppendList
void AppendList(const char **strv, int strc)
Definition: StringList.cpp:62
lldb_private::StringList::operator=
StringList & operator=(const std::vector< std::string > &rhs)
Definition: StringList.cpp:226
lldb_private::StringList::CopyList
std::string CopyList(const char *item_preamble=nullptr, const char *items_sep="\n") const
Definition: StringList.cpp:198
lldb_private::StringList::PopBack
void PopBack()
Definition: StringList.h:78
lldb_private::StringList::operator[]
const std::string & operator[](size_t idx) const
Definition: StringList.h:73
lldb_private::StringList::DeleteStringAtIndex
void DeleteStringAtIndex(size_t id)
Definition: StringList.cpp:147
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::StringList::LogDump
virtual void LogDump(Log *log, const char *name=nullptr)
Definition: StringList.cpp:232
lldb_private::Log
Definition: Log.h:49
lldb_private::StringList::LogDump
static void LogDump(Log *log, T s_iterable, const char *name=nullptr)
Definition: StringList.h:121
lldb_private::StringList::Join
void Join(const char *separator, Stream &strm)
Definition: StringList.cpp:92