File tree Expand file tree Collapse file tree 2 files changed +14
-14
lines changed
Expand file tree Collapse file tree 2 files changed +14
-14
lines changed Original file line number Diff line number Diff line change 99 * @return {Boolean } `true` if `buf` contains only correct UTF-8, else `false`
1010 * @public
1111 */
12- const isValidUTF8 = ( buf ) => {
13- var len = buf . length ;
14- var i = 0 ;
12+ function isValidUTF8 ( buf ) {
13+ const len = buf . length ;
14+ let i = 0 ;
1515
1616 while ( i < len ) {
17- if ( buf [ i ] < 0x80 ) { // 0xxxxxxx
17+ if ( ( buf [ i ] & 0x80 ) === 0x00 ) { // 0xxxxxxx
1818 i ++ ;
1919 } else if ( ( buf [ i ] & 0xe0 ) === 0xc0 ) { // 110xxxxx 10xxxxxx
2020 if (
@@ -23,21 +23,21 @@ const isValidUTF8 = (buf) => {
2323 ( buf [ i ] & 0xfe ) === 0xc0 // overlong
2424 ) {
2525 return false ;
26- } else {
27- i += 2 ;
2826 }
27+
28+ i += 2 ;
2929 } else if ( ( buf [ i ] & 0xf0 ) === 0xe0 ) { // 1110xxxx 10xxxxxx 10xxxxxx
3030 if (
3131 i + 2 >= len ||
3232 ( buf [ i + 1 ] & 0xc0 ) !== 0x80 ||
3333 ( buf [ i + 2 ] & 0xc0 ) !== 0x80 ||
3434 buf [ i ] === 0xe0 && ( buf [ i + 1 ] & 0xe0 ) === 0x80 || // overlong
35- buf [ i ] === 0xed && ( buf [ i + 1 ] & 0xe0 ) === 0xa0 // surrogate (U+D800 - U+DFFF)
35+ buf [ i ] === 0xed && ( buf [ i + 1 ] & 0xe0 ) === 0xa0 // surrogate (U+D800 - U+DFFF)
3636 ) {
3737 return false ;
38- } else {
39- i += 3 ;
4038 }
39+
40+ i += 3 ;
4141 } else if ( ( buf [ i ] & 0xf8 ) === 0xf0 ) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
4242 if (
4343 i + 3 >= len ||
@@ -48,15 +48,15 @@ const isValidUTF8 = (buf) => {
4848 buf [ i ] === 0xf4 && buf [ i + 1 ] > 0x8f || buf [ i ] > 0xf4 // > U+10FFFF
4949 ) {
5050 return false ;
51- } else {
52- i += 4 ;
5351 }
52+
53+ i += 4 ;
5454 } else {
5555 return false ;
5656 }
5757 }
5858
5959 return true ;
60- } ;
60+ }
6161
6262module . exports = isValidUTF8 ;
Original file line number Diff line number Diff line change @@ -36,13 +36,13 @@ napi_value IsValidUTF8(napi_env env, napi_callback_info info) {
3636 uint64_t chunk ;
3737 memcpy (& chunk , buf + i , 8 );
3838
39- if ((chunk & 0x8080808080808080 ) == 0 ) {
39+ if ((chunk & 0x8080808080808080 ) == 0x00 ) {
4040 i = j ;
4141 continue ;
4242 }
4343 }
4444
45- while ((buf [i ] & 0x80 ) == 0 ) { // 0xxxxxxx
45+ while ((buf [i ] & 0x80 ) == 0x00 ) { // 0xxxxxxx
4646 if (++ i == len ) {
4747 goto exit ;
4848 }
You can’t perform that action at this time.
0 commit comments