From 8b0c866f025f3b48db36b0e48451062806a1ab06 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 26 Jun 2017 12:49:05 +0100 Subject: Add hasHexPrefix() to AST::Literal --- libsolidity/analysis/TypeChecker.cpp | 2 +- libsolidity/ast/AST.cpp | 18 ++++++++++++------ libsolidity/ast/AST.h | 3 +++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index b276a2d4..436d960b 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -1886,7 +1886,7 @@ void TypeChecker::expectType(Expression const& _expression, Type const& _expecte { auto literal = dynamic_cast(&_expression); - if (literal && !boost::starts_with(literal->value(), "0x")) + if (literal && !literal->hasHexPrefix()) m_errorReporter.warning( _expression.location(), "Decimal literal assigned to bytesXX variable will be left-aligned. " diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index b929b6fe..54ded609 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -530,20 +530,26 @@ IdentifierAnnotation& Identifier::annotation() const return dynamic_cast(*m_annotation); } +bool Literal::hasHexPrefix() const +{ + if (token() != Token::Number) + return false; + return value().substr(0, 2) == "0x"; +} + bool Literal::looksLikeAddress() const { if (subDenomination() != SubDenomination::None) return false; - if (token() != Token::Number) + + if (!hasHexPrefix()) return false; - string lit = value(); - return lit.substr(0, 2) == "0x" && abs(int(lit.length()) - 42) <= 1; + return abs(int(value().length()) - 42) <= 1; } bool Literal::passesAddressChecksum() const { - string lit = value(); - solAssert(lit.substr(0, 2) == "0x", "Expected hex prefix"); - return dev::passesAddressChecksum(lit, true); + solAssert(hasHexPrefix(), "Expected hex prefix"); + return dev::passesAddressChecksum(value(), true); } diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 83572692..24b16176 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -1590,6 +1590,9 @@ public: SubDenomination subDenomination() const { return m_subDenomination; } + /// @returns true if this is a number with a hex prefix. + bool hasHexPrefix() const; + /// @returns true if this looks like a checksummed address. bool looksLikeAddress() const; /// @returns true if it passes the address checksum test. -- cgit v1.2.3 From 06fe61f89bc3f576176a665b851d9b95f9175618 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 28 Jun 2017 16:59:34 +0100 Subject: Rename to isHexNumber() --- libsolidity/analysis/TypeChecker.cpp | 2 +- libsolidity/ast/AST.cpp | 6 +++--- libsolidity/ast/AST.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 436d960b..1563467c 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -1886,7 +1886,7 @@ void TypeChecker::expectType(Expression const& _expression, Type const& _expecte { auto literal = dynamic_cast(&_expression); - if (literal && !literal->hasHexPrefix()) + if (literal && !literal->isHexNumber()) m_errorReporter.warning( _expression.location(), "Decimal literal assigned to bytesXX variable will be left-aligned. " diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index 54ded609..2df31eed 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -530,7 +530,7 @@ IdentifierAnnotation& Identifier::annotation() const return dynamic_cast(*m_annotation); } -bool Literal::hasHexPrefix() const +bool Literal::isHexNumber() const { if (token() != Token::Number) return false; @@ -542,7 +542,7 @@ bool Literal::looksLikeAddress() const if (subDenomination() != SubDenomination::None) return false; - if (!hasHexPrefix()) + if (!isHexNumber()) return false; return abs(int(value().length()) - 42) <= 1; @@ -550,6 +550,6 @@ bool Literal::looksLikeAddress() const bool Literal::passesAddressChecksum() const { - solAssert(hasHexPrefix(), "Expected hex prefix"); + solAssert(isHexNumber(), "Expected hex number"); return dev::passesAddressChecksum(value(), true); } diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 24b16176..e8831dc0 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -1591,7 +1591,7 @@ public: SubDenomination subDenomination() const { return m_subDenomination; } /// @returns true if this is a number with a hex prefix. - bool hasHexPrefix() const; + bool isHexNumber() const; /// @returns true if this looks like a checksummed address. bool looksLikeAddress() const; -- cgit v1.2.3 From c70d73809a6b36305f65e4ecb68acc3f63c7b304 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 28 Jun 2017 17:00:14 +0100 Subject: Use boost::starts_with --- libsolidity/ast/AST.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index 2df31eed..403f4b79 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -534,7 +534,7 @@ bool Literal::isHexNumber() const { if (token() != Token::Number) return false; - return value().substr(0, 2) == "0x"; + return boost::starts_with(value(), "0x"); } bool Literal::looksLikeAddress() const -- cgit v1.2.3