Blame


1 3b0f3d61 2020-01-22 neels /*
2 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
3 3b0f3d61 2020-01-22 neels *
4 3b0f3d61 2020-01-22 neels * Permission to use, copy, modify, and distribute this software for any
5 3b0f3d61 2020-01-22 neels * purpose with or without fee is hereby granted, provided that the above
6 3b0f3d61 2020-01-22 neels * copyright notice and this permission notice appear in all copies.
7 3b0f3d61 2020-01-22 neels *
8 3b0f3d61 2020-01-22 neels * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3b0f3d61 2020-01-22 neels * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3b0f3d61 2020-01-22 neels * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3b0f3d61 2020-01-22 neels * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3b0f3d61 2020-01-22 neels * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3b0f3d61 2020-01-22 neels * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3b0f3d61 2020-01-22 neels * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3b0f3d61 2020-01-22 neels */
16 3b0f3d61 2020-01-22 neels
17 3b0f3d61 2020-01-22 neels #pragma once
18 3b0f3d61 2020-01-22 neels
19 3b0f3d61 2020-01-22 neels #define DEBUG 0
20 3b0f3d61 2020-01-22 neels
21 3b0f3d61 2020-01-22 neels #if DEBUG
22 3b0f3d61 2020-01-22 neels #include <stdio.h>
23 3b0f3d61 2020-01-22 neels #define print(args...) fprintf(stderr, ##args)
24 3b0f3d61 2020-01-22 neels #define debug print
25 3b0f3d61 2020-01-22 neels #define debug_dump dump
26 3b0f3d61 2020-01-22 neels #define debug_dump_atom dump_atom
27 3b0f3d61 2020-01-22 neels #define debug_dump_atoms dump_atoms
28 3b0f3d61 2020-01-22 neels
29 3b0f3d61 2020-01-22 neels static inline void dump_atom(const struct diff_data *left, const struct diff_data *right, const struct diff_atom *atom)
30 3b0f3d61 2020-01-22 neels {
31 3b0f3d61 2020-01-22 neels if (!atom) {
32 3b0f3d61 2020-01-22 neels print("NULL atom\n");
33 3b0f3d61 2020-01-22 neels return;
34 3b0f3d61 2020-01-22 neels }
35 3b0f3d61 2020-01-22 neels if (left)
36 3b0f3d61 2020-01-22 neels print(" %3ld", diff_atom_root_idx(left, atom));
37 3b0f3d61 2020-01-22 neels if (right && atom->patience.pos_in_other)
38 3b0f3d61 2020-01-22 neels print(" %3ld", diff_atom_root_idx(right, atom->patience.pos_in_other));
39 3b0f3d61 2020-01-22 neels
40 3b0f3d61 2020-01-22 neels print(" %s%s '", atom->patience.unique_here ? "u" : " ", atom->patience.unique_in_both ? "c" : " ");
41 3b0f3d61 2020-01-22 neels const char *s;
42 3b0f3d61 2020-01-22 neels for (s = atom->at; s < (const char*)(atom->at + atom->len); s++) {
43 3b0f3d61 2020-01-22 neels if (*s == '\r')
44 3b0f3d61 2020-01-22 neels print("\\r");
45 3b0f3d61 2020-01-22 neels else if (*s == '\n')
46 3b0f3d61 2020-01-22 neels print("\\n");
47 fbd55577 2020-01-27 neels else if ((*s < 32 || *s >= 127) && (*s != '\t'))
48 3b0f3d61 2020-01-22 neels print("\\x%02x", *s);
49 3b0f3d61 2020-01-22 neels else
50 3b0f3d61 2020-01-22 neels print("%c", *s);
51 3b0f3d61 2020-01-22 neels }
52 3b0f3d61 2020-01-22 neels print("'\n");
53 3b0f3d61 2020-01-22 neels }
54 3b0f3d61 2020-01-22 neels
55 3b0f3d61 2020-01-22 neels static inline void dump_atoms(const struct diff_data *d, struct diff_atom *atom, unsigned int count)
56 3b0f3d61 2020-01-22 neels {
57 13b15273 2020-01-27 neels if (count > 42) {
58 13b15273 2020-01-27 neels dump_atoms(d, atom, 20);
59 13b15273 2020-01-27 neels print("[%u lines skipped]\n", count - 20 - 20);
60 13b15273 2020-01-27 neels dump_atoms(d, atom + count - 20, 20);
61 13b15273 2020-01-27 neels return;
62 13b15273 2020-01-27 neels } else {
63 13b15273 2020-01-27 neels struct diff_atom *i;
64 13b15273 2020-01-27 neels foreach_diff_atom(i, atom, count) {
65 13b15273 2020-01-27 neels dump_atom(d, NULL, i);
66 13b15273 2020-01-27 neels }
67 3b0f3d61 2020-01-22 neels }
68 3b0f3d61 2020-01-22 neels }
69 3b0f3d61 2020-01-22 neels
70 3b0f3d61 2020-01-22 neels static inline void dump(struct diff_data *d)
71 3b0f3d61 2020-01-22 neels {
72 3b0f3d61 2020-01-22 neels dump_atoms(d, d->atoms.head, d->atoms.len);
73 3b0f3d61 2020-01-22 neels }
74 3b0f3d61 2020-01-22 neels
75 3b0f3d61 2020-01-22 neels static inline void dump_myers_graph(const struct diff_data *l, const struct diff_data *r,
76 3b0f3d61 2020-01-22 neels int *kd)
77 3b0f3d61 2020-01-22 neels {
78 3b0f3d61 2020-01-22 neels int x;
79 3b0f3d61 2020-01-22 neels int y;
80 3b0f3d61 2020-01-22 neels print(" ");
81 3b0f3d61 2020-01-22 neels for (x = 0; x <= l->atoms.len; x++)
82 3b0f3d61 2020-01-22 neels print("%2d", x);
83 3b0f3d61 2020-01-22 neels print("\n");
84 3b0f3d61 2020-01-22 neels
85 3b0f3d61 2020-01-22 neels for (y = 0; y <= r->atoms.len; y++) {
86 3b0f3d61 2020-01-22 neels print("%2d ", y);
87 3b0f3d61 2020-01-22 neels for (x = 0; x <= l->atoms.len; x++) {
88 3b0f3d61 2020-01-22 neels
89 3b0f3d61 2020-01-22 neels /* print d advancements from kd, if any. */
90 3b0f3d61 2020-01-22 neels int d = -1;
91 3b0f3d61 2020-01-22 neels if (kd) {
92 3b0f3d61 2020-01-22 neels int max = l->atoms.len + r->atoms.len;
93 3b0f3d61 2020-01-22 neels size_t kd_len = max + 1 + max;
94 3b0f3d61 2020-01-22 neels int *kd_pos = kd;
95 3b0f3d61 2020-01-22 neels int di;
96 3b0f3d61 2020-01-22 neels #define xk_to_y(X, K) ((X) - (K))
97 3b0f3d61 2020-01-22 neels for (di = 0; di < max; di++) {
98 3b0f3d61 2020-01-22 neels int ki;
99 3b0f3d61 2020-01-22 neels for (ki = di; ki >= -di; ki -= 2) {
100 3b0f3d61 2020-01-22 neels if (x == kd_pos[ki]
101 3b0f3d61 2020-01-22 neels && y == xk_to_y(x, ki)) {
102 3b0f3d61 2020-01-22 neels d = di;
103 3b0f3d61 2020-01-22 neels break;
104 3b0f3d61 2020-01-22 neels }
105 3b0f3d61 2020-01-22 neels }
106 3b0f3d61 2020-01-22 neels if (d >= 0)
107 3b0f3d61 2020-01-22 neels break;
108 3b0f3d61 2020-01-22 neels kd_pos += kd_len;
109 3b0f3d61 2020-01-22 neels }
110 3b0f3d61 2020-01-22 neels }
111 3b0f3d61 2020-01-22 neels if (d >= 0)
112 3b0f3d61 2020-01-22 neels print("%d", d);
113 3b0f3d61 2020-01-22 neels else
114 3b0f3d61 2020-01-22 neels print("o");
115 3b0f3d61 2020-01-22 neels if (x < l->atoms.len && d < 10)
116 3b0f3d61 2020-01-22 neels print("-");
117 3b0f3d61 2020-01-22 neels }
118 3b0f3d61 2020-01-22 neels print("\n");
119 3b0f3d61 2020-01-22 neels if (y == r->atoms.len)
120 3b0f3d61 2020-01-22 neels break;
121 3b0f3d61 2020-01-22 neels
122 3b0f3d61 2020-01-22 neels print(" ");
123 3b0f3d61 2020-01-22 neels for (x = 0; x < l->atoms.len; x++) {
124 3b0f3d61 2020-01-22 neels if (diff_atom_same(&l->atoms.head[x], &r->atoms.head[y]))
125 3b0f3d61 2020-01-22 neels print("|\\");
126 3b0f3d61 2020-01-22 neels else
127 3b0f3d61 2020-01-22 neels print("| ");
128 3b0f3d61 2020-01-22 neels }
129 3b0f3d61 2020-01-22 neels print("|\n");
130 3b0f3d61 2020-01-22 neels }
131 3b0f3d61 2020-01-22 neels }
132 3b0f3d61 2020-01-22 neels
133 72057b70 2020-01-27 neels static inline void debug_dump_myers_graph(const struct diff_data *l, const struct diff_data *r,
134 72057b70 2020-01-27 neels int *kd)
135 72057b70 2020-01-27 neels {
136 72057b70 2020-01-27 neels if (l->atoms.len > 99 || r->atoms.len > 99)
137 72057b70 2020-01-27 neels return;
138 72057b70 2020-01-27 neels dump_myers_graph(l, r, kd);
139 72057b70 2020-01-27 neels }
140 72057b70 2020-01-27 neels
141 3b0f3d61 2020-01-22 neels #else
142 3b0f3d61 2020-01-22 neels #define debug(args...)
143 3b0f3d61 2020-01-22 neels #define debug_dump(args...)
144 3b0f3d61 2020-01-22 neels #define debug_dump_atom(args...)
145 3b0f3d61 2020-01-22 neels #define debug_dump_atoms(args...)
146 3b0f3d61 2020-01-22 neels #define debug_dump_myers_graph(args...)
147 3b0f3d61 2020-01-22 neels #endif