2 var common = require('../common');
3 var assert = require('assert');
5 var Buffer = require('buffer').Buffer;
7 var b = new Buffer('abcdef');
8 var buf_a = new Buffer('a');
9 var buf_bc = new Buffer('bc');
10 var buf_f = new Buffer('f');
11 var buf_z = new Buffer('z');
12 var buf_empty = new Buffer('');
14 assert.equal(b.indexOf('a'), 0);
15 assert.equal(b.indexOf('a', 1), -1);
16 assert.equal(b.indexOf('a', -1), -1);
17 assert.equal(b.indexOf('a', -4), -1);
18 assert.equal(b.indexOf('a', -b.length), 0);
19 assert.equal(b.indexOf('a', NaN), 0);
20 assert.equal(b.indexOf('a', -Infinity), 0);
21 assert.equal(b.indexOf('a', Infinity), -1);
22 assert.equal(b.indexOf('bc'), 1);
23 assert.equal(b.indexOf('bc', 2), -1);
24 assert.equal(b.indexOf('bc', -1), -1);
25 assert.equal(b.indexOf('bc', -3), -1);
26 assert.equal(b.indexOf('bc', -5), 1);
27 assert.equal(b.indexOf('bc', NaN), 1);
28 assert.equal(b.indexOf('bc', -Infinity), 1);
29 assert.equal(b.indexOf('bc', Infinity), -1);
30 assert.equal(b.indexOf('f'), b.length - 1);
31 assert.equal(b.indexOf('z'), -1);
32 assert.equal(b.indexOf(''), -1);
33 assert.equal(b.indexOf('', 1), -1);
34 assert.equal(b.indexOf('', b.length + 1), -1);
35 assert.equal(b.indexOf('', Infinity), -1);
36 assert.equal(b.indexOf(buf_a), 0);
37 assert.equal(b.indexOf(buf_a, 1), -1);
38 assert.equal(b.indexOf(buf_a, -1), -1);
39 assert.equal(b.indexOf(buf_a, -4), -1);
40 assert.equal(b.indexOf(buf_a, -b.length), 0);
41 assert.equal(b.indexOf(buf_a, NaN), 0);
42 assert.equal(b.indexOf(buf_a, -Infinity), 0);
43 assert.equal(b.indexOf(buf_a, Infinity), -1);
44 assert.equal(b.indexOf(buf_bc), 1);
45 assert.equal(b.indexOf(buf_bc, 2), -1);
46 assert.equal(b.indexOf(buf_bc, -1), -1);
47 assert.equal(b.indexOf(buf_bc, -3), -1);
48 assert.equal(b.indexOf(buf_bc, -5), 1);
49 assert.equal(b.indexOf(buf_bc, NaN), 1);
50 assert.equal(b.indexOf(buf_bc, -Infinity), 1);
51 assert.equal(b.indexOf(buf_bc, Infinity), -1);
52 assert.equal(b.indexOf(buf_f), b.length - 1);
53 assert.equal(b.indexOf(buf_z), -1);
54 assert.equal(b.indexOf(buf_empty), -1);
55 assert.equal(b.indexOf(buf_empty, 1), -1);
56 assert.equal(b.indexOf(buf_empty, b.length + 1), -1);
57 assert.equal(b.indexOf(buf_empty, Infinity), -1);
58 assert.equal(b.indexOf(0x61), 0);
59 assert.equal(b.indexOf(0x61, 1), -1);
60 assert.equal(b.indexOf(0x61, -1), -1);
61 assert.equal(b.indexOf(0x61, -4), -1);
62 assert.equal(b.indexOf(0x61, -b.length), 0);
63 assert.equal(b.indexOf(0x61, NaN), 0);
64 assert.equal(b.indexOf(0x61, -Infinity), 0);
65 assert.equal(b.indexOf(0x61, Infinity), -1);
66 assert.equal(b.indexOf(0x0), -1);
69 assert.equal(b.indexOf('d', 2), 3);
70 assert.equal(b.indexOf('f', 5), 5);
71 assert.equal(b.indexOf('f', -1), 5);
72 assert.equal(b.indexOf('f', 6), -1);
74 assert.equal(b.indexOf(Buffer('d'), 2), 3);
75 assert.equal(b.indexOf(Buffer('f'), 5), 5);
76 assert.equal(b.indexOf(Buffer('f'), -1), 5);
77 assert.equal(b.indexOf(Buffer('f'), 6), -1);
79 assert.equal(Buffer('ff').indexOf(Buffer('f'), 1, 'ucs2'), -1);
83 Buffer(b.toString('hex'), 'hex')
84 .indexOf('64', 0, 'hex'), 3);
86 Buffer(b.toString('hex'), 'hex')
87 .indexOf(Buffer('64', 'hex'), 0, 'hex'), 3);
89 // test base64 encoding
91 Buffer(b.toString('base64'), 'base64')
92 .indexOf('ZA==', 0, 'base64'), 3);
94 Buffer(b.toString('base64'), 'base64')
95 .indexOf(Buffer('ZA==', 'base64'), 0, 'base64'), 3);
97 // test ascii encoding
99 Buffer(b.toString('ascii'), 'ascii')
100 .indexOf('d', 0, 'ascii'), 3);
102 Buffer(b.toString('ascii'), 'ascii')
103 .indexOf(Buffer('d', 'ascii'), 0, 'ascii'), 3);
105 // test binary encoding
107 Buffer(b.toString('binary'), 'binary')
108 .indexOf('d', 0, 'binary'), 3);
110 Buffer(b.toString('binary'), 'binary')
111 .indexOf(Buffer('d', 'binary'), 0, 'binary'), 3);
114 // test usc2 encoding
115 var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
117 assert.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2'));
118 assert.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2'));
119 assert.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2'));
120 assert.equal(4, twoByteString.indexOf(
121 new Buffer('\u03a3', 'ucs2'), -6, 'ucs2'));
122 assert.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
124 var mixedByteStringUcs2 =
125 new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
126 assert.equal(6, mixedByteStringUcs2.indexOf('bc', 0, 'ucs2'));
127 assert.equal(10, mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2'));
128 assert.equal(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2'));
131 6, mixedByteStringUcs2.indexOf(new Buffer('bc', 'ucs2'), 0, 'ucs2'));
133 10, mixedByteStringUcs2.indexOf(new Buffer('\u03a3', 'ucs2'), 0, 'ucs2'));
135 -1, mixedByteStringUcs2.indexOf(new Buffer('\u0396', 'ucs2'), 0, 'ucs2'));
137 var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
139 // Test single char pattern
140 assert.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2'));
141 assert.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha');
142 assert.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma');
143 assert.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma');
144 assert.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon');
145 assert.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta');
147 // Test multi-char pattern
149 0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
151 2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
153 4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
155 6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
157 var mixedByteStringUtf8 = new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395');
158 assert.equal(5, mixedByteStringUtf8.indexOf('bc'));
159 assert.equal(5, mixedByteStringUtf8.indexOf('bc', 5));
160 assert.equal(5, mixedByteStringUtf8.indexOf('bc', -8));
161 assert.equal(7, mixedByteStringUtf8.indexOf('\u03a3'));
162 assert.equal(-1, mixedByteStringUtf8.indexOf('\u0396'));
165 // Test complex string indexOf algorithms. Only trigger for long strings.
166 // Long string that isn't a simple repeat of a shorter string.
167 var longString = 'A';
168 for (var i = 66; i < 76; i++) { // from 'B' to 'K'
169 longString = longString + String.fromCharCode(i) + longString;
172 var longBufferString = new Buffer(longString);
174 // pattern of 15 chars, repeated every 16 chars in long
175 var pattern = 'ABACABADABACABA';
176 for (var i = 0; i < longBufferString.length - pattern.length; i += 7) {
177 var index = longBufferString.indexOf(pattern, i);
178 assert.equal((i + 15) & ~0xf, index, 'Long ABACABA...-string at index ' + i);
180 assert.equal(510, longBufferString.indexOf('AJABACA'), 'Long AJABACA, First J');
182 1534, longBufferString.indexOf('AJABACA', 511), 'Long AJABACA, Second J');
184 pattern = 'JABACABADABACABA';
186 511, longBufferString.indexOf(pattern), 'Long JABACABA..., First J');
188 1535, longBufferString.indexOf(pattern, 512), 'Long JABACABA..., Second J');
190 // Search for a non-ASCII string in a pure ASCII string.
191 var asciiString = new Buffer(
192 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf');
193 assert.equal(-1, asciiString.indexOf('\x2061'));
194 assert.equal(3, asciiString.indexOf('leb', 0));
196 // Search in string containing many non-ASCII chars.
197 var allCodePoints = [];
198 for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
199 var allCharsString = String.fromCharCode.apply(String, allCodePoints);
200 var allCharsBufferUtf8 = new Buffer(allCharsString);
201 var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
203 // Search for string long enough to trigger complex search with ASCII pattern
205 assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
206 assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
208 // Find substrings in Utf8.
209 var lengths = [1, 3, 15]; // Single char, simple and complex.
210 var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
211 for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
212 for (var i = 0; i < indices.length; i++) {
213 var index = indices[i];
214 var length = lengths[lengthIndex];
216 if (index + length > 0x7F) {
220 if (index + length > 0x7FF) {
224 if (index + length > 0xFFFF) {
228 var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
229 assert.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8));
231 var patternStringUtf8 = patternBufferUtf8.toString();
232 assert.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8));
236 // Find substrings in Usc2.
237 var lengths = [2, 4, 16]; // Single char, simple and complex.
238 var indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
239 for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
240 for (var i = 0; i < indices.length; i++) {
241 var index = indices[i] * 2;
242 var length = lengths[lengthIndex];
244 var patternBufferUcs2 =
245 allCharsBufferUcs2.slice(index, index + length);
247 index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2'));
249 var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
251 index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2'));
255 assert.throws(function() {
256 b.indexOf(function() { });
258 assert.throws(function() {
261 assert.throws(function() {