Blob


1 /*
2 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #define DEBUG 0
19 #if DEBUG
20 #include <stdio.h>
21 #include <unistd.h>
22 #define print(args...) fprintf(stderr, ##args)
23 #define debug print
24 #define debug_dump dump
25 #define debug_dump_atom dump_atom
26 #define debug_dump_atoms dump_atoms
28 static inline void
29 print_atom_byte(unsigned char c) {
30 if (c == '\r')
31 print("\\r");
32 else if (c == '\n')
33 print("\\n");
34 else if ((c < 32 || c >= 127) && (c != '\t'))
35 print("\\x%02x", c);
36 else
37 print("%c", c);
38 }
40 static inline void
41 dump_atom(const struct diff_data *left, const struct diff_data *right,
42 const struct diff_atom *atom)
43 {
44 if (!atom) {
45 print("NULL atom\n");
46 return;
47 }
48 if (left)
49 print(" %3u", diff_atom_root_idx(left, atom));
50 if (right && atom->patience.pos_in_other)
51 print(" %3u",
52 diff_atom_root_idx(right, atom->patience.pos_in_other));
54 print(" %s%s '",
55 atom->patience.unique_in_both ? "u" : " ");
56 if (atom->at == NULL) {
57 off_t remain = atom->len;
58 if (fseek(atom->d->root->f, atom->pos, SEEK_SET) == -1)
59 abort(); /* cannot return error */
60 while (remain > 0) {
61 char buf[16];
62 size_t r;
63 int i;
64 r = fread(buf, 1, MIN(remain, sizeof(buf)),
65 atom->d->root->f);
66 if (r == -1)
67 abort(); /* cannot return error */
68 if (r == 0)
69 break;
70 remain -= r;
71 for (i = 0; i < r; i++)
72 print_atom_byte(buf[i]);
73 }
74 } else {
75 const char *s;
76 for (s = atom->at; s < (const char*)(atom->at + atom->len); s++)
77 print_atom_byte(*s);
78 }
79 print("'\n");
80 }
82 static inline void
83 dump_atoms(const struct diff_data *d, struct diff_atom *atom,
84 unsigned int count)
85 {
86 if (count > 42) {
87 dump_atoms(d, atom, 20);
88 print("[%u lines skipped]\n", count - 20 - 20);
89 dump_atoms(d, atom + count - 20, 20);
90 return;
91 } else {
92 struct diff_atom *i;
93 foreach_diff_atom(i, atom, count) {
94 dump_atom(d, NULL, i);
95 }
96 }
97 }
99 static inline void
100 dump(struct diff_data *d)
102 dump_atoms(d, d->atoms.head, d->atoms.len);
105 /* kd is a quadratic space myers matrix from the original Myers algorithm.
106 * kd_forward and kd_backward are linear slices of a myers matrix from the Myers
107 * Divide algorithm.
108 */
109 static inline void
110 dump_myers_graph(const struct diff_data *l, const struct diff_data *r,
111 int *kd, int *kd_forward, int kd_forward_d,
112 int *kd_backward, int kd_backward_d)
114 #define COLOR_YELLOW "\033[1;33m"
115 #define COLOR_GREEN "\033[1;32m"
116 #define COLOR_BLUE "\033[1;34m"
117 #define COLOR_RED "\033[1;31m"
118 #define COLOR_END "\033[0;m"
119 int x;
120 int y;
121 print(" ");
122 for (x = 0; x <= l->atoms.len; x++)
123 print("%2d", x % 100);
124 print("\n");
126 for (y = 0; y <= r->atoms.len; y++) {
127 print("%3d ", y);
128 for (x = 0; x <= l->atoms.len; x++) {
130 /* print d advancements from kd, if any. */
131 char label = 'o';
132 char *color = NULL;
133 if (kd) {
134 int max = l->atoms.len + r->atoms.len;
135 size_t kd_len = max + 1 + max;
136 int *kd_pos = kd;
137 int di;
138 #define xk_to_y(X, K) ((X) - (K))
139 for (di = 0; di < max; di++) {
140 int ki;
141 for (ki = di; ki >= -di; ki -= 2) {
142 if (x != kd_pos[ki]
143 || y != xk_to_y(x, ki))
144 continue;
145 label = '0' + (di % 10);
146 color = COLOR_YELLOW;
147 break;
149 if (label != 'o')
150 break;
151 kd_pos += kd_len;
154 if (kd_forward && kd_forward_d >= 0) {
155 #define xc_to_y(X, C, DELTA) ((X) - (C) + (DELTA))
156 int ki;
157 for (ki = kd_forward_d;
158 ki >= -kd_forward_d;
159 ki -= 2) {
160 if (x != kd_forward[ki])
161 continue;
162 if (y != xk_to_y(x, ki))
163 continue;
164 label = 'F';
165 color = COLOR_GREEN;
166 break;
169 if (kd_backward && kd_backward_d >= 0) {
170 int delta = (int)r->atoms.len
171 - (int)l->atoms.len;
172 int ki;
173 for (ki = kd_backward_d;
174 ki >= -kd_backward_d;
175 ki -= 2) {
176 if (x != kd_backward[ki])
177 continue;
178 if (y != xc_to_y(x, ki, delta))
179 continue;
180 if (label == 'o') {
181 label = 'B';
182 color = COLOR_BLUE;
183 } else {
184 label = 'X';
185 color = COLOR_RED;
187 break;
190 if (color)
191 print("%s", color);
192 print("%c", label);
193 if (color)
194 print("%s", COLOR_END);
195 if (x < l->atoms.len)
196 print("-");
198 print("\n");
199 if (y == r->atoms.len)
200 break;
202 print(" ");
203 for (x = 0; x < l->atoms.len; x++) {
204 bool same;
205 diff_atom_same(&same, &l->atoms.head[x],
206 &r->atoms.head[y]);
207 if (same)
208 print("|\\");
209 else
210 print("| ");
212 print("|\n");
216 static inline void
217 debug_dump_myers_graph(const struct diff_data *l, const struct diff_data *r,
218 int *kd, int *kd_forward, int kd_forward_d,
219 int *kd_backward, int kd_backward_d)
221 if (l->atoms.len > 99 || r->atoms.len > 99)
222 return;
223 dump_myers_graph(l, r, kd, kd_forward, kd_forward_d,
224 kd_backward, kd_backward_d);
227 #else
228 #define debug(args...)
229 #define debug_dump(args...)
230 #define debug_dump_atom(args...)
231 #define debug_dump_atoms(args...)
232 #define debug_dump_myers_graph(args...)
233 #endif