- char *OrigFile1Start = File1Start;
- char *OrigFile2Start = File2Start;
-
- // If the files need padding, do so now.
- PadFileIfNeeded(File1Start, File1End, F1P);
- PadFileIfNeeded(File2Start, File2End, F2P);
-
- bool CompareFailed = false;
- while (1) {
- // Scan for the end of file or next difference.
- while (F1P < File1End && F2P < File2End && *F1P == *F2P)
- ++F1P, ++F2P;
-
- if (F1P >= File1End || F2P >= File2End) break;
-
- // Okay, we must have found a difference. Backup to the start of the
- // current number each stream is at so that we can compare from the
- // beginning.
- F1P = BackupNumber(F1P, File1Start);
- F2P = BackupNumber(F2P, File2Start);
-
- // Now that we are at the start of the numbers, compare them, exiting if
- // they don't match.
- if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
- CompareFailed = true;
- break;
- }
- }
+ ErrorOr<std::unique_ptr<MemoryBuffer>> F2OrErr = MemoryBuffer::getFile(NameB);
+ if (std::error_code EC = F2OrErr.getError()) {
+ if (Error)
+ *Error = EC.message();
+ return 2;
+ }
+ MemoryBuffer &F2 = *F2OrErr.get();
+
+ // Okay, now that we opened the files, scan them for the first difference.
+ const char *File1Start = F1.getBufferStart();
+ const char *File2Start = F2.getBufferStart();
+ const char *File1End = F1.getBufferEnd();
+ const char *File2End = F2.getBufferEnd();
+ const char *F1P = File1Start;
+ const char *F2P = File2Start;
+ uint64_t A_size = F1.getBufferSize();
+ uint64_t B_size = F2.getBufferSize();
+
+ // Are the buffers identical? Common case: Handle this efficiently.
+ if (A_size == B_size &&
+ std::memcmp(File1Start, File2Start, A_size) == 0)
+ return 0;
+
+ // Otherwise, we are done a tolerances are set.
+ if (AbsTol == 0 && RelTol == 0) {
+ if (Error)
+ *Error = "Files differ without tolerance allowance";
+ return 1; // Files different!
+ }
+
+ bool CompareFailed = false;
+ while (1) {
+ // Scan for the end of file or next difference.
+ while (F1P < File1End && F2P < File2End && *F1P == *F2P)
+ ++F1P, ++F2P;
+
+ if (F1P >= File1End || F2P >= File2End) break;