Compute and return the Damerau-Levenshtein optimal string alignment edit distance between two strings. https://github.com/softwx/SoftWx.Match This method is not threadsafe. One of the strings to compare. The other string to compare. 0 if the strings are equivalent, otherwise a positive number
| 59 | /// <returns>0 if the strings are equivalent, otherwise a positive number whose |
| 60 | /// magnitude increases as difference between the strings increases.</returns> |
| 61 | int Distance(std::string_view string1, std::string_view string2) override { |
| 62 | if (string1.empty()) { |
| 63 | return static_cast<int>(string2.size()); |
| 64 | } |
| 65 | if (string2.empty()) { |
| 66 | return static_cast<int>(string1.size()); |
| 67 | } |
| 68 | |
| 69 | // if strings of different lengths, ensure shorter string is in string1. This can result in a little |
| 70 | // faster speed by spending more time spinning just the inner loop during the main processing. |
| 71 | if (string1.size() > string2.size()) { |
| 72 | std::swap(string1, string2); |
| 73 | } |
| 74 | |
| 75 | // identify common suffix and/or prefix that can be ignored |
| 76 | int len1 = 0, len2 = 0, start = 0; |
| 77 | PrefixSuffixPrep(string1, string2, len1, len2, start); |
| 78 | if (len1 == 0) { |
| 79 | return len2; |
| 80 | } |
| 81 | |
| 82 | if (len2 > static_cast<int>(this->_baseChar1Costs.size())) { |
| 83 | _baseChar1Costs.resize(len2, 0); |
| 84 | _basePrevChar1Costs.resize(len2, 0); |
| 85 | } |
| 86 | return Distance(string1, string2, len1, len2, start, _baseChar1Costs, _basePrevChar1Costs); |
| 87 | } |
| 88 | |
| 89 | int Distance(std::string_view string1, std::string_view string2, std::size_t maxEditDistance) override { |
| 90 | if (string1.empty() || string2.empty()) { |