blob: 8a5f15d13602b8afaa84fc337fb4d4577eeda11d [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001
2exports = module.exports = function(bytes)
3{
4 var i = 0;
5 while(i < bytes.length)
6 {
7 if( (// ASCII
8 bytes[i] == 0x09 ||
9 bytes[i] == 0x0A ||
10 bytes[i] == 0x0D ||
11 (0x20 <= bytes[i] && bytes[i] <= 0x7E)
12 )
13 ) {
14 i += 1;
15 continue;
16 }
17
18 if( (// non-overlong 2-byte
19 (0xC2 <= bytes[i] && bytes[i] <= 0xDF) &&
20 (0x80 <= bytes[i+1] && bytes[i+1] <= 0xBF)
21 )
22 ) {
23 i += 2;
24 continue;
25 }
26
27 if( (// excluding overlongs
28 bytes[i] == 0xE0 &&
29 (0xA0 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
30 (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
31 ) ||
32 (// straight 3-byte
33 ((0xE1 <= bytes[i] && bytes[i] <= 0xEC) ||
34 bytes[i] == 0xEE ||
35 bytes[i] == 0xEF) &&
36 (0x80 <= bytes[i + 1] && bytes[i+1] <= 0xBF) &&
37 (0x80 <= bytes[i+2] && bytes[i+2] <= 0xBF)
38 ) ||
39 (// excluding surrogates
40 bytes[i] == 0xED &&
41 (0x80 <= bytes[i+1] && bytes[i+1] <= 0x9F) &&
42 (0x80 <= bytes[i+2] && bytes[i+2] <= 0xBF)
43 )
44 ) {
45 i += 3;
46 continue;
47 }
48
49 if( (// planes 1-3
50 bytes[i] == 0xF0 &&
51 (0x90 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
52 (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
53 (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
54 ) ||
55 (// planes 4-15
56 (0xF1 <= bytes[i] && bytes[i] <= 0xF3) &&
57 (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
58 (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
59 (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
60 ) ||
61 (// plane 16
62 bytes[i] == 0xF4 &&
63 (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x8F) &&
64 (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
65 (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
66 )
67 ) {
68 i += 4;
69 continue;
70 }
71
72 return false;
73 }
74
75 return true;
76}