|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const { isLatin1 } = require('buffer'); |
| 6 | + |
| 7 | +function reference(str) { |
| 8 | + for (let i = 0; i < str.length; i++) { |
| 9 | + if (str.charCodeAt(i) > 0xFF) return false; |
| 10 | + } |
| 11 | + return true; |
| 12 | +} |
| 13 | + |
| 14 | +// Basic cases. |
| 15 | +assert.strictEqual(isLatin1(''), true); |
| 16 | +assert.strictEqual(isLatin1('hello'), true); |
| 17 | +assert.strictEqual(isLatin1('\x00'), true); |
| 18 | +assert.strictEqual(isLatin1('\x7f\x80'), true); |
| 19 | +assert.strictEqual(isLatin1('caf\u00e9'), true); |
| 20 | +assert.strictEqual(isLatin1('\u00ff'), true); |
| 21 | +assert.strictEqual(isLatin1('\u0100'), false); |
| 22 | +assert.strictEqual(isLatin1('\u20ac'), false); |
| 23 | +assert.strictEqual(isLatin1('\uffff'), false); |
| 24 | +// Surrogate pairs and lone surrogates are > 0xFF. |
| 25 | +assert.strictEqual(isLatin1('\ud83d\ude00'), false); |
| 26 | +assert.strictEqual(isLatin1('\ud800'), false); |
| 27 | +assert.strictEqual(isLatin1('\udfff'), false); |
| 28 | + |
| 29 | +// Position of the offending code unit must not matter, and long strings must |
| 30 | +// exercise the vectorized paths. |
| 31 | +for (const len of [1, 7, 8, 15, 16, 31, 32, 33, 63, 64, 65, 1000, 4099]) { |
| 32 | + const base = 'a\u00ff'.repeat(len).slice(0, len); |
| 33 | + assert.strictEqual(isLatin1(base), true); |
| 34 | + for (const pos of [0, len >> 1, len - 1]) { |
| 35 | + for (const ch of ['\u0100', '\u1234', '\ud800', '\uffff']) { |
| 36 | + const str = base.slice(0, pos) + ch + base.slice(pos + 1); |
| 37 | + assert.strictEqual(isLatin1(str), false, `len=${len} pos=${pos}`); |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Strings stored with a two-byte representation that only contain code units |
| 43 | +// <= 0xFF must still be reported as latin1. |
| 44 | +{ |
| 45 | + const twoByte = '\u0100' + 'abc\u00e9\u00ff'.repeat(100); |
| 46 | + const sliced = twoByte.slice(1); |
| 47 | + assert.strictEqual(isLatin1(twoByte), false); |
| 48 | + assert.strictEqual(isLatin1(sliced), true); |
| 49 | + assert.strictEqual(isLatin1(twoByte.substring(1, 20)), true); |
| 50 | +} |
| 51 | + |
| 52 | +// Cons strings (results of concatenation) with mixed representations. |
| 53 | +{ |
| 54 | + let cons = ''; |
| 55 | + for (let i = 0; i < 100; i++) cons += `x${i}\u00e9`; |
| 56 | + assert.strictEqual(isLatin1(cons), true); |
| 57 | + assert.strictEqual(isLatin1(cons + '\u0100'), false); |
| 58 | + assert.strictEqual(isLatin1('\u0100' + cons), false); |
| 59 | + assert.strictEqual(isLatin1(cons + '\u0100'.slice(1) + cons), true); |
| 60 | +} |
| 61 | + |
| 62 | +// Randomized comparison with the reference implementation. |
| 63 | +for (let i = 0; i < 1000; i++) { |
| 64 | + const len = Math.floor(Math.random() * 100); |
| 65 | + const max = Math.random() < 0.5 ? 0x100 : 0x10000; |
| 66 | + let str = ''; |
| 67 | + for (let j = 0; j < len; j++) { |
| 68 | + // Keep the probability of producing a code unit > 0xFF low so that |
| 69 | + // both outcomes are exercised. |
| 70 | + const code = Math.random() < 0.98 ? |
| 71 | + Math.floor(Math.random() * 0x100) : |
| 72 | + Math.floor(Math.random() * max); |
| 73 | + str += String.fromCharCode(code); |
| 74 | + } |
| 75 | + assert.strictEqual(isLatin1(str), reference(str), JSON.stringify(str)); |
| 76 | +} |
| 77 | + |
| 78 | +// Invalid argument types. |
| 79 | +[ |
| 80 | + undefined, null, 1, 1n, true, {}, [], Symbol('a'), |
| 81 | + Buffer.from('a'), new Uint8Array(1), new ArrayBuffer(1), |
| 82 | + new String('a'), |
| 83 | +].forEach((input) => { |
| 84 | + assert.throws(() => isLatin1(input), { code: 'ERR_INVALID_ARG_TYPE' }); |
| 85 | +}); |
0 commit comments