Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purge using namespace std from libsolutil #14499

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions libsolutil/CommonData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#include <boost/algorithm/string.hpp>

using namespace std;
using namespace solidity;
using namespace solidity::util;

Expand All @@ -41,7 +40,7 @@ static char const* lowerHexChars = "0123456789abcdef";

}

string solidity::util::toHex(uint8_t _data, HexCase _case)
std::string solidity::util::toHex(uint8_t _data, HexCase _case)
{
assertThrow(_case != HexCase::Mixed, BadHexCase, "Mixed case can only be used for byte arrays.");

Expand All @@ -53,7 +52,7 @@ string solidity::util::toHex(uint8_t _data, HexCase _case)
};
}

string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _case)
std::string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _case)
{
std::string ret(_data.size() * 2 + (_prefix == HexPrefix::Add ? 2 : 0), 0);

Expand Down Expand Up @@ -90,7 +89,7 @@ int solidity::util::fromHex(char _i, WhenError _throw)
if (_i >= 'A' && _i <= 'F')
return _i - 'A' + 10;
if (_throw == WhenError::Throw)
assertThrow(false, BadHexCharacter, to_string(_i));
assertThrow(false, BadHexCharacter, std::to_string(_i));
else
return -1;
}
Expand Down Expand Up @@ -125,31 +124,31 @@ bytes solidity::util::fromHex(std::string const& _s, WhenError _throw)
}


bool solidity::util::passesAddressChecksum(string const& _str, bool _strict)
bool solidity::util::passesAddressChecksum(std::string const& _str, bool _strict)
{
string s = _str.substr(0, 2) == "0x" ? _str : "0x" + _str;
std::string s = _str.substr(0, 2) == "0x" ? _str : "0x" + _str;

if (s.length() != 42)
return false;

if (!_strict && (
s.find_first_of("abcdef") == string::npos ||
s.find_first_of("ABCDEF") == string::npos
s.find_first_of("abcdef") == std::string::npos ||
s.find_first_of("ABCDEF") == std::string::npos
))
return true;

return s == solidity::util::getChecksummedAddress(s);
}

string solidity::util::getChecksummedAddress(string const& _addr)
std::string solidity::util::getChecksummedAddress(std::string const& _addr)
{
string s = _addr.substr(0, 2) == "0x" ? _addr.substr(2) : _addr;
std::string s = _addr.substr(0, 2) == "0x" ? _addr.substr(2) : _addr;
assertThrow(s.length() == 40, InvalidAddress, "");
assertThrow(s.find_first_not_of("0123456789abcdefABCDEF") == string::npos, InvalidAddress, "");
assertThrow(s.find_first_not_of("0123456789abcdefABCDEF") == std::string::npos, InvalidAddress, "");

h256 hash = keccak256(boost::algorithm::to_lower_copy(s, std::locale::classic()));

string ret = "0x";
std::string ret = "0x";
for (unsigned i = 0; i < 40; ++i)
{
char addressCharacter = s[i];
Expand All @@ -162,16 +161,16 @@ string solidity::util::getChecksummedAddress(string const& _addr)
return ret;
}

bool solidity::util::isValidHex(string const& _string)
bool solidity::util::isValidHex(std::string const& _string)
{
if (_string.substr(0, 2) != "0x")
return false;
if (_string.find_first_not_of("0123456789abcdefABCDEF", 2) != string::npos)
if (_string.find_first_not_of("0123456789abcdefABCDEF", 2) != std::string::npos)
return false;
return true;
}

bool solidity::util::isValidDecimal(string const& _string)
bool solidity::util::isValidDecimal(std::string const& _string)
{
if (_string.empty())
return false;
Expand All @@ -180,12 +179,12 @@ bool solidity::util::isValidDecimal(string const& _string)
// No leading zeros
if (_string.front() == '0')
return false;
if (_string.find_first_not_of("0123456789") != string::npos)
if (_string.find_first_not_of("0123456789") != std::string::npos)
return false;
return true;
}

string solidity::util::formatAsStringOrNumber(string const& _value)
std::string solidity::util::formatAsStringOrNumber(std::string const& _value)
{
assertThrow(_value.length() <= 32, StringTooLong, "String to be formatted longer than 32 bytes.");

Expand All @@ -197,9 +196,9 @@ string solidity::util::formatAsStringOrNumber(string const& _value)
}


string solidity::util::escapeAndQuoteString(string const& _input)
std::string solidity::util::escapeAndQuoteString(std::string const& _input)
{
string out;
std::string out;

for (char c: _input)
if (c == '\\')
Expand All @@ -214,8 +213,8 @@ string solidity::util::escapeAndQuoteString(string const& _input)
out += "\\t";
else if (!isPrint(c))
{
ostringstream o;
o << "\\x" << std::hex << setfill('0') << setw(2) << (unsigned)(unsigned char)(c);
std::ostringstream o;
o << "\\x" << std::hex << std::setfill('0') << std::setw(2) << (unsigned)(unsigned char)(c);
out += o.str();
}
else
Expand Down
25 changes: 12 additions & 13 deletions libsolutil/CommonIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <termios.h>
#endif

using namespace std;
using namespace solidity::util;

namespace
Expand All @@ -55,35 +54,35 @@ inline T readFile(boost::filesystem::path const& _file)

// get length of file:
is.seekg(0, is.end);
streamoff length = is.tellg();
std::streamoff length = is.tellg();
if (length == 0)
return ret; // do not read empty file (MSVC does not like it)
is.seekg(0, is.beg);

ret.resize((static_cast<size_t>(length) + c_elementSize - 1) / c_elementSize);
is.read(const_cast<char*>(reinterpret_cast<char const*>(ret.data())), static_cast<streamsize>(length));
is.read(const_cast<char*>(reinterpret_cast<char const*>(ret.data())), static_cast<std::streamsize>(length));
return ret;
}

}

