1+ #pragma once
2+
13#include " globals.hpp"
24#include " types.hpp"
35
4-
56#include < string>
6- #include < string_view>
77#include < vector>
8+ #include < array>
89#include < algorithm>
9- #include < iostream>
10-
11- using sv = std::string_view;
1210
1311// https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm
14- // this function is used for argument corrections in CLI tools.
12+ // this function is used for argument corrections in CLI tools.
1513// The performance might be questionable, but it's nothing critical.
1614// It's only used for finding unrecognised arguments anyway, it's
17- // a debug utility for crashes.
18- u8 wagner_fischer (
19- sv a,
20- sv b
21- ) {
22- u8 a_length = static_cast <u8 >(a.length ());
23- u8 b_length = static_cast <u8 >(b.length ());
24-
25- if (a_length > b_length) {
26- std::swap (a, b);
27- std::swap (a_length, b_length);
28- }
29-
30- std::vector<u8 > curr_row (a_length + 1 );
31- std::vector<u8 > prev_row (a_length + 1 );
32-
33- curr_row.reserve (std::max (a_length, b_length));
34- prev_row.reserve (std::max (a_length, b_length));
35-
36- for (u8 j = 0 ; j <= a_length; ++j) {
37- prev_row[j] = j;
38- }
39-
40- for (u8 i = 1 ; i <= b_length; ++i) {
41- curr_row[0 ] = i;
42-
43- for (u8 j = 1 ; j <= a_length; ++j) {
44- u8 add = prev_row[j] + 1 ;
45- u8 del = curr_row[j - 1 ] + 1 ;
46- u8 change = prev_row[j - 1 ];
47-
48- if (a[j - 1 ] != b[i - 1 ]) {
49- change += 1 ;
50- }
51-
52- curr_row[j] = std::min ({add, del, change});
53- }
54-
55- std::swap (prev_row, curr_row);
56- }
57-
58- return prev_row[a_length];
59- }
60-
15+ // a debug utility for crashes.
16+ u8 wagner_fischer (const std::string& a_input, const std::string& b_input);
17+ void manage_output (const std::vector<std::string>& suggestions);
6118
6219template <std::size_t N>
6320std::vector<std::string> suggest (
64- const sv misspelled_word,
65- const std::array<std::pair<const char *, arg_enum>, N> & dictionary
21+ const std::string& misspelled_word,
22+ const std::array<std::pair<const char *, arg_enum>, N>& dictionary
6623) {
6724 std::vector<std::pair<u8 , std::string>> candidates;
6825 candidates.reserve (N);
@@ -75,38 +32,19 @@ std::vector<std::string> suggest(
7532 }
7633
7734 std::sort (
78- candidates.begin (),
35+ candidates.begin (),
7936 candidates.end (),
80- [](const auto & a, const auto & b) {
37+ [](const std::pair< u8 , std::string> & a, const std::pair< u8 , std::string> & b) {
8138 return a.first < b.first ;
8239 }
8340 );
8441
85- std::vector<std::string> suggestions = {} ;
42+ std::vector<std::string> suggestions;
8643 suggestions.reserve (candidates.size ());
8744
88- for (const auto & [distance, word] : candidates) {
89- suggestions.push_back (word);
45+ for (const auto & candidate : candidates) {
46+ suggestions.push_back (candidate. second ); // word
9047 }
9148
9249 return suggestions;
93- }
94-
95-
96- void manage_output (const std::vector<std::string>& suggestions) {
97- if (suggestions.empty ()) {
98- return ;
99- }
100-
101- std::cerr << " Did you mean: \" " ;
102-
103- for (std::size_t i = 0 ; i < suggestions.size (); ++i) {
104- if (i > 0 ) {
105- std::cerr << " , " ;
106- }
107- std::cerr << bold + suggestions.at (i);
108- std::cerr << ansi_exit;
109- }
110-
111- std::cerr << " \" ?\n " ;
11250}
0 commit comments