Skip to content

Commit

Permalink
Add helpers escapeIdentifier to Types
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Feb 26, 2018
1 parent 272262e commit 7517059
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
20 changes: 20 additions & 0 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ string parenthesizeUserIdentifier(string const& _internal)

}

string Type::escapeIdentifier(string const& _identifier)
{
string ret = _identifier;
boost::algorithm::replace_all(ret, "$", "_$$$_");
boost::algorithm::replace_all(ret, ",", "_$_");
boost::algorithm::replace_all(ret, "(", "$_");
boost::algorithm::replace_all(ret, ")", "_$");
return ret;
}

string Type::unescapeIdentifier(string const& _identifier)
{
string ret = _identifier;
boost::algorithm::replace_all(ret, "_$_", ",");
boost::algorithm::replace_all(ret, "_$$$_", "$");
boost::algorithm::replace_all(ret, "$_", "(");
boost::algorithm::replace_all(ret, "_$", ")");
return ret;
}

TypePointer Type::fromElementaryTypeName(ElementaryTypeNameToken const& _type)
{
solAssert(Token::isElementaryTypeName(_type.token()),
Expand Down
8 changes: 7 additions & 1 deletion libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,16 @@ class Type: private boost::noncopyable, public std::enable_shared_from_this<Type
/// @returns a valid solidity identifier such that two types should compare equal if and
/// only if they have the same identifier.
/// The identifier should start with "t_".
virtual std::string identifier() const = 0;

/// More complex identifier strings use "parentheses", where $_ is interpreted as as
/// "opening parenthesis", _$ as "closing parenthesis", _$_ as "comma" and any $ that
/// appears as part of a user-supplied identifier is escaped as _$$$_.
virtual std::string identifier() const = 0;
/// @returns an escaped identifier (will not contain any parenthesis or commas)
static std::string escapeIdentifier(std::string const& _identifier);
/// @returns an unescaped identifier
static std::string unescapeIdentifier(std::string const& _identifier);

virtual bool isImplicitlyConvertibleTo(Type const& _other) const { return *this == _other; }
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const
{
Expand Down

0 comments on commit 7517059

Please sign in to comment.