@ -71,12 +71,12 @@ assert.strictEqual(b.indexOf('f', 5), 5);
assert . strictEqual ( b . indexOf ( 'f' , - 1 ) , 5 ) ;
assert . strictEqual ( b . indexOf ( 'f' , 6 ) , - 1 ) ;
assert . e qual( b . indexOf ( Buffer . from ( 'd' ) , 2 ) , 3 ) ;
assert . e qual( b . indexOf ( Buffer . from ( 'f' ) , 5 ) , 5 ) ;
assert . e qual( b . indexOf ( Buffer . from ( 'f' ) , - 1 ) , 5 ) ;
assert . e qual( b . indexOf ( Buffer . from ( 'f' ) , 6 ) , - 1 ) ;
assert . strictE qual( b . indexOf ( Buffer . from ( 'd' ) , 2 ) , 3 ) ;
assert . strictE qual( b . indexOf ( Buffer . from ( 'f' ) , 5 ) , 5 ) ;
assert . strictE qual( b . indexOf ( Buffer . from ( 'f' ) , - 1 ) , 5 ) ;
assert . strictE qual( b . indexOf ( Buffer . from ( 'f' ) , 6 ) , - 1 ) ;
assert . e qual( Buffer . from ( 'ff' ) . indexOf ( Buffer . from ( 'f' ) , 1 , 'ucs2' ) , - 1 ) ;
assert . strictE qual( Buffer . from ( 'ff' ) . indexOf ( Buffer . from ( 'f' ) , 1 , 'ucs2' ) , - 1 ) ;
// test invalid and uppercase encoding
assert . strictEqual ( b . indexOf ( 'b' , 'utf8' ) , 1 ) ;
@ -176,62 +176,67 @@ assert.strictEqual(
// test optional offset with passed encoding
assert . e qual( Buffer . from ( 'aaaa0' ) . indexOf ( '30' , 'hex' ) , 4 ) ;
assert . e qual( Buffer . from ( 'aaaa00a' ) . indexOf ( '3030' , 'hex' ) , 4 ) ;
assert . strictE qual( Buffer . from ( 'aaaa0' ) . indexOf ( '30' , 'hex' ) , 4 ) ;
assert . strictE qual( Buffer . from ( 'aaaa00a' ) . indexOf ( '3030' , 'hex' ) , 4 ) ;
{
// test usc2 encoding
const twoByteString = Buffer . from ( '\u039a\u0391\u03a3\u03a3\u0395' , 'ucs2' ) ;
assert . e qual( 8 , twoByteString . indexOf ( '\u0395' , 4 , 'ucs2' ) ) ;
assert . e qual( 6 , twoByteString . indexOf ( '\u03a3' , - 4 , 'ucs2' ) ) ;
assert . e qual( 4 , twoByteString . indexOf ( '\u03a3' , - 6 , 'ucs2' ) ) ;
assert . e qual( 4 , twoByteString . indexOf (
assert . strictE qual( 8 , twoByteString . indexOf ( '\u0395' , 4 , 'ucs2' ) ) ;
assert . strictE qual( 6 , twoByteString . indexOf ( '\u03a3' , - 4 , 'ucs2' ) ) ;
assert . strictE qual( 4 , twoByteString . indexOf ( '\u03a3' , - 6 , 'ucs2' ) ) ;
assert . strictE qual( 4 , twoByteString . indexOf (
Buffer . from ( '\u03a3' , 'ucs2' ) , - 6 , 'ucs2' ) ) ;
assert . e qual( - 1 , twoByteString . indexOf ( '\u03a3' , - 2 , 'ucs2' ) ) ;
assert . strictE qual( - 1 , twoByteString . indexOf ( '\u03a3' , - 2 , 'ucs2' ) ) ;
}
const mixedByteStringUcs2 =
Buffer . from ( '\u039a\u0391abc\u03a3\u03a3\u0395' , 'ucs2' ) ;
assert . e qual( 6 , mixedByteStringUcs2 . indexOf ( 'bc' , 0 , 'ucs2' ) ) ;
assert . e qual( 10 , mixedByteStringUcs2 . indexOf ( '\u03a3' , 0 , 'ucs2' ) ) ;
assert . e qual( - 1 , mixedByteStringUcs2 . indexOf ( '\u0396' , 0 , 'ucs2' ) ) ;
assert . strictE qual( 6 , mixedByteStringUcs2 . indexOf ( 'bc' , 0 , 'ucs2' ) ) ;
assert . strictE qual( 10 , mixedByteStringUcs2 . indexOf ( '\u03a3' , 0 , 'ucs2' ) ) ;
assert . strictE qual( - 1 , mixedByteStringUcs2 . indexOf ( '\u0396' , 0 , 'ucs2' ) ) ;
assert . e qual(
assert . strictE qual(
6 , mixedByteStringUcs2 . indexOf ( Buffer . from ( 'bc' , 'ucs2' ) , 0 , 'ucs2' ) ) ;
assert . e qual(
assert . strictE qual(
10 , mixedByteStringUcs2 . indexOf ( Buffer . from ( '\u03a3' , 'ucs2' ) , 0 , 'ucs2' ) ) ;
assert . e qual(
assert . strictE qual(
- 1 , mixedByteStringUcs2 . indexOf ( Buffer . from ( '\u0396' , 'ucs2' ) , 0 , 'ucs2' ) ) ;
{
const twoByteString = Buffer . from ( '\u039a\u0391\u03a3\u03a3\u0395' , 'ucs2' ) ;
// Test single char pattern
assert . equal ( 0 , twoByteString . indexOf ( '\u039a' , 0 , 'ucs2' ) ) ;
assert . equal ( 2 , twoByteString . indexOf ( '\u0391' , 0 , 'ucs2' ) , 'Alpha' ) ;
assert . equal ( 4 , twoByteString . indexOf ( '\u03a3' , 0 , 'ucs2' ) , 'First Sigma' ) ;
assert . equal ( 6 , twoByteString . indexOf ( '\u03a3' , 6 , 'ucs2' ) , 'Second Sigma' ) ;
assert . equal ( 8 , twoByteString . indexOf ( '\u0395' , 0 , 'ucs2' ) , 'Epsilon' ) ;
assert . equal ( - 1 , twoByteString . indexOf ( '\u0392' , 0 , 'ucs2' ) , 'Not beta' ) ;
assert . strictEqual ( 0 , twoByteString . indexOf ( '\u039a' , 0 , 'ucs2' ) ) ;
assert . strictEqual ( 2 , twoByteString . indexOf ( '\u0391' , 0 , 'ucs2' ) ,
'Alpha' ) ;
assert . strictEqual ( 4 , twoByteString . indexOf ( '\u03a3' , 0 , 'ucs2' ) ,
'First Sigma' ) ;
assert . strictEqual ( 6 , twoByteString . indexOf ( '\u03a3' , 6 , 'ucs2' ) ,
'Second Sigma' ) ;
assert . strictEqual ( 8 , twoByteString . indexOf ( '\u0395' , 0 , 'ucs2' ) ,
'Epsilon' ) ;
assert . strictEqual ( - 1 , twoByteString . indexOf ( '\u0392' , 0 , 'ucs2' ) ,
'Not beta' ) ;
// Test multi-char pattern
assert . equal (
assert . strictE qual(
0 , twoByteString . indexOf ( '\u039a\u0391' , 0 , 'ucs2' ) , 'Lambda Alpha' ) ;
assert . equal (
assert . strictE qual(
2 , twoByteString . indexOf ( '\u0391\u03a3' , 0 , 'ucs2' ) , 'Alpha Sigma' ) ;
assert . e qual(
assert . strictE qual(
4 , twoByteString . indexOf ( '\u03a3\u03a3' , 0 , 'ucs2' ) , 'Sigma Sigma' ) ;
assert . e qual(
assert . strictE qual(
6 , twoByteString . indexOf ( '\u03a3\u0395' , 0 , 'ucs2' ) , 'Sigma Epsilon' ) ;
}
const mixedByteStringUtf8 = Buffer . from ( '\u039a\u0391abc\u03a3\u03a3\u0395' ) ;
assert . e qual( 5 , mixedByteStringUtf8 . indexOf ( 'bc' ) ) ;
assert . e qual( 5 , mixedByteStringUtf8 . indexOf ( 'bc' , 5 ) ) ;
assert . e qual( 5 , mixedByteStringUtf8 . indexOf ( 'bc' , - 8 ) ) ;
assert . e qual( 7 , mixedByteStringUtf8 . indexOf ( '\u03a3' ) ) ;
assert . e qual( - 1 , mixedByteStringUtf8 . indexOf ( '\u0396' ) ) ;
assert . strictE qual( 5 , mixedByteStringUtf8 . indexOf ( 'bc' ) ) ;
assert . strictE qual( 5 , mixedByteStringUtf8 . indexOf ( 'bc' , 5 ) ) ;
assert . strictE qual( 5 , mixedByteStringUtf8 . indexOf ( 'bc' , - 8 ) ) ;
assert . strictE qual( 7 , mixedByteStringUtf8 . indexOf ( '\u03a3' ) ) ;
assert . strictE qual( - 1 , mixedByteStringUtf8 . indexOf ( '\u0396' ) ) ;
// Test complex string indexOf algorithms. Only trigger for long strings.
@ -247,23 +252,25 @@ const longBufferString = Buffer.from(longString);
let pattern = 'ABACABADABACABA' ;
for ( let i = 0 ; i < longBufferString . length - pattern . length ; i += 7 ) {
const index = longBufferString . indexOf ( pattern , i ) ;
assert . equal ( ( i + 15 ) & ~ 0xf , index , 'Long ABACABA...-string at index ' + i ) ;
assert . strictEqual ( ( i + 15 ) & ~ 0xf , index ,
'Long ABACABA...-string at index ' + i ) ;
}
assert . equal ( 510 , longBufferString . indexOf ( 'AJABACA' ) , 'Long AJABACA, First J' ) ;
assert . equal (
assert . strictEqual ( 510 , longBufferString . indexOf ( 'AJABACA' ) ,
'Long AJABACA, First J' ) ;
assert . strictEqual (
1534 , longBufferString . indexOf ( 'AJABACA' , 511 ) , 'Long AJABACA, Second J' ) ;
pattern = 'JABACABADABACABA' ;
assert . e qual(
assert . strictE qual(
511 , longBufferString . indexOf ( pattern ) , 'Long JABACABA..., First J' ) ;
assert . e qual(
assert . strictE qual(
1535 , longBufferString . indexOf ( pattern , 512 ) , 'Long JABACABA..., Second J' ) ;
// Search for a non-ASCII string in a pure ASCII string.
const asciiString = Buffer . from (
'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf' ) ;
assert . e qual( - 1 , asciiString . indexOf ( '\x2061' ) ) ;
assert . e qual( 3 , asciiString . indexOf ( 'leb' , 0 ) ) ;
assert . strictE qual( - 1 , asciiString . indexOf ( '\x2061' ) ) ;
assert . strictE qual( 3 , asciiString . indexOf ( 'leb' , 0 ) ) ;
// Search in string containing many non-ASCII chars.
const allCodePoints = [ ] ;
@ -274,8 +281,8 @@ const allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2');
// Search for string long enough to trigger complex search with ASCII pattern
// and UC16 subject.
assert . e qual( - 1 , allCharsBufferUtf8 . indexOf ( 'notfound' ) ) ;
assert . e qual( - 1 , allCharsBufferUcs2 . indexOf ( 'notfound' ) ) ;
assert . strictE qual( - 1 , allCharsBufferUtf8 . indexOf ( 'notfound' ) ) ;
assert . strictE qual( - 1 , allCharsBufferUcs2 . indexOf ( 'notfound' ) ) ;
// Needle is longer than haystack, but only because it's encoded as UTF-16
assert . strictEqual ( Buffer . from ( 'aaaa' ) . indexOf ( 'a' . repeat ( 4 ) , 'ucs2' ) , - 1 ) ;
@ -308,10 +315,10 @@ assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1);
}
const patternBufferUtf8 = allCharsBufferUtf8 . slice ( index , index + length ) ;
assert . e qual( index , allCharsBufferUtf8 . indexOf ( patternBufferUtf8 ) ) ;
assert . strictE qual( index , allCharsBufferUtf8 . indexOf ( patternBufferUtf8 ) ) ;
const patternStringUtf8 = patternBufferUtf8 . toString ( ) ;
assert . e qual( index , allCharsBufferUtf8 . indexOf ( patternStringUtf8 ) ) ;
assert . strictE qual( index , allCharsBufferUtf8 . indexOf ( patternStringUtf8 ) ) ;
}
}
}
@ -327,11 +334,11 @@ assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1);
const patternBufferUcs2 =
allCharsBufferUcs2 . slice ( index , index + length ) ;
assert . e qual(
assert . strictE qual(
index , allCharsBufferUcs2 . indexOf ( patternBufferUcs2 , 0 , 'ucs2' ) ) ;
const patternStringUcs2 = patternBufferUcs2 . toString ( 'ucs2' ) ;
assert . e qual(
assert . strictE qual(
index , allCharsBufferUcs2 . indexOf ( patternStringUcs2 , 0 , 'ucs2' ) ) ;
}
}
@ -352,62 +359,62 @@ assert.throws(function() {
// Test lastIndexOf basic functionality; Buffer b contains 'abcdef'.
// lastIndexOf string:
assert . e qual( b . lastIndexOf ( 'a' ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 'a' , 1 ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 'b' , 1 ) , 1 ) ;
assert . e qual( b . lastIndexOf ( 'c' , 1 ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( 'a' , - 1 ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 'a' , - 4 ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 'a' , - b . length ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 'a' , - b . length - 1 ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( 'a' , NaN ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 'a' , - Infinity ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( 'a' , Infinity ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 'a' ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 'a' , 1 ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 'b' , 1 ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( 'c' , 1 ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( 'a' , - 1 ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 'a' , - 4 ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 'a' , - b . length ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 'a' , - b . length - 1 ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( 'a' , NaN ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 'a' , - Infinity ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( 'a' , Infinity ) , 0 ) ;
// lastIndexOf Buffer:
assert . e qual( b . lastIndexOf ( buf_a ) , 0 ) ;
assert . e qual( b . lastIndexOf ( buf_a , 1 ) , 0 ) ;
assert . e qual( b . lastIndexOf ( buf_a , - 1 ) , 0 ) ;
assert . e qual( b . lastIndexOf ( buf_a , - 4 ) , 0 ) ;
assert . e qual( b . lastIndexOf ( buf_a , - b . length ) , 0 ) ;
assert . e qual( b . lastIndexOf ( buf_a , - b . length - 1 ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( buf_a , NaN ) , 0 ) ;
assert . e qual( b . lastIndexOf ( buf_a , - Infinity ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( buf_a , Infinity ) , 0 ) ;
assert . e qual( b . lastIndexOf ( buf_bc ) , 1 ) ;
assert . e qual( b . lastIndexOf ( buf_bc , 2 ) , 1 ) ;
assert . e qual( b . lastIndexOf ( buf_bc , - 1 ) , 1 ) ;
assert . e qual( b . lastIndexOf ( buf_bc , - 3 ) , 1 ) ;
assert . e qual( b . lastIndexOf ( buf_bc , - 5 ) , 1 ) ;
assert . e qual( b . lastIndexOf ( buf_bc , - 6 ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( buf_bc , NaN ) , 1 ) ;
assert . e qual( b . lastIndexOf ( buf_bc , - Infinity ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( buf_bc , Infinity ) , 1 ) ;
assert . e qual( b . lastIndexOf ( buf_f ) , b . length - 1 ) ;
assert . e qual( b . lastIndexOf ( buf_z ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( buf_empty ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( buf_empty , 1 ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( buf_empty , b . length + 1 ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( buf_empty , Infinity ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_a ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( buf_a , 1 ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( buf_a , - 1 ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( buf_a , - 4 ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( buf_a , - b . length ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( buf_a , - b . length - 1 ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_a , NaN ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( buf_a , - Infinity ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_a , Infinity ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( buf_bc ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_bc , 2 ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_bc , - 1 ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_bc , - 3 ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_bc , - 5 ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_bc , - 6 ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_bc , NaN ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_bc , - Infinity ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_bc , Infinity ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_f ) , b . length - 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_z ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_empty ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_empty , 1 ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_empty , b . length + 1 ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( buf_empty , Infinity ) , - 1 ) ;
// lastIndexOf number:
assert . e qual( b . lastIndexOf ( 0x61 ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 0x61 , 1 ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 0x61 , - 1 ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 0x61 , - 4 ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 0x61 , - b . length ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 0x61 , - b . length - 1 ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( 0x61 , NaN ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 0x61 , - Infinity ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( 0x61 , Infinity ) , 0 ) ;
assert . e qual( b . lastIndexOf ( 0x0 ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( 0x61 ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 0x61 , 1 ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 0x61 , - 1 ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 0x61 , - 4 ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 0x61 , - b . length ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 0x61 , - b . length - 1 ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( 0x61 , NaN ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 0x61 , - Infinity ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( 0x61 , Infinity ) , 0 ) ;
assert . strictE qual( b . lastIndexOf ( 0x0 ) , - 1 ) ;
// Test weird offset arguments.
// Behaviour should match String.lastIndexOf:
assert . e qual( b . lastIndexOf ( 'b' , 0 ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( 'b' , undefined ) , 1 ) ;
assert . e qual( b . lastIndexOf ( 'b' , null ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( 'b' , { } ) , 1 ) ;
assert . e qual( b . lastIndexOf ( 'b' , [ ] ) , - 1 ) ;
assert . e qual( b . lastIndexOf ( 'b' , [ 2 ] ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( 'b' , 0 ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( 'b' , undefined ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( 'b' , null ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( 'b' , { } ) , 1 ) ;
assert . strictE qual( b . lastIndexOf ( 'b' , [ ] ) , - 1 ) ;
assert . strictE qual( b . lastIndexOf ( 'b' , [ 2 ] ) , 1 ) ;
// Test needles longer than the haystack.
assert . strictEqual ( b . lastIndexOf ( 'aaaaaaaaaaaaaaa' , 'ucs2' ) , - 1 ) ;
@ -435,20 +442,21 @@ assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好'), 7), -1);
// Test lastIndexOf on a longer buffer:
const bufferString = new Buffer ( 'a man a plan a canal panama' ) ;
assert . equal ( 15 , bufferString . lastIndexOf ( 'canal' ) ) ;
assert . equal ( 21 , bufferString . lastIndexOf ( 'panama' ) ) ;
assert . equal ( 0 , bufferString . lastIndexOf ( 'a man a plan a canal panama' ) ) ;
assert . equal ( - 1 , bufferString . lastIndexOf ( 'a man a plan a canal mexico' ) ) ;
assert . equal ( - 1 , bufferString . lastIndexOf ( 'a man a plan a canal mexico city' ) ) ;
assert . equal ( - 1 , bufferString . lastIndexOf ( Buffer . from ( 'a' . repeat ( 1000 ) ) ) ) ;
assert . equal ( 0 , bufferString . lastIndexOf ( 'a man a plan' , 4 ) ) ;
assert . equal ( 13 , bufferString . lastIndexOf ( 'a ' ) ) ;
assert . equal ( 13 , bufferString . lastIndexOf ( 'a ' , 13 ) ) ;
assert . equal ( 6 , bufferString . lastIndexOf ( 'a ' , 12 ) ) ;
assert . equal ( 0 , bufferString . lastIndexOf ( 'a ' , 5 ) ) ;
assert . equal ( 13 , bufferString . lastIndexOf ( 'a ' , - 1 ) ) ;
assert . equal ( 0 , bufferString . lastIndexOf ( 'a ' , - 27 ) ) ;
assert . equal ( - 1 , bufferString . lastIndexOf ( 'a ' , - 28 ) ) ;
assert . strictEqual ( 15 , bufferString . lastIndexOf ( 'canal' ) ) ;
assert . strictEqual ( 21 , bufferString . lastIndexOf ( 'panama' ) ) ;
assert . strictEqual ( 0 , bufferString . lastIndexOf ( 'a man a plan a canal panama' ) ) ;
assert . strictEqual ( - 1 , bufferString . lastIndexOf ( 'a man a plan a canal mexico' ) ) ;
assert . strictEqual ( - 1 , bufferString
. lastIndexOf ( 'a man a plan a canal mexico city' ) ) ;
assert . strictEqual ( - 1 , bufferString . lastIndexOf ( Buffer . from ( 'a' . repeat ( 1000 ) ) ) ) ;
assert . strictEqual ( 0 , bufferString . lastIndexOf ( 'a man a plan' , 4 ) ) ;
assert . strictEqual ( 13 , bufferString . lastIndexOf ( 'a ' ) ) ;
assert . strictEqual ( 13 , bufferString . lastIndexOf ( 'a ' , 13 ) ) ;
assert . strictEqual ( 6 , bufferString . lastIndexOf ( 'a ' , 12 ) ) ;
assert . strictEqual ( 0 , bufferString . lastIndexOf ( 'a ' , 5 ) ) ;
assert . strictEqual ( 13 , bufferString . lastIndexOf ( 'a ' , - 1 ) ) ;
assert . strictEqual ( 0 , bufferString . lastIndexOf ( 'a ' , - 27 ) ) ;
assert . strictEqual ( - 1 , bufferString . lastIndexOf ( 'a ' , - 28 ) ) ;
// Test lastIndexOf for the case that the first character can be found,
// but in a part of the buffer that does not make search to search
@ -472,9 +480,9 @@ assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 3));
// Now, we test the BOYER-MOORE-HORSPOOL strategy.
// Test lastIndexOf on a long buffer w multiple matches:
pattern = 'JABACABADABACABA' ;
assert . e qual( 1535 , longBufferString . lastIndexOf ( pattern ) ) ;
assert . e qual( 1535 , longBufferString . lastIndexOf ( pattern , 1535 ) ) ;
assert . e qual( 511 , longBufferString . lastIndexOf ( pattern , 1534 ) ) ;
assert . strictE qual( 1535 , longBufferString . lastIndexOf ( pattern ) ) ;
assert . strictE qual( 1535 , longBufferString . lastIndexOf ( pattern , 1535 ) ) ;
assert . strictE qual( 511 , longBufferString . lastIndexOf ( pattern , 1534 ) ) ;
// Finally, give it a really long input to trigger fallback from BMH to
// regular BOYER-MOORE (which has better worst-case complexity).
@ -496,19 +504,19 @@ for (let i = 0; i < 1000000; i++) {
parts . push ( ( countBits ( i ) % 2 === 0 ) ? 'yolo' : 'swag' ) ;
}
const reallyLong = new Buffer ( parts . join ( ' ' ) ) ;
assert . e qual( 'yolo swag swag yolo' , reallyLong . slice ( 0 , 19 ) . toString ( ) ) ;
assert . strictE qual( 'yolo swag swag yolo' , reallyLong . slice ( 0 , 19 ) . toString ( ) ) ;
// Expensive reverse searches. Stress test lastIndexOf:
pattern = reallyLong . slice ( 0 , 100000 ) ; // First 1/50th of the pattern.
assert . e qual( 4751360 , reallyLong . lastIndexOf ( pattern ) ) ;
assert . e qual( 3932160 , reallyLong . lastIndexOf ( pattern , 4000000 ) ) ;
assert . e qual( 2949120 , reallyLong . lastIndexOf ( pattern , 3000000 ) ) ;
assert . strictE qual( 4751360 , reallyLong . lastIndexOf ( pattern ) ) ;
assert . strictE qual( 3932160 , reallyLong . lastIndexOf ( pattern , 4000000 ) ) ;
assert . strictE qual( 2949120 , reallyLong . lastIndexOf ( pattern , 3000000 ) ) ;
pattern = reallyLong . slice ( 100000 , 200000 ) ; // Second 1/50th.
assert . e qual( 4728480 , reallyLong . lastIndexOf ( pattern ) ) ;
assert . strictE qual( 4728480 , reallyLong . lastIndexOf ( pattern ) ) ;
pattern = reallyLong . slice ( 0 , 1000000 ) ; // First 1/5th.
assert . e qual( 3932160 , reallyLong . lastIndexOf ( pattern ) ) ;
assert . strictE qual( 3932160 , reallyLong . lastIndexOf ( pattern ) ) ;
pattern = reallyLong . slice ( 0 , 2000000 ) ; // first 2/5ths.
assert . e qual( 0 , reallyLong . lastIndexOf ( pattern ) ) ;
assert . strictE qual( 0 , reallyLong . lastIndexOf ( pattern ) ) ;
// test truncation of Number arguments to uint8
{