Change string formatting check to assertion.

This commit is contained in:
chriseth 2019-11-28 14:33:38 +01:00
parent 301215f186
commit 460861922e
3 changed files with 8 additions and 7 deletions

View File

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

View File

@ -356,6 +356,7 @@ bool isValidDecimal(std::string const& _string);
/// @returns a quoted string if all characters are printable ASCII chars, /// @returns a quoted string if all characters are printable ASCII chars,
/// or its hex representation otherwise. /// or its hex representation otherwise.
/// _value cannot be longer than 32 bytes.
std::string formatAsStringOrNumber(std::string const& _value); std::string formatAsStringOrNumber(std::string const& _value);
template<typename Container, typename Compare> template<typename Container, typename Compare>

View File

@ -49,6 +49,7 @@ DEV_SIMPLE_EXCEPTION(BadHexCharacter);
DEV_SIMPLE_EXCEPTION(BadHexCase); DEV_SIMPLE_EXCEPTION(BadHexCase);
DEV_SIMPLE_EXCEPTION(FileError); DEV_SIMPLE_EXCEPTION(FileError);
DEV_SIMPLE_EXCEPTION(DataTooLong); DEV_SIMPLE_EXCEPTION(DataTooLong);
DEV_SIMPLE_EXCEPTION(StringTooLong);
// error information to be added to exceptions // error information to be added to exceptions
using errinfo_comment = boost::error_info<struct tag_comment, std::string>; using errinfo_comment = boost::error_info<struct tag_comment, std::string>;