1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4 5const b = Buffer.from('abcdef'); 6const buf_a = Buffer.from('a'); 7const buf_bc = Buffer.from('bc'); 8const buf_f = Buffer.from('f'); 9const buf_z = Buffer.from('z'); 10const buf_empty = Buffer.from(''); 11 12const s = 'abcdef'; 13 14assert.strictEqual(b.indexOf('a'), 0); 15assert.strictEqual(b.indexOf('a', 1), -1); 16assert.strictEqual(b.indexOf('a', -1), -1); 17assert.strictEqual(b.indexOf('a', -4), -1); 18assert.strictEqual(b.indexOf('a', -b.length), 0); 19assert.strictEqual(b.indexOf('a', NaN), 0); 20assert.strictEqual(b.indexOf('a', -Infinity), 0); 21assert.strictEqual(b.indexOf('a', Infinity), -1); 22assert.strictEqual(b.indexOf('bc'), 1); 23assert.strictEqual(b.indexOf('bc', 2), -1); 24assert.strictEqual(b.indexOf('bc', -1), -1); 25assert.strictEqual(b.indexOf('bc', -3), -1); 26assert.strictEqual(b.indexOf('bc', -5), 1); 27assert.strictEqual(b.indexOf('bc', NaN), 1); 28assert.strictEqual(b.indexOf('bc', -Infinity), 1); 29assert.strictEqual(b.indexOf('bc', Infinity), -1); 30assert.strictEqual(b.indexOf('f'), b.length - 1); 31assert.strictEqual(b.indexOf('z'), -1); 32assert.strictEqual(b.indexOf(''), 0); 33assert.strictEqual(b.indexOf('', 1), 1); 34assert.strictEqual(b.indexOf('', b.length + 1), b.length); 35assert.strictEqual(b.indexOf('', Infinity), b.length); 36assert.strictEqual(b.indexOf(buf_a), 0); 37assert.strictEqual(b.indexOf(buf_a, 1), -1); 38assert.strictEqual(b.indexOf(buf_a, -1), -1); 39assert.strictEqual(b.indexOf(buf_a, -4), -1); 40assert.strictEqual(b.indexOf(buf_a, -b.length), 0); 41assert.strictEqual(b.indexOf(buf_a, NaN), 0); 42assert.strictEqual(b.indexOf(buf_a, -Infinity), 0); 43assert.strictEqual(b.indexOf(buf_a, Infinity), -1); 44assert.strictEqual(b.indexOf(buf_bc), 1); 45assert.strictEqual(b.indexOf(buf_bc, 2), -1); 46assert.strictEqual(b.indexOf(buf_bc, -1), -1); 47assert.strictEqual(b.indexOf(buf_bc, -3), -1); 48assert.strictEqual(b.indexOf(buf_bc, -5), 1); 49assert.strictEqual(b.indexOf(buf_bc, NaN), 1); 50assert.strictEqual(b.indexOf(buf_bc, -Infinity), 1); 51assert.strictEqual(b.indexOf(buf_bc, Infinity), -1); 52assert.strictEqual(b.indexOf(buf_f), b.length - 1); 53assert.strictEqual(b.indexOf(buf_z), -1); 54assert.strictEqual(b.indexOf(buf_empty), 0); 55assert.strictEqual(b.indexOf(buf_empty, 1), 1); 56assert.strictEqual(b.indexOf(buf_empty, b.length + 1), b.length); 57assert.strictEqual(b.indexOf(buf_empty, Infinity), b.length); 58assert.strictEqual(b.indexOf(0x61), 0); 59assert.strictEqual(b.indexOf(0x61, 1), -1); 60assert.strictEqual(b.indexOf(0x61, -1), -1); 61assert.strictEqual(b.indexOf(0x61, -4), -1); 62assert.strictEqual(b.indexOf(0x61, -b.length), 0); 63assert.strictEqual(b.indexOf(0x61, NaN), 0); 64assert.strictEqual(b.indexOf(0x61, -Infinity), 0); 65assert.strictEqual(b.indexOf(0x61, Infinity), -1); 66assert.strictEqual(b.indexOf(0x0), -1); 67 68// test offsets 69assert.strictEqual(b.indexOf('d', 2), 3); 70assert.strictEqual(b.indexOf('f', 5), 5); 71assert.strictEqual(b.indexOf('f', -1), 5); 72assert.strictEqual(b.indexOf('f', 6), -1); 73 74assert.strictEqual(b.indexOf(Buffer.from('d'), 2), 3); 75assert.strictEqual(b.indexOf(Buffer.from('f'), 5), 5); 76assert.strictEqual(b.indexOf(Buffer.from('f'), -1), 5); 77assert.strictEqual(b.indexOf(Buffer.from('f'), 6), -1); 78 79assert.strictEqual(Buffer.from('ff').indexOf(Buffer.from('f'), 1, 'ucs2'), -1); 80 81// Test invalid and uppercase encoding 82assert.strictEqual(b.indexOf('b', 'utf8'), 1); 83assert.strictEqual(b.indexOf('b', 'UTF8'), 1); 84assert.strictEqual(b.indexOf('62', 'HEX'), 1); 85assert.throws(() => b.indexOf('bad', 'enc'), /Unknown encoding: enc/); 86 87// test hex encoding 88assert.strictEqual( 89 Buffer.from(b.toString('hex'), 'hex') 90 .indexOf('64', 0, 'hex'), 91 3 92); 93assert.strictEqual( 94 Buffer.from(b.toString('hex'), 'hex') 95 .indexOf(Buffer.from('64', 'hex'), 0, 'hex'), 96 3 97); 98 99// Test base64 encoding 100assert.strictEqual( 101 Buffer.from(b.toString('base64'), 'base64') 102 .indexOf('ZA==', 0, 'base64'), 103 3 104); 105assert.strictEqual( 106 Buffer.from(b.toString('base64'), 'base64') 107 .indexOf(Buffer.from('ZA==', 'base64'), 0, 'base64'), 108 3 109); 110 111// test ascii encoding 112assert.strictEqual( 113 Buffer.from(b.toString('ascii'), 'ascii') 114 .indexOf('d', 0, 'ascii'), 115 3 116); 117assert.strictEqual( 118 Buffer.from(b.toString('ascii'), 'ascii') 119 .indexOf(Buffer.from('d', 'ascii'), 0, 'ascii'), 120 3 121); 122 123// Test latin1 encoding 124assert.strictEqual( 125 Buffer.from(b.toString('latin1'), 'latin1') 126 .indexOf('d', 0, 'latin1'), 127 3 128); 129assert.strictEqual( 130 Buffer.from(b.toString('latin1'), 'latin1') 131 .indexOf(Buffer.from('d', 'latin1'), 0, 'latin1'), 132 3 133); 134assert.strictEqual( 135 Buffer.from('aa\u00e8aa', 'latin1') 136 .indexOf('\u00e8', 'latin1'), 137 2 138); 139assert.strictEqual( 140 Buffer.from('\u00e8', 'latin1') 141 .indexOf('\u00e8', 'latin1'), 142 0 143); 144assert.strictEqual( 145 Buffer.from('\u00e8', 'latin1') 146 .indexOf(Buffer.from('\u00e8', 'latin1'), 'latin1'), 147 0 148); 149 150// Test binary encoding 151assert.strictEqual( 152 Buffer.from(b.toString('binary'), 'binary') 153 .indexOf('d', 0, 'binary'), 154 3 155); 156assert.strictEqual( 157 Buffer.from(b.toString('binary'), 'binary') 158 .indexOf(Buffer.from('d', 'binary'), 0, 'binary'), 159 3 160); 161assert.strictEqual( 162 Buffer.from('aa\u00e8aa', 'binary') 163 .indexOf('\u00e8', 'binary'), 164 2 165); 166assert.strictEqual( 167 Buffer.from('\u00e8', 'binary') 168 .indexOf('\u00e8', 'binary'), 169 0 170); 171assert.strictEqual( 172 Buffer.from('\u00e8', 'binary') 173 .indexOf(Buffer.from('\u00e8', 'binary'), 'binary'), 174 0 175); 176 177 178// Test optional offset with passed encoding 179assert.strictEqual(Buffer.from('aaaa0').indexOf('30', 'hex'), 4); 180assert.strictEqual(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4); 181 182{ 183 // Test usc2 and utf16le encoding 184 ['ucs2', 'utf16le'].forEach((encoding) => { 185 const twoByteString = Buffer.from( 186 '\u039a\u0391\u03a3\u03a3\u0395', encoding); 187 188 assert.strictEqual(twoByteString.indexOf('\u0395', 4, encoding), 8); 189 assert.strictEqual(twoByteString.indexOf('\u03a3', -4, encoding), 6); 190 assert.strictEqual(twoByteString.indexOf('\u03a3', -6, encoding), 4); 191 assert.strictEqual(twoByteString.indexOf( 192 Buffer.from('\u03a3', encoding), -6, encoding), 4); 193 assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, encoding)); 194 }); 195} 196 197const mixedByteStringUcs2 = 198 Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2'); 199assert.strictEqual(mixedByteStringUcs2.indexOf('bc', 0, 'ucs2'), 6); 200assert.strictEqual(mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2'), 10); 201assert.strictEqual(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2')); 202 203assert.strictEqual( 204 mixedByteStringUcs2.indexOf(Buffer.from('bc', 'ucs2'), 0, 'ucs2'), 6); 205assert.strictEqual( 206 mixedByteStringUcs2.indexOf(Buffer.from('\u03a3', 'ucs2'), 0, 'ucs2'), 10); 207assert.strictEqual( 208 -1, mixedByteStringUcs2.indexOf(Buffer.from('\u0396', 'ucs2'), 0, 'ucs2')); 209 210{ 211 const twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); 212 213 // Test single char pattern 214 assert.strictEqual(twoByteString.indexOf('\u039a', 0, 'ucs2'), 0); 215 let index = twoByteString.indexOf('\u0391', 0, 'ucs2'); 216 assert.strictEqual(index, 2, `Alpha - at index ${index}`); 217 index = twoByteString.indexOf('\u03a3', 0, 'ucs2'); 218 assert.strictEqual(index, 4, `First Sigma - at index ${index}`); 219 index = twoByteString.indexOf('\u03a3', 6, 'ucs2'); 220 assert.strictEqual(index, 6, `Second Sigma - at index ${index}`); 221 index = twoByteString.indexOf('\u0395', 0, 'ucs2'); 222 assert.strictEqual(index, 8, `Epsilon - at index ${index}`); 223 index = twoByteString.indexOf('\u0392', 0, 'ucs2'); 224 assert.strictEqual(-1, index, `Not beta - at index ${index}`); 225 226 // Test multi-char pattern 227 index = twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'); 228 assert.strictEqual(index, 0, `Lambda Alpha - at index ${index}`); 229 index = twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'); 230 assert.strictEqual(index, 2, `Alpha Sigma - at index ${index}`); 231 index = twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'); 232 assert.strictEqual(index, 4, `Sigma Sigma - at index ${index}`); 233 index = twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'); 234 assert.strictEqual(index, 6, `Sigma Epsilon - at index ${index}`); 235} 236 237const mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395'); 238assert.strictEqual(mixedByteStringUtf8.indexOf('bc'), 5); 239assert.strictEqual(mixedByteStringUtf8.indexOf('bc', 5), 5); 240assert.strictEqual(mixedByteStringUtf8.indexOf('bc', -8), 5); 241assert.strictEqual(mixedByteStringUtf8.indexOf('\u03a3'), 7); 242assert.strictEqual(mixedByteStringUtf8.indexOf('\u0396'), -1); 243 244 245// Test complex string indexOf algorithms. Only trigger for long strings. 246// Long string that isn't a simple repeat of a shorter string. 247let longString = 'A'; 248for (let i = 66; i < 76; i++) { // from 'B' to 'K' 249 longString = longString + String.fromCharCode(i) + longString; 250} 251 252const longBufferString = Buffer.from(longString); 253 254// Pattern of 15 chars, repeated every 16 chars in long 255let pattern = 'ABACABADABACABA'; 256for (let i = 0; i < longBufferString.length - pattern.length; i += 7) { 257 const index = longBufferString.indexOf(pattern, i); 258 assert.strictEqual((i + 15) & ~0xf, index, 259 `Long ABACABA...-string at index ${i}`); 260} 261 262let index = longBufferString.indexOf('AJABACA'); 263assert.strictEqual(index, 510, `Long AJABACA, First J - at index ${index}`); 264index = longBufferString.indexOf('AJABACA', 511); 265assert.strictEqual(index, 1534, `Long AJABACA, Second J - at index ${index}`); 266 267pattern = 'JABACABADABACABA'; 268index = longBufferString.indexOf(pattern); 269assert.strictEqual(index, 511, `Long JABACABA..., First J - at index ${index}`); 270index = longBufferString.indexOf(pattern, 512); 271assert.strictEqual( 272 index, 1535, `Long JABACABA..., Second J - at index ${index}`); 273 274// Search for a non-ASCII string in a pure ASCII string. 275const asciiString = Buffer.from( 276 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf'); 277assert.strictEqual(-1, asciiString.indexOf('\x2061')); 278assert.strictEqual(asciiString.indexOf('leb', 0), 3); 279 280// Search in string containing many non-ASCII chars. 281const allCodePoints = []; 282for (let i = 0; i < 65534; i++) allCodePoints[i] = i; 283const allCharsString = String.fromCharCode.apply(String, allCodePoints) + 284 String.fromCharCode(65534, 65535); 285const allCharsBufferUtf8 = Buffer.from(allCharsString); 286const allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2'); 287 288// Search for string long enough to trigger complex search with ASCII pattern 289// and UC16 subject. 290assert.strictEqual(-1, allCharsBufferUtf8.indexOf('notfound')); 291assert.strictEqual(-1, allCharsBufferUcs2.indexOf('notfound')); 292 293// Needle is longer than haystack, but only because it's encoded as UTF-16 294assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1); 295 296assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'utf8'), 0); 297assert.strictEqual(Buffer.from('aaaa').indexOf('你好', 'ucs2'), -1); 298 299// Haystack has odd length, but the needle is UCS2. 300assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1); 301 302{ 303 // Find substrings in Utf8. 304 const lengths = [1, 3, 15]; // Single char, simple and complex. 305 const indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b]; 306 for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { 307 for (let i = 0; i < indices.length; i++) { 308 const index = indices[i]; 309 let length = lengths[lengthIndex]; 310 311 if (index + length > 0x7F) { 312 length = 2 * length; 313 } 314 315 if (index + length > 0x7FF) { 316 length = 3 * length; 317 } 318 319 if (index + length > 0xFFFF) { 320 length = 4 * length; 321 } 322 323 const patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length); 324 assert.strictEqual(index, allCharsBufferUtf8.indexOf(patternBufferUtf8)); 325 326 const patternStringUtf8 = patternBufferUtf8.toString(); 327 assert.strictEqual(index, allCharsBufferUtf8.indexOf(patternStringUtf8)); 328 } 329 } 330} 331 332{ 333 // Find substrings in Usc2. 334 const lengths = [2, 4, 16]; // Single char, simple and complex. 335 const indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0]; 336 for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { 337 for (let i = 0; i < indices.length; i++) { 338 const index = indices[i] * 2; 339 const length = lengths[lengthIndex]; 340 341 const patternBufferUcs2 = 342 allCharsBufferUcs2.slice(index, index + length); 343 assert.strictEqual( 344 index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2')); 345 346 const patternStringUcs2 = patternBufferUcs2.toString('ucs2'); 347 assert.strictEqual( 348 index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2')); 349 } 350 } 351} 352 353[ 354 () => {}, 355 {}, 356 [], 357].forEach((val) => { 358 assert.throws( 359 () => b.indexOf(val), 360 { 361 code: 'ERR_INVALID_ARG_TYPE', 362 name: 'TypeError', 363 message: 'The "value" argument must be one of type number or string ' + 364 'or an instance of Buffer or Uint8Array.' + 365 common.invalidArgTypeHelper(val) 366 } 367 ); 368}); 369 370// Test weird offset arguments. 371// The following offsets coerce to NaN or 0, searching the whole Buffer 372assert.strictEqual(b.indexOf('b', undefined), 1); 373assert.strictEqual(b.indexOf('b', {}), 1); 374assert.strictEqual(b.indexOf('b', 0), 1); 375assert.strictEqual(b.indexOf('b', null), 1); 376assert.strictEqual(b.indexOf('b', []), 1); 377 378// The following offset coerces to 2, in other words +[2] === 2 379assert.strictEqual(b.indexOf('b', [2]), -1); 380 381// Behavior should match String.indexOf() 382assert.strictEqual( 383 b.indexOf('b', undefined), 384 s.indexOf('b', undefined)); 385assert.strictEqual( 386 b.indexOf('b', {}), 387 s.indexOf('b', {})); 388assert.strictEqual( 389 b.indexOf('b', 0), 390 s.indexOf('b', 0)); 391assert.strictEqual( 392 b.indexOf('b', null), 393 s.indexOf('b', null)); 394assert.strictEqual( 395 b.indexOf('b', []), 396 s.indexOf('b', [])); 397assert.strictEqual( 398 b.indexOf('b', [2]), 399 s.indexOf('b', [2])); 400 401// All code for handling encodings is shared between Buffer.indexOf and 402// Buffer.lastIndexOf, so only testing the separate lastIndexOf semantics. 403 404// Test lastIndexOf basic functionality; Buffer b contains 'abcdef'. 405// lastIndexOf string: 406assert.strictEqual(b.lastIndexOf('a'), 0); 407assert.strictEqual(b.lastIndexOf('a', 1), 0); 408assert.strictEqual(b.lastIndexOf('b', 1), 1); 409assert.strictEqual(b.lastIndexOf('c', 1), -1); 410assert.strictEqual(b.lastIndexOf('a', -1), 0); 411assert.strictEqual(b.lastIndexOf('a', -4), 0); 412assert.strictEqual(b.lastIndexOf('a', -b.length), 0); 413assert.strictEqual(b.lastIndexOf('a', -b.length - 1), -1); 414assert.strictEqual(b.lastIndexOf('a', NaN), 0); 415assert.strictEqual(b.lastIndexOf('a', -Infinity), -1); 416assert.strictEqual(b.lastIndexOf('a', Infinity), 0); 417// lastIndexOf Buffer: 418assert.strictEqual(b.lastIndexOf(buf_a), 0); 419assert.strictEqual(b.lastIndexOf(buf_a, 1), 0); 420assert.strictEqual(b.lastIndexOf(buf_a, -1), 0); 421assert.strictEqual(b.lastIndexOf(buf_a, -4), 0); 422assert.strictEqual(b.lastIndexOf(buf_a, -b.length), 0); 423assert.strictEqual(b.lastIndexOf(buf_a, -b.length - 1), -1); 424assert.strictEqual(b.lastIndexOf(buf_a, NaN), 0); 425assert.strictEqual(b.lastIndexOf(buf_a, -Infinity), -1); 426assert.strictEqual(b.lastIndexOf(buf_a, Infinity), 0); 427assert.strictEqual(b.lastIndexOf(buf_bc), 1); 428assert.strictEqual(b.lastIndexOf(buf_bc, 2), 1); 429assert.strictEqual(b.lastIndexOf(buf_bc, -1), 1); 430assert.strictEqual(b.lastIndexOf(buf_bc, -3), 1); 431assert.strictEqual(b.lastIndexOf(buf_bc, -5), 1); 432assert.strictEqual(b.lastIndexOf(buf_bc, -6), -1); 433assert.strictEqual(b.lastIndexOf(buf_bc, NaN), 1); 434assert.strictEqual(b.lastIndexOf(buf_bc, -Infinity), -1); 435assert.strictEqual(b.lastIndexOf(buf_bc, Infinity), 1); 436assert.strictEqual(b.lastIndexOf(buf_f), b.length - 1); 437assert.strictEqual(b.lastIndexOf(buf_z), -1); 438assert.strictEqual(b.lastIndexOf(buf_empty), b.length); 439assert.strictEqual(b.lastIndexOf(buf_empty, 1), 1); 440assert.strictEqual(b.lastIndexOf(buf_empty, b.length + 1), b.length); 441assert.strictEqual(b.lastIndexOf(buf_empty, Infinity), b.length); 442// lastIndexOf number: 443assert.strictEqual(b.lastIndexOf(0x61), 0); 444assert.strictEqual(b.lastIndexOf(0x61, 1), 0); 445assert.strictEqual(b.lastIndexOf(0x61, -1), 0); 446assert.strictEqual(b.lastIndexOf(0x61, -4), 0); 447assert.strictEqual(b.lastIndexOf(0x61, -b.length), 0); 448assert.strictEqual(b.lastIndexOf(0x61, -b.length - 1), -1); 449assert.strictEqual(b.lastIndexOf(0x61, NaN), 0); 450assert.strictEqual(b.lastIndexOf(0x61, -Infinity), -1); 451assert.strictEqual(b.lastIndexOf(0x61, Infinity), 0); 452assert.strictEqual(b.lastIndexOf(0x0), -1); 453 454// Test weird offset arguments. 455// The following offsets coerce to NaN, searching the whole Buffer 456assert.strictEqual(b.lastIndexOf('b', undefined), 1); 457assert.strictEqual(b.lastIndexOf('b', {}), 1); 458 459// The following offsets coerce to 0 460assert.strictEqual(b.lastIndexOf('b', 0), -1); 461assert.strictEqual(b.lastIndexOf('b', null), -1); 462assert.strictEqual(b.lastIndexOf('b', []), -1); 463 464// The following offset coerces to 2, in other words +[2] === 2 465assert.strictEqual(b.lastIndexOf('b', [2]), 1); 466 467// Behavior should match String.lastIndexOf() 468assert.strictEqual( 469 b.lastIndexOf('b', undefined), 470 s.lastIndexOf('b', undefined)); 471assert.strictEqual( 472 b.lastIndexOf('b', {}), 473 s.lastIndexOf('b', {})); 474assert.strictEqual( 475 b.lastIndexOf('b', 0), 476 s.lastIndexOf('b', 0)); 477assert.strictEqual( 478 b.lastIndexOf('b', null), 479 s.lastIndexOf('b', null)); 480assert.strictEqual( 481 b.lastIndexOf('b', []), 482 s.lastIndexOf('b', [])); 483assert.strictEqual( 484 b.lastIndexOf('b', [2]), 485 s.lastIndexOf('b', [2])); 486 487// Test needles longer than the haystack. 488assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'ucs2'), -1); 489assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'utf8'), -1); 490assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'latin1'), -1); 491assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'binary'), -1); 492assert.strictEqual(b.lastIndexOf(Buffer.from('aaaaaaaaaaaaaaa')), -1); 493assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 2, 'ucs2'), -1); 494assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 3, 'utf8'), -1); 495assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 5, 'latin1'), -1); 496assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 5, 'binary'), -1); 497assert.strictEqual(b.lastIndexOf(Buffer.from('aaaaaaaaaaaaaaa'), 7), -1); 498 499// 你好 expands to a total of 6 bytes using UTF-8 and 4 bytes using UTF-16 500assert.strictEqual(buf_bc.lastIndexOf('你好', 'ucs2'), -1); 501assert.strictEqual(buf_bc.lastIndexOf('你好', 'utf8'), -1); 502assert.strictEqual(buf_bc.lastIndexOf('你好', 'latin1'), -1); 503assert.strictEqual(buf_bc.lastIndexOf('你好', 'binary'), -1); 504assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好')), -1); 505assert.strictEqual(buf_bc.lastIndexOf('你好', 2, 'ucs2'), -1); 506assert.strictEqual(buf_bc.lastIndexOf('你好', 3, 'utf8'), -1); 507assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'latin1'), -1); 508assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'binary'), -1); 509assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好'), 7), -1); 510 511// Test lastIndexOf on a longer buffer: 512const bufferString = Buffer.from('a man a plan a canal panama'); 513assert.strictEqual(bufferString.lastIndexOf('canal'), 15); 514assert.strictEqual(bufferString.lastIndexOf('panama'), 21); 515assert.strictEqual(bufferString.lastIndexOf('a man a plan a canal panama'), 0); 516assert.strictEqual(-1, bufferString.lastIndexOf('a man a plan a canal mexico')); 517assert.strictEqual(-1, bufferString 518 .lastIndexOf('a man a plan a canal mexico city')); 519assert.strictEqual(-1, bufferString.lastIndexOf(Buffer.from('a'.repeat(1000)))); 520assert.strictEqual(bufferString.lastIndexOf('a man a plan', 4), 0); 521assert.strictEqual(bufferString.lastIndexOf('a '), 13); 522assert.strictEqual(bufferString.lastIndexOf('a ', 13), 13); 523assert.strictEqual(bufferString.lastIndexOf('a ', 12), 6); 524assert.strictEqual(bufferString.lastIndexOf('a ', 5), 0); 525assert.strictEqual(bufferString.lastIndexOf('a ', -1), 13); 526assert.strictEqual(bufferString.lastIndexOf('a ', -27), 0); 527assert.strictEqual(-1, bufferString.lastIndexOf('a ', -28)); 528 529// Test lastIndexOf for the case that the first character can be found, 530// but in a part of the buffer that does not make search to search 531// due do length constraints. 532const abInUCS2 = Buffer.from('ab', 'ucs2'); 533assert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'latin1').lastIndexOf('µ')); 534assert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'binary').lastIndexOf('µ')); 535assert.strictEqual(-1, Buffer.from('bc').lastIndexOf('ab')); 536assert.strictEqual(-1, Buffer.from('abc').lastIndexOf('qa')); 537assert.strictEqual(-1, Buffer.from('abcdef').lastIndexOf('qabc')); 538assert.strictEqual(-1, Buffer.from('bc').lastIndexOf(Buffer.from('ab'))); 539assert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf('ab', 'ucs2')); 540assert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf(abInUCS2)); 541 542assert.strictEqual(Buffer.from('abc').lastIndexOf('ab'), 0); 543assert.strictEqual(Buffer.from('abc').lastIndexOf('ab', 1), 0); 544assert.strictEqual(Buffer.from('abc').lastIndexOf('ab', 2), 0); 545assert.strictEqual(Buffer.from('abc').lastIndexOf('ab', 3), 0); 546 547// The above tests test the LINEAR and SINGLE-CHAR strategies. 548// Now, we test the BOYER-MOORE-HORSPOOL strategy. 549// Test lastIndexOf on a long buffer w multiple matches: 550pattern = 'JABACABADABACABA'; 551assert.strictEqual(longBufferString.lastIndexOf(pattern), 1535); 552assert.strictEqual(longBufferString.lastIndexOf(pattern, 1535), 1535); 553assert.strictEqual(longBufferString.lastIndexOf(pattern, 1534), 511); 554 555// Finally, give it a really long input to trigger fallback from BMH to 556// regular BOYER-MOORE (which has better worst-case complexity). 557 558// Generate a really long Thue-Morse sequence of 'yolo' and 'swag', 559// "yolo swag swag yolo swag yolo yolo swag" ..., goes on for about 5MB. 560// This is hard to search because it all looks similar, but never repeats. 561 562// countBits returns the number of bits in the binary representation of n. 563function countBits(n) { 564 let count; 565 for (count = 0; n > 0; count++) { 566 n = n & (n - 1); // remove top bit 567 } 568 return count; 569} 570const parts = []; 571for (let i = 0; i < 1000000; i++) { 572 parts.push((countBits(i) % 2 === 0) ? 'yolo' : 'swag'); 573} 574const reallyLong = Buffer.from(parts.join(' ')); 575assert.strictEqual(reallyLong.slice(0, 19).toString(), 'yolo swag swag yolo'); 576 577// Expensive reverse searches. Stress test lastIndexOf: 578pattern = reallyLong.slice(0, 100000); // First 1/50th of the pattern. 579assert.strictEqual(reallyLong.lastIndexOf(pattern), 4751360); 580assert.strictEqual(reallyLong.lastIndexOf(pattern, 4000000), 3932160); 581assert.strictEqual(reallyLong.lastIndexOf(pattern, 3000000), 2949120); 582pattern = reallyLong.slice(100000, 200000); // Second 1/50th. 583assert.strictEqual(reallyLong.lastIndexOf(pattern), 4728480); 584pattern = reallyLong.slice(0, 1000000); // First 1/5th. 585assert.strictEqual(reallyLong.lastIndexOf(pattern), 3932160); 586pattern = reallyLong.slice(0, 2000000); // first 2/5ths. 587assert.strictEqual(reallyLong.lastIndexOf(pattern), 0); 588 589// Test truncation of Number arguments to uint8 590{ 591 const buf = Buffer.from('this is a test'); 592 assert.strictEqual(buf.indexOf(0x6973), 3); 593 assert.strictEqual(buf.indexOf(0x697320), 4); 594 assert.strictEqual(buf.indexOf(0x69732069), 2); 595 assert.strictEqual(buf.indexOf(0x697374657374), 0); 596 assert.strictEqual(buf.indexOf(0x69737374), 0); 597 assert.strictEqual(buf.indexOf(0x69737465), 11); 598 assert.strictEqual(buf.indexOf(0x69737465), 11); 599 assert.strictEqual(buf.indexOf(-140), 0); 600 assert.strictEqual(buf.indexOf(-152), 1); 601 assert.strictEqual(buf.indexOf(0xff), -1); 602 assert.strictEqual(buf.indexOf(0xffff), -1); 603} 604 605// Test that Uint8Array arguments are okay. 606{ 607 const needle = new Uint8Array([ 0x66, 0x6f, 0x6f ]); 608 const haystack = Buffer.from('a foo b foo'); 609 assert.strictEqual(haystack.indexOf(needle), 2); 610 assert.strictEqual(haystack.lastIndexOf(needle), haystack.length - 3); 611} 612 613// Avoid abort because of invalid usage 614// see https://github.com/nodejs/node/issues/32753 615{ 616 assert.throws(() => { 617 const buffer = require('buffer'); 618 new buffer.Buffer.prototype.lastIndexOf(1, 'str'); 619 }, { 620 code: 'ERR_INVALID_ARG_TYPE', 621 name: 'TypeError', 622 message: 'The "buffer" argument must be an instance of Buffer, ' + 623 'TypedArray, or DataView. ' + 624 'Received an instance of lastIndexOf' 625 }); 626} 627