Blame


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