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));
51 if (atom->at == NULL) {
52 off_t remain = atom->len;
53 if (fseek(atom->root->f, atom->pos, SEEK_SET) == -1)
54 abort(); /* cannot return error */
55 while (remain > 0) {
56 char buf[16];
57 size_t r;
58 int i;
59 r = fread(buf, 1, MIN(remain, sizeof(buf)),
60 atom->root->f);
61 if (r == -1)
62 abort(); /* cannot return error */
63 if (r == 0)
64 break;
65 remain -= r;
66 for (i = 0; i < r; i++)
67 print_atom_byte(buf[i]);
68 }
69 } else {
70 const char *s;
71 for (s = atom->at; s < (const char*)(atom->at + atom->len); s++)
72 print_atom_byte(*s);
73 }
74 print("'\n");
75 }
77 static inline void
78 dump_atoms(const struct diff_data *d, struct diff_atom *atom,
79 unsigned int count)
80 {
81 if (count > 42) {
82 dump_atoms(d, atom, 20);
83 print("[%u lines skipped]\n", count - 20 - 20);
84 dump_atoms(d, atom + count - 20, 20);
85 return;
86 } else {
87 struct diff_atom *i;
88 foreach_diff_atom(i, atom, count) {
89 dump_atom(d, NULL, i);
90 }
91 }
92 }
94 static inline void
95 dump(struct diff_data *d)
96 {
97 dump_atoms(d, d->atoms.head, d->atoms.len);
98 }
100 /* kd is a quadratic space myers matrix from the original Myers algorithm.
101 * kd_forward and kd_backward are linear slices of a myers matrix from the Myers
102 * Divide algorithm.
103 */
104 static inline void
105 dump_myers_graph(const struct diff_data *l, const struct diff_data *r,
106 int *kd, int *kd_forward, int kd_forward_d,
107 int *kd_backward, int kd_backward_d)
109 #define COLOR_YELLOW "\033[1;33m"
110 #define COLOR_GREEN "\033[1;32m"
111 #define COLOR_BLUE "\033[1;34m"
112 #define COLOR_RED "\033[1;31m"
113 #define COLOR_END "\033[0;m"
114 int x;
115 int y;
116 print(" ");
117 for (x = 0; x <= l->atoms.len; x++)
118 print("%2d", x % 100);
119 print("\n");
121 for (y = 0; y <= r->atoms.len; y++) {
122 print("%3d ", y);
123 for (x = 0; x <= l->atoms.len; x++) {
125 /* print d advancements from kd, if any. */
126 char label = 'o';
127 char *color = NULL;
128 if (kd) {
129 int max = l->atoms.len + r->atoms.len;
130 size_t kd_len = max + 1 + max;
131 int *kd_pos = kd;
132 int di;
133 #define xk_to_y(X, K) ((X) - (K))
134 for (di = 0; di < max; di++) {
135 int ki;
136 for (ki = di; ki >= -di; ki -= 2) {
137 if (x != kd_pos[ki]
138 || y != xk_to_y(x, ki))
139 continue;
140 label = '0' + (di % 10);
141 color = COLOR_YELLOW;
142 break;
144 if (label != 'o')
145 break;
146 kd_pos += kd_len;
149 if (kd_forward && kd_forward_d >= 0) {
150 #define xc_to_y(X, C, DELTA) ((X) - (C) + (DELTA))
151 int ki;
152 for (ki = kd_forward_d;
153 ki >= -kd_forward_d;
154 ki -= 2) {
155 if (x != kd_forward[ki])
156 continue;
157 if (y != xk_to_y(x, ki))
158 continue;
159 label = 'F';
160 color = COLOR_GREEN;
161 break;
164 if (kd_backward && kd_backward_d >= 0) {
165 int delta = (int)r->atoms.len
166 - (int)l->atoms.len;
167 int ki;
168 for (ki = kd_backward_d;
169 ki >= -kd_backward_d;
170 ki -= 2) {
171 if (x != kd_backward[ki])
172 continue;
173 if (y != xc_to_y(x, ki, delta))
174 continue;
175 if (label == 'o') {
176 label = 'B';
177 color = COLOR_BLUE;
178 } else {
179 label = 'X';
180 color = COLOR_RED;
182 break;
185 if (color)
186 print("%s", color);
187 print("%c", label);
188 if (color)
189 print("%s", COLOR_END);
190 if (x < l->atoms.len)
191 print("-");
193 print("\n");
194 if (y == r->atoms.len)
195 break;
197 print(" ");
198 for (x = 0; x < l->atoms.len; x++) {
199 bool same;
200 diff_atom_same(&same, &l->atoms.head[x],
201 &r->atoms.head[y]);
202 if (same)
203 print("|\\");
204 else
205 print("| ");
207 print("|\n");
211 static inline void
212 debug_dump_myers_graph(const struct diff_data *l, const struct diff_data *r,
213 int *kd, int *kd_forward, int kd_forward_d,
214 int *kd_backward, int kd_backward_d)
216 if (l->atoms.len > 99 || r->atoms.len > 99)
217 return;
218 dump_myers_graph(l, r, kd, kd_forward, kd_forward_d,
219 kd_backward, kd_backward_d);
222 #else
223 #define debug(args...)
224 #define debug_dump(args...)
225 #define debug_dump_atom(args...)
226 #define debug_dump_atoms(args...)
227 #define debug_dump_myers_graph(args...)
228 #endif