aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libdevcore/StringUtils.cpp69
-rw-r--r--libdevcore/StringUtils.h10
-rw-r--r--libsolidity/analysis/DeclarationContainer.cpp9
-rw-r--r--libsolidity/analysis/DeclarationContainer.h1
-rw-r--r--libsolidity/analysis/NameAndTypeResolver.cpp15
-rw-r--r--test/libdevcore/StringUtils.cpp32
6 files changed, 86 insertions, 50 deletions
diff --git a/libdevcore/StringUtils.cpp b/libdevcore/StringUtils.cpp
index 7b2b4f5e..1dbe151f 100644
--- a/libdevcore/StringUtils.cpp
+++ b/libdevcore/StringUtils.cpp
@@ -29,16 +29,24 @@
using namespace std;
using namespace dev;
-namespace dev
+bool dev::stringWithinDistance(string const& _str1, string const& _str2, size_t _maxDistance)
{
-
-bool stringWithinDistance(string const& _name1, string const& _name2, size_t _maxDistance)
-{
- if (_name1 == _name2)
+ if (_str1 == _str2)
return true;
- size_t n1 = _name1.size(), n2 = _name2.size();
- vector<vector<size_t>> dp(n1 + 1, vector<size_t>(n2 + 1));
+ size_t n1 = _str1.size(), n2 = _str2.size();
+ size_t distance = stringDistance(_str1, _str2);
+
+ // 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;
+}
+
+size_t dev::stringDistance(string const& _str1, string const& _str2)
+{
+ size_t n1 = _str1.size(), n2 = _str2.size();
+ // Optimize by storing only last 2 rows and current row. So first index is considered modulo 3
+ vector<vector<size_t>> dp(3, 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
@@ -46,32 +54,37 @@ bool stringWithinDistance(string const& _name1, string const& _name2, size_t _ma
{
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]);
+ if (min(i1, i2) == 0) // base case
+ dp[i1 % 3][i2] = max(i1, i2);
else
- // Different chars so try substitution
- dp[i1][i2] = min(dp[i1][i2], dp[i1 - 1][i2 - 1] + 1);
+ {
+ dp[i1 % 3][i2] = min(dp[(i1-1) % 3][i2] + 1, dp[i1 % 3][i2-1] + 1); // deletion and insertion
+ if (_str1[i1-1] == _str2[i2-1]) // same chars, can skip
+ dp[i1 % 3][i2] = min(dp[i1 % 3][i2], dp[(i1-1) % 3][i2-1]);
+ else // different chars so try substitution
+ dp[i1 % 3][i2] = min(dp[i1 % 3][i2], dp[(i1-1) % 3][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);
- }
+ if (i1 > 1 && i2 > 1 && _str1[i1-1] == _str2[i2-2] && _str1[i1-2] == _str2[i2-1]) // Try transposing
+ dp[i1 % 3][i2] = min(dp[i1 % 3][i2], dp[(i1-2) % 3][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;
+ return dp[n1 % 3][n2];
}
+string dev::quotedAlternativesList(vector<string> const& suggestions) {
+ if (suggestions.empty())
+ return "";
+ if (suggestions.size() == 1)
+ return "\"" + suggestions.front() + "\"";
+
+ string choices = "\"" + suggestions.front() + "\"";
+ for (size_t i = 1; i + 1 < suggestions.size(); ++i)
+ choices += ", \"" + suggestions[i] + "\"";
+
+ choices += " or \"" + suggestions.back() + "\"";
+
+ return choices;
}
+
diff --git a/libdevcore/StringUtils.h b/libdevcore/StringUtils.h
index d3f9f8d9..acd93e32 100644
--- a/libdevcore/StringUtils.h
+++ b/libdevcore/StringUtils.h
@@ -24,12 +24,16 @@
#pragma once
#include <string>
+#include <vector>
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);
+// Calculates the Damerau–Levenshtein distance between _str1 and _str2 and returns true if that distance is not greater than _maxDistance
+bool stringWithinDistance(std::string const& _str1, std::string const& _str2, size_t _maxDistance);
+// Calculates the Damerau–Levenshtein distance between _str1 and _str2
+size_t stringDistance(std::string const& _str1, std::string const& _str2);
+// Return a string having elements of suggestions as quoted, alternative suggestions. e.g. "a", "b" or "c"
+std::string quotedAlternativesList(std::vector<std::string> const& suggestions);
}
diff --git a/libsolidity/analysis/DeclarationContainer.cpp b/libsolidity/analysis/DeclarationContainer.cpp
index f9a52dc6..7508ad9e 100644
--- a/libsolidity/analysis/DeclarationContainer.cpp
+++ b/libsolidity/analysis/DeclarationContainer.cpp
@@ -119,20 +119,19 @@ vector<Declaration const*> DeclarationContainer::resolveName(ASTString const& _n
vector<ASTString> DeclarationContainer::similarNames(ASTString const& _name) const
{
+ static size_t const MAXIMUM_EDIT_DISTANCE = 2;
+
vector<ASTString> similar;
for (auto const& declaration: m_declarations)
{
string const& declarationName = declaration.first;
- if (stringWithinDistance(_name, declarationName, MAXIMUM_DISTANCE))
+ if (stringWithinDistance(_name, declarationName, MAXIMUM_EDIT_DISTANCE))
similar.push_back(declarationName);
}
if (m_enclosingContainer)
- {
- vector<ASTString> enclosingSimilar = m_enclosingContainer->similarNames(_name);
- similar.insert(similar.end(), enclosingSimilar.begin(), enclosingSimilar.end());
- }
+ similar += m_enclosingContainer->similarNames(_name);
return similar;
}
diff --git a/libsolidity/analysis/DeclarationContainer.h b/libsolidity/analysis/DeclarationContainer.h
index 6c1459c7..94bbe129 100644
--- a/libsolidity/analysis/DeclarationContainer.h
+++ b/libsolidity/analysis/DeclarationContainer.h
@@ -68,7 +68,6 @@ private:
std::map<ASTString, std::vector<Declaration const*>> m_declarations;
std::map<ASTString, std::vector<Declaration const*>> m_invisibleDeclarations;
- static size_t const MAXIMUM_DISTANCE = 2;
};
}
diff --git a/libsolidity/analysis/NameAndTypeResolver.cpp b/libsolidity/analysis/NameAndTypeResolver.cpp
index 21f2af5f..5e4d414b 100644
--- a/libsolidity/analysis/NameAndTypeResolver.cpp
+++ b/libsolidity/analysis/NameAndTypeResolver.cpp
@@ -25,6 +25,7 @@
#include <libsolidity/ast/AST.h>
#include <libsolidity/analysis/TypeChecker.h>
#include <libsolidity/interface/ErrorReporter.h>
+#include <libdevcore/StringUtils.h>
#include <boost/algorithm/string.hpp>
@@ -427,19 +428,7 @@ vector<_T const*> NameAndTypeResolver::cThreeMerge(list<list<_T const*>>& _toMer
string NameAndTypeResolver::similarNameSuggestions(ASTString const& _name) const
{
- vector<ASTString> suggestions = m_currentScope->similarNames(_name);
- if (suggestions.empty())
- return "";
- if (suggestions.size() == 1)
- return "\"" + suggestions.front() + "\"";
-
- string choices = "\"" + suggestions.front() + "\"";
- for (size_t i = 1; i + 1 < suggestions.size(); ++i)
- choices += ", \"" + suggestions[i] + "\"";
-
- choices += " or \"" + suggestions.back() + "\"";
-
- return choices;
+ return quotedAlternativesList(m_currentScope->similarNames(_name));
}
DeclarationRegistrationHelper::DeclarationRegistrationHelper(
diff --git a/test/libdevcore/StringUtils.cpp b/test/libdevcore/StringUtils.cpp
index 7a5fe26e..597457cc 100644
--- a/test/libdevcore/StringUtils.cpp
+++ b/test/libdevcore/StringUtils.cpp
@@ -50,6 +50,38 @@ BOOST_AUTO_TEST_CASE(test_similarity)
BOOST_CHECK_EQUAL(stringWithinDistance("", "", 2), true);
}
+BOOST_AUTO_TEST_CASE(test_dldistance)
+{
+ BOOST_CHECK_EQUAL(stringDistance("hello", "hellw"), 1);
+ BOOST_CHECK_EQUAL(stringDistance("hello", "helol"), 1);
+ BOOST_CHECK_EQUAL(stringDistance("hello", "helo"), 1);
+ BOOST_CHECK_EQUAL(stringDistance("hello", "helllo"), 1);
+ BOOST_CHECK_EQUAL(stringDistance("hello", "hlllo"), 1);
+ BOOST_CHECK_EQUAL(stringDistance("hello", "hllllo"), 2);
+ BOOST_CHECK_EQUAL(stringDistance("a", ""), 1);
+ BOOST_CHECK_EQUAL(stringDistance("abc", "ba"), 2);
+ BOOST_CHECK_EQUAL(stringDistance("abc", "abcdef"), 3);
+ BOOST_CHECK_EQUAL(stringDistance("abcd", "wxyz"), 4);
+ BOOST_CHECK_EQUAL(stringDistance("", ""), 0);
+ BOOST_CHECK_EQUAL(stringDistance("abcdefghijklmnopqrstuvwxyz", "abcabcabcabcabcabcabcabca"), 23);
+
+}
+
+BOOST_AUTO_TEST_CASE(test_alternatives_list)
+{
+ vector<string> strings;
+ BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "");
+ strings.push_back("a");
+ BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\"");
+ strings.push_back("b");
+ BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\" or \"b\"");
+ strings.push_back("c");
+ BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\", \"b\" or \"c\"");
+ strings.push_back("d");
+ BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\", \"b\", \"c\" or \"d\"");
+}
+
+
BOOST_AUTO_TEST_SUITE_END()
}