diff options
author | tjr <tjr@FreeBSD.org> | 2004-05-22 15:41:03 +0000 |
---|---|---|
committer | tjr <tjr@FreeBSD.org> | 2004-05-22 15:41:03 +0000 |
commit | ac834a03f9eb1ac57dcf4198a93bb236d806f0b2 (patch) | |
tree | 60f061fc81e6e384df239afb039e49b148fa8fdb | |
parent | f5a461b2700c74383086e755fdaed26dc05d848c (diff) | |
download | FreeBSD-src-ac834a03f9eb1ac57dcf4198a93bb236d806f0b2.zip FreeBSD-src-ac834a03f9eb1ac57dcf4198a93bb236d806f0b2.tar.gz |
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.
-rw-r--r-- | lib/libc/stdio/fgetwc.c | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/lib/libc/stdio/fgetwc.c b/lib/libc/stdio/fgetwc.c index 4463042..eb7426d 100644 --- a/lib/libc/stdio/fgetwc.c +++ b/lib/libc/stdio/fgetwc.c @@ -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); |