diff options
author | Balajiganapathi S <balajiganapathi.s@gmail.com> | 2017-11-18 00:11:15 +0800 |
---|---|---|
committer | Alex Beregszaszi <alex@rtfs.hu> | 2018-02-13 22:59:33 +0800 |
commit | b1417b318f3b3cbfd0bf79a4cabc656825a84e5a (patch) | |
tree | 3586f671841b6605b8b6cb7e41e6a96c931907da | |
parent | 2859834e58e37e7b15a15f7df60feef3e1527c97 (diff) | |
download | dexon-solidity-b1417b318f3b3cbfd0bf79a4cabc656825a84e5a.tar.gz dexon-solidity-b1417b318f3b3cbfd0bf79a4cabc656825a84e5a.tar.zst dexon-solidity-b1417b318f3b3cbfd0bf79a4cabc656825a84e5a.zip |
Move string distance function to utils and format error message
-rw-r--r-- | libdevcore/StringUtils.cpp | 77 | ||||
-rw-r--r-- | libdevcore/StringUtils.h | 35 | ||||
-rw-r--r-- | libsolidity/analysis/DeclarationContainer.cpp | 44 | ||||
-rw-r--r-- | libsolidity/analysis/DeclarationContainer.h | 2 | ||||
-rw-r--r-- | libsolidity/analysis/NameAndTypeResolver.cpp | 8 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 8 |
6 files changed, 122 insertions, 52 deletions
diff --git a/libdevcore/StringUtils.cpp b/libdevcore/StringUtils.cpp new file mode 100644 index 00000000..7b2b4f5e --- /dev/null +++ b/libdevcore/StringUtils.cpp @@ -0,0 +1,77 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see <http://www.gnu.org/licenses/>. +*/ +/** @file StringUtils.h + * @author Balajiganapathi S <balajiganapathi.s@gmail.com> + * @date 2017 + * + * String routines + */ + +#include "StringUtils.h" +#include <algorithm> +#include <string> +#include <vector> + +using namespace std; +using namespace dev; + +namespace dev +{ + +bool stringWithinDistance(string const& _name1, string const& _name2, size_t _maxDistance) +{ + if (_name1 == _name2) + return true; + + size_t n1 = _name1.size(), n2 = _name2.size(); + vector<vector<size_t>> dp(n1 + 1, vector<size_t>(n2 + 1)); + + // In this dp formulation of Damerau–Levenshtein distance we are assuming that the strings are 1-based to make base case storage easier. + // So index accesser to _name1 and _name2 have to be adjusted accordingly + for (size_t i1 = 0; i1 <= n1; ++i1) + { + for (size_t i2 = 0; i2 <= n2; ++i2) + { + if (min(i1, i2) == 0) + // Base case + dp[i1][i2] = max(i1, i2); + else + { + dp[i1][i2] = min(dp[i1 - 1][i2] + 1, dp[i1][i2 - 1] + 1); + // Deletion and insertion + if (_name1[i1 - 1] == _name2[i2 - 1]) + // Same chars, can skip + dp[i1][i2] = min(dp[i1][i2], dp[i1 - 1][i2 - 1]); + else + // Different chars so try substitution + dp[i1][i2] = min(dp[i1][i2], dp[i1 - 1][i2 - 1] + 1); + + if (i1 > 1 && i2 > 1 && _name1[i1 - 1] == _name2[i2 - 2] && _name1[i1 - 2] == _name2[i2 - 1]) + // Try transposing + dp[i1][i2] = min(dp[i1][i2], dp[i1 - 2][i2 - 2] + 1); + } + } + } + + size_t distance = dp[n1][n2]; + + // if distance is not greater than _maxDistance, and distance is strictly less than length of both names, + // they can be considered similar this is to avoid irrelevant suggestions + return distance <= _maxDistance && distance < n1 && distance < n2; +} + +} diff --git a/libdevcore/StringUtils.h b/libdevcore/StringUtils.h new file mode 100644 index 00000000..d3f9f8d9 --- /dev/null +++ b/libdevcore/StringUtils.h @@ -0,0 +1,35 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see <http://www.gnu.org/licenses/>. +*/ +/** @file StringUtils.h + * @author Balajiganapathi S <balajiganapathi.s@gmail.com> + * @date 2017 + * + * String routines + */ + +#pragma once + +#include <string> + +namespace dev +{ + +/// Calculates the Damerau–Levenshtein distance between @a _name1 and @a _name2 and +/// @returns true if that distance is not greater than @a _maxDistance +bool stringWithinDistance(std::string const& _name1, std::string const& _name2, size_t _maxDistance); + +} diff --git a/libsolidity/analysis/DeclarationContainer.cpp b/libsolidity/analysis/DeclarationContainer.cpp index d41bc7d3..f9a52dc6 100644 --- a/libsolidity/analysis/DeclarationContainer.cpp +++ b/libsolidity/analysis/DeclarationContainer.cpp @@ -23,6 +23,7 @@ #include <libsolidity/analysis/DeclarationContainer.h> #include <libsolidity/ast/AST.h> #include <libsolidity/ast/Types.h> +#include <libdevcore/StringUtils.h> using namespace std; using namespace dev; @@ -123,7 +124,7 @@ vector<ASTString> DeclarationContainer::similarNames(ASTString const& _name) con for (auto const& declaration: m_declarations) { string const& declarationName = declaration.first; - if (DeclarationContainer::areSimilarNames(_name, declarationName)) + if (stringWithinDistance(_name, declarationName, MAXIMUM_DISTANCE)) similar.push_back(declarationName); } @@ -135,44 +136,3 @@ vector<ASTString> DeclarationContainer::similarNames(ASTString const& _name) con return similar; } - -bool DeclarationContainer::areSimilarNames(ASTString const& _name1, ASTString const& _name2) -{ - if (_name1 == _name2) - return true; - - size_t n1 = _name1.size(), n2 = _name2.size(); - vector<vector<size_t>> dp(n1 + 1, vector<size_t>(n2 + 1)); - - // In this dp formulation of Damerau–Levenshtein distance we are assuming that the strings are 1-based to make base case storage easier. - // So index accesser to _name1 and _name2 have to be adjusted accordingly - for (size_t i1 = 0; i1 <= n1; ++i1) - { - for (size_t i2 = 0; i2 <= n2; ++i2) - { - if (min(i1, i2) == 0) // base case - dp[i1][i2] = max(i1, i2); - else - { - dp[i1][i2] = min(dp[i1-1][i2] + 1, dp[i1][i2-1] + 1); - // Deletion and insertion - if (_name1[i1-1] == _name2[i2-1]) - // Same chars, can skip - dp[i1][i2] = min(dp[i1][i2], dp[i1-1][i2-1]); - else - // Different chars so try substitution - dp[i1][i2] = min(dp[i1][i2], dp[i1-1][i2-1] + 1); - - if (i1 > 1 && i2 > 1 && _name1[i1-1] == _name2[i2-2] && _name1[i1-2] == _name2[i2-1]) - // Try transposing - dp[i1][i2] = min(dp[i1][i2], dp[i1-2][i2-2] + 1); - } - } - } - - size_t distance = dp[n1][n2]; - - // If distance is not greater than MAXIMUM_DISTANCE, and distance is strictly less than length of both names, - // they can be considered similar this is to avoid irrelevant suggestions - return distance <= MAXIMUM_DISTANCE && distance < n1 && distance < n2; -} diff --git a/libsolidity/analysis/DeclarationContainer.h b/libsolidity/analysis/DeclarationContainer.h index 991140d8..6c1459c7 100644 --- a/libsolidity/analysis/DeclarationContainer.h +++ b/libsolidity/analysis/DeclarationContainer.h @@ -68,8 +68,6 @@ private: std::map<ASTString, std::vector<Declaration const*>> m_declarations; std::map<ASTString, std::vector<Declaration const*>> m_invisibleDeclarations; - // Calculates the Damerau–Levenshtein distance and decides whether the two names are similar - static bool areSimilarNames(ASTString const& _name1, ASTString const& _name2); static size_t const MAXIMUM_DISTANCE = 2; }; diff --git a/libsolidity/analysis/NameAndTypeResolver.cpp b/libsolidity/analysis/NameAndTypeResolver.cpp index a9a62529..21f2af5f 100644 --- a/libsolidity/analysis/NameAndTypeResolver.cpp +++ b/libsolidity/analysis/NameAndTypeResolver.cpp @@ -431,13 +431,13 @@ string NameAndTypeResolver::similarNameSuggestions(ASTString const& _name) const if (suggestions.empty()) return ""; if (suggestions.size() == 1) - return suggestions.front(); + return "\"" + suggestions.front() + "\""; - string choices = suggestions.front(); + string choices = "\"" + suggestions.front() + "\""; for (size_t i = 1; i + 1 < suggestions.size(); ++i) - choices += ", " + suggestions[i]; + choices += ", \"" + suggestions[i] + "\""; - choices += " or " + suggestions.back(); + choices += " or \"" + suggestions.back() + "\""; return choices; } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 51d81a28..c4741657 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2089,7 +2089,7 @@ BOOST_AUTO_TEST_CASE(similar_name_suggestions_expected) function g() public { fun(); } } )"; - CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean func?"); + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean \"func\"?"); } BOOST_AUTO_TEST_CASE(no_name_suggestion) @@ -2115,7 +2115,7 @@ BOOST_AUTO_TEST_CASE(multiple_similar_suggestions) } } )"; - CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean var1, var2, var3, var4 or var5?"); + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean \"var1\", \"var2\", \"var3\", \"var4\" or \"var5\"?"); } BOOST_AUTO_TEST_CASE(multiple_scopes_suggestions) @@ -2129,7 +2129,7 @@ BOOST_AUTO_TEST_CASE(multiple_scopes_suggestions) } } )"; - CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean log8, log9, log0, log1, log2, log3 or log4?"); + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean \"log8\", \"log9\", \"log0\", \"log1\", \"log2\", \"log3\" or \"log4\"?"); } BOOST_AUTO_TEST_CASE(inheritence_suggestions) @@ -2142,7 +2142,7 @@ BOOST_AUTO_TEST_CASE(inheritence_suggestions) } } )"; - CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean func?"); + CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean \"func\"?"); } BOOST_AUTO_TEST_CASE(no_spurious_suggestions) |