2017-11-17 16:11:15 +00:00
|
|
|
|
/*
|
|
|
|
|
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/>.
|
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-11-17 16:11:15 +00:00
|
|
|
|
/** @file StringUtils.h
|
|
|
|
|
* @author Balajiganapathi S <balajiganapathi.s@gmail.com>
|
|
|
|
|
* @date 2017
|
|
|
|
|
*
|
|
|
|
|
* String routines
|
|
|
|
|
*/
|
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
|
#include <libsolutil/StringUtils.h>
|
2017-11-17 16:11:15 +00:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
|
using namespace solidity::util;
|
2017-11-17 16:11:15 +00:00
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
|
bool solidity::util::stringWithinDistance(string const& _str1, string const& _str2, size_t _maxDistance, size_t _lenThreshold)
|
2017-11-17 16:11:15 +00:00
|
|
|
|
{
|
2017-11-21 16:50:35 +00:00
|
|
|
|
if (_str1 == _str2)
|
2017-11-17 16:11:15 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
2018-02-09 16:45:45 +00:00
|
|
|
|
size_t n1 = _str1.size();
|
|
|
|
|
size_t n2 = _str2.size();
|
2018-08-10 10:31:19 +00:00
|
|
|
|
if (_lenThreshold > 0 && n1 * n2 > _lenThreshold)
|
|
|
|
|
return false;
|
|
|
|
|
|
2017-11-21 16:50:35 +00:00
|
|
|
|
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
|
2018-02-09 16:45:45 +00:00
|
|
|
|
return distance <= _maxDistance && distance < n1 && distance < n2;
|
2017-11-21 16:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
|
size_t solidity::util::stringDistance(string const& _str1, string const& _str2)
|
2017-11-21 16:50:35 +00:00
|
|
|
|
{
|
2018-02-09 16:45:45 +00:00
|
|
|
|
size_t n1 = _str1.size();
|
|
|
|
|
size_t n2 = _str2.size();
|
2017-11-21 16:50:35 +00:00
|
|
|
|
// Optimize by storing only last 2 rows and current row. So first index is considered modulo 3
|
2018-02-09 16:53:30 +00:00
|
|
|
|
// This is a two-dimensional array of size 3 x (n2 + 1).
|
|
|
|
|
vector<size_t> dp(3 * (n2 + 1));
|
2017-11-17 16:11:15 +00:00
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
2018-02-09 16:49:50 +00:00
|
|
|
|
size_t x = 0;
|
|
|
|
|
if (min(i1, i2) == 0) // base case
|
|
|
|
|
x = max(i1, i2);
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-02-09 16:53:30 +00:00
|
|
|
|
size_t left = dp[(i1 - 1) % 3 + i2 * 3];
|
|
|
|
|
size_t up = dp[(i1 % 3) + (i2 - 1) * 3];
|
|
|
|
|
size_t upleft = dp[((i1 - 1) % 3) + (i2 - 1) * 3];
|
2018-02-09 16:49:50 +00:00
|
|
|
|
// deletion and insertion
|
|
|
|
|
x = min(left + 1, up + 1);
|
|
|
|
|
if (_str1[i1-1] == _str2[i2-1])
|
|
|
|
|
// same chars, can skip
|
|
|
|
|
x = min(x, upleft);
|
2017-11-17 16:11:15 +00:00
|
|
|
|
else
|
2018-02-09 16:49:50 +00:00
|
|
|
|
// different chars so try substitution
|
|
|
|
|
x = min(x, upleft + 1);
|
|
|
|
|
|
|
|
|
|
// transposing
|
|
|
|
|
if (i1 > 1 && i2 > 1 && _str1[i1 - 1] == _str2[i2 - 2] && _str1[i1 - 2] == _str2[i2 - 1])
|
2018-02-09 16:53:30 +00:00
|
|
|
|
x = min(x, dp[((i1 - 2) % 3) + (i2 - 2) * 3] + 1);
|
2018-02-09 16:49:50 +00:00
|
|
|
|
}
|
2018-02-09 16:53:30 +00:00
|
|
|
|
dp[(i1 % 3) + i2 * 3] = x;
|
2017-11-17 16:11:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 16:53:30 +00:00
|
|
|
|
return dp[(n1 % 3) + n2 * 3];
|
2017-11-17 16:11:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
|
string solidity::util::quotedAlternativesList(vector<string> const& suggestions)
|
2018-02-09 16:45:45 +00:00
|
|
|
|
{
|
2018-08-02 14:57:16 +00:00
|
|
|
|
vector<string> quotedSuggestions;
|
2017-11-21 16:50:35 +00:00
|
|
|
|
|
2018-08-02 14:57:16 +00:00
|
|
|
|
for (auto& suggestion: suggestions)
|
2018-12-10 18:02:39 +00:00
|
|
|
|
quotedSuggestions.emplace_back("\"" + suggestion + "\"");
|
2017-11-21 16:50:35 +00:00
|
|
|
|
|
2018-08-02 14:57:16 +00:00
|
|
|
|
return joinHumanReadable(quotedSuggestions, ", ", " or ");
|
2017-11-17 16:11:15 +00:00
|
|
|
|
}
|
2017-11-21 16:50:35 +00:00
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
|
string solidity::util::suffixedVariableNameList(string const& _baseName, size_t _startSuffix, size_t _endSuffix)
|
2019-07-05 15:15:38 +00:00
|
|
|
|
{
|
|
|
|
|
string result;
|
|
|
|
|
if (_startSuffix < _endSuffix)
|
|
|
|
|
{
|
|
|
|
|
result = _baseName + to_string(_startSuffix++);
|
|
|
|
|
while (_startSuffix < _endSuffix)
|
|
|
|
|
result += ", " + _baseName + to_string(_startSuffix++);
|
|
|
|
|
}
|
|
|
|
|
else if (_endSuffix < _startSuffix)
|
|
|
|
|
{
|
|
|
|
|
result = _baseName + to_string(_endSuffix++);
|
|
|
|
|
while (_endSuffix < _startSuffix)
|
|
|
|
|
result = _baseName + to_string(_endSuffix++) + ", " + result;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|