From 758a72f55360a1f3a5400aeca29bc2ea0647bad1 Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Sat, 6 Apr 2024 17:36:24 +0300 Subject: win32: ascii-optimize winansi_fputc, winansi_putchar Other winansi IO wrappers, like winansi_fputs, optimize by calling the libc API directly if no special handling is needed, e.g. if the input is fully ASCII and without escape sequences - without converting the output codepage or interpreting escapes. Now the fputc and putchar wrappers do that as well. And as a simplification, putchar is now also a wrapper of fputc. Other than possibly minor speedup, this can also help buffered streams remain buffered, because the codepage conversion using writeCon_utf8 is unbuffered and first flushes the stream, so by avoiding the conversion and calling the libc API directly, we also avoid premature flush of a buffered stream. This did happen, as "time" is buffered since commit 54dbf0fa5, so previously it was flushed early when using putchar, while now it remains buffered by default (but can still be flushed early if the -f format string contains non-ASCII chars). --- win32/winansi.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/win32/winansi.c b/win32/winansi.c index dc98b9d2b..0dd2e17f2 100644 --- a/win32/winansi.c +++ b/win32/winansi.c @@ -850,13 +850,7 @@ static int ansi_emulate(const char *s, FILE *stream) int winansi_putchar(int c) { - char t = c; - char *s = &t; - - if (!is_console(STDOUT_FILENO)) - return putchar(c); - - return conv_fwriteCon(stdout, s, 1) == EOF ? EOF : (unsigned char)c; + return winansi_fputc(c, stdout); } int winansi_puts(const char *s) @@ -947,7 +941,7 @@ int winansi_fputc(int c, FILE *stream) char t = c; char *s = &t; - if (!is_console(fileno(stream))) { + if ((unsigned char)c <= 0x7f || !is_console(fileno(stream))) { SetLastError(0); if ((ret=fputc(c, stream)) == EOF) check_pipe(stream); -- cgit v1.2.3-55-g6feb