diff --git a/src/analyze.c b/src/analyze.c index ddb601d..920445c 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -50,16 +50,10 @@ static void discard_confusing_lines (struct file_data filevec[]) { - int f; - lin i; - char *discarded[2]; - lin *equiv_count[2]; - lin *p; - /* Allocate our results. */ - p = xmalloc ((filevec[0].buffered_lines + filevec[1].buffered_lines) - * (2 * sizeof *p)); - for (f = 0; f < 2; f++) + lin *p = xmalloc ((filevec[0].buffered_lines + filevec[1].buffered_lines) + * (2 * sizeof *p)); + for (int f = 0; f < 2; f++) { filevec[f].undiscarded = p; p += filevec[f].buffered_lines; filevec[f].realindexes = p; p += filevec[f].buffered_lines; @@ -69,16 +63,18 @@ discard_confusing_lines (struct file_data filevec[]) that fall in equivalence class I. */ p = xcalloc (filevec[0].equiv_max, 2 * sizeof *p); + lin *equiv_count[2]; equiv_count[0] = p; equiv_count[1] = p + filevec[0].equiv_max; - for (i = 0; i < filevec[0].buffered_lines; ++i) + for (lin i = 0; i < filevec[0].buffered_lines; i++) ++equiv_count[0][filevec[0].equivs[i]]; - for (i = 0; i < filevec[1].buffered_lines; ++i) + for (lin i = 0; i < filevec[1].buffered_lines; i++) ++equiv_count[1][filevec[1].equivs[i]]; /* Set up tables of which lines are going to be discarded. */ + char *discarded[2]; discarded[0] = xzalloc (filevec[0].buffered_lines + filevec[1].buffered_lines); discarded[1] = discarded[0] + filevec[0].buffered_lines; @@ -86,26 +82,24 @@ discard_confusing_lines (struct file_data filevec[]) /* Mark to be discarded each line that matches no line of the other file. If a line matches many lines, mark it as provisionally discardable. */ - for (f = 0; f < 2; f++) + for (int f = 0; f < 2; f++) { size_t end = filevec[f].buffered_lines; char *discards = discarded[f]; lin *counts = equiv_count[1 - f]; lin *equivs = filevec[f].equivs; size_t many = 5; - size_t tem = end / 64; /* Multiply MANY by approximate square root of number of lines. That is the threshold for provisionally discardable lines. */ - while ((tem = tem >> 2) > 0) + for (size_t tem = end / 64; (tem = tem >> 2) > 0; ) many *= 2; - for (i = 0; i < end; i++) + for (lin i = 0; i < end; i++) { - lin nmatch; if (equivs[i] == 0) continue; - nmatch = counts[equivs[i]]; + lin nmatch = counts[equivs[i]]; if (nmatch == 0) discards[i] = 1; else if (nmatch > many) @@ -117,12 +111,12 @@ discard_confusing_lines (struct file_data filevec[]) in a run of discardables, with nonprovisionals at the beginning and end. */ - for (f = 0; f < 2; f++) + for (int f = 0; f < 2; f++) { lin end = filevec[f].buffered_lines; register char *discards = discarded[f]; - for (i = 0; i < end; i++) + for (lin i = 0; i < end; i++) { /* Cancel provisional discards not in middle of run of discards. */ if (discards[i] == 2) @@ -131,7 +125,6 @@ discard_confusing_lines (struct file_data filevec[]) { /* We have found a nonprovisional discard. */ register lin j; - lin length; lin provisional = 0; /* Find end of this run of discardable lines. @@ -150,7 +143,7 @@ discard_confusing_lines (struct file_data filevec[]) /* Now we have the length of a run of discardable lines whose first and last are not provisional. */ - length = j - i; + lin length = j - i; /* If 1/4 of the lines in the run are provisional, cancel discarding of all provisional lines in the run. */ @@ -162,21 +155,20 @@ discard_confusing_lines (struct file_data filevec[]) } else { - register lin consec; lin minimum = 1; - lin tem = length >> 2; /* MINIMUM is approximate square root of LENGTH/4. A subrun of two or more provisionals can stand when LENGTH is at least 16. A subrun of 4 or more can stand when LENGTH >= 64. */ - while (0 < (tem >>= 2)) + for (lin tem = length >> 2; 0 < (tem >>= 2); ) minimum <<= 1; minimum++; /* Cancel any subrun of MINIMUM or more provisionals within the larger run. */ - for (j = 0, consec = 0; j < length; j++) + lin consec = 0; + for (j = 0; j < length; j++) if (discards[i + j] != 2) consec = 0; else if (minimum == ++consec) @@ -226,12 +218,12 @@ discard_confusing_lines (struct file_data filevec[]) } /* Actually discard the lines. */ - for (f = 0; f < 2; f++) + for (int f = 0; f < 2; f++) { char *discards = discarded[f]; lin end = filevec[f].buffered_lines; lin j = 0; - for (i = 0; i < end; ++i) + for (lin i = 0; i < end; i++) if (minimal || discards[i] == 0) { filevec[f].undiscarded[j] = filevec[f].equivs[i]; @@ -259,9 +251,7 @@ discard_confusing_lines (struct file_data filevec[]) static void shift_boundaries (struct file_data filevec[]) { - int f; - - for (f = 0; f < 2; f++) + for (int f = 0; f < 2; f++) { char *changed = filevec[f].changed; char *other_changed = filevec[1 - f].changed; @@ -272,8 +262,6 @@ shift_boundaries (struct file_data filevec[]) while (1) { - lin runlength, start, corresponding; - /* Scan forwards to find beginning of another run of changes. Also keep track of the corresponding point in the other file. */ @@ -287,7 +275,7 @@ shift_boundaries (struct file_data filevec[]) if (i == i_end) break; - start = i; + lin start = i; /* Find the end of this run of changes. */ @@ -296,6 +284,8 @@ shift_boundaries (struct file_data filevec[]) while (other_changed[j]) j++; + lin runlength, corresponding; + do { /* Record the length of this run of changes, so that @@ -462,12 +452,8 @@ briefly_report (int changes, struct file_data const filevec[]) int diff_2_files (struct comparison *cmp) { - int f; - struct change *e, *p; - struct change *script; int changes; - /* If we have detected that either file is binary, compare the two files as binary. This can happen only when the first chunk is read. @@ -499,13 +485,13 @@ diff_2_files (struct comparison *cmp) STAT_BLOCKSIZE (cmp->file[1].stat), lcm_max), lcm_max); - for (f = 0; f < 2; f++) + for (int f = 0; f < 2; f++) cmp->file[f].buffer = xrealloc (cmp->file[f].buffer, buffer_size); for (;; cmp->file[0].buffered = cmp->file[1].buffered = 0) { /* Read a buffer's worth from both files. */ - for (f = 0; f < 2; f++) + for (int f = 0; f < 2; f++) if (0 <= cmp->file[f].desc) file_block_read (&cmp->file[f], buffer_size - cmp->file[f].buffered); @@ -533,10 +519,6 @@ diff_2_files (struct comparison *cmp) } else { - struct context ctxt; - lin diags; - lin too_expensive; - /* Allocate vectors for the results of comparison: a flag for each line of each file, saying whether that line is an insertion or deletion. @@ -556,10 +538,11 @@ diff_2_files (struct comparison *cmp) /* Now do the main comparison algorithm, considering just the undiscarded lines. */ + struct context ctxt; ctxt.xvec = cmp->file[0].undiscarded; ctxt.yvec = cmp->file[1].undiscarded; - diags = (cmp->file[0].nondiscarded_lines - + cmp->file[1].nondiscarded_lines + 3); + lin diags = (cmp->file[0].nondiscarded_lines + + cmp->file[1].nondiscarded_lines + 3); ctxt.fdiag = xmalloc (diags * (2 * sizeof *ctxt.fdiag)); ctxt.bdiag = ctxt.fdiag + diags; ctxt.fdiag += cmp->file[1].nondiscarded_lines + 1; @@ -570,7 +553,7 @@ diff_2_files (struct comparison *cmp) /* Set TOO_EXPENSIVE to be the approximate square root of the input size, bounded below by 4096. 4096 seems to be good for circa-2016 CPUs; see Bug#16848 and Bug#24715. */ - too_expensive = 1; + lin too_expensive = 1; for (; diags != 0; diags >>= 2) too_expensive <<= 1; ctxt.too_expensive = MAX (4096, too_expensive); @@ -590,27 +573,22 @@ diff_2_files (struct comparison *cmp) /* Get the results of comparison in the form of a chain of 'struct change's -- an edit script. */ - - if (output_style == OUTPUT_ED) - script = build_reverse_script (cmp->file); - else - script = build_script (cmp->file); + struct change *script = ((output_style == OUTPUT_ED + ? build_reverse_script + : build_script) + (cmp->file)); /* Set CHANGES if we had any diffs. If some changes are ignored, we must scan the script to decide. */ if (ignore_blank_lines || ignore_regexp.fastmap) { - struct change *next = script; changes = 0; - while (next && changes == 0) + for (struct change *next = script; next && changes == 0; ) { - struct change *this, *end; - lin first0, last0, first1, last1; - /* Find a set of changes that belong together. */ - this = next; - end = find_change (next); + struct change *this = next; + struct change *end = find_change (next); /* Disconnect them from the rest of the changes, making them a hunk, and remember the rest for next iteration. */ @@ -618,6 +596,7 @@ diff_2_files (struct comparison *cmp) end->link = 0; /* Determine whether this hunk is really a difference. */ + lin first0, last0, first1, last1; if (analyze_hunk (this, &first0, &last0, &first1, &last1)) changes = 1; @@ -686,20 +665,21 @@ diff_2_files (struct comparison *cmp) free (flag_space); - for (f = 0; f < 2; f++) + for (int f = 0; f < 2; f++) { free (cmp->file[f].equivs); free (cmp->file[f].linbuf + cmp->file[f].linbuf_base); } - for (e = script; e; e = p) + for (struct change *e = script; e; ) { - p = e->link; + struct change *p = e->link; free (e); + e = p; } if (! robust_output_style (output_style)) - for (f = 0; f < 2; ++f) + for (int f = 0; f < 2; f++) if (cmp->file[f].missing_newline) { error (0, 0, "%s: %s\n",