Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a491c7

Browse files
Balajiganapathiaxic
authored andcommitted
Restructure code for alternative identifier suggestions
1 parent d123e77 commit 8a491c7

File tree

6 files changed

+86
-50
lines changed

6 files changed

+86
-50
lines changed

libdevcore/StringUtils.cpp

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,62 @@
2929
using namespace std;
3030
using namespace dev;
3131

32-
namespace dev
32+
bool dev::stringWithinDistance(string const& _str1, string const& _str2, size_t _maxDistance)
3333
{
34-
35-
bool stringWithinDistance(string const& _name1, string const& _name2, size_t _maxDistance)
36-
{
37-
if (_name1 == _name2)
34+
if (_str1 == _str2)
3835
return true;
3936

40-
size_t n1 = _name1.size(), n2 = _name2.size();
41-
vector<vector<size_t>> dp(n1 + 1, vector<size_t>(n2 + 1));
37+
size_t n1 = _str1.size(), n2 = _str2.size();
38+
size_t distance = stringDistance(_str1, _str2);
39+
40+
// if distance is not greater than _maxDistance, and distance is strictly less than length of both names, they can be considered similar
41+
// this is to avoid irrelevant suggestions
42+
return distance <= _maxDistance && distance < n1 && distance < n2;
43+
}
44+
45+
size_t dev::stringDistance(string const& _str1, string const& _str2)
46+
{
47+
size_t n1 = _str1.size(), n2 = _str2.size();
48+
// Optimize by storing only last 2 rows and current row. So first index is considered modulo 3
49+
vector<vector<size_t>> dp(3, vector<size_t>(n2 + 1));
4250

4351
// In this dp formulation of Damerau–Levenshtein distance we are assuming that the strings are 1-based to make base case storage easier.
4452
// So index accesser to _name1 and _name2 have to be adjusted accordingly
4553
for (size_t i1 = 0; i1 <= n1; ++i1)
4654
{
4755
for (size_t i2 = 0; i2 <= n2; ++i2)
4856
{
49-
if (min(i1, i2) == 0)
50-
// Base case
51-
dp[i1][i2] = max(i1, i2);
52-
else
53-
{
54-
dp[i1][i2] = min(dp[i1 - 1][i2] + 1, dp[i1][i2 - 1] + 1);
55-
// Deletion and insertion
56-
if (_name1[i1 - 1] == _name2[i2 - 1])
57-
// Same chars, can skip
58-
dp[i1][i2] = min(dp[i1][i2], dp[i1 - 1][i2 - 1]);
57+
if (min(i1, i2) == 0) // base case
58+
dp[i1 % 3][i2] = max(i1, i2);
5959
else
60-
// Different chars so try substitution
61-
dp[i1][i2] = min(dp[i1][i2], dp[i1 - 1][i2 - 1] + 1);
60+
{
61+
dp[i1 % 3][i2] = min(dp[(i1-1) % 3][i2] + 1, dp[i1 % 3][i2-1] + 1); // deletion and insertion
62+
if (_str1[i1-1] == _str2[i2-1]) // same chars, can skip
63+
dp[i1 % 3][i2] = min(dp[i1 % 3][i2], dp[(i1-1) % 3][i2-1]);
64+
else // different chars so try substitution
65+
dp[i1 % 3][i2] = min(dp[i1 % 3][i2], dp[(i1-1) % 3][i2-1] + 1);
6266

63-
if (i1 > 1 && i2 > 1 && _name1[i1 - 1] == _name2[i2 - 2] && _name1[i1 - 2] == _name2[i2 - 1])
64-
// Try transposing
65-
dp[i1][i2] = min(dp[i1][i2], dp[i1 - 2][i2 - 2] + 1);
66-
}
67+
if (i1 > 1 && i2 > 1 && _str1[i1-1] == _str2[i2-2] && _str1[i1-2] == _str2[i2-1]) // Try transposing
68+
dp[i1 % 3][i2] = min(dp[i1 % 3][i2], dp[(i1-2) % 3][i2-2] + 1);
69+
}
6770
}
6871
}
6972

70-
size_t distance = dp[n1][n2];
71-
72-
// if distance is not greater than _maxDistance, and distance is strictly less than length of both names,
73-
// they can be considered similar this is to avoid irrelevant suggestions
74-
return distance <= _maxDistance && distance < n1 && distance < n2;
73+
return dp[n1 % 3][n2];
7574
}
7675

76+
string dev::quotedAlternativesList(vector<string> const& suggestions) {
77+
if (suggestions.empty())
78+
return "";
79+
if (suggestions.size() == 1)
80+
return "\"" + suggestions.front() + "\"";
81+
82+
string choices = "\"" + suggestions.front() + "\"";
83+
for (size_t i = 1; i + 1 < suggestions.size(); ++i)
84+
choices += ", \"" + suggestions[i] + "\"";
85+
86+
choices += " or \"" + suggestions.back() + "\"";
87+
88+
return choices;
7789
}
90+

libdevcore/StringUtils.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
#pragma once
2525

2626
#include <string>
27+
#include <vector>
2728

2829
namespace dev
2930
{
3031

31-
/// Calculates the Damerau–Levenshtein distance between @a _name1 and @a _name2 and
32-
/// @returns true if that distance is not greater than @a _maxDistance
33-
bool stringWithinDistance(std::string const& _name1, std::string const& _name2, size_t _maxDistance);
32+
// Calculates the Damerau–Levenshtein distance between _str1 and _str2 and returns true if that distance is not greater than _maxDistance
33+
bool stringWithinDistance(std::string const& _str1, std::string const& _str2, size_t _maxDistance);
34+
// Calculates the Damerau–Levenshtein distance between _str1 and _str2
35+
size_t stringDistance(std::string const& _str1, std::string const& _str2);
36+
// Return a string having elements of suggestions as quoted, alternative suggestions. e.g. "a", "b" or "c"
37+
std::string quotedAlternativesList(std::vector<std::string> const& suggestions);
3438

3539
}

libsolidity/analysis/DeclarationContainer.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,19 @@ vector<Declaration const*> DeclarationContainer::resolveName(ASTString const& _n
119119

120120
vector<ASTString> DeclarationContainer::similarNames(ASTString const& _name) const
121121
{
122+
static size_t const MAXIMUM_EDIT_DISTANCE = 2;
123+
122124
vector<ASTString> similar;
123125

124126
for (auto const& declaration: m_declarations)
125127
{
126128
string const& declarationName = declaration.first;
127-
if (stringWithinDistance(_name, declarationName, MAXIMUM_DISTANCE))
129+
if (stringWithinDistance(_name, declarationName, MAXIMUM_EDIT_DISTANCE))
128130
similar.push_back(declarationName);
129131
}
130132

131133
if (m_enclosingContainer)
132-
{
133-
vector<ASTString> enclosingSimilar = m_enclosingContainer->similarNames(_name);
134-
similar.insert(similar.end(), enclosingSimilar.begin(), enclosingSimilar.end());
135-
}
134+
similar += m_enclosingContainer->similarNames(_name);
136135

137136
return similar;
138137
}

libsolidity/analysis/DeclarationContainer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class DeclarationContainer
6868
std::map<ASTString, std::vector<Declaration const*>> m_declarations;
6969
std::map<ASTString, std::vector<Declaration const*>> m_invisibleDeclarations;
7070

71-
static size_t const MAXIMUM_DISTANCE = 2;
7271
};
7372

7473
}

