xedistance.h

filter: Statementstmt 82.5% 71 / 86 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.5% 71 / 86 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« DamerauLevenshtein.xs ↑ Summary blib/lib/Text/EditDistance/DamerauLevenshtein.pm »
1 22 static size_t X_FN_NAME(
2 pTHX_
3 const char *pa, const char *pa_end,
4 const char *pb, const char *pb_end
5 ) {
6 assert(pa <= pa_end);
7 assert(pb <= pb_end);
8  
9 22 size_t cp_bound = 0;
10 87 for (; pa < pa_end && pb < pb_end; pa++, pb++, cp_bound++) {
11 49 if (!X_IS_CONTINUATION(*pa)) {
12 44 cp_bound = 0;
13 }
14  
15 75 if (*pa != *pb) {
16 10 pa -= cp_bound;
17 10 pb -= cp_bound;
18 10 cp_bound = 0;
19 for (;;) {
20 20 if (pa_end[-1] != pb_end[-1]) {
21 8 pa_end += cp_bound;
22 8 pb_end += cp_bound;
23 8 cp_bound = 0;
24 8 break;
25 }
26 12 pa_end--;
27 12 pb_end--;
28 9 if (X_IS_CONTINUATION(*pa_end)) {
29 6 cp_bound++;
30 } else {
31 6 cp_bound = 0;
32 }
33 12 if (!(pa < pa_end && pb < pb_end)) {
34 2 if (cp_bound) {
35 0 errno = EILSEQ;
36 0 return SIZE_MAX;
37 }
38 2 break;
39 }
40 }
41 10 break;
42 }
43 }
44  
45 size_t
46 22 len_a = X_LENGTH(pa, pa_end),
47 22 len_b = X_LENGTH(pb, pb_end);
48 22 if (len_a == 0) {
49 7 return len_b;
50 }
51 15 if (len_b == 0) {
52 7 return len_a;
53 }
54  
55 8 if (len_a > len_b) {
56 0 const char *p_tmp = pa;
57 0 pa = pb;
58 0 pb = p_tmp;
59 0 p_tmp = pa_end;
60 0 pa_end = pb_end;
61 0 pb_end = p_tmp;
62 0 size_t n_tmp = len_a;
63 0 len_a = len_b;
64 0 len_b = n_tmp;
65 }
66  
67 8 const size_t rwidth = len_a + 1u;
68  
69 8 if (
70 8 rwidth == 0 ||
71 8 SIZE_MAX - 2u < len_b ||
72 8 SIZE_MAX / sizeof (size_t) < rwidth ||
73 8 SIZE_MAX / (rwidth * sizeof (size_t)) < len_b + 2
74 ) {
75 0 errno = ENOMEM;
76 0 return SIZE_MAX;
77 }
78  
79 8 size_t *const alloc = safemalloc(rwidth * (len_b + 2) * sizeof (size_t));
80 8 if (!alloc) {
81 0 errno = ENOMEM;
82 0 return SIZE_MAX;
83 }
84  
85 8 size_t *const idy_cx = alloc, *const dist = alloc + rwidth;
86  
87 8 dist[0] = 0u;
88 23 for (size_t i = 1; i <= len_a; i++) {
89 15 dist[i] = i;
90 15 idy_cx[i - 1] = SIZE_MAX;
91 }
92 23 for (size_t i = 1; i <= len_b; i++) {
93 15 dist[i * rwidth] = i;
94 }
95  
96 8 const char *pa_orig = pa;
97 23 for (size_t j = 0; j < len_b; j++) {
98 15 const size_t cyn = X_CHARBYTES(pb, pb_end);
99 15 size_t idx_cy = SIZE_MAX;
100 50 for (size_t i = 0; i < len_a; i++) {
101 35 const size_t cxn = X_CHARBYTES(pa, pa_end);
102 35 size_t d = dist[j * rwidth + i];
103 35 if (X_CHAR_EQ(pa, cxn, pb, cyn)) {
104 5 idx_cy = i;
105 5 idy_cx[i] = j;
106 } else {
107 30 const size_t idy_cx_i = idy_cx[i];
108 30 size_t w = dist[(j + 1) * rwidth + i];
109 30 if (w < d) d = w;
110 30 w = dist[j * rwidth + i + 1];
111 30 if (w < d) d = w;
112 30 if (idx_cy != SIZE_MAX && idy_cx_i != SIZE_MAX) {
113 2 w = dist[idy_cx_i * rwidth + idx_cy] + i - idx_cy + j - idy_cx_i - 2u;
114 2 if (w < d) d = w;
115 }
116 30 d++;
117 assert(d != 0u);
118 }
119 35 dist[(j + 1) * rwidth + i + 1] = d;
120 #if 0
121 for (size_t dj = 0; dj <= len_b; dj++) {
122 fprintf(stderr, "|");
123 for (size_t di = 0; di <= len_a; di++) {
124 fprintf(stderr, " %3d |", di == 0 || dj < j + 1 || (dj == j + 1 && di <= i + 1) ? dist[dj * rwidth + di] : 99);
125 }
126 fprintf(stderr, "\n");
127 }
128 fprintf(stderr, "\n");
129 #endif
130 35 pa += cxn;
131 }
132 15 pa = pa_orig;
133 15 pb += cyn;
134 }
135  
136 8 const size_t r = dist[len_b * rwidth + len_a];
137 8 Safefree(alloc);
138 8 return r;
139 }
140  
141 #undef X_FN_NAME
142 #undef X_IS_CONTINUATION
143 #undef X_LENGTH
144 #undef X_CHARBYTES
145 #undef X_CHAR_EQ
« DamerauLevenshtein.xs ↑ Summary blib/lib/Text/EditDistance/DamerauLevenshtein.pm »