threads / patch / 59995

patchPrefer fgetc over fgets where possible

Subject: [PATCH] Prefer fgetc over fgets where possible

## tl;dr

4 messages between Jul 17, 2023 and Dec 18, 2024. Diffs are folded; open one to read it.

replies: 3people: 4as markdown or json

AtariDreams via GitGitGadget· Jul 17, 2023, 15:38 UTC · lore
From: Seija Kijin <doremylover123@gmail.com>

fputc is meant for single characters, fputs is for strings. We are better off inserting sole \n characters as characters, not whole strings.

Signed-off-by: Seija Kijin doremylover123@gmail.com
---
    Prefer fgetc over fgets where possible
    
    fputc is meant for single characters, fputs is for strings. We are
    better off inserting sole \n characters as characters, not whole
    strings.
    
    Signed-off-by: Seija Kijin doremylover123@gmail.com
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1550%2FAtariDreams%2Ffgetc-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1550/AtariDreams/fgetc-v1
Pull-Request: https://github.com/git/git/pull/1550
 wt-status.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Show changes to wt-status.c +2 −2
diff --git a/wt-status.c b/wt-status.c
index 8a1a4fb1f04..4c267395cdf 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1215,7 +1215,7 @@ static void wt_longstatus_print_tracking(struct wt_status *s)
 		color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
 				 comment_line_char);
 	else
-		fputs("\n", s->fp);
+		fputc('\n', s->fp);
 	strbuf_release(&sb);
 }
 
@@ -1814,7 +1814,7 @@ static void wt_longstatus_print_state(struct wt_status *s)
 	if (state->merge_in_progress) {
 		if (state->rebase_interactive_in_progress) {
 			show_rebase_information(s, state_color);
-			fputs("\n", s->fp);
+			fputc('\n', s->fp);
 		}
 		show_merge_in_progress(s, state_color);
 	} else if (state->am_in_progress)

base-commit: 830b4a04c45bf0a6db26defe02ed1f490acd18ee
-- 
gitgitgadget
Junio C Hamano· Jul 17, 2023, 16:51 UTC · re: AtariDreams via GitGitGadget · lore

Re: [PATCH] Prefer fgetc over fgets where possible

"AtariDreams via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 6 quoted lines
> From: Seija Kijin <doremylover123@gmail.com>
>
> fputc is meant for single characters,
> fputs is for strings. We are better off
> inserting sole \n characters as
> characters, not whole strings.

I do not see if these short lines are deliberate; are they meant to follow some sort of poetry styles?

In any case, while the above is correct, I do not see the patch noise is worth it in this particular case. Yes, if we are writing code snippets shown with the context in these hunks afresh, please carefully choose between fputs() and fputc(). But once the code is written and it is in, it is not worth to go back and fix it, unless we are fixing surrounding area and doing the clean-up as a "while at it" change.

By the way, because my mail program warned against an address that apparently refuses to receive any replies, I had to manually remove "AtariDreams <83477269+AtariDreams@users.noreply.github.com>" while composing this message. I'd appreciate it if you arrange to ensure that your next patch will not have such addresses on your CC: line.

Thanks.
Taylor Blau· Jul 17, 2023, 20:14 UTC · re: AtariDreams via GitGitGadget · lore

Re: [PATCH] Prefer fgetc over fgets where possible

On Mon, Jul 17, 2023 at 03:38:11PM +0000, AtariDreams via GitGitGadget wrote:
Show 8 quoted lines
> ---
>     Prefer fgetc over fgets where possible
>
>     fputc is meant for single characters, fputs is for strings. We are
>     better off inserting sole \n characters as characters, not whole
>     strings.
>
>     Signed-off-by: Seija Kijin doremylover123@gmail.com

I tend to agree with Junio's assessment that adding churn here isn't strictly necessary / worth it, but if we do end up taking this patch, note that the subject should reference `fputc()` and `fputs()` instead of `fgetc()` and `fgets()`.

Taylor
AreaZR via GitGitGadget· Dec 18, 2024, 01:16 UTC · re: AtariDreams via GitGitGadget · lore

