12 #include <string_view>
15 #include <boost/core/demangle.hpp>
20 inline int levenshteinDistance(
const std::string_view &
a,
21 const std::string_view &
b) {
24 Eigen::MatrixXi d = Eigen::MatrixXi::Zero(a.size() + 1, b.size() + 1);
28 for (std::size_t
i = 1;
i < a.size() + 1; ++
i) {
34 for (std::size_t
j = 1;
j < b.size() + 1; ++
j) {
39 for (std::size_t
j = 1;
j < b.size() + 1; ++
j) {
40 for (std::size_t
i = 1;
i < a.size() + 1; ++
i) {
41 const auto substitutionCost = a[
i] == b[
j] ? 0 : 1;
43 std::array<int, 3> possibilities = {{
46 d(
i - 1,
j - 1) + substitutionCost
49 d(
i,
j) = *std::min_element(possibilities.begin(), possibilities.end());
55 return d(a.size(), b.size());
61 const std::string_view &
name,
int distThreshold,
62 std::size_t maxNumber)
const {
63 std::vector<std::pair<int, std::string_view>>
names;
65 if (
const auto d = levenshteinDistance(
n, name); d < distThreshold) {
66 names.push_back({d,
n});
71 [&](
const auto &
a,
const auto &
b) {
return a.first < b.first; });
73 std::vector<std::string_view> selected_names;
74 for (std::size_t
i = 0;
i <
std::min(names.size(), maxNumber); ++
i) {
75 selected_names.push_back(names[
i].second);
78 return selected_names;
83 return std::string{
"Type mismatch for '" + name +
"'. Requested " +
84 boost::core::demangle(req) +
" but actually " +
85 boost::core::demangle(act)};