From nobody Tue Apr 19 13:51:37 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 144B811E04FF; Tue, 19 Apr 2022 13:51:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KjQFk00nrz4hZS; Tue, 19 Apr 2022 13:51:38 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1650376298; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=FdW8JK8b69bh0e1Gtwvr5y/r3Bmx1eaVfDRvsOA334Y=; b=IPjKkUCln5pnk1q+EQ/P/9dgC/mp209snm8vcIQ94H1AOtH2M+nJ77R41kndumvOd0qkiI 4AppSJOGNbIKPy3vqyHqh+zfR9uGMHaE1xhaRgRMJeddVc9+Roue+UZYCvX4stXut7SqXY nk0IH84tdphqi2KvSCOK7DDr1Sln3iXGeeL/8q+nyywu49bVsG6HQjEq1yp+XTevYirVq3 kbURnD2A+6kQHJVzfHBvlnyPXrPOgZwbvn7Bbi5HD+ZoOGkqvqhZSQc/1nAwBznSrk9+fd BF+bAweaRnicmILkI3VUM9PBkMgzAqBBmzd98c6Y+6HrtNv7fCv8pUPH2ge1aA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D40F42872D; Tue, 19 Apr 2022 13:51:37 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 23JDpbYV023587; Tue, 19 Apr 2022 13:51:37 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 23JDpbgO023586; Tue, 19 Apr 2022 13:51:37 GMT (envelope-from git) Date: Tue, 19 Apr 2022 13:51:37 GMT Message-Id: <202204191351.23JDpbgO023586@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Tom Jones Subject: git: 2184ca3f191d - main - diff3: Clean up printing of ranges for edscript output List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: thj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2184ca3f191d8750fb56b6d50997d437646c9384 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1650376298; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=FdW8JK8b69bh0e1Gtwvr5y/r3Bmx1eaVfDRvsOA334Y=; b=s4PgI9YoyNHJh/k17+6RXY5mKq+wTc6Pzibj2Hvuv6zNtnWUVDTwBhjbPeAwKD/tlU/EOp 7S+4EaA3X8GzlvFOTrQYEXLsYKoLtAM0n3ww2EUbHyjMxqcQiW5Gt6NmHdrV1vn62bdYtS t+EGyeyeDoOpR+1K9EvSp7akfn9wPBSheVngb9AXhykJ6MR666cJO5oHVczUpD9lYYVZyy GJo6C7/L7Q0PCDVc3bUzzqsiJVl+skalNJkEs3CVpBEBPPg2bi871bD17+Vh105wqIss7H swNY6ZnsQhQCOMnul/EjQImu0SylVyVTEDcQQ+QSnw/rNhFhMC9+5j0lrmqWmQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1650376298; a=rsa-sha256; cv=none; b=pMFJ57mJPpfV2mwxijzCmULegXXeIa2tqEQB34NxiEYVAzF4LBBtjRDrdt2wcpbsVsgdoG gF6xcBZjryIilqd6CkAXNp5Z/lulcHQqKSL5eNDqednIxfXEcVLYRvO0zF6jWZY4J00T7e dkGPfEXS8+6rjEBNl3E1yRXaEcXnn49OLZpsgfh+gcZRqZN1V/tIkIhfBZCLibV3c2uY9y HcwKLQim7WiBF0pY0zy/I7sjSqUABZ8pqE5SHxUs2m44s6HLzOMr1aNh/eWgtY9vQW8dyx 2wRbdEOeBowtXc9pD0B0yekx4FEWycwhxIzssLuCuvRkIdwlA88jHj3XweO/7A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by thj: URL: https://cgit.FreeBSD.org/src/commit/?id=2184ca3f191d8750fb56b6d50997d437646c9384 commit 2184ca3f191d8750fb56b6d50997d437646c9384 Author: Tom Jones AuthorDate: 2022-04-19 13:47:07 +0000 Commit: Tom Jones CommitDate: 2022-04-19 13:51:18 +0000 diff3: Clean up printing of ranges for edscript output Replace the edscript code that tracked and printed lines using byte offsets with code that can work from line offsets. This tidies up the reduces duplication in the edscript output code. It also fixes the usage of the de struct so that it only tracks diffs as line offsets rather than the usage changing from line offsets to byte offsets during the lifetime of diff3. Large files with large numbers of ranges will probably suffer in performance here, but as we don't use diff3 yet this isn't a regression. Include a warning for future hackers so they have a place to start hacking from. Reviewed by: pstef Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D34941 --- usr.bin/diff3/diff3.c | 72 ++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/usr.bin/diff3/diff3.c b/usr.bin/diff3/diff3.c index 37aa599eaab7..396b2620d22c 100644 --- a/usr.bin/diff3/diff3.c +++ b/usr.bin/diff3/diff3.c @@ -110,10 +110,9 @@ static struct diff *d13; static struct diff *d23; /* * "de" is used to gather editing scripts. These are later spewed out in - * reverse order. Its first element must be all zero, the "new" component - * of "de" contains line positions or byte positions depending on when you - * look (!?). Array overlap indicates which sections in "de" correspond to - * lines that are different in all three files. + * reverse order. Its first element must be all zero, the "old" and "new" + * components of "de" contain line positions. Array overlap indicates which + * sections in "de" correspond to lines that are different in all three files. */ static struct diff *de; static char *overlap; @@ -144,6 +143,7 @@ static void repos(int); static void edscript(int) __dead2; static void increase(void); static void usage(void) __dead2; +static void printrange(FILE *, struct range *); enum { DIFFPROG_OPT, @@ -507,19 +507,46 @@ edit(struct diff *diff, bool dup, int j) overlapcnt++; de[j].old.from = diff->old.from; de[j].old.to = diff->old.to; - de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL); - de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL); + de[j].new.from = diff->new.from; + de[j].new.to = diff->new.to; return (j); } +static void +printrange(FILE *p, struct range *r) +{ + char *line = NULL; + size_t len = 0; + int i = 1; + ssize_t rlen = 0; + + /* We haven't been asked to print anything */ + if (r->from == r->to) + return; + + if (r->from > r->to) + errx(EXIT_FAILURE, "invalid print range"); + + /* + * XXX-THJ: We read through all of the file for each range printed. + * This duplicates work and will probably impact performance on large + * files with lots of ranges. + */ + fseek(p, 0L, SEEK_SET); + while ((rlen = getline(&line, &len, p)) > 0) { + if (i >= r->from) + printf("%s", line); + if (++i > r->to - 1) + break; + } + free(line); +} + /* regurgitate */ static void edscript(int n) { - int k; bool delete; - size_t j; - char block[BUFSIZ]; for (; n > 0; n--) { delete = (de[n].new.from == de[n].new.to); @@ -527,34 +554,9 @@ edscript(int n) prange(&de[n].old, delete); } else { printf("%da\n", de[n].old.to - 1); - if (Aflag) { - printf("%s\n", f2mark); - fseek(fp[1], de[n].old.from, SEEK_SET); - for (k = de[n].old.to - de[n].old.from; k > 0; k -= j) { - j = k > BUFSIZ ? BUFSIZ : k; - if (fread(block, 1, j, fp[1]) != j) - errx(2, "logic error"); - fwrite(block, 1, j, stdout); - } - printf("\n"); - } printf("=======\n"); } - fseek(fp[2], (long)de[n].new.from, SEEK_SET); - for (k = de[n].new.to - de[n].new.from; k > 0; k -= j) { - size_t r; - - j = k > BUFSIZ ? BUFSIZ : k; - r = fread(block, 1, j, fp[2]); - if (r == 0) { - if (feof(fp[2])) - break; - errx(2, "logic error"); - } - if (r != j) - j = r; - (void)fwrite(block, 1, j, stdout); - } + printrange(fp[2], &de[n].new); if (!oflag || !overlap[n]) { if (!delete) printf(".\n");