diff options
Diffstat (limited to 'libdevcore')
-rw-r--r-- | libdevcore/CMakeLists.txt | 22 | ||||
-rw-r--r-- | libdevcore/Common.h | 3 | ||||
-rw-r--r-- | libdevcore/CommonData.h | 21 | ||||
-rw-r--r-- | libdevcore/FixedHash.h | 4 | ||||
-rw-r--r-- | libdevcore/IndentedWriter.cpp | 65 | ||||
-rw-r--r-- | libdevcore/IndentedWriter.h | 67 | ||||
-rw-r--r-- | libdevcore/Whiskers.cpp | 2 |
7 files changed, 165 insertions, 19 deletions
diff --git a/libdevcore/CMakeLists.txt b/libdevcore/CMakeLists.txt index c4f886a6..a1c4c2d3 100644 --- a/libdevcore/CMakeLists.txt +++ b/libdevcore/CMakeLists.txt @@ -1,14 +1,8 @@ -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSTATICLIB") - -aux_source_directory(. SRC_LIST) - -set(EXECUTABLE soldevcore) - -file(GLOB HEADERS "*.h") - -include_directories(..) -add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS}) - -eth_use(${EXECUTABLE} REQUIRED Dev::base) - -install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) +file(GLOB sources "*.cpp") +file(GLOB headers "*.h") + +add_library(devcore ${sources} ${headers}) +target_link_libraries(devcore PRIVATE ${Boost_FILESYSTEM_LIBRARIES} ${Boost_REGEX_LIBRARIES} ${Boost_SYSTEM_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) +target_include_directories(devcore SYSTEM PUBLIC ${Boost_INCLUDE_DIRS}) +target_include_directories(devcore PUBLIC "${CMAKE_SOURCE_DIR}") +add_dependencies(devcore solidity_BuildInfo.h) diff --git a/libdevcore/Common.h b/libdevcore/Common.h index c5b09a80..9d6dd408 100644 --- a/libdevcore/Common.h +++ b/libdevcore/Common.h @@ -44,7 +44,6 @@ #include <unordered_set> #include <functional> #include <string> -#include <chrono> #if defined(__GNUC__) #pragma warning(push) @@ -158,7 +157,7 @@ template <> inline u256 exp10<0>() class ScopeGuard { public: - ScopeGuard(std::function<void(void)> _f): m_f(_f) {} + explicit ScopeGuard(std::function<void(void)> _f): m_f(_f) {} ~ScopeGuard() { m_f(); } private: diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index ab4bfe68..5df8986a 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -145,6 +145,24 @@ inline std::string toHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd) return (prefix == HexPrefix::Add) ? "0x" + str : str; } +/// Returns decimal representation for small numbers and hex for large numbers. +inline std::string formatNumber(bigint const& _value) +{ + if (_value < 0) + return "-" + formatNumber(-_value); + if (_value > 0x1000000) + return toHex(toCompactBigEndian(_value), 2, HexPrefix::Add); + else + return _value.str(); +} + +inline std::string toCompactHexWithPrefix(u256 val) +{ + std::ostringstream ret; + ret << std::hex << val; + return "0x" + ret.str(); +} + // Algorithms for string and string-like collections. /// Escapes a string into the C-string representation. @@ -177,7 +195,8 @@ template <class T> inline std::vector<T> operator+(std::vector<T> const& _a, std::vector<T> const& _b) { std::vector<T> ret(_a); - return ret += _b; + ret += _b; + return ret; } template <class T, class V> diff --git a/libdevcore/FixedHash.h b/libdevcore/FixedHash.h index 5b1c7acf..141e9ffd 100644 --- a/libdevcore/FixedHash.h +++ b/libdevcore/FixedHash.h @@ -27,6 +27,7 @@ #include <cstdint> #include <algorithm> #include <boost/functional/hash.hpp> +#include <boost/io/ios_state.hpp> #include "CommonData.h" namespace dev @@ -59,7 +60,7 @@ public: enum ConstructFromHashType { AlignLeft, AlignRight, FailIfDifferent }; /// Construct an empty hash. - FixedHash() { m_data.fill(0); } + explicit FixedHash() { m_data.fill(0); } /// Construct from another hash, filling with zeroes or cropping as necessary. template <unsigned M> explicit FixedHash(FixedHash<M> const& _h, ConstructFromHashType _t = AlignLeft) { m_data.fill(0); unsigned c = std::min(M, N); for (unsigned i = 0; i < c; ++i) m_data[_t == AlignRight ? N - 1 - i : i] = _h[_t == AlignRight ? M - 1 - i : i]; } @@ -224,6 +225,7 @@ template<> inline size_t FixedHash<32>::hash::operator()(FixedHash<32> const& va template <unsigned N> inline std::ostream& operator<<(std::ostream& _out, FixedHash<N> const& _h) { + boost::io::ios_all_saver guard(_out); _out << std::noshowbase << std::hex << std::setfill('0'); for (unsigned i = 0; i < N; ++i) _out << std::setw(2) << (int)_h[i]; diff --git a/libdevcore/IndentedWriter.cpp b/libdevcore/IndentedWriter.cpp new file mode 100644 index 00000000..96aaf0fa --- /dev/null +++ b/libdevcore/IndentedWriter.cpp @@ -0,0 +1,65 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see <http://www.gnu.org/licenses/>. +*/ +/** + * @date 2017 + * Indented text writer. + */ + +#include <libdevcore/IndentedWriter.h> +#include <libdevcore/Assertions.h> + +using namespace std; +using namespace dev; + +string IndentedWriter::format() const +{ + string result; + for (auto const& line: m_lines) + result += string(line.indentation * 4, ' ') + line.contents + "\n"; + return result; +} + +void IndentedWriter::newLine() +{ + if (!m_lines.back().contents.empty()) + m_lines.push_back({ string(), m_lines.back().indentation }); +} + +void IndentedWriter::indent() +{ + newLine(); + m_lines.back().indentation++; +} + +void IndentedWriter::unindent() +{ + newLine(); + assertThrow(m_lines.back().indentation > 0, IndentedWriterError, "Negative indentation."); + m_lines.back().indentation--; +} + +void IndentedWriter::add(string const& _str) +{ + m_lines.back().contents += _str; +} + +void IndentedWriter::addLine(string const& _line) +{ + newLine(); + add(_line); + newLine(); +} diff --git a/libdevcore/IndentedWriter.h b/libdevcore/IndentedWriter.h new file mode 100644 index 00000000..4ddd87ed --- /dev/null +++ b/libdevcore/IndentedWriter.h @@ -0,0 +1,67 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see <http://www.gnu.org/licenses/>. +*/ +/** + * @date 2017 + * Indented text writer. + */ + +#pragma once + +#include <vector> +#include <string> + +#include <libdevcore/Exceptions.h> + +namespace dev +{ + +DEV_SIMPLE_EXCEPTION(IndentedWriterError); + +class IndentedWriter +{ +public: + explicit IndentedWriter(): m_lines(std::vector<Line>{{std::string(), 0}}) {} + + // Returns the formatted output. + std::string format() const; + + // Go one indentation level in. + void indent(); + + // Go one indentation level out. + void unindent(); + + // Add text. + void add(std::string const& _str); + + // Add text with new line. + void addLine(std::string const& _line); + + // Add new line. + void newLine(); + +private: + struct Line + { + std::string contents; + unsigned indentation; + }; + + std::vector<Line> m_lines; +}; + +} diff --git a/libdevcore/Whiskers.cpp b/libdevcore/Whiskers.cpp index 4bad8476..b0a4c755 100644 --- a/libdevcore/Whiskers.cpp +++ b/libdevcore/Whiskers.cpp @@ -90,7 +90,7 @@ string Whiskers::replace( string tagName(_match[1]); if (!tagName.empty()) { - assertThrow(_parameters.count(tagName), WhiskersError, "Tag " + tagName + " not found."); + assertThrow(_parameters.count(tagName), WhiskersError, "Value for tag " + tagName + " not provided."); return _parameters.at(tagName); } else |