• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROMEOS_DBUS_BINDINGS_INDENTED_TEXT_H_
6 #define CHROMEOS_DBUS_BINDINGS_INDENTED_TEXT_H_
7 
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 #include <base/macros.h>
13 
14 namespace chromeos_dbus_bindings {
15 
16 class IndentedText {
17  public:
18   IndentedText();
19   virtual ~IndentedText() = default;
20 
21   // Insert a blank line.
22   void AddBlankLine();
23 
24   // Insert a block of indented text.
25   void AddBlock(const IndentedText& block);
26   void AddBlockWithOffset(const IndentedText& block, size_t shift);
27 
28   // Add a line at the current indentation.
29   void AddLine(const std::string& line);
30   void AddLineWithOffset(const std::string& line, size_t shift);
31   // Adds a line and pushes an offset past the |nth_occurrence| of character |c|
32   // in that line, effectively allowing to align following line to the position
33   // following that character.
34   void AddLineAndPushOffsetTo(const std::string& line,
35                               size_t nth_occurrence,
36                               char c);
37 
38   // Adds a block of comments.
39   void AddComments(const std::string& doc_string);
40 
41   // Return a string representing the indented text.
42   std::string GetContents() const;
43 
44   // Return a list of lines representing the intended indented text, not
45   // including the \n.
46   std::vector<std::string> GetLines() const;
47 
48   // Add or remove an offset to the current stack of indentation offsets.
49   void PushOffset(size_t shift);
50   void PopOffset();
51 
52   // Reset to initial state.
53   void Reset();
54 
55 
56  private:
57   using IndentedLine = std::pair<std::string, size_t>;
58 
59   friend class IndentedTextTest;
60 
61   size_t offset_;
62   std::vector<size_t> offset_history_;
63   std::vector<IndentedLine> contents_;
64 
65   DISALLOW_COPY_AND_ASSIGN(IndentedText);
66 };
67 
68 }  // namespace chromeos_dbus_bindings
69 
70 #endif  // CHROMEOS_DBUS_BINDINGS_INDENTED_TEXT_H_
71