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 
14 #include <cstddef>
15 #include <string>
16 #include <vector>
17 
18 namespace lldb_private {
19 class Log;
20 class Stream;
21 }
22 
23 namespace lldb_private {
24 
25 class StringList {
26  typedef std::vector<std::string> collection;
27 
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  typedef collection::iterator iterator;
58  typedef collection::const_iterator const_iterator;
59 
60  iterator begin() { return m_strings.begin(); }
61  iterator end() { return m_strings.end(); }
62  const_iterator begin() const { return m_strings.begin(); }
63  const_iterator end() const { return m_strings.end(); }
64 
65  std::string &operator[](size_t idx) {
66  // No bounds checking, verify "idx" is good prior to calling this function
67  return m_strings[idx];
68  }
69 
70  const std::string &operator[](size_t idx) const {
71  // No bounds checking, verify "idx" is good prior to calling this function
72  return m_strings[idx];
73  }
74 
75  void PopBack() { m_strings.pop_back(); }
76  const char *GetStringAtIndex(size_t idx) const;
77 
78  void Join(const char *separator, Stream &strm);
79 
80  void Clear();
81 
83 
84  void InsertStringAtIndex(size_t idx, const std::string &str);
85 
86  void InsertStringAtIndex(size_t idx, std::string &&str);
87 
88  void InsertStringAtIndex(size_t id, const char *str);
89 
90  void DeleteStringAtIndex(size_t id);
91 
92  void RemoveBlankLines();
93 
94  size_t SplitIntoLines(const std::string &lines);
95 
96  size_t SplitIntoLines(const char *lines, size_t len);
97 
98  std::string CopyList(const char *item_preamble = nullptr,
99  const char *items_sep = "\n") const;
100 
101  StringList &operator<<(const char *str);
102 
103  StringList &operator<<(const std::string &s);
104 
105  StringList &operator<<(const StringList &strings);
106 
107  // Copy assignment for a vector of strings
108  StringList &operator=(const std::vector<std::string> &rhs);
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:
131 };
132 
133 } // namespace lldb_private
134 
135 #endif // LLDB_UTILITY_STRINGLIST_H
lldb_private::StringList::SetSize
void SetSize(size_t n)
Definition: StringList.h:53
lldb_private::StringList::Clear
void Clear()
Definition: StringList.cpp:99
lldb_private::StringList::begin
iterator begin()
Definition: StringList.h:60
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:65
lldb_private::StringList::SplitIntoLines
size_t SplitIntoLines(const std::string &lines)
Definition: StringList.cpp:146
lldb_private::StringList::InsertStringAtIndex
void InsertStringAtIndex(size_t idx, const std::string &str)
Definition: StringList.cpp:127
lldb_private::Stream
Definition: Stream.h:28
lldb_private::StringList::m_strings
collection m_strings
Definition: StringList.h:130
lldb_private::StringList::GetSize
size_t GetSize() const
Definition: StringList.cpp:68
lldb_private::StringList::end
iterator end()
Definition: StringList.h:61
lldb_private::StringList::StringList
StringList()
Definition: StringList.cpp:22
lldb_private::StringList::GetStringAtIndex
const char * GetStringAtIndex(size_t idx) const
Definition: StringList.cpp:80
lldb_private::StringList::collection
std::vector< std::string > collection
Definition: StringList.h:26
lldb_private::StringList::GetMaxStringLength
size_t GetMaxStringLength() const
Definition: StringList.cpp:70
lldb_private::StringList::end
const_iterator end() const
Definition: StringList.h:63
lldb_private::StringList::begin
const_iterator begin() const
Definition: StringList.h:62
lldb_private::StringList
Definition: StringList.h:25
lldb_private::StringList::iterator
collection::iterator iterator
Definition: StringList.h:57
lldb_private::StringList::LongestCommonPrefix
std::string LongestCommonPrefix()
Definition: StringList.cpp:101
lldb_private::StringList::RemoveBlankLines
void RemoveBlankLines()
Definition: StringList.cpp:179
lldb_private::StringList::operator<<
StringList & operator<<(const char *str)
Definition: StringList.cpp:205
lldb_private::StringList::~StringList
virtual ~StringList()
Definition: StringList.cpp:36
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::StringList::const_iterator
collection::const_iterator const_iterator
Definition: StringList.h:58
lldb_private::StringList::AppendList
void AppendList(const char **strv, int strc)
Definition: StringList.cpp:56
lldb_private::StringList::operator=
StringList & operator=(const std::vector< std::string > &rhs)
Definition: StringList.cpp:220
lldb_private::StringList::CopyList
std::string CopyList(const char *item_preamble=nullptr, const char *items_sep="\n") const
Definition: StringList.cpp:192
lldb_private::StringList::PopBack
void PopBack()
Definition: StringList.h:75
lldb_private::StringList::operator[]
const std::string & operator[](size_t idx) const
Definition: StringList.h:70
lldb_private::StringList::DeleteStringAtIndex
void DeleteStringAtIndex(size_t id)
Definition: StringList.cpp:141
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:226
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:118
lldb_private::StringList::Join
void Join(const char *separator, Stream &strm)
Definition: StringList.cpp:86