Skip to content

Commit

Permalink
Change string formatting check to assertion.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Nov 28, 2019
1 parent 301215f commit 4608619
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
13 changes: 6 additions & 7 deletions libdevcore/CommonData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,13 @@ bool dev::isValidDecimal(string const& _string)
return true;
}

// Returns a quoted string if all characters are printable ASCII chars,
// or its hex representation otherwise.
std::string dev::formatAsStringOrNumber(std::string const& _value)
string dev::formatAsStringOrNumber(string const& _value)
{
if (_value.length() <= 32)
for (auto const& c: _value)
if (c <= 0x1f || c >= 0x7f || c == '"')
return "0x" + h256(_value, h256::AlignLeft).hex();
assertThrow(_value.length() <= 32, StringTooLong, "String to be formatted longer than 32 bytes.");

for (auto const& c: _value)
if (c <= 0x1f || c >= 0x7f || c == '"')
return "0x" + h256(_value, h256::AlignLeft).hex();

return "\"" + _value + "\"";
}
1 change: 1 addition & 0 deletions libdevcore/CommonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ bool isValidDecimal(std::string const& _string);

/// @returns a quoted string if all characters are printable ASCII chars,
/// or its hex representation otherwise.
/// _value cannot be longer than 32 bytes.
std::string formatAsStringOrNumber(std::string const& _value);

template<typename Container, typename Compare>
Expand Down
1 change: 1 addition & 0 deletions libdevcore/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ DEV_SIMPLE_EXCEPTION(BadHexCharacter);
DEV_SIMPLE_EXCEPTION(BadHexCase);
DEV_SIMPLE_EXCEPTION(FileError);
DEV_SIMPLE_EXCEPTION(DataTooLong);
DEV_SIMPLE_EXCEPTION(StringTooLong);

// error information to be added to exceptions
using errinfo_comment = boost::error_info<struct tag_comment, std::string>;
Expand Down

0 comments on commit 4608619

Please sign in to comment.