aboutsummaryrefslogtreecommitdiffstats
path: root/libdevcore
diff options
context:
space:
mode:
Diffstat (limited to 'libdevcore')
-rw-r--r--libdevcore/Algorithms.h8
-rw-r--r--libdevcore/Common.h2
-rw-r--r--libdevcore/CommonIO.cpp20
-rw-r--r--libdevcore/CommonIO.h6
-rw-r--r--libdevcore/Exceptions.cpp4
-rw-r--r--libdevcore/StringUtils.cpp19
-rw-r--r--libdevcore/StringUtils.h38
7 files changed, 77 insertions, 20 deletions
diff --git a/libdevcore/Algorithms.h b/libdevcore/Algorithms.h
index b2540668..7fe2472d 100644
--- a/libdevcore/Algorithms.h
+++ b/libdevcore/Algorithms.h
@@ -32,11 +32,13 @@ template <typename V>
class CycleDetector
{
public:
+ using Visitor = std::function<void(V const&, CycleDetector&, size_t)>;
+
/// Initializes the cycle detector
/// @param _visit function that is given the current vertex
/// and is supposed to call @a run on all
/// adjacent vertices.
- explicit CycleDetector(std::function<void(V const&, CycleDetector&)> _visit):
+ explicit CycleDetector(Visitor _visit):
m_visit(std::move(_visit))
{ }
@@ -55,7 +57,7 @@ public:
m_processing.insert(&_vertex);
m_depth++;
- m_visit(_vertex, *this);
+ m_visit(_vertex, *this, m_depth);
m_depth--;
if (m_firstCycleVertex && m_depth == 1)
m_firstCycleVertex = &_vertex;
@@ -66,7 +68,7 @@ public:
}
private:
- std::function<void(V const&, CycleDetector&)> m_visit;
+ Visitor m_visit;
std::set<V const*> m_processing;
std::set<V const*> m_processed;
size_t m_depth = 0;
diff --git a/libdevcore/Common.h b/libdevcore/Common.h
index 2543855d..0363d9a2 100644
--- a/libdevcore/Common.h
+++ b/libdevcore/Common.h
@@ -40,7 +40,6 @@
#include <libdevcore/vector_ref.h>
#if defined(__GNUC__)
-#pragma warning(push)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif // defined(__GNUC__)
@@ -57,7 +56,6 @@
#include <boost/multiprecision/cpp_int.hpp>
#if defined(__GNUC__)
-#pragma warning(pop)
#pragma GCC diagnostic pop
#endif // defined(__GNUC__)
diff --git a/libdevcore/CommonIO.cpp b/libdevcore/CommonIO.cpp
index 0063a8d4..9693d02a 100644
--- a/libdevcore/CommonIO.cpp
+++ b/libdevcore/CommonIO.cpp
@@ -187,3 +187,23 @@ boost::filesystem::path dev::weaklyCanonicalFilesystemPath(boost::filesystem::pa
return head / tail;
}
}
+
+string dev::absolutePath(string const& _path, string const& _reference)
+{
+ boost::filesystem::path p(_path);
+ // Anything that does not start with `.` is an absolute path.
+ if (p.begin() == p.end() || (*p.begin() != "." && *p.begin() != ".."))
+ return _path;
+ boost::filesystem::path result(_reference);
+ result.remove_filename();
+ for (boost::filesystem::path::iterator it = p.begin(); it != p.end(); ++it)
+ if (*it == "..")
+ result = result.parent_path();
+ else if (*it != ".")
+ result /= *it;
+ return result.generic_string();
+}
+
+string dev::sanitizePath(string const& _path) {
+ return boost::filesystem::path(_path).generic_string();
+}
diff --git a/libdevcore/CommonIO.h b/libdevcore/CommonIO.h
index 9ba68e74..928b6d15 100644
--- a/libdevcore/CommonIO.h
+++ b/libdevcore/CommonIO.h
@@ -62,4 +62,10 @@ std::string toString(_T const& _t)
/// Should be replaced by the boost implementation as soon as support for boost<1.60 can be dropped.
boost::filesystem::path weaklyCanonicalFilesystemPath(boost::filesystem::path const &_path);
+/// @returns the absolute path corresponding to @a _path relative to @a _reference.
+std::string absolutePath(std::string const& _path, std::string const& _reference);
+
+/// Helper function to return path converted strings.
+std::string sanitizePath(std::string const& _path);
+
}
diff --git a/libdevcore/Exceptions.cpp b/libdevcore/Exceptions.cpp
index f204dbc2..97f03f6e 100644
--- a/libdevcore/Exceptions.cpp
+++ b/libdevcore/Exceptions.cpp
@@ -17,8 +17,6 @@
#include <libdevcore/Exceptions.h>
-#include <boost/lexical_cast.hpp>
-
using namespace std;
using namespace dev;
@@ -43,7 +41,7 @@ string Exception::lineInfo() const
ret += *file;
ret += ':';
if (line)
- ret += boost::lexical_cast<string>(*line);
+ ret += to_string(*line);
return ret;
}
diff --git a/libdevcore/StringUtils.cpp b/libdevcore/StringUtils.cpp
index 2ff86bd5..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
@@ -85,17 +88,11 @@ size_t dev::stringDistance(string const& _str1, string const& _str2)
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] + "\"";
+ vector<string> quotedSuggestions;
- choices += " or \"" + suggestions.back() + "\"";
+ for (auto& suggestion: suggestions)
+ quotedSuggestions.push_back("\"" + suggestion + "\"");
- return choices;
+ return joinHumanReadable(quotedSuggestions, ", ", " or ");
}
diff --git a/libdevcore/StringUtils.h b/libdevcore/StringUtils.h
index acd93e32..b02b9d12 100644
--- a/libdevcore/StringUtils.h
+++ b/libdevcore/StringUtils.h
@@ -30,10 +30,46 @@ 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"
std::string quotedAlternativesList(std::vector<std::string> 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<string>{"a", "b", "c"}, "; ", " or ") == "a; b or c"
+template<class T>
+std::string joinHumanReadable
+(
+ T const& _list,
+ std::string const& _separator = ", ",
+ std::string const& _lastSeparator = ""
+)
+{
+ auto const itEnd = end(_list);
+
+ std::string result;
+
+ for (auto it = begin(_list); it != itEnd; )
+ {
+ std::string element = *it;
+ bool first = (it == begin(_list));
+ ++it;
+ if (!first)
+ {
+ if (it == itEnd && !_lastSeparator.empty())
+ result += _lastSeparator; // last iteration
+ else
+ result += _separator;
+ }
+ result += std::move(element);
+ }
+
+ return result;
+}
+
}