Index: tools/fpcmp.c =================================================================== --- tools/fpcmp.c +++ tools/fpcmp.c @@ -217,7 +217,8 @@ int diff_files_with_tolerance(const char *path_a, const char *path_b, double absolute_tolerance, - double relative_tolerance) { + double relative_tolerance, + bool ignore_whitespace) { /* First, load the file buffers completely into memory. */ long A_size, B_size; char *data_a = load_file(path_a, &A_size); @@ -246,8 +247,34 @@ while (1) { // Scan for the end of file or next difference. - while (F1P < File1End && F2P < File2End && *F1P == *F2P) - ++F1P, ++F2P; + while (F1P < File1End && F2P < File2End) { + if (*F1P == *F2P) { + ++F1P, ++F2P; + continue; + } + + // If both chars are whitespace, then we have a + // whitespace-only difference (e.g. CR vs. LF). + // Without this check, we would be in an infinite loop. + if (!ignore_whitespace && isspace(*F1P) && isspace(*F2P)) + return 1; + + // With whitespace ignored, skip whitespace chars and recheck for + // the next difference. + if (ignore_whitespace) { + if (isspace(*F1P)) { + ++F1P; + continue; + } + + if (isspace(*F2P)) { + ++F2P; + continue; + } + } + + break; + } if (F1P >= File1End || F2P >= File2End) break; @@ -296,17 +323,19 @@ } void usage() { - fprintf(stderr, "usage: %s [-a VALUE] [-r VALUE] \n\n", + fprintf(stderr, "usage: %s [-a VALUE] [-r VALUE] [-i] \n\n", g_program); fprintf(stderr, "Compare two files using absolute and relative tolerances\n"); fprintf(stderr, "when comparing differences between two character\n"); fprintf(stderr, "which could be real numbers\n"); + fprintf(stderr, "The -i switch ignores whitespace differences\n"); exit(2); } int main(int argc, char * const argv[]) { double relative_tolerance = 0.0; double absolute_tolerance = 0.0; + bool ignore_whitespace = false; int i; g_program = argv[0]; @@ -341,6 +370,10 @@ } break; + case 'i': + ignore_whitespace = true; + break; + default: fprintf(stderr, "error: invalid argument '%s'\n\n", arg); usage(); @@ -352,6 +385,6 @@ usage(); } - return diff_files_with_tolerance(argv[i], argv[i + 1], - absolute_tolerance, relative_tolerance); + return diff_files_with_tolerance(argv[i], argv[i + 1], absolute_tolerance, + relative_tolerance, ignore_whitespace); }