Display string literal as hex in error messages if it is not printable ASCII

This commit is contained in:
Alex Beregszaszi
2020-09-23 17:33:39 +01:00
parent 0e5abbd4a9
commit a154594de6
12 changed files with 30 additions and 17 deletions
+16 -3
View File
@@ -1382,12 +1382,25 @@ bool StringLiteralType::operator==(Type const& _other) const
std::string StringLiteralType::toString(bool) const
{
auto isPrintableASCII = [](string const& s)
{
for (auto c: s)
{
if (static_cast<unsigned>(c) <= 0x1f || static_cast<unsigned>(c) >= 0x7f)
return false;
}
return true;
};
string ret = isPrintableASCII(m_value) ?
("literal_string \"" + m_value + "\"") :
("literal_string hex\"" + util::toHex(util::asBytes(m_value)) + "\"");
size_t invalidSequence;
if (!util::validateUTF8(m_value, invalidSequence))
return "literal_string (contains invalid UTF-8 sequence at position " + util::toString(invalidSequence) + ")";
ret += " (contains invalid UTF-8 sequence at position " + util::toString(invalidSequence) + ")";
return "literal_string \"" + m_value + "\"";
return ret;
}
TypePointer StringLiteralType::mobileType() const