summaryrefslogtreecommitdiff
path: root/mlir/lib/Support/IndentedOstream.cpp
blob: 4e2cad11712f8c61ca9d2c8a02eb5b11200abbe9 (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
//===- IndentedOstream.cpp - raw ostream wrapper to indent ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// raw_ostream subclass that keeps track of indentation for textual output
// where indentation helps readability.
//
//===----------------------------------------------------------------------===//

#include "mlir/Support/IndentedOstream.h"

using namespace mlir;

raw_indented_ostream &
mlir::raw_indented_ostream::printReindented(StringRef str,
                                            StringRef extraPrefix) {
  StringRef output = str;
  // Skip empty lines.
  while (!output.empty()) {
    auto split = output.split('\n');
    size_t indent = split.first.find_first_not_of(" \t");
    if (indent != StringRef::npos) {
      // Set an initial value.
      leadingWs = indent;
      break;
    }
    output = split.second;
  }
  // Determine the maximum indent.
  StringRef remaining = output;
  while (!remaining.empty()) {
    auto split = remaining.split('\n');
    size_t indent = split.first.find_first_not_of(" \t");
    if (indent != StringRef::npos)
      leadingWs = std::min(leadingWs, static_cast<int>(indent));
    remaining = split.second;
  }
  // Print, skipping the empty lines.
  std::swap(currentExtraPrefix, extraPrefix);
  *this << output;
  std::swap(currentExtraPrefix, extraPrefix);
  leadingWs = 0;
  return *this;
}

void mlir::raw_indented_ostream::write_impl(const char *ptr, size_t size) {
  StringRef str(ptr, size);
  // Print out indented.
  auto print = [this](StringRef str) {
    if (atStartOfLine)
      os.indent(currentIndent) << currentExtraPrefix << str.substr(leadingWs);
    else
      os << str.substr(leadingWs);
  };

  while (!str.empty()) {
    size_t idx = str.find('\n');
    if (idx == StringRef::npos) {
      if (!str.substr(leadingWs).empty()) {
        print(str);
        atStartOfLine = false;
      }
      break;
    }

    auto split =
        std::make_pair(str.slice(0, idx), str.slice(idx + 1, StringRef::npos));
    // Print empty new line without spaces if line only has spaces and no extra
    // prefix is requested.
    if (!split.first.ltrim().empty() || !currentExtraPrefix.empty())
      print(split.first);
    os << '\n';
    atStartOfLine = true;
    str = split.second;
  }
}