aboutsummaryrefslogtreecommitdiffstats
path: root/libdevcore/StringUtils.cpp
diff options
context:
space:
mode:
authorBalajiganapathi S <balajiganapathi.s@gmail.com>2017-11-22 00:50:35 +0800
committerAlex Beregszaszi <alex@rtfs.hu>2018-02-13 23:00:15 +0800
commit8a491c77ba9680afdf8c33664e905b978152b095 (patch)
tree04eb2325c2a6e20d6a21ef0d9fe64efc1629d9a6 /libdevcore/StringUtils.cpp
parentd123e777d33be3134ebbcda969f149e0e7ad0b0f (diff)
downloaddexon-solidity-8a491c77ba9680afdf8c33664e905b978152b095.tar.gz
dexon-solidity-8a491c77ba9680afdf8c33664e905b978152b095.tar.zst
dexon-solidity-8a491c77ba9680afdf8c33664e905b978152b095.zip
Restructure code for alternative identifier suggestions
Diffstat (limited to 'libdevcore/StringUtils.cpp')
-rw-r--r--libdevcore/StringUtils.cpp69
1 files changed, 41 insertions, 28 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;
}
+