1 85ab4559 2020-09-22 stsp /* Generic infrastructure to implement various diff algorithms. */
3 85ab4559 2020-09-22 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
5 85ab4559 2020-09-22 stsp * Permission to use, copy, modify, and distribute this software for any
6 85ab4559 2020-09-22 stsp * purpose with or without fee is hereby granted, provided that the above
7 85ab4559 2020-09-22 stsp * copyright notice and this permission notice appear in all copies.
9 85ab4559 2020-09-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 85ab4559 2020-09-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 85ab4559 2020-09-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 85ab4559 2020-09-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 85ab4559 2020-09-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 85ab4559 2020-09-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 85ab4559 2020-09-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 85ab4559 2020-09-22 stsp #define MAX(A,B) ((A)>(B)?(A):(B))
22 85ab4559 2020-09-22 stsp #define MIN(A,B) ((A)<(B)?(A):(B))
25 85ab4559 2020-09-22 stsp static inline bool
26 85ab4559 2020-09-22 stsp diff_range_empty(const struct diff_range *r)
28 85ab4559 2020-09-22 stsp return r->start == r->end;
31 85ab4559 2020-09-22 stsp static inline bool
32 85ab4559 2020-09-22 stsp diff_ranges_touch(const struct diff_range *a, const struct diff_range *b)
34 85ab4559 2020-09-22 stsp return (a->end >= b->start) && (a->start <= b->end);
37 85ab4559 2020-09-22 stsp static inline void
38 85ab4559 2020-09-22 stsp diff_ranges_merge(struct diff_range *a, const struct diff_range *b)
40 85ab4559 2020-09-22 stsp *a = (struct diff_range){
41 85ab4559 2020-09-22 stsp .start = MIN(a->start, b->start),
42 85ab4559 2020-09-22 stsp .end = MAX(a->end, b->end),
46 85ab4559 2020-09-22 stsp static inline int
47 85ab4559 2020-09-22 stsp diff_range_len(const struct diff_range *r)
51 85ab4559 2020-09-22 stsp return r->end - r->start;
54 85ab4559 2020-09-22 stsp /* Indicate whether two given diff atoms match. */
56 85ab4559 2020-09-22 stsp diff_atom_same(bool *same,
57 85ab4559 2020-09-22 stsp const struct diff_atom *left,
58 85ab4559 2020-09-22 stsp const struct diff_atom *right);
60 85ab4559 2020-09-22 stsp /* A diff chunk represents a set of atoms on the left and/or a set of atoms on
61 85ab4559 2020-09-22 stsp * the right.
63 85ab4559 2020-09-22 stsp * If solved == false:
64 85ab4559 2020-09-22 stsp * The diff algorithm has divided the source file, and this is a chunk that the
65 85ab4559 2020-09-22 stsp * inner_algo should run on next.
66 85ab4559 2020-09-22 stsp * The lines on the left should be diffed against the lines on the right.
67 85ab4559 2020-09-22 stsp * (If there are no left lines or no right lines, it implies solved == true,
68 85ab4559 2020-09-22 stsp * because there is nothing to diff.)
70 85ab4559 2020-09-22 stsp * If solved == true:
71 85ab4559 2020-09-22 stsp * If there are only left atoms, it is a chunk removing atoms from the left ("a
72 85ab4559 2020-09-22 stsp * minus chunk").
73 85ab4559 2020-09-22 stsp * If there are only right atoms, it is a chunk adding atoms from the right ("a
74 85ab4559 2020-09-22 stsp * plus chunk").
75 85ab4559 2020-09-22 stsp * If there are both left and right lines, it is a chunk of equal content on
76 85ab4559 2020-09-22 stsp * both sides, and left_count == right_count:
79 85ab4559 2020-09-22 stsp * - bar }-- diff_chunk{ left_start = &left.atoms.head[0], left_count = 3,
80 85ab4559 2020-09-22 stsp * - baz } right_start = NULL, right_count = 0 }
82 85ab4559 2020-09-22 stsp * goo }-- diff_chunk{ left_start = &left.atoms.head[3], left_count = 3,
83 85ab4559 2020-09-22 stsp * zoo } right_start = &right.atoms.head[0], right_count = 3 }
85 85ab4559 2020-09-22 stsp * +roo }-- diff_chunk{ left_start = NULL, left_count = 0,
86 85ab4559 2020-09-22 stsp * +too } right_start = &right.atoms.head[3], right_count = 3 }
89 85ab4559 2020-09-22 stsp struct diff_chunk {
90 85ab4559 2020-09-22 stsp bool solved;
91 85ab4559 2020-09-22 stsp struct diff_atom *left_start;
92 85ab4559 2020-09-22 stsp unsigned int left_count;
93 85ab4559 2020-09-22 stsp struct diff_atom *right_start;
94 85ab4559 2020-09-22 stsp unsigned int right_count;
97 85ab4559 2020-09-22 stsp #define DIFF_RESULT_ALLOC_BLOCKSIZE 128
99 85ab4559 2020-09-22 stsp enum diff_chunk_type {
100 85ab4559 2020-09-22 stsp CHUNK_EMPTY,
101 85ab4559 2020-09-22 stsp CHUNK_PLUS,
102 85ab4559 2020-09-22 stsp CHUNK_MINUS,
103 85ab4559 2020-09-22 stsp CHUNK_SAME,
104 85ab4559 2020-09-22 stsp CHUNK_ERROR,
107 85ab4559 2020-09-22 stsp static inline enum diff_chunk_type
108 85ab4559 2020-09-22 stsp diff_chunk_type(const struct diff_chunk *chunk)
110 85ab4559 2020-09-22 stsp if (!chunk->left_count && !chunk->right_count)
111 85ab4559 2020-09-22 stsp return CHUNK_EMPTY;
112 85ab4559 2020-09-22 stsp if (!chunk->solved)
113 85ab4559 2020-09-22 stsp return CHUNK_ERROR;
114 85ab4559 2020-09-22 stsp if (!chunk->right_count)
115 85ab4559 2020-09-22 stsp return CHUNK_MINUS;
116 85ab4559 2020-09-22 stsp if (!chunk->left_count)
117 85ab4559 2020-09-22 stsp return CHUNK_PLUS;
118 85ab4559 2020-09-22 stsp if (chunk->left_count != chunk->right_count)
119 85ab4559 2020-09-22 stsp return CHUNK_ERROR;
120 85ab4559 2020-09-22 stsp return CHUNK_SAME;
123 2f26640c 2020-10-17 stsp struct diff_chunk_context;
126 2f26640c 2020-10-17 stsp diff_chunk_context_empty(const struct diff_chunk_context *cc);
129 2f26640c 2020-10-17 stsp diff_chunk_contexts_touch(const struct diff_chunk_context *cc,
130 2f26640c 2020-10-17 stsp const struct diff_chunk_context *other);
133 2f26640c 2020-10-17 stsp diff_chunk_contexts_merge(struct diff_chunk_context *cc,
134 2f26640c 2020-10-17 stsp const struct diff_chunk_context *other);
136 85ab4559 2020-09-22 stsp struct diff_state {
137 85ab4559 2020-09-22 stsp /* The final result passed to the original diff caller. */
138 85ab4559 2020-09-22 stsp struct diff_result *result;
140 85ab4559 2020-09-22 stsp /* The root diff_data is in result->left,right, these are (possibly)
141 85ab4559 2020-09-22 stsp * subsections of the root data. */
142 85ab4559 2020-09-22 stsp struct diff_data left;
143 85ab4559 2020-09-22 stsp struct diff_data right;
145 85ab4559 2020-09-22 stsp unsigned int recursion_depth_left;
147 85ab4559 2020-09-22 stsp /* Remaining chunks from one diff algorithm pass, if any solved == false
148 85ab4559 2020-09-22 stsp * chunks came up. */
149 85ab4559 2020-09-22 stsp diff_chunk_arraylist_t temp_result;
151 8be88dfa 2020-10-21 stsp /* State buffer used by Myers algorithm. */
152 8be88dfa 2020-10-21 stsp int *kd_buf;
153 8be88dfa 2020-10-21 stsp size_t kd_buf_size; /* in units of sizeof(int), not bytes */
156 85ab4559 2020-09-22 stsp struct diff_chunk *diff_state_add_chunk(struct diff_state *state, bool solved,
157 85ab4559 2020-09-22 stsp struct diff_atom *left_start,
158 85ab4559 2020-09-22 stsp unsigned int left_count,
159 85ab4559 2020-09-22 stsp struct diff_atom *right_start,
160 85ab4559 2020-09-22 stsp unsigned int right_count);
162 2c20a3ed 2020-09-22 stsp struct diff_output_info;
164 2c20a3ed 2020-09-22 stsp int diff_output_lines(struct diff_output_info *output_info, FILE *dest,
165 2c20a3ed 2020-09-22 stsp const char *prefix, struct diff_atom *start_atom,
166 2c20a3ed 2020-09-22 stsp unsigned int count);
168 7021523c 2020-10-16 stsp int diff_output_trailing_newline_msg(struct diff_output_info *outinfo,
169 7021523c 2020-10-16 stsp FILE *dest,
170 7021523c 2020-10-16 stsp const struct diff_chunk *c);
171 b3fd1fa2 2020-12-10 stsp #define DIFF_FUNCTION_CONTEXT_SIZE 55
172 b3fd1fa2 2020-12-10 stsp int diff_output_match_function_prototype(char *prototype, size_t prototype_size,
173 b3fd1fa2 2020-12-10 stsp int *last_prototype_idx,
174 13e2caa3 2020-10-17 stsp const struct diff_result *result,
175 8993f425 2022-09-23 mark const struct diff_chunk_context *cc,
176 8993f425 2022-09-23 mark unsigned int ncontext);
178 2c20a3ed 2020-09-22 stsp struct diff_output_info *diff_output_info_alloc(void);
181 87c31341 2020-10-11 neels diff_data_init_subsection(struct diff_data *d, struct diff_data *parent,
182 87c31341 2020-10-11 neels struct diff_atom *from_atom, unsigned int atoms_count);