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 #pragma once
19 #define DEBUG 0
21 #if DEBUG
22 #include <stdio.h>
23 #define print(args...) fprintf(stderr, ##args)
24 #define debug print
25 #define debug_dump dump
26 #define debug_dump_atom dump_atom
27 #define debug_dump_atoms dump_atoms
29 static inline void dump_atom(const struct diff_data *left, const struct diff_data *right, const struct diff_atom *atom)
30 {
31 if (!atom) {
32 print("NULL atom\n");
33 return;
34 }
35 if (left)
36 print(" %3ld", diff_atom_root_idx(left, atom));
37 if (right && atom->patience.pos_in_other)
38 print(" %3ld", diff_atom_root_idx(right, atom->patience.pos_in_other));
40 print(" %s%s '", atom->patience.unique_here ? "u" : " ", atom->patience.unique_in_both ? "c" : " ");
41 const char *s;
42 for (s = atom->at; s < (const char*)(atom->at + atom->len); s++) {
43 if (*s == '\r')
44 print("\\r");
45 else if (*s == '\n')
46 print("\\n");
47 else if ((*s < 32 || *s >= 127) && (*s != '\t'))
48 print("\\x%02x", *s);
49 else
50 print("%c", *s);
51 }
52 print("'\n");
53 }
55 static inline void dump_atoms(const struct diff_data *d, struct diff_atom *atom, unsigned int count)
56 {
57 if (count > 42) {
58 dump_atoms(d, atom, 20);
59 print("[%u lines skipped]\n", count - 20 - 20);
60 dump_atoms(d, atom + count - 20, 20);
61 return;
62 } else {
63 struct diff_atom *i;
64 foreach_diff_atom(i, atom, count) {
65 dump_atom(d, NULL, i);
66 }
67 }
68 }
70 static inline void dump(struct diff_data *d)
71 {
72 dump_atoms(d, d->atoms.head, d->atoms.len);
73 }
75 static inline void dump_myers_graph(const struct diff_data *l, const struct diff_data *r,
76 int *kd)
77 {
78 int x;
79 int y;
80 print(" ");
81 for (x = 0; x <= l->atoms.len; x++)
82 print("%2d", x);
83 print("\n");
85 for (y = 0; y <= r->atoms.len; y++) {
86 print("%2d ", y);
87 for (x = 0; x <= l->atoms.len; x++) {
89 /* print d advancements from kd, if any. */
90 int d = -1;
91 if (kd) {
92 int max = l->atoms.len + r->atoms.len;
93 size_t kd_len = max + 1 + max;
94 int *kd_pos = kd;
95 int di;
96 #define xk_to_y(X, K) ((X) - (K))
97 for (di = 0; di < max; di++) {
98 int ki;
99 for (ki = di; ki >= -di; ki -= 2) {
100 if (x == kd_pos[ki]
101 && y == xk_to_y(x, ki)) {
102 d = di;
103 break;
106 if (d >= 0)
107 break;
108 kd_pos += kd_len;
111 if (d >= 0)
112 print("%d", d);
113 else
114 print("o");
115 if (x < l->atoms.len && d < 10)
116 print("-");
118 print("\n");
119 if (y == r->atoms.len)
120 break;
122 print(" ");
123 for (x = 0; x < l->atoms.len; x++) {
124 if (diff_atom_same(&l->atoms.head[x], &r->atoms.head[y]))
125 print("|\\");
126 else
127 print("| ");
129 print("|\n");
133 static inline void debug_dump_myers_graph(const struct diff_data *l, const struct diff_data *r,
134 int *kd)
136 if (l->atoms.len > 99 || r->atoms.len > 99)
137 return;
138 dump_myers_graph(l, r, kd);
141 #else
142 #define debug(args...)
143 #define debug_dump(args...)
144 #define debug_dump_atom(args...)
145 #define debug_dump_atoms(args...)
146 #define debug_dump_myers_graph(args...)
147 #endif