libsolidity/analysis/NameAndTypeResolver.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <libsolidity/ast/AST.h>
2626
#include <libsolidity/analysis/TypeChecker.h>
2727
#include <libsolidity/interface/ErrorReporter.h>
28+
#include <libdevcore/StringUtils.h>
2829

2930
#include <boost/algorithm/string.hpp>
3031

@@ -427,19 +428,7 @@ vector<_T const*> NameAndTypeResolver::cThreeMerge(list<list<_T const*>>& _toMer
427428

428429
string NameAndTypeResolver::similarNameSuggestions(ASTString const& _name) const
429430
{
430-
vector<ASTString> suggestions = m_currentScope->similarNames(_name);
431-
if (suggestions.empty())
432-
return "";
433-
if (suggestions.size() == 1)
434-
return "\"" + suggestions.front() + "\"";
435-
436-
string choices = "\"" + suggestions.front() + "\"";
437-
for (size_t i = 1; i + 1 < suggestions.size(); ++i)
438-
choices += ", \"" + suggestions[i] + "\"";
439-
440-
choices += " or \"" + suggestions.back() + "\"";
441-
442-
return choices;
431+
return quotedAlternativesList(m_currentScope->similarNames(_name));
443432
}
444433

445434
DeclarationRegistrationHelper::DeclarationRegistrationHelper(

test/libdevcore/StringUtils.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,38 @@ BOOST_AUTO_TEST_CASE(test_similarity)
5050
BOOST_CHECK_EQUAL(stringWithinDistance("", "", 2), true);
5151
}
5252

53+
BOOST_AUTO_TEST_CASE(test_dldistance)
54+
{
55+
BOOST_CHECK_EQUAL(stringDistance("hello", "hellw"), 1);
56+
BOOST_CHECK_EQUAL(stringDistance("hello", "helol"), 1);
57+
BOOST_CHECK_EQUAL(stringDistance("hello", "helo"), 1);
58+
BOOST_CHECK_EQUAL(stringDistance("hello", "helllo"), 1);
59+
BOOST_CHECK_EQUAL(stringDistance("hello", "hlllo"), 1);
60+
BOOST_CHECK_EQUAL(stringDistance("hello", "hllllo"), 2);
61+
BOOST_CHECK_EQUAL(stringDistance("a", ""), 1);
62+
BOOST_CHECK_EQUAL(stringDistance("abc", "ba"), 2);
63+
BOOST_CHECK_EQUAL(stringDistance("abc", "abcdef"), 3);
64+
BOOST_CHECK_EQUAL(stringDistance("abcd", "wxyz"), 4);
65+
BOOST_CHECK_EQUAL(stringDistance("", ""), 0);
66+
BOOST_CHECK_EQUAL(stringDistance("abcdefghijklmnopqrstuvwxyz", "abcabcabcabcabcabcabcabca"), 23);
67+
68+
}
69+
70+
BOOST_AUTO_TEST_CASE(test_alternatives_list)
71+
{
72+
vector<string> strings;
73+
BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "");
74+
strings.push_back("a");
75+
BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\"");
76+
strings.push_back("b");
77+
BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\" or \"b\"");
78+
strings.push_back("c");
79+
BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\", \"b\" or \"c\"");
80+
strings.push_back("d");
81+
BOOST_CHECK_EQUAL(quotedAlternativesList(strings), "\"a\", \"b\", \"c\" or \"d\"");
82+
}
83+
84+
5385
BOOST_AUTO_TEST_SUITE_END()
5486

5587
}

0 commit comments

Comments
 (0)