Fix ungetwc for multi byte chars

This commit is contained in:
Darley Barreto
2023-10-19 14:58:57 +00:00
committed by Jeremy Soller
parent 2c7ec0dc8e
commit 3d2dd71b8b
4 changed files with 13 additions and 3 deletions
+8 -1
View File
@@ -280,8 +280,15 @@ pub unsafe extern "C" fn ungetwc(wc: wint_t, stream: &mut FILE) -> wint_t {
if amount == usize::MAX {
return WEOF;
}
/*
We might have unget multiple bytes for a single wchar, eg, `ç` is [195, 167].
We need to unget them in reversed, so they are pused as [..., 167, 195, ...]
When we do fgetwc, we pop from the Vec, getting the write order of bytes [195, 167].
If we called ungetc in the non-reversed order, we would get [167, 195]
*/
for i in 0..amount {
ungetc(bytes[i] as c_int, &mut *stream);
ungetc(bytes[amount - 1 - i] as c_int, &mut *stream);
}
wc
+1
View File
@@ -103,6 +103,7 @@ EXPECT_NAMES=\
unistd/swab \
unistd/write \
waitpid \
wchar/fgetwc \
wchar/fwide \
wchar/mbrtowc \
wchar/mbsrtowcs \
+3 -1
View File
@@ -3,9 +3,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <locale.h>
int main()
{
setlocale(LC_ALL, "");
FILE *stream;
wint_t wc;
wint_t wc2;
+1 -1
View File
@@ -1 +1 @@
123A
123çA