Adjust isAscii check to disallow \t, \n, & \r characters (#1599)

* Adjust `isAscii` check to disallow `\t`, `\n`, & `\r` characters

* Readability (& unused code removal)

* Only > & <
This commit is contained in:
Jaco
2022-07-16 15:40:30 +03:00
committed by GitHub
parent 66ac9e15a6
commit 4a06d1382f
3 changed files with 11 additions and 6 deletions
+1
View File
@@ -5,6 +5,7 @@
Changes:
- Add `arrayUnzip` as reverse of `arrayZip`
- Adjust `isAscii` check to disallow `\t`, `\n`, & `\r` characters
## 10.0.2 Jul 10, 2022
+8 -4
View File
@@ -7,7 +7,11 @@ import { isAscii } from '.';
describe('isAscii', (): void => {
it('returns true for an ASCII string', (): void => {
expect(isAscii('Hello\tWorld!\n\rTesting')).toEqual(true);
expect(isAscii('Hello World! Testing')).toEqual(true);
});
it('returns false for an ASCII string (with formatters)', (): void => {
expect(isAscii('Hello\tWorld!\n\rTesting')).toEqual(false);
});
it('returns false on an non-ascii string', (): void => {
@@ -15,7 +19,7 @@ describe('isAscii', (): void => {
});
it('returns true for ASCII bytes', (): void => {
expect(isAscii(new Uint8Array([0x31, 0x32, 0x20, 10]))).toEqual(true);
expect(isAscii(stringToU8a('Hello World! Testing'))).toEqual(true);
});
it('returns true for empty string inputs', (): void => {
@@ -38,6 +42,6 @@ describe('isAscii', (): void => {
expect(isAscii('0x010203')).toEqual(false);
});
perf('isAscii (str)', 2_000_000, [['Hello\tWorld!\n\rTesting']], isAscii);
perf('isAscii (u8a)', 200_000, [[[stringToU8a('Hello\tWorld!\n\rTesting')]]], isAscii);
perf('isAscii (str)', 2_000_000, [['Hello World! Testing']], isAscii);
perf('isAscii (u8a)', 100_000, [[[stringToU8a('Hello World! Testing')]]], isAscii);
});
+2 -2
View File
@@ -15,7 +15,7 @@ function isAsciiStr (str: AnyString): boolean {
const b = str.charCodeAt(i);
// check is inlined here, it is faster than making a call
if (!((b < 127) && ((b >= 32) || (b === 10) || (b === 9) || (b === 13)))) {
if ((b < 32) || (b > 126)) {
return false;
}
}
@@ -31,7 +31,7 @@ function isAsciiBytes (u8a: Uint8Array | Buffer | number[]): boolean {
const b = u8a[i];
// check is inlined here, it is faster than making a call
if (!((b < 127) && ((b >= 32) || (b === 10) || (b === 9) || (b === 13)))) {
if ((b < 32) || (b > 126)) {
return false;
}
}