MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / df_hunk_body

Function df_hunk_body

src/mcp/test_impact_diff.c:194–242  ·  view source on GitHub ↗

The body of one hunk: `old_count` removed lines and `new_count` added ones, * in the counts the header gave. A "\ No newline at end of file" note may * follow any of them. DF_SHORT when the text holds fewer; the reader then * stands on the line that did not fit. */

Source from the content-addressed store, hash-verified

192 * follow any of them. DF_SHORT when the text holds fewer; the reader then
193 * stands on the line that did not fit. */
194static df_status_t df_hunk_body(cbm_diff_t *d, df_reader_t *rd, cbm_diff_hunk_t *hunk,
195 int old_count, int new_count) {
196 /* Every line takes at least its marker and a terminator, so a count the
197 * rest of the text cannot hold is not worth an allocation. */
198 size_t room = (size_t)(rd->end - rd->p);
199 if ((size_t)old_count + (size_t)new_count > room) {
200 return DF_SHORT;
201 }
202 const char **removed = cbm_arena_alloc(&d->arena, ((size_t)old_count + 1) * sizeof(*removed));
203 const char **added = cbm_arena_alloc(&d->arena, ((size_t)new_count + 1) * sizeof(*added));
204 if (!removed || !added) {
205 return DF_NO_MEMORY;
206 }
207 hunk->removed = removed;
208 hunk->added = added;
209 while (hunk->removed_count < old_count || hunk->added_count < new_count) {
210 df_reader_t at = *rd;
211 df_line_t line;
212 if (!df_next_line(rd, &line)) {
213 return DF_SHORT;
214 }
215 if (line.len > 0 && line.text[0] == '\\') {
216 continue;
217 }
218 bool is_removed = hunk->removed_count < old_count && line.len > 0 && line.text[0] == '-';
219 bool is_added =
220 !is_removed && hunk->removed_count == old_count && line.len > 0 && line.text[0] == '+';
221 if (!is_removed && !is_added) {
222 *rd = at;
223 return DF_SHORT;
224 }
225 const char *copy = cbm_arena_strndup(&d->arena, line.text + 1, line.len - 1);
226 if (!copy) {
227 return DF_NO_MEMORY;
228 }
229 if (is_removed) {
230 removed[hunk->removed_count++] = copy;
231 } else {
232 added[hunk->added_count++] = copy;
233 }
234 }
235 /* The note about a missing final newline can also come last. */
236 df_reader_t at = *rd;
237 df_line_t line;
238 if (df_next_line(rd, &line) && !(line.len > 0 && line.text[0] == '\\')) {
239 *rd = at;
240 }
241 return DF_OK;
242}
243
244/* A line of a file's header, before its first hunk. false = not one the reader
245 * knows. */

Callers 1

df_readFunction · 0.85

Calls 3

df_next_lineFunction · 0.85
cbm_arena_allocFunction · 0.50
cbm_arena_strndupFunction · 0.50

Tested by

no test coverage detected