smartstring/ts/smartstring.type.ts

100 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-10-10 15:28:23 +00:00
import * as plugins from './smartstring.plugins';
2019-10-10 16:03:04 +00:00
import * as base64 from './smartstring.base64';
2019-10-10 15:28:23 +00:00
export const isUtf8 = (stringArg: string) => {
const bytes = Buffer.from(stringArg);
let i = 0;
2019-10-10 16:14:07 +00:00
while (i < bytes.length) {
if (
// ASCII
bytes[i] === 0x09 ||
bytes[i] === 0x0a ||
bytes[i] === 0x0d ||
(0x20 <= bytes[i] && bytes[i] <= 0x7e)
) {
i += 1;
continue;
}
2019-10-10 15:28:23 +00:00
2019-10-10 16:14:07 +00:00
if (
// non-overlong 2-byte
0xc2 <= bytes[i] &&
bytes[i] <= 0xdf &&
2019-11-24 11:44:21 +00:00
0x80 <= bytes[i + 1] &&
bytes[i + 1] <= 0xbf
2019-10-10 16:14:07 +00:00
) {
i += 2;
continue;
}
2019-10-10 15:28:23 +00:00
2019-10-10 16:14:07 +00:00
if (
// excluding overlongs
(bytes[i] === 0xe0 &&
2019-11-24 11:44:21 +00:00
0xa0 <= bytes[i + 1] &&
bytes[i + 1] <= 0xbf &&
0x80 <= bytes[i + 2] &&
bytes[i + 2] <= 0xbf) || // straight 3-byte
2019-10-10 16:14:07 +00:00
(((0xe1 <= bytes[i] && bytes[i] <= 0xec) || bytes[i] === 0xee || bytes[i] === 0xef) &&
2019-11-24 11:44:21 +00:00
0x80 <= bytes[i + 1] &&
bytes[i + 1] <= 0xbf &&
0x80 <= bytes[i + 2] &&
bytes[i + 2] <= 0xbf) || // excluding surrogates
2019-10-10 16:14:07 +00:00
(bytes[i] === 0xed &&
2019-11-24 11:44:21 +00:00
0x80 <= bytes[i + 1] &&
bytes[i + 1] <= 0x9f &&
0x80 <= bytes[i + 2] &&
bytes[i + 2] <= 0xbf)
2019-10-10 16:14:07 +00:00
) {
i += 3;
continue;
}
2019-10-10 15:28:23 +00:00
2019-10-10 16:14:07 +00:00
if (
// planes 1-3
(bytes[i] === 0xf0 &&
2019-11-24 11:44:21 +00:00
0x90 <= bytes[i + 1] &&
bytes[i + 1] <= 0xbf &&
0x80 <= bytes[i + 2] &&
bytes[i + 2] <= 0xbf &&
0x80 <= bytes[i + 3] &&
bytes[i + 3] <= 0xbf) || // planes 4-15
2019-10-10 16:14:07 +00:00
(0xf1 <= bytes[i] &&
bytes[i] <= 0xf3 &&
2019-11-24 11:44:21 +00:00
0x80 <= bytes[i + 1] &&
bytes[i + 1] <= 0xbf &&
0x80 <= bytes[i + 2] &&
bytes[i + 2] <= 0xbf &&
0x80 <= bytes[i + 3] &&
bytes[i + 3] <= 0xbf) || // plane 16
2019-10-10 16:14:07 +00:00
(bytes[i] === 0xf4 &&
2019-11-24 11:44:21 +00:00
0x80 <= bytes[i + 1] &&
bytes[i + 1] <= 0x8f &&
0x80 <= bytes[i + 2] &&
bytes[i + 2] <= 0xbf &&
0x80 <= bytes[i + 3] &&
bytes[i + 3] <= 0xbf)
2019-10-10 16:14:07 +00:00
) {
i += 4;
continue;
2019-10-10 15:28:23 +00:00
}
2019-10-10 16:14:07 +00:00
return false;
}
return true;
2019-10-10 16:03:04 +00:00
};
export const isBase64 = (stringArg: string) => {
2019-10-10 16:24:03 +00:00
const notBase64 = /[^A-Z0-9+\/=]/i;
const len = stringArg.length;
if (!len || len % 4 !== 0 || notBase64.test(stringArg)) {
return false;
}
const firstPaddingChar = stringArg.indexOf('=');
2019-11-24 11:44:21 +00:00
return (
firstPaddingChar === -1 ||
2019-10-10 16:24:03 +00:00
firstPaddingChar === len - 1 ||
2019-11-24 11:44:21 +00:00
(firstPaddingChar === len - 2 && stringArg[len - 1] === '=')
);
2019-10-10 16:03:04 +00:00
};