1317027Sdim//===-- StringList.h --------------------------------------------*- C++ -*-===//
2317027Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6317027Sdim//
7317027Sdim//===----------------------------------------------------------------------===//
8317027Sdim
9317027Sdim#ifndef liblldb_StringList_h_
10317027Sdim#define liblldb_StringList_h_
11317027Sdim
12317027Sdim#include "llvm/ADT/StringRef.h"
13317027Sdim
14344779Sdim#include <stddef.h>
15317027Sdim#include <string>
16317027Sdim#include <vector>
17317027Sdim
18317027Sdimnamespace lldb_private {
19317027Sdimclass Log;
20317027Sdimclass Stream;
21317027Sdim}
22317027Sdim
23317027Sdimnamespace lldb_private {
24317027Sdim
25317027Sdimclass StringList {
26360784Sdim  typedef std::vector<std::string> collection;
27360784Sdim
28317027Sdimpublic:
29317027Sdim  StringList();
30317027Sdim
31327952Sdim  explicit StringList(const char *str);
32317027Sdim
33317027Sdim  StringList(const char **strv, int strc);
34317027Sdim
35317027Sdim  virtual ~StringList();
36317027Sdim
37317027Sdim  void AppendString(const std::string &s);
38317027Sdim
39317027Sdim  void AppendString(std::string &&s);
40317027Sdim
41317027Sdim  void AppendString(const char *str);
42317027Sdim
43317027Sdim  void AppendString(const char *str, size_t str_len);
44317027Sdim
45317027Sdim  void AppendString(llvm::StringRef str);
46317027Sdim
47317027Sdim  void AppendList(const char **strv, int strc);
48317027Sdim
49317027Sdim  void AppendList(StringList strings);
50317027Sdim
51317027Sdim  size_t GetSize() const;
52317027Sdim
53317027Sdim  void SetSize(size_t n) { m_strings.resize(n); }
54317027Sdim
55317027Sdim  size_t GetMaxStringLength() const;
56317027Sdim
57360784Sdim  typedef collection::iterator iterator;
58360784Sdim  typedef collection::const_iterator const_iterator;
59360784Sdim
60360784Sdim  iterator begin() { return m_strings.begin(); }
61360784Sdim  iterator end() { return m_strings.end(); }
62360784Sdim  const_iterator begin() const { return m_strings.begin(); }
63360784Sdim  const_iterator end() const { return m_strings.end(); }
64360784Sdim
65317027Sdim  std::string &operator[](size_t idx) {
66317027Sdim    // No bounds checking, verify "idx" is good prior to calling this function
67317027Sdim    return m_strings[idx];
68317027Sdim  }
69317027Sdim
70317027Sdim  const std::string &operator[](size_t idx) const {
71317027Sdim    // No bounds checking, verify "idx" is good prior to calling this function
72317027Sdim    return m_strings[idx];
73317027Sdim  }
74317027Sdim
75317027Sdim  void PopBack() { m_strings.pop_back(); }
76317027Sdim  const char *GetStringAtIndex(size_t idx) const;
77317027Sdim
78317027Sdim  void Join(const char *separator, Stream &strm);
79317027Sdim
80317027Sdim  void Clear();
81317027Sdim
82360784Sdim  std::string LongestCommonPrefix();
83317027Sdim
84317027Sdim  void InsertStringAtIndex(size_t idx, const std::string &str);
85317027Sdim
86317027Sdim  void InsertStringAtIndex(size_t idx, std::string &&str);
87317027Sdim
88317027Sdim  void InsertStringAtIndex(size_t id, const char *str);
89317027Sdim
90317027Sdim  void DeleteStringAtIndex(size_t id);
91317027Sdim
92317027Sdim  void RemoveBlankLines();
93317027Sdim
94317027Sdim  size_t SplitIntoLines(const std::string &lines);
95317027Sdim
96317027Sdim  size_t SplitIntoLines(const char *lines, size_t len);
97317027Sdim
98317027Sdim  std::string CopyList(const char *item_preamble = nullptr,
99317027Sdim                       const char *items_sep = "\n") const;
100317027Sdim
101317027Sdim  StringList &operator<<(const char *str);
102317027Sdim
103317027Sdim  StringList &operator<<(const std::string &s);
104317027Sdim
105317027Sdim  StringList &operator<<(StringList strings);
106317027Sdim
107317027Sdim  // Copy assignment for a vector of strings
108317027Sdim  StringList &operator=(const std::vector<std::string> &rhs);
109317027Sdim
110317027Sdim  // Dump the StringList to the given lldb_private::Log, `log`, one item per
111341825Sdim  // line. If given, `name` will be used to identify the start and end of the
112341825Sdim  // list in the output.
113317027Sdim  virtual void LogDump(Log *log, const char *name = nullptr);
114317027Sdim
115317027Sdim  // Static helper to convert an iterable of strings to a StringList, and then
116317027Sdim  // dump it with the semantics of the `LogDump` method.
117317027Sdim  template <typename T>
118317027Sdim  static void LogDump(Log *log, T s_iterable, const char *name = nullptr) {
119317027Sdim    if (!log)
120317027Sdim      return;
121317027Sdim    // Make a copy of the iterable as a StringList
122317027Sdim    StringList l{};
123317027Sdim    for (const auto &s : s_iterable)
124317027Sdim      l << s;
125317027Sdim
126317027Sdim    l.LogDump(log, name);
127317027Sdim  }
128317027Sdim
129317027Sdimprivate:
130360784Sdim  collection m_strings;
131317027Sdim};
132317027Sdim
133317027Sdim} // namespace lldb_private
134317027Sdim
135317027Sdim#endif // liblldb_StringList_h_
136