From 3705bcc9bc8c726a60c1eaa1d29cd1d240c0268a Mon Sep 17 00:00:00 2001 From: Evgeniy Filatov Date: Thu, 2 Aug 2018 17:57:16 +0300 Subject: added helper function that joins vectors of strings, refactored suggestions formattingi function to use it --- libdevcore/StringUtils.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'libdevcore/StringUtils.h') diff --git a/libdevcore/StringUtils.h b/libdevcore/StringUtils.h index acd93e32..4fb5a748 100644 --- a/libdevcore/StringUtils.h +++ b/libdevcore/StringUtils.h @@ -36,4 +36,43 @@ 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 const& suggestions); +/// Joins collection of strings into one string with separators between, last separator can be different. +/// @param _list collection of strings to join +/// @param _separator defaults to ", " +/// @param _lastSeparator (optional) will be used to separate last two strings instead of _separator +/// @example join(vector{"a", "b", "c"}, "; ", " or ") == "a; b or c" +template +std::string joinHumanReadable +( + T const& _list, + std::string const& _separator = ", ", + std::string const& _lastSeparator = "" +) +{ + auto it = begin(_list); + auto itEnd = end(_list); + + std::string result; + + // append first string + if (it != itEnd) + { + result += *it; + ++it; + } + + for (;it != itEnd; ++it) + { + if ((next(it) == itEnd) && !_lastSeparator.empty()) + result += _lastSeparator; // last iteration + else + result += _separator; + + // append string + result += *it; + } + + return result; +} + } -- cgit v1.2.3 From 7d9692c31d411a66ea194480bf3963cb97e2c257 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 9 Aug 2018 01:12:39 +0200 Subject: Explicitly use std::next to avoid boost version. --- libdevcore/StringUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libdevcore/StringUtils.h') diff --git a/libdevcore/StringUtils.h b/libdevcore/StringUtils.h index 4fb5a748..94c6e3c5 100644 --- a/libdevcore/StringUtils.h +++ b/libdevcore/StringUtils.h @@ -63,7 +63,7 @@ std::string joinHumanReadable for (;it != itEnd; ++it) { - if ((next(it) == itEnd) && !_lastSeparator.empty()) + if ((std::next(it) == itEnd) && !_lastSeparator.empty()) result += _lastSeparator; // last iteration else result += _separator; -- cgit v1.2.3 From 14e116c1d57e1797e7206299d0fdfed2aa03cdf2 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 14 Aug 2018 11:31:51 +0200 Subject: Make joinHumanReadable work for input iterators. --- libdevcore/StringUtils.h | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'libdevcore/StringUtils.h') diff --git a/libdevcore/StringUtils.h b/libdevcore/StringUtils.h index 94c6e3c5..f05a426b 100644 --- a/libdevcore/StringUtils.h +++ b/libdevcore/StringUtils.h @@ -49,27 +49,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; -- cgit v1.2.3 From 3fa8829845bf55df812f81356a3ec43149836bb5 Mon Sep 17 00:00:00 2001 From: bakaoh Date: Fri, 10 Aug 2018 17:31:19 +0700 Subject: Fixes #4718: High CPU usage when using large variable names --- libdevcore/StringUtils.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'libdevcore/StringUtils.h') diff --git a/libdevcore/StringUtils.h b/libdevcore/StringUtils.h index f05a426b..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" -- cgit v1.2.3