aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim J. Robbins <tjr@FreeBSD.org>2003-08-07 07:45:35 +0000
committerTim J. Robbins <tjr@FreeBSD.org>2003-08-07 07:45:35 +0000
commitdab4fca49b6e1a93ecf662f118fff6f9f0952f54 (patch)
treeb6327d0e19669c5d7f6f0e29c35db3892d4b4266
parenta50bc30203c47ffc62791a9be4621c6a4194a7a2 (diff)
downloadsrc-dab4fca49b6e1a93ecf662f118fff6f9f0952f54.tar.gz
src-dab4fca49b6e1a93ecf662f118fff6f9f0952f54.zip
Implement btowc() in terms of mbrtowc() instead of sgetrune(), and
wctob() in terms of wcrtomb() instead of sputrune(). There should be no functional differences, but there may be a small performance hit because we make an extra function call. The aim here is to have as few functions as possible calling s{get,put}rune() to make it easier to remove them in the future.
Notes
Notes: svn path=/head/; revision=118589
-rw-r--r--lib/libc/locale/btowc.c20
-rw-r--r--lib/libc/locale/wctob.c17
2 files changed, 27 insertions, 10 deletions
diff --git a/lib/libc/locale/btowc.c b/lib/libc/locale/btowc.c
index 585abf85f0b9..b0b24f2947b3 100644
--- a/lib/libc/locale/btowc.c
+++ b/lib/libc/locale/btowc.c
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 2002 Tim J. Robbins.
+ * Copyright (c) 2002, 2003 Tim J. Robbins.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -27,19 +27,29 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
-#include <rune.h>
+#include <stdio.h>
+#include <string.h>
#include <wchar.h>
wint_t
btowc(int c)
{
- rune_t r;
char cc;
+ wchar_t wc;
if (c == EOF)
return (WEOF);
cc = (char)c;
- if ((r = sgetrune(&cc, 1, NULL)) == _INVALID_RUNE)
+ /*
+ * We expect mbrtowc() to return 0 or 1, hence the check for n > 1
+ * which detects error return values as well as "impossible" byte
+ * counts.
+ *
+ * We pass NULL as the state pointer to mbrtowc() because we don't
+ * support state-dependent encodings and don't want to waste time
+ * creating a zeroed mbstate_t that will not be used.
+ */
+ if (mbrtowc(&wc, &cc, 1, NULL) > 1)
return (WEOF);
- return (r);
+ return (wc);
}
diff --git a/lib/libc/locale/wctob.c b/lib/libc/locale/wctob.c
index 070ae94ce650..da7afcf44142 100644
--- a/lib/libc/locale/wctob.c
+++ b/lib/libc/locale/wctob.c
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 2002 Tim J. Robbins.
+ * Copyright (c) 2002, 2003 Tim J. Robbins.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -27,15 +27,22 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
-#include <rune.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
#include <wchar.h>
int
wctob(wint_t c)
{
- char cc;
+ char buf[MB_LEN_MAX];
- if (c == WEOF || sputrune(c, &cc, 1, NULL) != 1)
+ /*
+ * We pass NULL as the state pointer to wcrtomb() because we don't
+ * support state-dependent encodings and don't want to waste time
+ * creating a zeroed mbstate_t that will not be used.
+ */
+ if (c == WEOF || wcrtomb(buf, c, NULL) != 1)
return (EOF);
- return ((unsigned char)cc);
+ return ((unsigned char)*buf);
}