Levenshtein distance for near-miss flag suggestions (two-row DP; inputs * are schema property names, well under the buffer sizes used here). */
| 13946 | /* Levenshtein distance for near-miss flag suggestions (two-row DP; inputs |
| 13947 | * are schema property names, well under the buffer sizes used here). */ |
| 13948 | static int cli_edit_distance(const char *a, const char *b) { |
| 13949 | enum { CLI_ED_MAX = 128 }; |
| 13950 | size_t la = strlen(a); |
| 13951 | size_t lb = strlen(b); |
| 13952 | if (la >= CLI_ED_MAX || lb >= CLI_ED_MAX) { |
| 13953 | return CLI_ED_MAX; |
| 13954 | } |
| 13955 | int prev[CLI_ED_MAX + 1]; |
| 13956 | int cur[CLI_ED_MAX + 1]; |
| 13957 | for (size_t j = 0; j <= lb; j++) { |
| 13958 | prev[j] = (int)j; |
| 13959 | } |
| 13960 | for (size_t i = 1; i <= la; i++) { |
| 13961 | cur[0] = (int)i; |
| 13962 | for (size_t j = 1; j <= lb; j++) { |
| 13963 | int cost = (a[i - 1] == b[j - 1]) ? 0 : 1; |
| 13964 | int del = prev[j] + 1; |
| 13965 | int ins = cur[j - 1] + 1; |
| 13966 | int sub = prev[j - 1] + cost; |
| 13967 | int m = del < ins ? del : ins; |
| 13968 | cur[j] = m < sub ? m : sub; |
| 13969 | } |
| 13970 | memcpy(prev, cur, (lb + 1) * sizeof(int)); |
| 13971 | } |
| 13972 | return prev[lb]; |
| 13973 | } |
| 13974 | |
| 13975 | /* Closest schema property to `key` for a "did you mean" suggestion, or NULL |
| 13976 | * when nothing is plausibly near (distance > half the key length, min 2). */ |