summaryrefslogtreecommitdiff
path: root/chromeos-dbus-bindings/indented_text.cc
blob: 2af6419be58b6250b84343111815c4cca396232b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos-dbus-bindings/indented_text.h"

#include <string>
#include <utility>
#include <vector>

#include <base/logging.h>
#include <base/strings/string_util.h>
#include <brillo/strings/string_utils.h>

using std::string;
using std::vector;

namespace chromeos_dbus_bindings {

IndentedText::IndentedText() : offset_(0) {}

void IndentedText::AddBlankLine() {
  AddLine("");
}

void IndentedText::AddBlock(const IndentedText& block) {
  AddBlockWithOffset(block, 0);
}

void IndentedText::AddBlockWithOffset(const IndentedText& block, size_t shift) {
  for (const auto& member : block.contents_) {
    AddLineWithOffset(member.first, member.second + shift);
  }
}

void IndentedText::AddLine(const std::string& line) {
  AddLineWithOffset(line, 0);
}

void IndentedText::AddLineWithOffset(const std::string& line, size_t shift) {
  contents_.emplace_back(line, shift + offset_);
}

void IndentedText::AddLineAndPushOffsetTo(const std::string& line,
                                          size_t occurrence,
                                          char c) {
  AddLine(line);
  size_t pos = 0;
  while (occurrence > 0) {
    pos = line.find(c, pos);
    CHECK(pos != string::npos);
    pos++;
    occurrence--;
  }
  PushOffset(pos);
}

void IndentedText::AddComments(const std::string& doc_string) {
  // Try to retain indentation in the comments. Find the first non-empty line
  // of the comment and find its whitespace indentation prefix.
  // For all subsequent lines, remove the same whitespace prefix as found
  // at the first line of the comment but keep any additional spaces to
  // maintain the comment layout.
  auto lines = brillo::string_utils::Split(doc_string, "\n", false, false);
  vector<string> lines_out;
  lines_out.reserve(lines.size());
  bool first_nonempty_found = false;
  std::string trim_prefix;
  for (string line : lines) {
    base::TrimWhitespaceASCII(line, base::TRIM_TRAILING, &line);
    if (!first_nonempty_found) {
      size_t pos = line.find_first_not_of(" \t");
      if (pos != std::string::npos) {
        first_nonempty_found = true;
        trim_prefix = line.substr(0, pos);
        lines_out.push_back(line.substr(pos));
      }
    } else {
      if (base::StartsWithASCII(line, trim_prefix, false)) {
        line = line.substr(trim_prefix.length());
      } else {
        base::TrimWhitespaceASCII(line, base::TRIM_LEADING, &line);
      }
      lines_out.push_back(line);
    }
  }

  // We already eliminated all empty lines at the beginning of the comment
  // block. Now remove the trailing empty lines.
  while (!lines_out.empty() && lines_out.back().empty())
    lines_out.pop_back();

  for (const string& line : lines_out) {
    const bool all_whitespace = (line.find_first_not_of(" \t") == string::npos);
    if (all_whitespace) {
      AddLine("//");
    } else {
      AddLine("// " + line);
    }
  }
}

string IndentedText::GetContents() const {
  string output;
  for (const string& line : GetLines()) {
    output.append(line);
    output.append("\n");
  }
  return output;
}

std::vector<std::string> IndentedText::GetLines() const {
  vector<string> result;
  for (const auto& member : contents_) {
    const string& line = member.first;
    size_t shift = line.empty() ? 0 : member.second;
    string indent(shift, ' ');
    result.push_back(indent + line);
  }
  return result;
}

void IndentedText::PushOffset(size_t shift) {
  offset_ += shift;
  offset_history_.push_back(shift);
}

void IndentedText::PopOffset() {
  CHECK(!offset_history_.empty());
  offset_ -= offset_history_.back();
  offset_history_.pop_back();
}

void IndentedText::Reset() {
  offset_ = 0;
  offset_history_.clear();
  contents_.clear();
}

}  // namespace chromeos_dbus_bindings