You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

400 lines
11 KiB

4 years ago
  1. 'use strict';
  2. var test = require('tape')
  3. var bindexOf = require('../')
  4. var b = new Buffer('abcdef');
  5. var buf_a = new Buffer('a');
  6. var buf_bc = new Buffer('bc');
  7. var buf_f = new Buffer('f');
  8. var buf_z = new Buffer('z');
  9. var buf_empty = new Buffer('');
  10. test('node 6 buffer indexOf tests', function(t) {
  11. t.equal(bindexOf(b, 'a'), 0);
  12. t.equal(bindexOf(b, 'a', 1), -1);
  13. t.equal(bindexOf(b, 'a', -1), -1);
  14. t.equal(bindexOf(b, 'a', -4), -1);
  15. t.equal(bindexOf(b, 'a', -b.length), 0);
  16. t.equal(bindexOf(b, 'a', NaN), 0);
  17. t.equal(bindexOf(b, 'a', -Infinity), 0);
  18. t.equal(bindexOf(b, 'a', Infinity), -1);
  19. t.equal(bindexOf(b, 'bc'), 1);
  20. t.equal(bindexOf(b, 'bc', 2), -1);
  21. t.equal(bindexOf(b, 'bc', -1), -1);
  22. t.equal(bindexOf(b, 'bc', -3), -1);
  23. t.equal(bindexOf(b, 'bc', -5), 1);
  24. t.equal(bindexOf(b, 'bc', NaN), 1);
  25. t.equal(bindexOf(b, 'bc', -Infinity), 1);
  26. t.equal(bindexOf(b, 'bc', Infinity), -1);
  27. t.equal(bindexOf(b, 'f'), b.length - 1);
  28. t.equal(bindexOf(b, 'z'), -1);
  29. t.equal(bindexOf(b, ''), -1);
  30. t.equal(bindexOf(b, '', 1), -1);
  31. t.equal(bindexOf(b, '', b.length + 1), -1);
  32. t.equal(bindexOf(b, '', Infinity), -1);
  33. t.equal(bindexOf(b, buf_a), 0);
  34. t.equal(bindexOf(b, buf_a, 1), -1);
  35. t.equal(bindexOf(b, buf_a, -1), -1);
  36. t.equal(bindexOf(b, buf_a, -4), -1);
  37. t.equal(bindexOf(b, buf_a, -b.length), 0);
  38. t.equal(bindexOf(b, buf_a, NaN), 0);
  39. t.equal(bindexOf(b, buf_a, -Infinity), 0);
  40. t.equal(bindexOf(b, buf_a, Infinity), -1);
  41. t.equal(bindexOf(b, buf_bc), 1);
  42. t.equal(bindexOf(b, buf_bc, 2), -1);
  43. t.equal(bindexOf(b, buf_bc, -1), -1);
  44. t.equal(bindexOf(b, buf_bc, -3), -1);
  45. t.equal(bindexOf(b, buf_bc, -5), 1);
  46. t.equal(bindexOf(b, buf_bc, NaN), 1);
  47. t.equal(bindexOf(b, buf_bc, -Infinity), 1);
  48. t.equal(bindexOf(b, buf_bc, Infinity), -1);
  49. t.equal(bindexOf(b, buf_f), b.length - 1);
  50. t.equal(bindexOf(b, buf_z), -1);
  51. t.equal(bindexOf(b, buf_empty), -1);
  52. t.equal(bindexOf(b, buf_empty, 1), -1);
  53. t.equal(bindexOf(b, buf_empty, b.length + 1), -1);
  54. t.equal(bindexOf(b, buf_empty, Infinity), -1);
  55. t.equal(bindexOf(b, 0x61), 0);
  56. t.equal(bindexOf(b, 0x61, 1), -1);
  57. t.equal(bindexOf(b, 0x61, -1), -1);
  58. t.equal(bindexOf(b, 0x61, -4), -1);
  59. t.equal(bindexOf(b, 0x61, -b.length), 0);
  60. t.equal(bindexOf(b, 0x61, NaN), 0);
  61. t.equal(bindexOf(b, 0x61, -Infinity), 0);
  62. t.equal(bindexOf(b, 0x61, Infinity), -1);
  63. t.equal(bindexOf(b, 0x0), -1);
  64. // test offsets
  65. t.equal(bindexOf(b, 'd', 2), 3);
  66. t.equal(bindexOf(b, 'f', 5), 5);
  67. t.equal(bindexOf(b, 'f', -1), 5);
  68. t.equal(bindexOf(b, 'f', 6), -1);
  69. t.equal(bindexOf(b, new Buffer('d'), 2), 3);
  70. t.equal(bindexOf(b, new Buffer('f'), 5), 5);
  71. t.equal(bindexOf(b, new Buffer('f'), -1), 5);
  72. t.equal(bindexOf(b, new Buffer('f'), 6), -1);
  73. // This one doesn't make any sense
  74. // t.equal(bindexOf(new Buffer('ff'), new Buffer('f'), 1, 'ucs2'), -1);
  75. // test hex encoding
  76. t.equal(
  77. bindexOf(
  78. new Buffer(b.toString('hex'), 'hex'),
  79. '64',
  80. 0,
  81. 'hex'
  82. ),
  83. 3
  84. );
  85. t.equal(
  86. bindexOf(
  87. new Buffer(b.toString('hex'), 'hex'),
  88. new Buffer('64', 'hex'), 0, 'hex'
  89. ),
  90. 3
  91. );
  92. // test base64 encoding
  93. t.equal(
  94. bindexOf(
  95. new Buffer(b.toString('base64'), 'base64'),
  96. 'ZA==', 0, 'base64'
  97. ),
  98. 3
  99. );
  100. t.equal(
  101. bindexOf(
  102. new Buffer(b.toString('base64'), 'base64'),
  103. new Buffer('ZA==', 'base64'), 0, 'base64'
  104. ),
  105. 3
  106. );
  107. // test ascii encoding
  108. t.equal(
  109. bindexOf(
  110. new Buffer(b.toString('ascii'), 'ascii'),
  111. 'd', 0, 'ascii'
  112. ),
  113. 3
  114. );
  115. t.equal(
  116. bindexOf(
  117. new Buffer(b.toString('ascii'), 'ascii'),
  118. new Buffer('d', 'ascii'), 0, 'ascii'
  119. ),
  120. 3
  121. );
  122. // test latin1 encoding
  123. // does not work in LTS
  124. /*
  125. t.equal(
  126. bindexOf(
  127. new Buffer(b.toString('latin1'), 'latin1'),
  128. 'd',
  129. 0,
  130. 'latin1'
  131. ),
  132. 3
  133. );
  134. t.equal(
  135. bindexOf(
  136. new Buffer(b.toString('latin1'), 'latin1'),
  137. new Buffer('d', 'latin1'),
  138. 0,
  139. 'latin1'
  140. ),
  141. 3
  142. );
  143. t.equal(
  144. bindexOf(
  145. new Buffer('aa\u00e8aa', 'latin1'),
  146. '\u00e8',
  147. 'latin1'
  148. ),
  149. 2
  150. );
  151. t.equal(
  152. bindexOf(
  153. new Buffer('\u00e8', 'latin1'),
  154. '\u00e8',
  155. 'latin1'
  156. ),
  157. 0
  158. );
  159. t.equal(
  160. bindexOf(
  161. new Buffer('\u00e8', 'latin1'),
  162. new Buffer('\u00e8', 'latin1'),
  163. 0,
  164. 'latin1'
  165. ),
  166. 0
  167. );
  168. */
  169. // test binary encoding
  170. t.equal(
  171. bindexOf(
  172. new Buffer(b.toString('binary'), 'binary'),
  173. 'd',
  174. 0,
  175. 'binary'
  176. ),
  177. 3
  178. );
  179. t.equal(
  180. bindexOf(
  181. new Buffer(b.toString('binary'), 'binary'),
  182. new Buffer('d', 'binary'),
  183. 0,
  184. 'binary'
  185. ),
  186. 3
  187. );
  188. t.equal(
  189. bindexOf(
  190. new Buffer('aa\u00e8aa', 'binary'),
  191. '\u00e8',
  192. 0,
  193. 'binary'
  194. ),
  195. 2
  196. );
  197. t.equal(
  198. bindexOf(
  199. new Buffer('\u00e8', 'binary'),
  200. '\u00e8',
  201. 0,
  202. 'binary'
  203. ),
  204. 0
  205. );
  206. t.equal(
  207. bindexOf(
  208. new Buffer('\u00e8', 'binary'),
  209. new Buffer('\u00e8', 'binary'),
  210. 0,
  211. 'binary'
  212. ),
  213. 0
  214. );
  215. // test optional offset with passed encoding
  216. t.equal(new Buffer('aaaa0').indexOf('30', 'hex'), 4);
  217. t.equal(new Buffer('aaaa00a').indexOf('3030', 'hex'), 4);
  218. {
  219. // test usc2 encoding
  220. var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
  221. t.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2'));
  222. t.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2'));
  223. t.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2'));
  224. t.equal(4, twoByteString.indexOf(
  225. new Buffer('\u03a3', 'ucs2'), -6, 'ucs2'));
  226. t.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
  227. }
  228. var mixedByteStringUcs2 =
  229. new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
  230. t.equal(6, mixedByteStringUcs2.indexOf('bc', 0, 'ucs2'));
  231. t.equal(10, mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2'));
  232. t.equal(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2'));
  233. t.equal(
  234. 6, mixedByteStringUcs2.indexOf(new Buffer('bc', 'ucs2'), 0, 'ucs2'));
  235. t.equal(
  236. 10, mixedByteStringUcs2.indexOf(new Buffer('\u03a3', 'ucs2'), 0, 'ucs2'));
  237. t.equal(
  238. -1, mixedByteStringUcs2.indexOf(new Buffer('\u0396', 'ucs2'), 0, 'ucs2'));
  239. {
  240. var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
  241. // Test single char pattern
  242. t.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2'));
  243. t.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha');
  244. t.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma');
  245. t.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma');
  246. t.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon');
  247. t.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta');
  248. // Test multi-char pattern
  249. t.equal(
  250. 0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
  251. t.equal(
  252. 2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
  253. t.equal(
  254. 4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
  255. t.equal(
  256. 6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
  257. }
  258. var mixedByteStringUtf8 = new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395');
  259. t.equal(5, mixedByteStringUtf8.indexOf('bc'));
  260. t.equal(5, mixedByteStringUtf8.indexOf('bc', 5));
  261. t.equal(5, mixedByteStringUtf8.indexOf('bc', -8));
  262. t.equal(7, mixedByteStringUtf8.indexOf('\u03a3'));
  263. t.equal(-1, mixedByteStringUtf8.indexOf('\u0396'));
  264. // Test complex string indexOf algorithms. Only trigger for long strings.
  265. // Long string that isn't a simple repeat of a shorter string.
  266. var longString = 'A';
  267. for (var i = 66; i < 76; i++) { // from 'B' to 'K'
  268. longString = longString + String.fromCharCode(i) + longString;
  269. }
  270. var longBufferString = new Buffer(longString);
  271. // pattern of 15 chars, repeated every 16 chars in long
  272. var pattern = 'ABACABADABACABA';
  273. for (var i = 0; i < longBufferString.length - pattern.length; i += 7) {
  274. var index = longBufferString.indexOf(pattern, i);
  275. t.equal((i + 15) & ~0xf, index, 'Long ABACABA...-string at index ' + i);
  276. }
  277. t.equal(510, longBufferString.indexOf('AJABACA'), 'Long AJABACA, First J');
  278. t.equal(
  279. 1534, longBufferString.indexOf('AJABACA', 511), 'Long AJABACA, Second J');
  280. pattern = 'JABACABADABACABA';
  281. t.equal(
  282. 511, longBufferString.indexOf(pattern), 'Long JABACABA..., First J');
  283. t.equal(
  284. 1535, longBufferString.indexOf(pattern, 512), 'Long JABACABA..., Second J');
  285. // Search for a non-ASCII string in a pure ASCII string.
  286. var asciiString = new Buffer(
  287. 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf');
  288. t.equal(-1, asciiString.indexOf('\x2061'));
  289. t.equal(3, asciiString.indexOf('leb', 0));
  290. // Search in string containing many non-ASCII chars.
  291. var allCodePoints = [];
  292. for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
  293. var allCharsString = String.fromCharCode.apply(String, allCodePoints);
  294. var allCharsBufferUtf8 = new Buffer(allCharsString);
  295. var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
  296. // Search for string long enough to trigger complex search with ASCII pattern
  297. // and UC16 subject.
  298. t.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
  299. t.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
  300. // Needle is longer than haystack, but only because it's encoded as UTF-16
  301. t.equal(new Buffer('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1);
  302. t.equal(new Buffer('aaaa').indexOf('a'.repeat(4), 'utf8'), 0);
  303. t.equal(new Buffer('aaaa').indexOf('你好', 'ucs2'), -1);
  304. // Haystack has odd length, but the needle is UCS2.
  305. t.equal(new Buffer('aaaaa').indexOf('b', 'ucs2'), -1);
  306. {
  307. // Find substrings in Utf8.
  308. var lengths = [1, 3, 15]; // Single char, simple and complex.
  309. var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
  310. for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
  311. for (var i = 0; i < indices.length; i++) {
  312. var index = indices[i];
  313. var length = lengths[lengthIndex];
  314. if (index + length > 0x7F) {
  315. length = 2 * length;
  316. }
  317. if (index + length > 0x7FF) {
  318. length = 3 * length;
  319. }
  320. if (index + length > 0xFFFF) {
  321. length = 4 * length;
  322. }
  323. var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
  324. t.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8));
  325. var patternStringUtf8 = patternBufferUtf8.toString();
  326. t.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8));
  327. }
  328. }
  329. }
  330. {
  331. // Find substrings in Usc2.
  332. var lengths = [2, 4, 16]; // Single char, simple and complex.
  333. var indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
  334. for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
  335. for (var i = 0; i < indices.length; i++) {
  336. var index = indices[i] * 2;
  337. var length = lengths[lengthIndex];
  338. var patternBufferUcs2 =
  339. allCharsBufferUcs2.slice(index, index + length);
  340. t.equal(
  341. index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2'));
  342. var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
  343. t.equal(
  344. index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2'));
  345. }
  346. }
  347. }
  348. t.throws(function() {
  349. bindexOf(b, function() { });
  350. });
  351. t.throws(function() {
  352. bindexOf(b, {});
  353. });
  354. t.throws(function() {
  355. bindexOf(b, []);
  356. });
  357. t.end();
  358. });