1 /* Generic infrastructure to implement various diff algorithms. */
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #define MAX(A,B) ((A)>(B)?(A):(B))
22 #define MIN(A,B) ((A)<(B)?(A):(B))
26 diff_range_empty(const struct diff_range *r)
28 return r->start == r->end;
32 diff_ranges_touch(const struct diff_range *a, const struct diff_range *b)
34 return (a->end >= b->start) && (a->start <= b->end);
38 diff_ranges_merge(struct diff_range *a, const struct diff_range *b)
40 *a = (struct diff_range){
41 .start = MIN(a->start, b->start),
42 .end = MAX(a->end, b->end),
47 diff_range_len(const struct diff_range *r)
51 return r->end - r->start;
54 /* List of all possible return codes of a diff invocation. */
55 #define DIFF_RC_USE_DIFF_ALGO_FALLBACK -1
57 /* Any positive return values are errno values from sys/errno.h */
62 struct diff_data *root; /* back pointer to root diff data */
64 off_t pos; /* if not memory-mapped */
65 const uint8_t *at; /* if memory-mapped */
68 /* This hash is just a very cheap speed up for finding *mismatching*
69 * atoms. When hashes match, we still need to compare entire atoms to
70 * find out whether they are indeed identical or not. */
75 diff_atom_cmp(int *cmp,
76 const struct diff_atom *left,
77 const struct diff_atom *right);
79 /* Indicate whether two given diff atoms match. */
81 diff_atom_same(bool *same,
82 const struct diff_atom *left,
83 const struct diff_atom *right);
85 /* The atom's index in the entire file. For atoms divided by lines of text, this
86 * yields the line number (starting with 0). Also works for diff_data that
87 * reference only a subsection of a file, always reflecting the global position
88 * in the file (and not the relative position within the subsection). */
89 #define diff_atom_root_idx(DIFF_DATA, ATOM) \
90 ((ATOM) && ((ATOM) >= (DIFF_DATA)->root->atoms.head) \
91 ? (unsigned int)((ATOM) - ((DIFF_DATA)->root->atoms.head)) \
92 : (DIFF_DATA)->root->atoms.len)
94 /* The atom's index within DIFF_DATA. For atoms divided by lines of text, this
95 * yields the line number (starting with 0). */
96 #define diff_atom_idx(DIFF_DATA, ATOM) \
97 ((ATOM) && ((ATOM) >= (DIFF_DATA)->atoms.head) \
98 ? (unsigned int)((ATOM) - ((DIFF_DATA)->atoms.head)) \
99 : (DIFF_DATA)->atoms.len)
101 #define foreach_diff_atom(ATOM, FIRST_ATOM, COUNT) \
102 for ((ATOM) = (FIRST_ATOM); \
104 && ((ATOM) >= (FIRST_ATOM)) \
105 && ((ATOM) - (FIRST_ATOM) < (COUNT)); \
108 #define diff_data_foreach_atom(ATOM, DIFF_DATA) \
109 foreach_diff_atom(ATOM, (DIFF_DATA)->atoms.head, (DIFF_DATA)->atoms.len)
111 #define diff_data_foreach_atom_from(FROM, ATOM, DIFF_DATA) \
112 for ((ATOM) = (FROM); \
114 && ((ATOM) >= (DIFF_DATA)->atoms.head) \
115 && ((ATOM) - (DIFF_DATA)->atoms.head < (DIFF_DATA)->atoms.len); \
118 #define diff_data_foreach_atom_backwards_from(FROM, ATOM, DIFF_DATA) \
119 for ((ATOM) = (FROM); \
121 && ((ATOM) >= (DIFF_DATA)->atoms.head) \
122 && ((ATOM) - (DIFF_DATA)->atoms.head >= 0); \
125 /* A diff chunk represents a set of atoms on the left and/or a set of atoms on
128 * If solved == false:
129 * The diff algorithm has divided the source file, and this is a chunk that the
130 * inner_algo should run on next.
131 * The lines on the left should be diffed against the lines on the right.
132 * (If there are no left lines or no right lines, it implies solved == true,
133 * because there is nothing to diff.)
136 * If there are only left atoms, it is a chunk removing atoms from the left ("a
138 * If there are only right atoms, it is a chunk adding atoms from the right ("a
140 * If there are both left and right lines, it is a chunk of equal content on
141 * both sides, and left_count == right_count:
144 * - bar }-- diff_chunk{ left_start = &left.atoms.head[0], left_count = 3,
145 * - baz } right_start = NULL, right_count = 0 }
147 * goo }-- diff_chunk{ left_start = &left.atoms.head[3], left_count = 3,
148 * zoo } right_start = &right.atoms.head[0], right_count = 3 }
150 * +roo }-- diff_chunk{ left_start = NULL, left_count = 0,
151 * +too } right_start = &right.atoms.head[3], right_count = 3 }
156 struct diff_atom *left_start;
157 unsigned int left_count;
158 struct diff_atom *right_start;
159 unsigned int right_count;
162 #define DIFF_RESULT_ALLOC_BLOCKSIZE 128
164 enum diff_chunk_type {
172 static inline enum diff_chunk_type
173 diff_chunk_type(const struct diff_chunk *chunk)
175 if (!chunk->left_count && !chunk->right_count)
179 if (!chunk->right_count)
181 if (!chunk->left_count)
183 if (chunk->left_count != chunk->right_count)
188 struct diff_chunk_context;
191 diff_chunk_context_empty(const struct diff_chunk_context *cc);
194 diff_chunk_contexts_touch(const struct diff_chunk_context *cc,
195 const struct diff_chunk_context *other);
198 diff_chunk_contexts_merge(struct diff_chunk_context *cc,
199 const struct diff_chunk_context *other);
202 /* The final result passed to the original diff caller. */
203 struct diff_result *result;
205 /* The root diff_data is in result->left,right, these are (possibly)
206 * subsections of the root data. */
207 struct diff_data left;
208 struct diff_data right;
210 unsigned int recursion_depth_left;
212 /* Remaining chunks from one diff algorithm pass, if any solved == false
214 diff_chunk_arraylist_t temp_result;
216 /* State buffer used by Myers algorithm. */
218 size_t kd_buf_size; /* in units of sizeof(int), not bytes */
221 struct diff_chunk *diff_state_add_chunk(struct diff_state *state, bool solved,
222 struct diff_atom *left_start,
223 unsigned int left_count,
224 struct diff_atom *right_start,
225 unsigned int right_count);
227 struct diff_output_info;
229 int diff_output_lines(struct diff_output_info *output_info, FILE *dest,
230 const char *prefix, struct diff_atom *start_atom,
233 int diff_output_trailing_newline_msg(struct diff_output_info *outinfo,
235 const struct diff_chunk *c);
236 int diff_output_match_function_prototype(char **prototype,
237 const struct diff_result *result,
238 const struct diff_chunk_context *cc);
240 struct diff_output_info *diff_output_info_alloc(void);
243 diff_data_init_subsection(struct diff_data *d, struct diff_data *parent,
244 struct diff_atom *from_atom, unsigned int atoms_count);