Fix unified diff headers.

The length part (only) of each range is optional when equal to one.
See http://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html

PiperOrigin-RevId: 765326445
Change-Id: I4aec68e82f889e3b4f01861d3b6a16a8b2785ce6
This commit is contained in:
Abseil Team 2025-05-30 13:58:02 -07:00 committed by Copybara-Service
parent 7427a6b5e3
commit e9092b12dc
3 changed files with 15 additions and 15 deletions

View File

@ -1488,17 +1488,17 @@ class Hunk {
// Print a unified diff header for one hunk.
// The format is
// "@@ -<left_start>,<left_length> +<right_start>,<right_length> @@"
// where the left/right parts are omitted if unnecessary.
// where the left/right lengths are omitted if unnecessary.
void PrintHeader(std::ostream* ss) const {
*ss << "@@ ";
if (removes_) {
*ss << "-" << left_start_ << "," << (removes_ + common_);
size_t left_length = removes_ + common_;
size_t right_length = adds_ + common_;
*ss << "@@ " << "-" << left_start_;
if (left_length != 1) {
*ss << "," << left_length;
}
if (removes_ && adds_) {
*ss << " ";
}
if (adds_) {
*ss << "+" << right_start_ << "," << (adds_ + common_);
*ss << " " << "+" << right_start_;
if (right_length != 1) {
*ss << "," << right_length;
}
*ss << " @@\n";
}

View File

@ -62,7 +62,7 @@ Expected equality of these values:
Which is: "\"Line\0 1\"\nLine 2"
"Line 2"
With diff:
@@ -1,2 @@
@@ -1,2 +1 @@
-\"Line\0 1\"
Line 2

View File

@ -3579,13 +3579,13 @@ TEST(EditDistance, TestSuites) {
{__LINE__, "A", "A", " ", ""},
{__LINE__, "ABCDE", "ABCDE", " ", ""},
// Simple adds.
{__LINE__, "X", "XA", " +", "@@ +1,2 @@\n X\n+A\n"},
{__LINE__, "X", "XABCD", " ++++", "@@ +1,5 @@\n X\n+A\n+B\n+C\n+D\n"},
{__LINE__, "X", "XA", " +", "@@ -1 +1,2 @@\n X\n+A\n"},
{__LINE__, "X", "XABCD", " ++++", "@@ -1 +1,5 @@\n X\n+A\n+B\n+C\n+D\n"},
// Simple removes.
{__LINE__, "XA", "X", " -", "@@ -1,2 @@\n X\n-A\n"},
{__LINE__, "XABCD", "X", " ----", "@@ -1,5 @@\n X\n-A\n-B\n-C\n-D\n"},
{__LINE__, "XA", "X", " -", "@@ -1,2 +1 @@\n X\n-A\n"},
{__LINE__, "XABCD", "X", " ----", "@@ -1,5 +1 @@\n X\n-A\n-B\n-C\n-D\n"},
// Simple replaces.
{__LINE__, "A", "a", "/", "@@ -1,1 +1,1 @@\n-A\n+a\n"},
{__LINE__, "A", "a", "/", "@@ -1 +1 @@\n-A\n+a\n"},
{__LINE__, "ABCD", "abcd", "////",
"@@ -1,4 +1,4 @@\n-A\n-B\n-C\n-D\n+a\n+b\n+c\n+d\n"},
// Path finding.