mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Move string distance function to utils and format error message
This commit is contained in:
committed by
Alex Beregszaszi
parent
2859834e58
commit
b1417b318f
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user