Skip to content

Commit

Permalink
Merge pull request #14517 from ethereum/indent-refactor
Browse files Browse the repository at this point in the history
Generalize helpers for indenting and prefixing
  • Loading branch information
cameel authored Oct 13, 2023
2 parents 106ae7e + b56e26a commit 3dab116
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 280 deletions.
2 changes: 0 additions & 2 deletions libsolutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ set(sources
ErrorCodes.h
FixedHash.h
FunctionSelector.h
IndentedWriter.cpp
IndentedWriter.h
IpfsHash.cpp
IpfsHash.h
JSON.cpp
Expand Down
65 changes: 0 additions & 65 deletions libsolutil/IndentedWriter.cpp

This file was deleted.

66 changes: 0 additions & 66 deletions libsolutil/IndentedWriter.h

This file was deleted.

35 changes: 35 additions & 0 deletions libsolutil/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
*/

#include <libsolutil/StringUtils.h>

#include <boost/algorithm/string/trim.hpp>

#include <sstream>
#include <string>
#include <vector>

Expand Down Expand Up @@ -190,3 +194,34 @@ std::string solidity::util::formatNumberReadable(bigint const& _value, bool _use
return sign + str;
}

std::string solidity::util::prefixLines(
std::string const& _input,
std::string const& _prefix,
bool _trimPrefix
)
{
std::ostringstream output;
printPrefixed(output, _input, _prefix, _trimPrefix, false /* _ensureFinalNewline */);
return output.str();
}

void solidity::util::printPrefixed(
std::ostream& _output,
std::string const& _input,
std::string const& _prefix,
bool _trimPrefix,
bool _ensureFinalNewline
)
{
std::istringstream input(_input);
std::string line;
while (std::getline(input, line))
{
if (line.empty() && _trimPrefix)
_output << boost::trim_right_copy(_prefix);
else
_output << _prefix << line;
if (!input.eof() || _ensureFinalNewline)
_output << '\n';
}
}
34 changes: 33 additions & 1 deletion libsolutil/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,44 @@ inline bool isDigit(char _c)
return isdigit(_c, std::locale::classic());
}

// Checks if character is printable using classic "C" locale
/// Checks if character is printable using classic "C" locale
/// @param _c character to be checked
/// @return true if _c is a printable character, false otherwise.
inline bool isPrint(char _c)
{
return isprint(_c, std::locale::classic());
}

/// Adds a prefix to every line in the input.
/// @see printPrefixed()
std::string prefixLines(
std::string const& _input,
std::string const& _prefix,
bool _trimPrefix = true
);

/// Prints to a stream, adding a prefix to every line in the input.
/// Assumes \n as the line separator.
/// @param _trimPrefix If true, the function avoids introducing trailing whitespace on empty lines.
/// This is achieved by removing trailing spaces from the prefix on such lines.
/// Note that tabs and newlines are not removed, only spaces are.
/// @param _finalNewline If true, an extra \n will be printed at the end of @a _input if it does
/// not already end with one.
void printPrefixed(
std::ostream& _output,
std::string const& _input,
std::string const& _prefix,
bool _trimPrefix = true,
bool _ensureFinalNewline = true
);

/// Adds a standard indent of 4 spaces to every line in the input.
/// Assumes \n as the line separator.
/// @param _indentEmptyLines If true, the indent will be applied to empty lines as well, resulting
/// such lines containing trailing whitespace.
inline std::string indent(std::string const& _input, bool _indentEmptyLines = false)
{
return prefixLines(_input, " ", !_indentEmptyLines);
}

}
14 changes: 0 additions & 14 deletions libyul/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include <libsolutil/StringUtils.h>

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/replace.hpp>

#include <range/v3/view/transform.hpp>

Expand All @@ -40,18 +38,6 @@ using namespace solidity::langutil;
using namespace solidity::util;
using namespace solidity::yul;

namespace
{

std::string indent(std::string const& _input)
{
if (_input.empty())
return _input;
return boost::replace_all_copy(" " + _input, "\n", "\n ");
}

}

std::string Data::toString(Dialect const*, DebugInfoSelection const&, CharStreamProvider const*) const
{
return "data \"" + name.str() + "\" hex\"" + util::toHex(data) + "\"";
Expand Down
1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ set(libsolutil_sources
libsolutil/CommonIO.cpp
libsolutil/FixedHash.cpp
libsolutil/FunctionSelector.cpp
libsolutil/IndentedWriter.cpp
libsolutil/IpfsHash.cpp
libsolutil/IterateReplacing.cpp
libsolutil/JSON.cpp
Expand Down
8 changes: 3 additions & 5 deletions test/CommonSyntaxTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <test/Common.h>
#include <test/TestCase.h>
#include <libsolutil/CommonIO.h>
#include <libsolutil/StringUtils.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -144,11 +145,8 @@ void CommonSyntaxTest::printSource(ostream& _stream, string const& _linePrefix,
else
{
if (outputSourceNames)
_stream << _linePrefix << "==== Source: " + name << " ====" << endl;
stringstream stream(source);
string line;
while (getline(stream, line))
_stream << _linePrefix << line << endl;
printPrefixed(_stream, "==== Source: " + name + " ====", _linePrefix);
printPrefixed(_stream, source, _linePrefix);
}
}

Expand Down
23 changes: 6 additions & 17 deletions test/TestCase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include <test/TestCase.h>

#include <libsolutil/AnsiColorized.h>
#include <libsolutil/StringUtils.h>

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>

#include <iostream>
Expand All @@ -31,6 +31,7 @@ using namespace std;
using namespace solidity;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace solidity::util;

void TestCase::printSettings(ostream& _stream, const string& _linePrefix, const bool)
{
Expand Down Expand Up @@ -69,26 +70,14 @@ void TestCase::expect(string::iterator& _it, string::iterator _end, string::valu
++_it;
}

void TestCase::printIndented(ostream& _stream, string const& _output, string const& _linePrefix) const
{
stringstream output(_output);
string line;
while (getline(output, line))
if (line.empty())
// Avoid trailing spaces.
_stream << boost::trim_right_copy(_linePrefix) << endl;
else
_stream << _linePrefix << line << endl;
}

void TestCase::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
printPrefixed(_stream, m_source, _linePrefix);
}

void TestCase::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
printPrefixed(_stream, m_obtainedResult, _linePrefix);
}

TestCase::TestResult TestCase::checkResult(std::ostream& _stream, const std::string& _linePrefix, bool const _formatted)
Expand All @@ -99,10 +88,10 @@ TestCase::TestResult TestCase::checkResult(std::ostream& _stream, const std::str
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::CYAN})
<< _linePrefix << "Expected result:" << endl;
// TODO could compute a simple diff with highlighted lines
printIndented(_stream, m_expectation, nextIndentLevel);
printPrefixed(_stream, m_expectation, nextIndentLevel);
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::CYAN})
<< _linePrefix << "Obtained result:" << endl;
printIndented(_stream, m_obtainedResult, nextIndentLevel);
printPrefixed(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
}
return TestResult::Success;
Expand Down
1 change: 0 additions & 1 deletion test/TestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class TestCase
++_it;
}

void printIndented(std::ostream& _stream, std::string const& _output, std::string const& _linePrefix = "") const;
TestCase::TestResult checkResult(std::ostream& _stream, const std::string& _linePrefix, bool const _formatted);

std::string m_source;
Expand Down
Loading

0 comments on commit 3dab116

Please sign in to comment.