#include "mlir/Support/IndentedOstream.h"
using namespace mlir;
raw_indented_ostream &
mlir::raw_indented_ostream::printReindented(StringRef str) {
StringRef output = str;
while (!output.empty()) {
auto split = output.split('\n');
size_t indent = split.first.find_first_not_of(" \t");
if (indent != StringRef::npos) {
leadingWs = indent;
break;
}
output = split.second;
}
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;
}
*this << output;
leadingWs = 0;
return *this;
}
void mlir::raw_indented_ostream::write_impl(const char *ptr, size_t size) {
StringRef str(ptr, size);
auto print = [this](StringRef str) {
if (atStartOfLine)
os.indent(currentIndent) << 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));
if (!split.first.ltrim().empty())
print(split.first);
os << '\n';
atStartOfLine = true;
str = split.second;
}
}