Skip to content

Commit

Permalink
PORT: read_pending_input/3 and friends: make 16 bit encodings work on…
Browse files Browse the repository at this point in the history
… Windows.
  • Loading branch information
JanWielemaker committed Nov 25, 2024
1 parent ff6c9cd commit 284f41f
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/os/pl-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -3084,18 +3084,34 @@ read_pending_input(DECL_LD term_t input, term_t list, term_t tail, int chars)
}
case ENC_UTF16BE:
case ENC_UTF16LE:
#if SIZEOF_WCHAR_T == 2
case ENC_WCHAR:
#endif
{ size_t count = 0;
const char *us = buf;
const char *es = buf+n;
size_t done = 0, i;
bool be; /* big endian */

#if SIZEOF_WCHAR_T == 2
if ( s->encoding == ENC_WCHAR )
{ union
{ wchar_t wc;
char c[2];
} t = { .wc = 'A' };
be = t.c[1] == 'A';
} else
#endif
{ be = s->encoding == ENC_UTF16BE;
}

while(us+2<=es)
{ int c = get_ucs2(us, s->encoding == ENC_UTF16BE);
{ int c = get_ucs2(us, be);

us += 2;
if ( IS_UTF16_LEAD(c) )
{ if ( us+2 <= es )
{ int c2 = get_ucs2(us, s->encoding == ENC_UTF16BE);
{ int c2 = get_ucs2(us, be);

if ( IS_UTF16_TRAIL(c2) )
{ count++;
Expand All @@ -3121,15 +3137,13 @@ read_pending_input(DECL_LD term_t input, term_t list, term_t tail, int chars)
if ( c == '\r' && skip_cr(s) )
continue;

#if SIZEOF_WCHAR_T > 2
if ( IS_UTF16_LEAD(c) )
{ int c2 = get_ucs2(us, s->encoding == ENC_UTF16BE);

done += 2;
us += 2;
c = utf16_decode(c, c2);
}
#endif

if ( s->position )
S__fupdatefilepos_getc(s, c);
Expand All @@ -3142,6 +3156,7 @@ read_pending_input(DECL_LD term_t input, term_t list, term_t tail, int chars)
re_buffer(s, buf+done, n-done);
break;
}
#if SIZEOF_WCHAR_T != 2
case ENC_WCHAR:
{ const pl_wchar_t *ws = (const pl_wchar_t*)buf;
size_t count = (size_t)n/sizeof(pl_wchar_t);
Expand All @@ -3167,6 +3182,7 @@ read_pending_input(DECL_LD term_t input, term_t list, term_t tail, int chars)
re_buffer(s, buf+done, n-done);
break;
}
#endif
case ENC_UNKNOWN:
default:
assert(0);
Expand Down

0 comments on commit 284f41f

Please sign in to comment.