diff options
author | Anurag Dashputre <anurag4u80@gmail.com> | 2018-08-23 14:26:45 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-23 14:26:45 +0800 |
commit | 8497dcd721ff0a113374c0c1e1778d44265398a6 (patch) | |
tree | 2833ab7b3c7513647c0476d0e5d33dc11fcd6951 /libdevcore | |
parent | 55524788e2829b3a2b9c6c513f78ba2074aa3385 (diff) | |
parent | 410d288dfc2e08c42df58c7e01ad5c332ce92727 (diff) | |
download | dexon-solidity-8497dcd721ff0a113374c0c1e1778d44265398a6.tar dexon-solidity-8497dcd721ff0a113374c0c1e1778d44265398a6.tar.gz dexon-solidity-8497dcd721ff0a113374c0c1e1778d44265398a6.tar.bz2 dexon-solidity-8497dcd721ff0a113374c0c1e1778d44265398a6.tar.lz dexon-solidity-8497dcd721ff0a113374c0c1e1778d44265398a6.tar.xz dexon-solidity-8497dcd721ff0a113374c0c1e1778d44265398a6.tar.zst dexon-solidity-8497dcd721ff0a113374c0c1e1778d44265398a6.zip |
Merge branch 'develop' into anurag_issue_3667
Diffstat (limited to 'libdevcore')
-rw-r--r-- | libdevcore/StringUtils.cpp | 5 | ||||
-rw-r--r-- | libdevcore/StringUtils.h | 31 |
2 files changed, 18 insertions, 18 deletions
diff --git a/libdevcore/StringUtils.cpp b/libdevcore/StringUtils.cpp index 01bdc675..50bf7cce 100644 --- a/libdevcore/StringUtils.cpp +++ b/libdevcore/StringUtils.cpp @@ -29,13 +29,16 @@ using namespace std; using namespace dev; -bool dev::stringWithinDistance(string const& _str1, string const& _str2, size_t _maxDistance) +bool dev::stringWithinDistance(string const& _str1, string const& _str2, size_t _maxDistance, size_t _lenThreshold) { if (_str1 == _str2) return true; size_t n1 = _str1.size(); size_t n2 = _str2.size(); + if (_lenThreshold > 0 && n1 * n2 > _lenThreshold) + return false; + 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 diff --git a/libdevcore/StringUtils.h b/libdevcore/StringUtils.h index 94c6e3c5..b02b9d12 100644 --- a/libdevcore/StringUtils.h +++ b/libdevcore/StringUtils.h @@ -30,7 +30,8 @@ namespace dev { // 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); +// if _lenThreshold > 0 and the product of the strings length is greater than _lenThreshold, the function will return false +bool stringWithinDistance(std::string const& _str1, std::string const& _str2, size_t _maxDistance, size_t _lenThreshold = 0); // 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" @@ -49,27 +50,23 @@ std::string joinHumanReadable std::string const& _lastSeparator = "" ) { - auto it = begin(_list); - auto itEnd = end(_list); + auto const itEnd = end(_list); std::string result; - // append first string - if (it != itEnd) + for (auto it = begin(_list); it != itEnd; ) { - result += *it; + std::string element = *it; + bool first = (it == begin(_list)); ++it; - } - - for (;it != itEnd; ++it) - { - if ((std::next(it) == itEnd) && !_lastSeparator.empty()) - result += _lastSeparator; // last iteration - else - result += _separator; - - // append string - result += *it; + if (!first) + { + if (it == itEnd && !_lastSeparator.empty()) + result += _lastSeparator; // last iteration + else + result += _separator; + } + result += std::move(element); } return result; |