Skip to content

Commit

Permalink
Some small cleanups for multibyte functions
Browse files Browse the repository at this point in the history
  • Loading branch information
th-otto committed Jul 17, 2024
1 parent 5e1107c commit 059189c
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 14 deletions.
6 changes: 1 addition & 5 deletions multibyte/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
| x )
#define F(x) ( ( x>=5 ? 0 : \
x==0 ? R(0x90,0xc0) : \
x==4 ? R(0x80,0xa0) : \
x==4 ? R(0x80,0x90) : \
R(0x80,0xc0) ) \
| ( R(0x80,0xc0) >> 6 ) \
| ( R(0x80,0xc0) >> 12 ) \
Expand All @@ -32,7 +32,3 @@ const uint32_t bittab[] = {
E(0x8),E(0x9),E(0xa),E(0xb),E(0xc),E(0xd),E(0xe),E(0xf),
F(0x0),F(0x1),F(0x2),F(0x3),F(0x4)
};

#ifdef BROKEN_VISIBILITY
__asm__(".hidden __fsmu8");
#endif
6 changes: 5 additions & 1 deletion multibyte/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ extern const uint32_t bittab[]; /* ATTR_LIBC_VISIBILITY; */
#define OOB(c,b) (((((b)>>3)-0x10)|(((b)>>3)+((int32_t)(c)>>26))) & ~7)

/* Interval [a,b). Either a must be 80 or b must be c0, lower 3 bits clear. */
#define R(a,b) ((uint32_t)((a==0x80 ? 0x40-b : -a) << 23))
#define R(a,b) ((uint32_t)(a==0x80 ? 0x40u-b : 0u-a) << 23)
#define FAILSTATE R(0x80,0x80)

#define SA 0xc2u
#define SB 0xf4u

/* Arbitrary encoding for representing code units instead of characters. */
#define CODEUNIT(c) (0xdfff & (signed char)(c))
#define IS_CODEUNIT(c) ((unsigned)(c)-0xdf80 < 0x80)
4 changes: 2 additions & 2 deletions multibyte/mbrlen.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@

size_t mbrlen(const char *__restrict s, size_t n, mbstate_t *__restrict st)
{
static unsigned internal;
return mbrtowc(0, s, n, st ? st : (mbstate_t *)&internal);
static mbstate_t internal;
return mbrtowc(0, s, n, st ? st : &internal);
}
3 changes: 2 additions & 1 deletion multibyte/mbrtowc.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ size_t mbrtowc(wchar_t *__restrict wc, const char *__restrict src, size_t n, mbs
unsigned c;
const unsigned char *s = (const void *)src;
const unsigned N = n;
wchar_t dummy;

if (!st) st = &internal_state;
c = st->__opaque1;

if (!s) {
if (c) goto ilseq;
return 0;
} else if (!wc) wc = (void *)&wc;
} else if (!wc) wc = &dummy;

if (!n) return -2;
if (!c) {
Expand Down
2 changes: 1 addition & 1 deletion multibyte/mbsinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@

int mbsinit(const mbstate_t *st)
{
return !st || !*(unsigned *)st;
return !st || !st->__opaque1;
}
2 changes: 1 addition & 1 deletion multibyte/mbsnrtowcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ size_t mbsnrtowcs(wchar_t *__restrict wcs, const char **__restrict src, size_t n
break;
}
/* have to roll back partial character */
*(unsigned *)st = 0;
st->__opaque1 = 0;
break;
}
s += l; n -= l;
Expand Down
4 changes: 2 additions & 2 deletions multibyte/mbsrtowcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ size_t mbsrtowcs(wchar_t *__restrict ws, const char **__restrict src, size_t wn,
size_t wn0 = wn;
unsigned c = 0;

if (st && (c = *(unsigned *)st)) {
if (st && (c = st->__opaque1) != 0) {
if (ws) {
*(unsigned *)st = 0;
st->__opaque1 = 0;
goto resume;
} else {
goto resume0;
Expand Down
3 changes: 2 additions & 1 deletion multibyte/mbtowc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ int mbtowc(wchar_t *__restrict wc, const char *__restrict src, size_t n)
{
unsigned c;
const unsigned char *s = (const void *)src;
wchar_t dummy;

if (!s) return 0;
if (!n) goto ilseq;
if (!wc) wc = (void *)&wc;
if (!wc) wc = &dummy;

if (*s < 0x80) return !!(*wc = *s);
if (*s-SA > SB-SA) goto ilseq;
Expand Down

0 comments on commit 059189c

Please sign in to comment.