summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdio/fputwc.c
diff options
context:
space:
mode:
authortjr <tjr@FreeBSD.org>2002-09-18 12:17:28 +0000
committertjr <tjr@FreeBSD.org>2002-09-18 12:17:28 +0000
commitaef6a01e62c6a07fc147d9fb374637729c123e76 (patch)
tree8c2b286725e30957651db56d4e146c20a54c9a4c /lib/libc/stdio/fputwc.c
parentbb655f01d394e53797a84b0158e8017980c464a7 (diff)
downloadFreeBSD-src-aef6a01e62c6a07fc147d9fb374637729c123e76.zip
FreeBSD-src-aef6a01e62c6a07fc147d9fb374637729c123e76.tar.gz
Optimise the common case where no special encoding is in use (LC_CTYPE is "C"
or "POSIX", other European locales). Use __sgetc() and __sputc() where possible to avoid a wasteful lock and unlock for each byte and to avoid function call overhead.
Diffstat (limited to 'lib/libc/stdio/fputwc.c')
-rw-r--r--lib/libc/stdio/fputwc.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/lib/libc/stdio/fputwc.c b/lib/libc/stdio/fputwc.c
index c12581c..34751dd 100644
--- a/lib/libc/stdio/fputwc.c
+++ b/lib/libc/stdio/fputwc.c
@@ -29,6 +29,7 @@ __FBSDID("$FreeBSD$");
#include "namespace.h"
#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
@@ -43,15 +44,32 @@ fputwc(wchar_t wc, FILE *fp)
mbstate_t mbs;
size_t i, len;
- ORIENTLOCK(fp, 1);
+ FLOCKFILE(fp);
+ ORIENT(fp, 1);
- memset(&mbs, 0, sizeof(mbs));
- if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1)
- return (WEOF);
+ if (MB_LEN_MAX == 1 && wc > 0 && wc <= UCHAR_MAX) {
+ /*
+ * Assume single-byte locale with no special encoding.
+ * A more careful test would be to check
+ * _CurrentRuneLocale->encoding.
+ */
+ *buf = (unsigned char)wc;
+ len = 1;
+ } else {
+ memset(&mbs, 0, sizeof(mbs));
+ if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1) {
+ FUNLOCKFILE(fp);
+ return (WEOF);
+ }
+ }
for (i = 0; i < len; i++)
- if (fputc((unsigned char)buf[i], fp) == EOF)
+ if (__sputc((unsigned char)buf[i], fp) == EOF) {
+ FUNLOCKFILE(fp);
return (WEOF);
+ }
+
+ FUNLOCKFILE(fp);
return ((wint_t)wc);
}
OpenPOWER on IntegriCloud