[PATCH v2] Prefer fgetc over fgets where possible

From: Seija Kijin <doremylover123@gmail.com>

fputc is meant for single characters, fputs is for strings. We are better off inserting sole \n characters as characters, not whole strings.

Signed-off-by: Seija Kijin doremylover123@gmail.com
---
    Prefer fgetc over fgets where possible
    
    fputc is meant for single characters, fputs is for strings. We are
    better off inserting sole \n characters as characters, not whole
    strings.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1550%2FAreaZR%2Ffgetc-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1550/AreaZR/fgetc-v2
Pull-Request: https://github.com/git/git/pull/1550
Range-diff vs v1:
 1:  f833a7cc857 ! 1:  26f329befbd Prefer fgetc over fgets where possible
     @@ Commit message
      
          Signed-off-by: Seija Kijin doremylover123@gmail.com
      
     + ## bisect.c ##
     +@@ bisect.c: static void show_list(const char *debug, int counted, int nr,
     + 		subject_len = find_commit_subject(buf, &subject_start);
     + 		if (subject_len)
     + 			fprintf(stderr, " %.*s", subject_len, subject_start);
     +-		fprintf(stderr, "\n");
     ++		fputc('\n', stderr);
     + 	}
     + }
     + 
     +
     + ## commit-graph.c ##
     +@@ commit-graph.c: static void graph_report(const char *fmt, ...)
     + 	verify_commit_graph_error = 1;
     + 	va_start(ap, fmt);
     + 	vfprintf(stderr, fmt, ap);
     +-	fprintf(stderr, "\n");
     ++	fputc('\n', stderr);
     + 	va_end(ap);
     + }
     + 
     +
       ## wt-status.c ##
      @@ wt-status.c: static void wt_longstatus_print_tracking(struct wt_status *s)
     - 		color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
     - 				 comment_line_char);
     + 		color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%s",
     + 				 comment_line_str);
       	else
      -		fputs("\n", s->fp);
      +		fputc('\n', s->fp);
 bisect.c       | 2 +-
 commit-graph.c | 2 +-
 wt-status.c    | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
Show changes to 3 files +4 −4

bisect.c, commit-graph.c, wt-status.c

diff --git a/bisect.c b/bisect.c
index d71c4e4b44b..3b1e034013f 100644
--- a/bisect.c
+++ b/bisect.c
@@ -179,7 +179,7 @@ static void show_list(const char *debug, int counted, int nr,
 		subject_len = find_commit_subject(buf, &subject_start);
 		if (subject_len)
 			fprintf(stderr, " %.*s", subject_len, subject_start);
-		fprintf(stderr, "\n");
+		fputc('\n', stderr);
 	}
 }
 
diff --git a/commit-graph.c b/commit-graph.c
index e2e2083951c..b649598916a 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -2696,7 +2696,7 @@ static void graph_report(const char *fmt, ...)
 	verify_commit_graph_error = 1;
 	va_start(ap, fmt);
 	vfprintf(stderr, fmt, ap);
-	fprintf(stderr, "\n");
+	fputc('\n', stderr);
 	va_end(ap);
 }
 
diff --git a/wt-status.c b/wt-status.c
index 6a8c05d1cff..75b8e900a4c 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1227,7 +1227,7 @@ static void wt_longstatus_print_tracking(struct wt_status *s)
 		color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%s",
 				 comment_line_str);
 	else
-		fputs("\n", s->fp);
+		fputc('\n', s->fp);
 	strbuf_release(&sb);
 }
 
@@ -1832,7 +1832,7 @@ static void wt_longstatus_print_state(struct wt_status *s)
 	if (state->merge_in_progress) {
 		if (state->rebase_interactive_in_progress) {
 			show_rebase_information(s, state_color);
-			fputs("\n", s->fp);
+			fputc('\n', s->fp);
 		}
 		show_merge_in_progress(s, state_color);
 	} else if (state->am_in_progress)

base-commit: 063bcebf0c917140ca0e705cbe0fdea127e90086
-- 
gitgitgadget

← back to recent threads