summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2008-05-14 23:44:05 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2008-05-14 23:44:05 (GMT)
commit5d22c050733f3c464badae1520c3892479d3e116 (patch)
tree6827ce7e412b91ad19454609bb9179d562d10978
parentfcf9bb5b04f9a00936d492cdeff482cc2a154e23 (diff)
downloadmoo-unicode.zip
moo-unicode.tar.gz
moo-unicode.tar.bz2
moo-unicode.tar.xz
Optimize put_utf() slightlyunicode
Try to reduce the number of tests we do in the common case.
-rw-r--r--utf.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/utf.c b/utf.c
index aea3da9..82ef36e 100644
--- a/utf.c
+++ b/utf.c
@@ -147,12 +147,10 @@ int get_utf_call(int (*c_getch) (void *), void *c_data, int *state)
goto done;
}
-int put_utf(char **pp, int v)
+int put_utf(char **pp, int vv)
{
char *p = *pp;
-
- if (v < 0 || v > 0x10ffff || (v >= 0xd800 && v <= 0xdfff))
- return -1; /* Invalid UCS */
+ unsigned int v = vv;
if (v <= 0x7f) {
*p++ = v;
@@ -160,14 +158,18 @@ int put_utf(char **pp, int v)
*p++ = 0xc0 | (v >> 6);
*p++ = 0x80 | (v & 0x3f);
} else if (v <= 0xffff) {
+ if ((v - 0xd800) <= (0xdfff-0xd800))
+ return -1; /* Invalid UCS (surrogate) */
*p++ = 0xe0 | (v >> 12);
*p++ = 0x80 | ((v >> 6) & 0x3f);
*p++ = 0x80 | (v & 0x3f);
- } else {
+ } else if (v <= 0x10ffff) {
*p++ = 0xf0 | (v >> 18);
*p++ = 0x80 | ((v >> 12) & 0x3f);
*p++ = 0x80 | ((v >> 6) & 0x3f);
*p++ = 0x80 | (v & 0x3f);
+ } else {
+ return -1; /* Invalid UCS (out of range) */
}
*pp = p;