mirror of
https://github.com/hanzoai/utf-8-validate.git
synced 2026-09-24 17:03:57 +00:00
[perf] Add optimization for ASCII case
Read chunks of 8 bytes until a non-ASCII byte is found.
This commit is contained in:
+57
-36
@@ -1,5 +1,6 @@
|
||||
#define NAPI_VERSION 1
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <node_api.h>
|
||||
|
||||
napi_value IsValidUTF8(napi_env env, napi_callback_info info) {
|
||||
@@ -10,13 +11,13 @@ napi_value IsValidUTF8(napi_env env, napi_callback_info info) {
|
||||
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
|
||||
assert(status == napi_ok);
|
||||
|
||||
uint8_t *s;
|
||||
size_t length;
|
||||
uint8_t *buf;
|
||||
size_t len;
|
||||
|
||||
status = napi_get_buffer_info(env, argv[0], (void **)&s, &length);
|
||||
status = napi_get_buffer_info(env, argv[0], (void **)&buf, &len);
|
||||
assert(status == napi_ok);
|
||||
|
||||
uint8_t *end = s + length;
|
||||
size_t i = 0;
|
||||
|
||||
//
|
||||
// This code has been taken from utf8_check.c which was developed by
|
||||
@@ -25,51 +26,71 @@ napi_value IsValidUTF8(napi_env env, napi_callback_info info) {
|
||||
// For original code / licensing please refer to
|
||||
// https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c
|
||||
//
|
||||
while (s < end) {
|
||||
if (*s < 0x80) { // 0xxxxxxx
|
||||
s++;
|
||||
} else if ((s[0] & 0xe0) == 0xc0) { // 110xxxxx 10xxxxxx
|
||||
if (
|
||||
s + 1 == end ||
|
||||
(s[1] & 0xc0) != 0x80 ||
|
||||
(s[0] & 0xfe) == 0xc0 // overlong
|
||||
) {
|
||||
break;
|
||||
} else {
|
||||
s += 2;
|
||||
while (i < len) {
|
||||
size_t j = i + 8;
|
||||
|
||||
if (j <= len) {
|
||||
//
|
||||
// Read 8 bytes and check if they are ASCII.
|
||||
//
|
||||
uint64_t chunk;
|
||||
memcpy(&chunk, buf + i, 8);
|
||||
|
||||
if ((chunk & 0x8080808080808080) == 0) {
|
||||
i = j;
|
||||
continue;
|
||||
}
|
||||
} else if ((s[0] & 0xf0) == 0xe0) { // 1110xxxx 10xxxxxx 10xxxxxx
|
||||
if (
|
||||
s + 2 >= end ||
|
||||
(s[1] & 0xc0) != 0x80 ||
|
||||
(s[2] & 0xc0) != 0x80 ||
|
||||
(s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) ||
|
||||
(s[0] == 0xed && (s[1] & 0xe0) == 0xa0)
|
||||
) {
|
||||
break;
|
||||
} else {
|
||||
s += 3;
|
||||
}
|
||||
|
||||
while ((buf[i] & 0x80) == 0) { // 0xxxxxxx
|
||||
if (++i == len) {
|
||||
goto exit;
|
||||
}
|
||||
} else if ((s[0] & 0xf8) == 0xf0) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
}
|
||||
|
||||
if ((buf[i] & 0xe0) == 0xc0) { // 110xxxxx 10xxxxxx
|
||||
if (
|
||||
s + 3 >= end ||
|
||||
(s[1] & 0xc0) != 0x80 ||
|
||||
(s[2] & 0xc0) != 0x80 ||
|
||||
(s[3] & 0xc0) != 0x80 ||
|
||||
(s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) || // overlong
|
||||
(s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4 // > U+10FFFF
|
||||
i + 1 == len ||
|
||||
(buf[i + 1] & 0xc0) != 0x80 ||
|
||||
(buf[i] & 0xfe) == 0xc0 // overlong
|
||||
) {
|
||||
break;
|
||||
} else {
|
||||
s += 4;
|
||||
i += 2;
|
||||
}
|
||||
} else if ((buf[i] & 0xf0) == 0xe0) { // 1110xxxx 10xxxxxx 10xxxxxx
|
||||
if (
|
||||
i + 2 >= len ||
|
||||
(buf[i + 1] & 0xc0) != 0x80 ||
|
||||
(buf[i + 2] & 0xc0) != 0x80 ||
|
||||
(buf[i] == 0xe0 && (buf[i + 1] & 0xe0) == 0x80) || // overlong
|
||||
(buf[i] == 0xed && (buf[i + 1] & 0xe0) == 0xa0) // surrogate (U+D800 - U+DFFF)
|
||||
) {
|
||||
break;
|
||||
} else {
|
||||
i += 3;
|
||||
}
|
||||
} else if ((buf[i] & 0xf8) == 0xf0) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
if (
|
||||
i + 3 >= len ||
|
||||
(buf[i + 1] & 0xc0) != 0x80 ||
|
||||
(buf[i + 2] & 0xc0) != 0x80 ||
|
||||
(buf[i + 3] & 0xc0) != 0x80 ||
|
||||
(buf[i] == 0xf0 && (buf[i + 1] & 0xf0) == 0x80) || // overlong
|
||||
(buf[i] == 0xf4 && buf[i + 1] > 0x8f) || buf[i] > 0xf4 // > U+10FFFF
|
||||
) {
|
||||
break;
|
||||
} else {
|
||||
i += 4;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
exit:;
|
||||
napi_value result;
|
||||
status = napi_get_boolean(env, s == end, &result);
|
||||
status = napi_get_boolean(env, i == len, &result);
|
||||
assert(status == napi_ok);
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user