string solidity::util::readFileAsString(boost::filesystem::path const& _file)
std::string solidity::util::readFileAsString(boost::filesystem::path const& _file)
{
return readFile<string>(_file);
return readFile<std::string>(_file);
}

string solidity::util::readUntilEnd(istream& _stdin)
std::string solidity::util::readUntilEnd(std::istream& _stdin)
{
ostringstream ss;
std::ostringstream ss;
ss << _stdin.rdbuf();
return ss.str();
}

string solidity::util::readBytes(istream& _input, size_t _length)
std::string solidity::util::readBytes(std::istream& _input, size_t _length)
{
string output;
std::string output;
output.resize(_length);
_input.read(output.data(), static_cast<streamsize>(_length));
_input.read(output.data(), static_cast<std::streamsize>(_length));
// If read() reads fewer bytes it sets failbit in addition to eofbit.
if (_input.fail())
output.resize(static_cast<size_t>(_input.gcount()));
Expand Down Expand Up @@ -135,10 +134,10 @@ class DisableConsoleBuffering
int solidity::util::readStandardInputChar()
{
DisableConsoleBuffering disableConsoleBuffering;
return cin.get();
return std::cin.get();
}

string solidity::util::absolutePath(string const& _path, string const& _reference)
std::string solidity::util::absolutePath(std::string const& _path, std::string const& _reference)
{
boost::filesystem::path p(_path);
// Anything that does not start with `.` is an absolute path.
Expand All @@ -158,6 +157,6 @@ string solidity::util::absolutePath(string const& _path, string const& _referenc
return result.generic_string();
}

string solidity::util::sanitizePath(string const& _path) {
std::string solidity::util::sanitizePath(std::string const& _path) {
return boost::filesystem::path(_path).generic_string();
}
11 changes: 5 additions & 6 deletions libsolutil/Exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@

#include <libsolutil/Exceptions.h>

using namespace std;
using namespace solidity::util;

char const* Exception::what() const noexcept
{
// Return the comment if available.
if (string const* cmt = comment())
if (std::string const* cmt = comment())
return cmt->data();

// Fallback to base what().
Expand All @@ -33,20 +32,20 @@ char const* Exception::what() const noexcept
return std::exception::what();
}

string Exception::lineInfo() const
std::string Exception::lineInfo() const
{
char const* const* file = boost::get_error_info<boost::throw_file>(*this);
int const* line = boost::get_error_info<boost::throw_line>(*this);
string ret;
std::string ret;
if (file)
ret += *file;
ret += ':';
if (line)
ret += to_string(*line);
ret += std::to_string(*line);
return ret;
}

string const* Exception::comment() const noexcept
std::string const* Exception::comment() const noexcept
{
return boost::get_error_info<errinfo_comment>(*this);
}
13 changes: 6 additions & 7 deletions libsolutil/IndentedWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@
#include <libsolutil/IndentedWriter.h>
#include <libsolutil/Assertions.h>

using namespace std;
using namespace solidity::util;

string IndentedWriter::format() const
std::string IndentedWriter::format() const
{
string result;
std::string result;
for (auto const& line: m_lines)
result += string(line.indentation * 4, ' ') + line.contents + "\n";
result += std::string(line.indentation * 4, ' ') + line.contents + "\n";
return result;
}

void IndentedWriter::newLine()
{
if (!m_lines.back().contents.empty())
m_lines.emplace_back(Line{string(), m_lines.back().indentation});
m_lines.emplace_back(Line{std::string(), m_lines.back().indentation});
}

void IndentedWriter::indent()
Expand All @@ -53,12 +52,12 @@ void IndentedWriter::unindent()
m_lines.back().indentation--;
}

void IndentedWriter::add(string const& _str)
void IndentedWriter::add(std::string const& _str)
{
m_lines.back().contents += _str;
}

void IndentedWriter::addLine(string const& _line)
void IndentedWriter::addLine(std::string const& _line)
{
newLine();
add(_line);
Expand Down
15 changes: 7 additions & 8 deletions libsolutil/IpfsHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <libsolutil/CommonData.h>
#include <libsolutil/Numeric.h>

using namespace std;
using namespace solidity;
using namespace solidity::util;

Expand Down Expand Up @@ -56,11 +55,11 @@ bytes encodeLinkData(bytes const& _data)
return bytes{0x12} + varintEncoding(_data.size()) + _data;
}

string base58Encode(bytes const& _data)
std::string base58Encode(bytes const& _data)
{
static string const alphabet{"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"};
static std::string const alphabet{"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"};
bigint data(util::toHex(_data, HexPrefix::Add));
string output;
std::string output;
while (data)
{
output += alphabet[static_cast<size_t>(data % alphabet.size())];
Expand All @@ -84,7 +83,7 @@ struct Chunk
size_t blockSize = 0;
};

using Chunks = vector<Chunk>;
using Chunks = std::vector<Chunk>;

Chunk combineLinks(Chunks& _links)
{
Expand Down Expand Up @@ -153,7 +152,7 @@ bytes groupChunksBottomUp(Chunks _currentLevel)
}
}

bytes solidity::util::ipfsHash(string _data)
bytes solidity::util::ipfsHash(std::string _data)
{
size_t const maxChunkSize = 1024 * 256;
size_t chunkCount = _data.length() / maxChunkSize + (_data.length() % maxChunkSize > 0 ? 1 : 0);
Expand All @@ -164,7 +163,7 @@ bytes solidity::util::ipfsHash(string _data)
for (size_t chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++)
{
bytes chunkBytes = asBytes(
_data.substr(chunkIndex * maxChunkSize, min(maxChunkSize, _data.length() - chunkIndex * maxChunkSize))
_data.substr(chunkIndex * maxChunkSize, std::min(maxChunkSize, _data.length() - chunkIndex * maxChunkSize))
);

bytes lengthAsVarint = varintEncoding(chunkBytes.size());
Expand Down Expand Up @@ -197,7 +196,7 @@ bytes solidity::util::ipfsHash(string _data)
return groupChunksBottomUp(std::move(allChunks));
}

string solidity::util::ipfsHashBase58(string _data)
std::string solidity::util::ipfsHashBase58(std::string _data)
{
return base58Encode(ipfsHash(std::move(_data)));
}
Loading