11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst b = Buffer.from('abcdef'); 61cb0ef41Sopenharmony_ciconst buf_a = Buffer.from('a'); 71cb0ef41Sopenharmony_ciconst buf_bc = Buffer.from('bc'); 81cb0ef41Sopenharmony_ciconst buf_f = Buffer.from('f'); 91cb0ef41Sopenharmony_ciconst buf_z = Buffer.from('z'); 101cb0ef41Sopenharmony_ciconst buf_empty = Buffer.from(''); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst s = 'abcdef'; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('a'), 0); 151cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('a', 1), -1); 161cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('a', -1), -1); 171cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('a', -4), -1); 181cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('a', -b.length), 0); 191cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('a', NaN), 0); 201cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('a', -Infinity), 0); 211cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('a', Infinity), -1); 221cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('bc'), 1); 231cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('bc', 2), -1); 241cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('bc', -1), -1); 251cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('bc', -3), -1); 261cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('bc', -5), 1); 271cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('bc', NaN), 1); 281cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('bc', -Infinity), 1); 291cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('bc', Infinity), -1); 301cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('f'), b.length - 1); 311cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('z'), -1); 321cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(''), 0); 331cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('', 1), 1); 341cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('', b.length + 1), b.length); 351cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('', Infinity), b.length); 361cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_a), 0); 371cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_a, 1), -1); 381cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_a, -1), -1); 391cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_a, -4), -1); 401cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_a, -b.length), 0); 411cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_a, NaN), 0); 421cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_a, -Infinity), 0); 431cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_a, Infinity), -1); 441cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_bc), 1); 451cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_bc, 2), -1); 461cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_bc, -1), -1); 471cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_bc, -3), -1); 481cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_bc, -5), 1); 491cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_bc, NaN), 1); 501cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_bc, -Infinity), 1); 511cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_bc, Infinity), -1); 521cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_f), b.length - 1); 531cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_z), -1); 541cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_empty), 0); 551cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_empty, 1), 1); 561cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_empty, b.length + 1), b.length); 571cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(buf_empty, Infinity), b.length); 581cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(0x61), 0); 591cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(0x61, 1), -1); 601cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(0x61, -1), -1); 611cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(0x61, -4), -1); 621cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(0x61, -b.length), 0); 631cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(0x61, NaN), 0); 641cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(0x61, -Infinity), 0); 651cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(0x61, Infinity), -1); 661cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(0x0), -1); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci// test offsets 691cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('d', 2), 3); 701cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('f', 5), 5); 711cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('f', -1), 5); 721cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('f', 6), -1); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(Buffer.from('d'), 2), 3); 751cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(Buffer.from('f'), 5), 5); 761cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(Buffer.from('f'), -1), 5); 771cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf(Buffer.from('f'), 6), -1); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('ff').indexOf(Buffer.from('f'), 1, 'ucs2'), -1); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci// Test invalid and uppercase encoding 821cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('b', 'utf8'), 1); 831cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('b', 'UTF8'), 1); 841cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('62', 'HEX'), 1); 851cb0ef41Sopenharmony_ciassert.throws(() => b.indexOf('bad', 'enc'), /Unknown encoding: enc/); 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci// test hex encoding 881cb0ef41Sopenharmony_ciassert.strictEqual( 891cb0ef41Sopenharmony_ci Buffer.from(b.toString('hex'), 'hex') 901cb0ef41Sopenharmony_ci .indexOf('64', 0, 'hex'), 911cb0ef41Sopenharmony_ci 3 921cb0ef41Sopenharmony_ci); 931cb0ef41Sopenharmony_ciassert.strictEqual( 941cb0ef41Sopenharmony_ci Buffer.from(b.toString('hex'), 'hex') 951cb0ef41Sopenharmony_ci .indexOf(Buffer.from('64', 'hex'), 0, 'hex'), 961cb0ef41Sopenharmony_ci 3 971cb0ef41Sopenharmony_ci); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci// Test base64 encoding 1001cb0ef41Sopenharmony_ciassert.strictEqual( 1011cb0ef41Sopenharmony_ci Buffer.from(b.toString('base64'), 'base64') 1021cb0ef41Sopenharmony_ci .indexOf('ZA==', 0, 'base64'), 1031cb0ef41Sopenharmony_ci 3 1041cb0ef41Sopenharmony_ci); 1051cb0ef41Sopenharmony_ciassert.strictEqual( 1061cb0ef41Sopenharmony_ci Buffer.from(b.toString('base64'), 'base64') 1071cb0ef41Sopenharmony_ci .indexOf(Buffer.from('ZA==', 'base64'), 0, 'base64'), 1081cb0ef41Sopenharmony_ci 3 1091cb0ef41Sopenharmony_ci); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci// Test base64url encoding 1121cb0ef41Sopenharmony_ciassert.strictEqual( 1131cb0ef41Sopenharmony_ci Buffer.from(b.toString('base64url'), 'base64url') 1141cb0ef41Sopenharmony_ci .indexOf('ZA==', 0, 'base64url'), 1151cb0ef41Sopenharmony_ci 3 1161cb0ef41Sopenharmony_ci); 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci// test ascii encoding 1191cb0ef41Sopenharmony_ciassert.strictEqual( 1201cb0ef41Sopenharmony_ci Buffer.from(b.toString('ascii'), 'ascii') 1211cb0ef41Sopenharmony_ci .indexOf('d', 0, 'ascii'), 1221cb0ef41Sopenharmony_ci 3 1231cb0ef41Sopenharmony_ci); 1241cb0ef41Sopenharmony_ciassert.strictEqual( 1251cb0ef41Sopenharmony_ci Buffer.from(b.toString('ascii'), 'ascii') 1261cb0ef41Sopenharmony_ci .indexOf(Buffer.from('d', 'ascii'), 0, 'ascii'), 1271cb0ef41Sopenharmony_ci 3 1281cb0ef41Sopenharmony_ci); 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci// Test latin1 encoding 1311cb0ef41Sopenharmony_ciassert.strictEqual( 1321cb0ef41Sopenharmony_ci Buffer.from(b.toString('latin1'), 'latin1') 1331cb0ef41Sopenharmony_ci .indexOf('d', 0, 'latin1'), 1341cb0ef41Sopenharmony_ci 3 1351cb0ef41Sopenharmony_ci); 1361cb0ef41Sopenharmony_ciassert.strictEqual( 1371cb0ef41Sopenharmony_ci Buffer.from(b.toString('latin1'), 'latin1') 1381cb0ef41Sopenharmony_ci .indexOf(Buffer.from('d', 'latin1'), 0, 'latin1'), 1391cb0ef41Sopenharmony_ci 3 1401cb0ef41Sopenharmony_ci); 1411cb0ef41Sopenharmony_ciassert.strictEqual( 1421cb0ef41Sopenharmony_ci Buffer.from('aa\u00e8aa', 'latin1') 1431cb0ef41Sopenharmony_ci .indexOf('\u00e8', 'latin1'), 1441cb0ef41Sopenharmony_ci 2 1451cb0ef41Sopenharmony_ci); 1461cb0ef41Sopenharmony_ciassert.strictEqual( 1471cb0ef41Sopenharmony_ci Buffer.from('\u00e8', 'latin1') 1481cb0ef41Sopenharmony_ci .indexOf('\u00e8', 'latin1'), 1491cb0ef41Sopenharmony_ci 0 1501cb0ef41Sopenharmony_ci); 1511cb0ef41Sopenharmony_ciassert.strictEqual( 1521cb0ef41Sopenharmony_ci Buffer.from('\u00e8', 'latin1') 1531cb0ef41Sopenharmony_ci .indexOf(Buffer.from('\u00e8', 'latin1'), 'latin1'), 1541cb0ef41Sopenharmony_ci 0 1551cb0ef41Sopenharmony_ci); 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci// Test binary encoding 1581cb0ef41Sopenharmony_ciassert.strictEqual( 1591cb0ef41Sopenharmony_ci Buffer.from(b.toString('binary'), 'binary') 1601cb0ef41Sopenharmony_ci .indexOf('d', 0, 'binary'), 1611cb0ef41Sopenharmony_ci 3 1621cb0ef41Sopenharmony_ci); 1631cb0ef41Sopenharmony_ciassert.strictEqual( 1641cb0ef41Sopenharmony_ci Buffer.from(b.toString('binary'), 'binary') 1651cb0ef41Sopenharmony_ci .indexOf(Buffer.from('d', 'binary'), 0, 'binary'), 1661cb0ef41Sopenharmony_ci 3 1671cb0ef41Sopenharmony_ci); 1681cb0ef41Sopenharmony_ciassert.strictEqual( 1691cb0ef41Sopenharmony_ci Buffer.from('aa\u00e8aa', 'binary') 1701cb0ef41Sopenharmony_ci .indexOf('\u00e8', 'binary'), 1711cb0ef41Sopenharmony_ci 2 1721cb0ef41Sopenharmony_ci); 1731cb0ef41Sopenharmony_ciassert.strictEqual( 1741cb0ef41Sopenharmony_ci Buffer.from('\u00e8', 'binary') 1751cb0ef41Sopenharmony_ci .indexOf('\u00e8', 'binary'), 1761cb0ef41Sopenharmony_ci 0 1771cb0ef41Sopenharmony_ci); 1781cb0ef41Sopenharmony_ciassert.strictEqual( 1791cb0ef41Sopenharmony_ci Buffer.from('\u00e8', 'binary') 1801cb0ef41Sopenharmony_ci .indexOf(Buffer.from('\u00e8', 'binary'), 'binary'), 1811cb0ef41Sopenharmony_ci 0 1821cb0ef41Sopenharmony_ci); 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci// Test optional offset with passed encoding 1861cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('aaaa0').indexOf('30', 'hex'), 4); 1871cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4); 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci{ 1901cb0ef41Sopenharmony_ci // Test usc2 and utf16le encoding 1911cb0ef41Sopenharmony_ci ['ucs2', 'utf16le'].forEach((encoding) => { 1921cb0ef41Sopenharmony_ci const twoByteString = Buffer.from( 1931cb0ef41Sopenharmony_ci '\u039a\u0391\u03a3\u03a3\u0395', encoding); 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci assert.strictEqual(twoByteString.indexOf('\u0395', 4, encoding), 8); 1961cb0ef41Sopenharmony_ci assert.strictEqual(twoByteString.indexOf('\u03a3', -4, encoding), 6); 1971cb0ef41Sopenharmony_ci assert.strictEqual(twoByteString.indexOf('\u03a3', -6, encoding), 4); 1981cb0ef41Sopenharmony_ci assert.strictEqual(twoByteString.indexOf( 1991cb0ef41Sopenharmony_ci Buffer.from('\u03a3', encoding), -6, encoding), 4); 2001cb0ef41Sopenharmony_ci assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, encoding)); 2011cb0ef41Sopenharmony_ci }); 2021cb0ef41Sopenharmony_ci} 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ciconst mixedByteStringUcs2 = 2051cb0ef41Sopenharmony_ci Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2'); 2061cb0ef41Sopenharmony_ciassert.strictEqual(mixedByteStringUcs2.indexOf('bc', 0, 'ucs2'), 6); 2071cb0ef41Sopenharmony_ciassert.strictEqual(mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2'), 10); 2081cb0ef41Sopenharmony_ciassert.strictEqual(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2')); 2091cb0ef41Sopenharmony_ci 2101cb0ef41Sopenharmony_ciassert.strictEqual( 2111cb0ef41Sopenharmony_ci mixedByteStringUcs2.indexOf(Buffer.from('bc', 'ucs2'), 0, 'ucs2'), 6); 2121cb0ef41Sopenharmony_ciassert.strictEqual( 2131cb0ef41Sopenharmony_ci mixedByteStringUcs2.indexOf(Buffer.from('\u03a3', 'ucs2'), 0, 'ucs2'), 10); 2141cb0ef41Sopenharmony_ciassert.strictEqual( 2151cb0ef41Sopenharmony_ci -1, mixedByteStringUcs2.indexOf(Buffer.from('\u0396', 'ucs2'), 0, 'ucs2')); 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci{ 2181cb0ef41Sopenharmony_ci const twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_ci // Test single char pattern 2211cb0ef41Sopenharmony_ci assert.strictEqual(twoByteString.indexOf('\u039a', 0, 'ucs2'), 0); 2221cb0ef41Sopenharmony_ci let index = twoByteString.indexOf('\u0391', 0, 'ucs2'); 2231cb0ef41Sopenharmony_ci assert.strictEqual(index, 2, `Alpha - at index ${index}`); 2241cb0ef41Sopenharmony_ci index = twoByteString.indexOf('\u03a3', 0, 'ucs2'); 2251cb0ef41Sopenharmony_ci assert.strictEqual(index, 4, `First Sigma - at index ${index}`); 2261cb0ef41Sopenharmony_ci index = twoByteString.indexOf('\u03a3', 6, 'ucs2'); 2271cb0ef41Sopenharmony_ci assert.strictEqual(index, 6, `Second Sigma - at index ${index}`); 2281cb0ef41Sopenharmony_ci index = twoByteString.indexOf('\u0395', 0, 'ucs2'); 2291cb0ef41Sopenharmony_ci assert.strictEqual(index, 8, `Epsilon - at index ${index}`); 2301cb0ef41Sopenharmony_ci index = twoByteString.indexOf('\u0392', 0, 'ucs2'); 2311cb0ef41Sopenharmony_ci assert.strictEqual(-1, index, `Not beta - at index ${index}`); 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci // Test multi-char pattern 2341cb0ef41Sopenharmony_ci index = twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'); 2351cb0ef41Sopenharmony_ci assert.strictEqual(index, 0, `Lambda Alpha - at index ${index}`); 2361cb0ef41Sopenharmony_ci index = twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'); 2371cb0ef41Sopenharmony_ci assert.strictEqual(index, 2, `Alpha Sigma - at index ${index}`); 2381cb0ef41Sopenharmony_ci index = twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'); 2391cb0ef41Sopenharmony_ci assert.strictEqual(index, 4, `Sigma Sigma - at index ${index}`); 2401cb0ef41Sopenharmony_ci index = twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'); 2411cb0ef41Sopenharmony_ci assert.strictEqual(index, 6, `Sigma Epsilon - at index ${index}`); 2421cb0ef41Sopenharmony_ci} 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ciconst mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395'); 2451cb0ef41Sopenharmony_ciassert.strictEqual(mixedByteStringUtf8.indexOf('bc'), 5); 2461cb0ef41Sopenharmony_ciassert.strictEqual(mixedByteStringUtf8.indexOf('bc', 5), 5); 2471cb0ef41Sopenharmony_ciassert.strictEqual(mixedByteStringUtf8.indexOf('bc', -8), 5); 2481cb0ef41Sopenharmony_ciassert.strictEqual(mixedByteStringUtf8.indexOf('\u03a3'), 7); 2491cb0ef41Sopenharmony_ciassert.strictEqual(mixedByteStringUtf8.indexOf('\u0396'), -1); 2501cb0ef41Sopenharmony_ci 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci// Test complex string indexOf algorithms. Only trigger for long strings. 2531cb0ef41Sopenharmony_ci// Long string that isn't a simple repeat of a shorter string. 2541cb0ef41Sopenharmony_cilet longString = 'A'; 2551cb0ef41Sopenharmony_cifor (let i = 66; i < 76; i++) { // from 'B' to 'K' 2561cb0ef41Sopenharmony_ci longString = longString + String.fromCharCode(i) + longString; 2571cb0ef41Sopenharmony_ci} 2581cb0ef41Sopenharmony_ci 2591cb0ef41Sopenharmony_ciconst longBufferString = Buffer.from(longString); 2601cb0ef41Sopenharmony_ci 2611cb0ef41Sopenharmony_ci// Pattern of 15 chars, repeated every 16 chars in long 2621cb0ef41Sopenharmony_cilet pattern = 'ABACABADABACABA'; 2631cb0ef41Sopenharmony_cifor (let i = 0; i < longBufferString.length - pattern.length; i += 7) { 2641cb0ef41Sopenharmony_ci const index = longBufferString.indexOf(pattern, i); 2651cb0ef41Sopenharmony_ci assert.strictEqual((i + 15) & ~0xf, index, 2661cb0ef41Sopenharmony_ci `Long ABACABA...-string at index ${i}`); 2671cb0ef41Sopenharmony_ci} 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_cilet index = longBufferString.indexOf('AJABACA'); 2701cb0ef41Sopenharmony_ciassert.strictEqual(index, 510, `Long AJABACA, First J - at index ${index}`); 2711cb0ef41Sopenharmony_ciindex = longBufferString.indexOf('AJABACA', 511); 2721cb0ef41Sopenharmony_ciassert.strictEqual(index, 1534, `Long AJABACA, Second J - at index ${index}`); 2731cb0ef41Sopenharmony_ci 2741cb0ef41Sopenharmony_cipattern = 'JABACABADABACABA'; 2751cb0ef41Sopenharmony_ciindex = longBufferString.indexOf(pattern); 2761cb0ef41Sopenharmony_ciassert.strictEqual(index, 511, `Long JABACABA..., First J - at index ${index}`); 2771cb0ef41Sopenharmony_ciindex = longBufferString.indexOf(pattern, 512); 2781cb0ef41Sopenharmony_ciassert.strictEqual( 2791cb0ef41Sopenharmony_ci index, 1535, `Long JABACABA..., Second J - at index ${index}`); 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci// Search for a non-ASCII string in a pure ASCII string. 2821cb0ef41Sopenharmony_ciconst asciiString = Buffer.from( 2831cb0ef41Sopenharmony_ci 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf'); 2841cb0ef41Sopenharmony_ciassert.strictEqual(-1, asciiString.indexOf('\x2061')); 2851cb0ef41Sopenharmony_ciassert.strictEqual(asciiString.indexOf('leb', 0), 3); 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ci// Search in string containing many non-ASCII chars. 2881cb0ef41Sopenharmony_ciconst allCodePoints = []; 2891cb0ef41Sopenharmony_cifor (let i = 0; i < 65534; i++) allCodePoints[i] = i; 2901cb0ef41Sopenharmony_ciconst allCharsString = String.fromCharCode.apply(String, allCodePoints) + 2911cb0ef41Sopenharmony_ci String.fromCharCode(65534, 65535); 2921cb0ef41Sopenharmony_ciconst allCharsBufferUtf8 = Buffer.from(allCharsString); 2931cb0ef41Sopenharmony_ciconst allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2'); 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_ci// Search for string long enough to trigger complex search with ASCII pattern 2961cb0ef41Sopenharmony_ci// and UC16 subject. 2971cb0ef41Sopenharmony_ciassert.strictEqual(-1, allCharsBufferUtf8.indexOf('notfound')); 2981cb0ef41Sopenharmony_ciassert.strictEqual(-1, allCharsBufferUcs2.indexOf('notfound')); 2991cb0ef41Sopenharmony_ci 3001cb0ef41Sopenharmony_ci// Needle is longer than haystack, but only because it's encoded as UTF-16 3011cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1); 3021cb0ef41Sopenharmony_ci 3031cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'utf8'), 0); 3041cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('aaaa').indexOf('你好', 'ucs2'), -1); 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_ci// Haystack has odd length, but the needle is UCS2. 3071cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1); 3081cb0ef41Sopenharmony_ci 3091cb0ef41Sopenharmony_ci{ 3101cb0ef41Sopenharmony_ci // Find substrings in Utf8. 3111cb0ef41Sopenharmony_ci const lengths = [1, 3, 15]; // Single char, simple and complex. 3121cb0ef41Sopenharmony_ci const indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b]; 3131cb0ef41Sopenharmony_ci for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { 3141cb0ef41Sopenharmony_ci for (let i = 0; i < indices.length; i++) { 3151cb0ef41Sopenharmony_ci const index = indices[i]; 3161cb0ef41Sopenharmony_ci let length = lengths[lengthIndex]; 3171cb0ef41Sopenharmony_ci 3181cb0ef41Sopenharmony_ci if (index + length > 0x7F) { 3191cb0ef41Sopenharmony_ci length = 2 * length; 3201cb0ef41Sopenharmony_ci } 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ci if (index + length > 0x7FF) { 3231cb0ef41Sopenharmony_ci length = 3 * length; 3241cb0ef41Sopenharmony_ci } 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ci if (index + length > 0xFFFF) { 3271cb0ef41Sopenharmony_ci length = 4 * length; 3281cb0ef41Sopenharmony_ci } 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci const patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length); 3311cb0ef41Sopenharmony_ci assert.strictEqual(index, allCharsBufferUtf8.indexOf(patternBufferUtf8)); 3321cb0ef41Sopenharmony_ci 3331cb0ef41Sopenharmony_ci const patternStringUtf8 = patternBufferUtf8.toString(); 3341cb0ef41Sopenharmony_ci assert.strictEqual(index, allCharsBufferUtf8.indexOf(patternStringUtf8)); 3351cb0ef41Sopenharmony_ci } 3361cb0ef41Sopenharmony_ci } 3371cb0ef41Sopenharmony_ci} 3381cb0ef41Sopenharmony_ci 3391cb0ef41Sopenharmony_ci{ 3401cb0ef41Sopenharmony_ci // Find substrings in Usc2. 3411cb0ef41Sopenharmony_ci const lengths = [2, 4, 16]; // Single char, simple and complex. 3421cb0ef41Sopenharmony_ci const indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0]; 3431cb0ef41Sopenharmony_ci for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { 3441cb0ef41Sopenharmony_ci for (let i = 0; i < indices.length; i++) { 3451cb0ef41Sopenharmony_ci const index = indices[i] * 2; 3461cb0ef41Sopenharmony_ci const length = lengths[lengthIndex]; 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_ci const patternBufferUcs2 = 3491cb0ef41Sopenharmony_ci allCharsBufferUcs2.slice(index, index + length); 3501cb0ef41Sopenharmony_ci assert.strictEqual( 3511cb0ef41Sopenharmony_ci index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2')); 3521cb0ef41Sopenharmony_ci 3531cb0ef41Sopenharmony_ci const patternStringUcs2 = patternBufferUcs2.toString('ucs2'); 3541cb0ef41Sopenharmony_ci assert.strictEqual( 3551cb0ef41Sopenharmony_ci index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2')); 3561cb0ef41Sopenharmony_ci } 3571cb0ef41Sopenharmony_ci } 3581cb0ef41Sopenharmony_ci} 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_ci[ 3611cb0ef41Sopenharmony_ci () => {}, 3621cb0ef41Sopenharmony_ci {}, 3631cb0ef41Sopenharmony_ci [], 3641cb0ef41Sopenharmony_ci].forEach((val) => { 3651cb0ef41Sopenharmony_ci assert.throws( 3661cb0ef41Sopenharmony_ci () => b.indexOf(val), 3671cb0ef41Sopenharmony_ci { 3681cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 3691cb0ef41Sopenharmony_ci name: 'TypeError', 3701cb0ef41Sopenharmony_ci message: 'The "value" argument must be one of type number or string ' + 3711cb0ef41Sopenharmony_ci 'or an instance of Buffer or Uint8Array.' + 3721cb0ef41Sopenharmony_ci common.invalidArgTypeHelper(val) 3731cb0ef41Sopenharmony_ci } 3741cb0ef41Sopenharmony_ci ); 3751cb0ef41Sopenharmony_ci}); 3761cb0ef41Sopenharmony_ci 3771cb0ef41Sopenharmony_ci// Test weird offset arguments. 3781cb0ef41Sopenharmony_ci// The following offsets coerce to NaN or 0, searching the whole Buffer 3791cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('b', undefined), 1); 3801cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('b', {}), 1); 3811cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('b', 0), 1); 3821cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('b', null), 1); 3831cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('b', []), 1); 3841cb0ef41Sopenharmony_ci 3851cb0ef41Sopenharmony_ci// The following offset coerces to 2, in other words +[2] === 2 3861cb0ef41Sopenharmony_ciassert.strictEqual(b.indexOf('b', [2]), -1); 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ci// Behavior should match String.indexOf() 3891cb0ef41Sopenharmony_ciassert.strictEqual( 3901cb0ef41Sopenharmony_ci b.indexOf('b', undefined), 3911cb0ef41Sopenharmony_ci s.indexOf('b', undefined)); 3921cb0ef41Sopenharmony_ciassert.strictEqual( 3931cb0ef41Sopenharmony_ci b.indexOf('b', {}), 3941cb0ef41Sopenharmony_ci s.indexOf('b', {})); 3951cb0ef41Sopenharmony_ciassert.strictEqual( 3961cb0ef41Sopenharmony_ci b.indexOf('b', 0), 3971cb0ef41Sopenharmony_ci s.indexOf('b', 0)); 3981cb0ef41Sopenharmony_ciassert.strictEqual( 3991cb0ef41Sopenharmony_ci b.indexOf('b', null), 4001cb0ef41Sopenharmony_ci s.indexOf('b', null)); 4011cb0ef41Sopenharmony_ciassert.strictEqual( 4021cb0ef41Sopenharmony_ci b.indexOf('b', []), 4031cb0ef41Sopenharmony_ci s.indexOf('b', [])); 4041cb0ef41Sopenharmony_ciassert.strictEqual( 4051cb0ef41Sopenharmony_ci b.indexOf('b', [2]), 4061cb0ef41Sopenharmony_ci s.indexOf('b', [2])); 4071cb0ef41Sopenharmony_ci 4081cb0ef41Sopenharmony_ci// All code for handling encodings is shared between Buffer.indexOf and 4091cb0ef41Sopenharmony_ci// Buffer.lastIndexOf, so only testing the separate lastIndexOf semantics. 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ci// Test lastIndexOf basic functionality; Buffer b contains 'abcdef'. 4121cb0ef41Sopenharmony_ci// lastIndexOf string: 4131cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('a'), 0); 4141cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('a', 1), 0); 4151cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('b', 1), 1); 4161cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('c', 1), -1); 4171cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('a', -1), 0); 4181cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('a', -4), 0); 4191cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('a', -b.length), 0); 4201cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('a', -b.length - 1), -1); 4211cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('a', NaN), 0); 4221cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('a', -Infinity), -1); 4231cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('a', Infinity), 0); 4241cb0ef41Sopenharmony_ci// lastIndexOf Buffer: 4251cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_a), 0); 4261cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_a, 1), 0); 4271cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_a, -1), 0); 4281cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_a, -4), 0); 4291cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_a, -b.length), 0); 4301cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_a, -b.length - 1), -1); 4311cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_a, NaN), 0); 4321cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_a, -Infinity), -1); 4331cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_a, Infinity), 0); 4341cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_bc), 1); 4351cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_bc, 2), 1); 4361cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_bc, -1), 1); 4371cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_bc, -3), 1); 4381cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_bc, -5), 1); 4391cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_bc, -6), -1); 4401cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_bc, NaN), 1); 4411cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_bc, -Infinity), -1); 4421cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_bc, Infinity), 1); 4431cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_f), b.length - 1); 4441cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_z), -1); 4451cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_empty), b.length); 4461cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_empty, 1), 1); 4471cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_empty, b.length + 1), b.length); 4481cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(buf_empty, Infinity), b.length); 4491cb0ef41Sopenharmony_ci// lastIndexOf number: 4501cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(0x61), 0); 4511cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(0x61, 1), 0); 4521cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(0x61, -1), 0); 4531cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(0x61, -4), 0); 4541cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(0x61, -b.length), 0); 4551cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(0x61, -b.length - 1), -1); 4561cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(0x61, NaN), 0); 4571cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(0x61, -Infinity), -1); 4581cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(0x61, Infinity), 0); 4591cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(0x0), -1); 4601cb0ef41Sopenharmony_ci 4611cb0ef41Sopenharmony_ci// Test weird offset arguments. 4621cb0ef41Sopenharmony_ci// The following offsets coerce to NaN, searching the whole Buffer 4631cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('b', undefined), 1); 4641cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('b', {}), 1); 4651cb0ef41Sopenharmony_ci 4661cb0ef41Sopenharmony_ci// The following offsets coerce to 0 4671cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('b', 0), -1); 4681cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('b', null), -1); 4691cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('b', []), -1); 4701cb0ef41Sopenharmony_ci 4711cb0ef41Sopenharmony_ci// The following offset coerces to 2, in other words +[2] === 2 4721cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('b', [2]), 1); 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_ci// Behavior should match String.lastIndexOf() 4751cb0ef41Sopenharmony_ciassert.strictEqual( 4761cb0ef41Sopenharmony_ci b.lastIndexOf('b', undefined), 4771cb0ef41Sopenharmony_ci s.lastIndexOf('b', undefined)); 4781cb0ef41Sopenharmony_ciassert.strictEqual( 4791cb0ef41Sopenharmony_ci b.lastIndexOf('b', {}), 4801cb0ef41Sopenharmony_ci s.lastIndexOf('b', {})); 4811cb0ef41Sopenharmony_ciassert.strictEqual( 4821cb0ef41Sopenharmony_ci b.lastIndexOf('b', 0), 4831cb0ef41Sopenharmony_ci s.lastIndexOf('b', 0)); 4841cb0ef41Sopenharmony_ciassert.strictEqual( 4851cb0ef41Sopenharmony_ci b.lastIndexOf('b', null), 4861cb0ef41Sopenharmony_ci s.lastIndexOf('b', null)); 4871cb0ef41Sopenharmony_ciassert.strictEqual( 4881cb0ef41Sopenharmony_ci b.lastIndexOf('b', []), 4891cb0ef41Sopenharmony_ci s.lastIndexOf('b', [])); 4901cb0ef41Sopenharmony_ciassert.strictEqual( 4911cb0ef41Sopenharmony_ci b.lastIndexOf('b', [2]), 4921cb0ef41Sopenharmony_ci s.lastIndexOf('b', [2])); 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci// Test needles longer than the haystack. 4951cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'ucs2'), -1); 4961cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'utf8'), -1); 4971cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'latin1'), -1); 4981cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'binary'), -1); 4991cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(Buffer.from('aaaaaaaaaaaaaaa')), -1); 5001cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 2, 'ucs2'), -1); 5011cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 3, 'utf8'), -1); 5021cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 5, 'latin1'), -1); 5031cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 5, 'binary'), -1); 5041cb0ef41Sopenharmony_ciassert.strictEqual(b.lastIndexOf(Buffer.from('aaaaaaaaaaaaaaa'), 7), -1); 5051cb0ef41Sopenharmony_ci 5061cb0ef41Sopenharmony_ci// 你好 expands to a total of 6 bytes using UTF-8 and 4 bytes using UTF-16 5071cb0ef41Sopenharmony_ciassert.strictEqual(buf_bc.lastIndexOf('你好', 'ucs2'), -1); 5081cb0ef41Sopenharmony_ciassert.strictEqual(buf_bc.lastIndexOf('你好', 'utf8'), -1); 5091cb0ef41Sopenharmony_ciassert.strictEqual(buf_bc.lastIndexOf('你好', 'latin1'), -1); 5101cb0ef41Sopenharmony_ciassert.strictEqual(buf_bc.lastIndexOf('你好', 'binary'), -1); 5111cb0ef41Sopenharmony_ciassert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好')), -1); 5121cb0ef41Sopenharmony_ciassert.strictEqual(buf_bc.lastIndexOf('你好', 2, 'ucs2'), -1); 5131cb0ef41Sopenharmony_ciassert.strictEqual(buf_bc.lastIndexOf('你好', 3, 'utf8'), -1); 5141cb0ef41Sopenharmony_ciassert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'latin1'), -1); 5151cb0ef41Sopenharmony_ciassert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'binary'), -1); 5161cb0ef41Sopenharmony_ciassert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好'), 7), -1); 5171cb0ef41Sopenharmony_ci 5181cb0ef41Sopenharmony_ci// Test lastIndexOf on a longer buffer: 5191cb0ef41Sopenharmony_ciconst bufferString = Buffer.from('a man a plan a canal panama'); 5201cb0ef41Sopenharmony_ciassert.strictEqual(bufferString.lastIndexOf('canal'), 15); 5211cb0ef41Sopenharmony_ciassert.strictEqual(bufferString.lastIndexOf('panama'), 21); 5221cb0ef41Sopenharmony_ciassert.strictEqual(bufferString.lastIndexOf('a man a plan a canal panama'), 0); 5231cb0ef41Sopenharmony_ciassert.strictEqual(-1, bufferString.lastIndexOf('a man a plan a canal mexico')); 5241cb0ef41Sopenharmony_ciassert.strictEqual(-1, bufferString 5251cb0ef41Sopenharmony_ci .lastIndexOf('a man a plan a canal mexico city')); 5261cb0ef41Sopenharmony_ciassert.strictEqual(-1, bufferString.lastIndexOf(Buffer.from('a'.repeat(1000)))); 5271cb0ef41Sopenharmony_ciassert.strictEqual(bufferString.lastIndexOf('a man a plan', 4), 0); 5281cb0ef41Sopenharmony_ciassert.strictEqual(bufferString.lastIndexOf('a '), 13); 5291cb0ef41Sopenharmony_ciassert.strictEqual(bufferString.lastIndexOf('a ', 13), 13); 5301cb0ef41Sopenharmony_ciassert.strictEqual(bufferString.lastIndexOf('a ', 12), 6); 5311cb0ef41Sopenharmony_ciassert.strictEqual(bufferString.lastIndexOf('a ', 5), 0); 5321cb0ef41Sopenharmony_ciassert.strictEqual(bufferString.lastIndexOf('a ', -1), 13); 5331cb0ef41Sopenharmony_ciassert.strictEqual(bufferString.lastIndexOf('a ', -27), 0); 5341cb0ef41Sopenharmony_ciassert.strictEqual(-1, bufferString.lastIndexOf('a ', -28)); 5351cb0ef41Sopenharmony_ci 5361cb0ef41Sopenharmony_ci// Test lastIndexOf for the case that the first character can be found, 5371cb0ef41Sopenharmony_ci// but in a part of the buffer that does not make search to search 5381cb0ef41Sopenharmony_ci// due do length constraints. 5391cb0ef41Sopenharmony_ciconst abInUCS2 = Buffer.from('ab', 'ucs2'); 5401cb0ef41Sopenharmony_ciassert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'latin1').lastIndexOf('µ')); 5411cb0ef41Sopenharmony_ciassert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'binary').lastIndexOf('µ')); 5421cb0ef41Sopenharmony_ciassert.strictEqual(-1, Buffer.from('bc').lastIndexOf('ab')); 5431cb0ef41Sopenharmony_ciassert.strictEqual(-1, Buffer.from('abc').lastIndexOf('qa')); 5441cb0ef41Sopenharmony_ciassert.strictEqual(-1, Buffer.from('abcdef').lastIndexOf('qabc')); 5451cb0ef41Sopenharmony_ciassert.strictEqual(-1, Buffer.from('bc').lastIndexOf(Buffer.from('ab'))); 5461cb0ef41Sopenharmony_ciassert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf('ab', 'ucs2')); 5471cb0ef41Sopenharmony_ciassert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf(abInUCS2)); 5481cb0ef41Sopenharmony_ci 5491cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('abc').lastIndexOf('ab'), 0); 5501cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('abc').lastIndexOf('ab', 1), 0); 5511cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('abc').lastIndexOf('ab', 2), 0); 5521cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('abc').lastIndexOf('ab', 3), 0); 5531cb0ef41Sopenharmony_ci 5541cb0ef41Sopenharmony_ci// The above tests test the LINEAR and SINGLE-CHAR strategies. 5551cb0ef41Sopenharmony_ci// Now, we test the BOYER-MOORE-HORSPOOL strategy. 5561cb0ef41Sopenharmony_ci// Test lastIndexOf on a long buffer w multiple matches: 5571cb0ef41Sopenharmony_cipattern = 'JABACABADABACABA'; 5581cb0ef41Sopenharmony_ciassert.strictEqual(longBufferString.lastIndexOf(pattern), 1535); 5591cb0ef41Sopenharmony_ciassert.strictEqual(longBufferString.lastIndexOf(pattern, 1535), 1535); 5601cb0ef41Sopenharmony_ciassert.strictEqual(longBufferString.lastIndexOf(pattern, 1534), 511); 5611cb0ef41Sopenharmony_ci 5621cb0ef41Sopenharmony_ci// Finally, give it a really long input to trigger fallback from BMH to 5631cb0ef41Sopenharmony_ci// regular BOYER-MOORE (which has better worst-case complexity). 5641cb0ef41Sopenharmony_ci 5651cb0ef41Sopenharmony_ci// Generate a really long Thue-Morse sequence of 'yolo' and 'swag', 5661cb0ef41Sopenharmony_ci// "yolo swag swag yolo swag yolo yolo swag" ..., goes on for about 5MB. 5671cb0ef41Sopenharmony_ci// This is hard to search because it all looks similar, but never repeats. 5681cb0ef41Sopenharmony_ci 5691cb0ef41Sopenharmony_ci// countBits returns the number of bits in the binary representation of n. 5701cb0ef41Sopenharmony_cifunction countBits(n) { 5711cb0ef41Sopenharmony_ci let count; 5721cb0ef41Sopenharmony_ci for (count = 0; n > 0; count++) { 5731cb0ef41Sopenharmony_ci n = n & (n - 1); // remove top bit 5741cb0ef41Sopenharmony_ci } 5751cb0ef41Sopenharmony_ci return count; 5761cb0ef41Sopenharmony_ci} 5771cb0ef41Sopenharmony_ciconst parts = []; 5781cb0ef41Sopenharmony_cifor (let i = 0; i < 1000000; i++) { 5791cb0ef41Sopenharmony_ci parts.push((countBits(i) % 2 === 0) ? 'yolo' : 'swag'); 5801cb0ef41Sopenharmony_ci} 5811cb0ef41Sopenharmony_ciconst reallyLong = Buffer.from(parts.join(' ')); 5821cb0ef41Sopenharmony_ciassert.strictEqual(reallyLong.slice(0, 19).toString(), 'yolo swag swag yolo'); 5831cb0ef41Sopenharmony_ci 5841cb0ef41Sopenharmony_ci// Expensive reverse searches. Stress test lastIndexOf: 5851cb0ef41Sopenharmony_cipattern = reallyLong.slice(0, 100000); // First 1/50th of the pattern. 5861cb0ef41Sopenharmony_ciassert.strictEqual(reallyLong.lastIndexOf(pattern), 4751360); 5871cb0ef41Sopenharmony_ciassert.strictEqual(reallyLong.lastIndexOf(pattern, 4000000), 3932160); 5881cb0ef41Sopenharmony_ciassert.strictEqual(reallyLong.lastIndexOf(pattern, 3000000), 2949120); 5891cb0ef41Sopenharmony_cipattern = reallyLong.slice(100000, 200000); // Second 1/50th. 5901cb0ef41Sopenharmony_ciassert.strictEqual(reallyLong.lastIndexOf(pattern), 4728480); 5911cb0ef41Sopenharmony_cipattern = reallyLong.slice(0, 1000000); // First 1/5th. 5921cb0ef41Sopenharmony_ciassert.strictEqual(reallyLong.lastIndexOf(pattern), 3932160); 5931cb0ef41Sopenharmony_cipattern = reallyLong.slice(0, 2000000); // first 2/5ths. 5941cb0ef41Sopenharmony_ciassert.strictEqual(reallyLong.lastIndexOf(pattern), 0); 5951cb0ef41Sopenharmony_ci 5961cb0ef41Sopenharmony_ci// Test truncation of Number arguments to uint8 5971cb0ef41Sopenharmony_ci{ 5981cb0ef41Sopenharmony_ci const buf = Buffer.from('this is a test'); 5991cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(0x6973), 3); 6001cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(0x697320), 4); 6011cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(0x69732069), 2); 6021cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(0x697374657374), 0); 6031cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(0x69737374), 0); 6041cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(0x69737465), 11); 6051cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(0x69737465), 11); 6061cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(-140), 0); 6071cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(-152), 1); 6081cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(0xff), -1); 6091cb0ef41Sopenharmony_ci assert.strictEqual(buf.indexOf(0xffff), -1); 6101cb0ef41Sopenharmony_ci} 6111cb0ef41Sopenharmony_ci 6121cb0ef41Sopenharmony_ci// Test that Uint8Array arguments are okay. 6131cb0ef41Sopenharmony_ci{ 6141cb0ef41Sopenharmony_ci const needle = new Uint8Array([ 0x66, 0x6f, 0x6f ]); 6151cb0ef41Sopenharmony_ci const haystack = Buffer.from('a foo b foo'); 6161cb0ef41Sopenharmony_ci assert.strictEqual(haystack.indexOf(needle), 2); 6171cb0ef41Sopenharmony_ci assert.strictEqual(haystack.lastIndexOf(needle), haystack.length - 3); 6181cb0ef41Sopenharmony_ci} 6191cb0ef41Sopenharmony_ci 6201cb0ef41Sopenharmony_ci// Avoid abort because of invalid usage 6211cb0ef41Sopenharmony_ci// see https://github.com/nodejs/node/issues/32753 6221cb0ef41Sopenharmony_ci{ 6231cb0ef41Sopenharmony_ci assert.throws(() => { 6241cb0ef41Sopenharmony_ci const buffer = require('buffer'); 6251cb0ef41Sopenharmony_ci new buffer.Buffer.prototype.lastIndexOf(1, 'str'); 6261cb0ef41Sopenharmony_ci }, { 6271cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 6281cb0ef41Sopenharmony_ci name: 'TypeError', 6291cb0ef41Sopenharmony_ci message: 'The "buffer" argument must be an instance of Buffer, ' + 6301cb0ef41Sopenharmony_ci 'TypedArray, or DataView. ' + 6311cb0ef41Sopenharmony_ci 'Received an instance of lastIndexOf' 6321cb0ef41Sopenharmony_ci }); 6331cb0ef41Sopenharmony_ci} 634