Perform conversions straight from the stream buffer instead of scanning

through byte by byte with mbrtowc(). In the usual case (buffer is big
enough to contain the multibyte character, character does not straddle
buffer boundary) this results in only one call to mbrtowc() for each
wide character read.
This commit is contained in:
Tim J. Robbins 2004-05-22 15:41:03 +00:00
parent 87275e436a
commit d6ed810a67
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=129584

View File

@ -77,30 +77,31 @@ fgetwc(FILE *fp)
static __inline wint_t
__fgetwc_nbf(FILE *fp)
{
size_t n, nconv;
int c;
char cc;
wchar_t wc;
size_t nconv;
n = 0;
for (;;) {
if ((c = __sgetc(fp)) == EOF) {
if (n == 0)
return (WEOF);
if (fp->_r <= 0 && __srefill(fp))
return (WEOF);
do {
nconv = mbrtowc(&wc, fp->_p, fp->_r, &fp->_extra->mbstate);
if (nconv == (size_t)-1)
break;
}
n++;
cc = (char)c;
nconv = mbrtowc(&wc, &cc, 1, &fp->_extra->mbstate);
if (nconv == (size_t)-2)
else if (nconv == (size_t)-2)
continue;
else if (nconv == (size_t)-1)
break;
else if (nconv == 0)
else if (nconv == 0) {
/*
* Assume that the only valid representation of
* the null wide character is a single null byte.
*/
fp->_p++;
fp->_r--;
return (L'\0');
else
} else {
fp->_p += nconv;
fp->_r -= nconv;
return (wc);
}
}
} while (__srefill(fp) == 0);
fp->_flags |= __SERR;
errno = EILSEQ;
return (WEOF);