]> Savannah Git Hosting - gnulib.git/commitdiff
mbfile: Support pushback characters also right before EOF.
authorBruno Haible <bruno@clisp.org>
Tue, 31 Dec 2024 01:09:47 +0000 (02:09 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 31 Dec 2024 01:09:47 +0000 (02:09 +0100)
* lib/mbfile.h (mbfile_multi_getc): Read pushed-back character before
testing for sticky EOF.
* tests/test-mbfile.c (main): Test pushback at EOF.

ChangeLog
lib/mbfile.h
tests/test-mbfile.c

index fa02cafc9800c0d3929cf0ca00cf739602b7cbeb..19be98ae37559381ef71ebb887122693e7be0a0f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2024-12-30  Bruno Haible  <bruno@clisp.org>
 
+       mbfile: Support pushback characters also right before EOF.
+       * lib/mbfile.h (mbfile_multi_getc): Read pushed-back character before
+       testing for sticky EOF.
+       * tests/test-mbfile.c (main): Test pushback at EOF.
+
        mbfile: Allow 2 pushback characters.
        * lib/mbfile.h: Include <stdlib.h>.
        (MBFILE_MAX_PUSHBACK): New macro.
index b110aff229d23aca9f5926804e2a2df6b243c82d..b36ed1c3507700181ef181082c3f9ef7afc1957e 100644 (file)
@@ -89,11 +89,6 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
   unsigned int new_bufcount;
   size_t bytes;
 
-  /* If EOF has already been seen, don't use getc.  This matters if
-     mbf->fp is connected to an interactive tty.  */
-  if (mbf->eof_seen)
-    goto eof;
-
   /* Return character pushed back, if there is one.  */
   if (mbf->pushback_count > 0)
     {
@@ -102,6 +97,11 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
       return;
     }
 
+  /* If EOF has already been seen, don't use getc.  This matters if
+     mbf->fp is connected to an interactive tty.  */
+  if (mbf->eof_seen)
+    goto eof;
+
   new_bufcount = mbf->bufcount;
 
   /* If mbf->state is not in an initial state, some more 32-bit wide character
index 3565404294b3d9e46681aacb0edd90430ae61330..c59803d308e70ea9b932a706dde9b6b018d9a4e2 100644 (file)
@@ -45,6 +45,7 @@ main ()
   /* The input consists of 4 UTF-8 characters:
      '$', U+00A5, U+20AC, U+0001F403.  */
   mbf_char_t next;
+  mbf_char_t prev;
 
   mbf_getc (next, mbstdin);
   ASSERT (!mb_iseof (next));
@@ -65,9 +66,24 @@ main ()
   ASSERT (!mb_iseof (next));
   ASSERT (mb_len (next) == 4);
   ASSERT (mb_iseq (next, 0x1F403));
+  mb_copy (&prev, &next);
 
   mbf_getc (next, mbstdin);
   ASSERT (mb_iseof (next));
 
+  /* Even at EOF, we need to be able to push back 2 characters.  */
+  mbf_ungetc (next, mbstdin);
+  mbf_ungetc (prev, mbstdin);
+
+  mbf_char_t renext;
+
+  mbf_getc (renext, mbstdin);
+  ASSERT (!mb_iseof (renext));
+  ASSERT (mb_len (renext) == 4);
+  ASSERT (mb_iseq (renext, 0x1F403));
+
+  mbf_getc (renext, mbstdin);
+  ASSERT (mb_iseof (renext));
+
   return test_exit_status;
 }