Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

test-buffer-includes.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. var Buffer = require('../../').Buffer;
  5. var b = Buffer.from('abcdef');
  6. var buf_a = Buffer.from('a');
  7. var buf_bc = Buffer.from('bc');
  8. var buf_f = Buffer.from('f');
  9. var buf_z = Buffer.from('z');
  10. var buf_empty = Buffer.from('');
  11. assert(b.includes('a'));
  12. assert(!b.includes('a', 1));
  13. assert(!b.includes('a', -1));
  14. assert(!b.includes('a', -4));
  15. assert(b.includes('a', -b.length));
  16. assert(b.includes('a', NaN));
  17. assert(b.includes('a', -Infinity));
  18. assert(!b.includes('a', Infinity));
  19. assert(b.includes('bc'));
  20. assert(!b.includes('bc', 2));
  21. assert(!b.includes('bc', -1));
  22. assert(!b.includes('bc', -3));
  23. assert(b.includes('bc', -5));
  24. assert(b.includes('bc', NaN));
  25. assert(b.includes('bc', -Infinity));
  26. assert(!b.includes('bc', Infinity));
  27. assert(b.includes('f'), b.length - 1);
  28. assert(!b.includes('z'));
  29. assert(!b.includes(''));
  30. assert(!b.includes('', 1));
  31. assert(!b.includes('', b.length + 1));
  32. assert(!b.includes('', Infinity));
  33. assert(b.includes(buf_a));
  34. assert(!b.includes(buf_a, 1));
  35. assert(!b.includes(buf_a, -1));
  36. assert(!b.includes(buf_a, -4));
  37. assert(b.includes(buf_a, -b.length));
  38. assert(b.includes(buf_a, NaN));
  39. assert(b.includes(buf_a, -Infinity));
  40. assert(!b.includes(buf_a, Infinity));
  41. assert(b.includes(buf_bc));
  42. assert(!b.includes(buf_bc, 2));
  43. assert(!b.includes(buf_bc, -1));
  44. assert(!b.includes(buf_bc, -3));
  45. assert(b.includes(buf_bc, -5));
  46. assert(b.includes(buf_bc, NaN));
  47. assert(b.includes(buf_bc, -Infinity));
  48. assert(!b.includes(buf_bc, Infinity));
  49. assert(b.includes(buf_f), b.length - 1);
  50. assert(!b.includes(buf_z));
  51. assert(!b.includes(buf_empty));
  52. assert(!b.includes(buf_empty, 1));
  53. assert(!b.includes(buf_empty, b.length + 1));
  54. assert(!b.includes(buf_empty, Infinity));
  55. assert(b.includes(0x61));
  56. assert(!b.includes(0x61, 1));
  57. assert(!b.includes(0x61, -1));
  58. assert(!b.includes(0x61, -4));
  59. assert(b.includes(0x61, -b.length));
  60. assert(b.includes(0x61, NaN));
  61. assert(b.includes(0x61, -Infinity));
  62. assert(!b.includes(0x61, Infinity));
  63. assert(!b.includes(0x0));
  64. // test offsets
  65. assert(b.includes('d', 2));
  66. assert(b.includes('f', 5));
  67. assert(b.includes('f', -1));
  68. assert(!b.includes('f', 6));
  69. assert(b.includes(Buffer.from('d'), 2));
  70. assert(b.includes(Buffer.from('f'), 5));
  71. assert(b.includes(Buffer.from('f'), -1));
  72. assert(!b.includes(Buffer.from('f'), 6));
  73. assert(!Buffer.from('ff').includes(Buffer.from('f'), 1, 'ucs2'));
  74. // test hex encoding
  75. assert.strictEqual(
  76. Buffer.from(b.toString('hex'), 'hex')
  77. .includes('64', 0, 'hex'),
  78. true
  79. );
  80. assert.strictEqual(
  81. Buffer.from(b.toString('hex'), 'hex')
  82. .includes(Buffer.from('64', 'hex'), 0, 'hex'),
  83. true
  84. );
  85. // test base64 encoding
  86. assert.strictEqual(
  87. Buffer.from(b.toString('base64'), 'base64')
  88. .includes('ZA==', 0, 'base64'),
  89. true
  90. );
  91. assert.strictEqual(
  92. Buffer.from(b.toString('base64'), 'base64')
  93. .includes(Buffer.from('ZA==', 'base64'), 0, 'base64'),
  94. true
  95. );
  96. // test ascii encoding
  97. assert.strictEqual(
  98. Buffer.from(b.toString('ascii'), 'ascii')
  99. .includes('d', 0, 'ascii'),
  100. true
  101. );
  102. assert.strictEqual(
  103. Buffer.from(b.toString('ascii'), 'ascii')
  104. .includes(Buffer.from('d', 'ascii'), 0, 'ascii'),
  105. true
  106. );
  107. // test latin1 encoding
  108. assert.strictEqual(
  109. Buffer.from(b.toString('latin1'), 'latin1')
  110. .includes('d', 0, 'latin1'),
  111. true
  112. );
  113. assert.strictEqual(
  114. Buffer.from(b.toString('latin1'), 'latin1')
  115. .includes(Buffer.from('d', 'latin1'), 0, 'latin1'),
  116. true
  117. );
  118. // test binary encoding
  119. assert.strictEqual(
  120. Buffer.from(b.toString('binary'), 'binary')
  121. .includes('d', 0, 'binary'),
  122. true
  123. );
  124. assert.strictEqual(
  125. Buffer.from(b.toString('binary'), 'binary')
  126. .includes(Buffer.from('d', 'binary'), 0, 'binary'),
  127. true
  128. );
  129. // test usc2 encoding
  130. var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
  131. assert(twoByteString.includes('\u0395', 4, 'ucs2'));
  132. assert(twoByteString.includes('\u03a3', -4, 'ucs2'));
  133. assert(twoByteString.includes('\u03a3', -6, 'ucs2'));
  134. assert(twoByteString.includes(
  135. Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2'));
  136. assert(!twoByteString.includes('\u03a3', -2, 'ucs2'));
  137. var mixedByteStringUcs2 =
  138. Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
  139. assert(mixedByteStringUcs2.includes('bc', 0, 'ucs2'));
  140. assert(mixedByteStringUcs2.includes('\u03a3', 0, 'ucs2'));
  141. assert(!mixedByteStringUcs2.includes('\u0396', 0, 'ucs2'));
  142. assert(
  143. 6, mixedByteStringUcs2.includes(Buffer.from('bc', 'ucs2'), 0, 'ucs2'));
  144. assert(
  145. 10, mixedByteStringUcs2.includes(Buffer.from('\u03a3', 'ucs2'),
  146. 0, 'ucs2'));
  147. assert(
  148. -1, mixedByteStringUcs2.includes(Buffer.from('\u0396', 'ucs2'),
  149. 0, 'ucs2'));
  150. twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
  151. // Test single char pattern
  152. assert(twoByteString.includes('\u039a', 0, 'ucs2'));
  153. assert(twoByteString.includes('\u0391', 0, 'ucs2'), 'Alpha');
  154. assert(twoByteString.includes('\u03a3', 0, 'ucs2'), 'First Sigma');
  155. assert(twoByteString.includes('\u03a3', 6, 'ucs2'), 'Second Sigma');
  156. assert(twoByteString.includes('\u0395', 0, 'ucs2'), 'Epsilon');
  157. assert(!twoByteString.includes('\u0392', 0, 'ucs2'), 'Not beta');
  158. // Test multi-char pattern
  159. assert(twoByteString.includes('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
  160. assert(twoByteString.includes('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
  161. assert(twoByteString.includes('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
  162. assert(twoByteString.includes('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
  163. var mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395');
  164. assert(mixedByteStringUtf8.includes('bc'));
  165. assert(mixedByteStringUtf8.includes('bc', 5));
  166. assert(mixedByteStringUtf8.includes('bc', -8));
  167. assert(mixedByteStringUtf8.includes('\u03a3'));
  168. assert(!mixedByteStringUtf8.includes('\u0396'));
  169. // Test complex string includes algorithms. Only trigger for long strings.
  170. // Long string that isn't a simple repeat of a shorter string.
  171. var longString = 'A';
  172. for (var i = 66; i < 76; i++) { // from 'B' to 'K'
  173. longString = longString + String.fromCharCode(i) + longString;
  174. }
  175. var longBufferString = Buffer.from(longString);
  176. // pattern of 15 chars, repeated every 16 chars in long
  177. var pattern = 'ABACABADABACABA';
  178. for (var i = 0; i < longBufferString.length - pattern.length; i += 7) {
  179. var includes = longBufferString.includes(pattern, i);
  180. assert(includes, 'Long ABACABA...-string at index ' + i);
  181. }
  182. assert(longBufferString.includes('AJABACA'), 'Long AJABACA, First J');
  183. assert(longBufferString.includes('AJABACA', 511), 'Long AJABACA, Second J');
  184. pattern = 'JABACABADABACABA';
  185. assert(longBufferString.includes(pattern), 'Long JABACABA..., First J');
  186. assert(longBufferString.includes(pattern, 512), 'Long JABACABA..., Second J');
  187. // Search for a non-ASCII string in a pure ASCII string.
  188. var asciiString = Buffer.from(
  189. 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf');
  190. assert(!asciiString.includes('\x2061'));
  191. assert(asciiString.includes('leb', 0));
  192. // Search in string containing many non-ASCII chars.
  193. var allCodePoints = [];
  194. for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
  195. var allCharsString = String.fromCharCode.apply(String, allCodePoints);
  196. var allCharsBufferUtf8 = Buffer.from(allCharsString);
  197. var allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2');
  198. // Search for string long enough to trigger complex search with ASCII pattern
  199. // and UC16 subject.
  200. assert(!allCharsBufferUtf8.includes('notfound'));
  201. assert(!allCharsBufferUcs2.includes('notfound'));
  202. // Find substrings in Utf8.
  203. var lengths = [1, 3, 15]; // Single char, simple and complex.
  204. var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
  205. for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
  206. for (var i = 0; i < indices.length; i++) {
  207. var index = indices[i];
  208. var length = lengths[lengthIndex];
  209. if (index + length > 0x7F) {
  210. length = 2 * length;
  211. }
  212. if (index + length > 0x7FF) {
  213. length = 3 * length;
  214. }
  215. if (index + length > 0xFFFF) {
  216. length = 4 * length;
  217. }
  218. var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
  219. assert(index, allCharsBufferUtf8.includes(patternBufferUtf8));
  220. var patternStringUtf8 = patternBufferUtf8.toString();
  221. assert(index, allCharsBufferUtf8.includes(patternStringUtf8));
  222. }
  223. }
  224. // Find substrings in Usc2.
  225. lengths = [2, 4, 16]; // Single char, simple and complex.
  226. indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
  227. for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
  228. for (var i = 0; i < indices.length; i++) {
  229. var index = indices[i] * 2;
  230. var length = lengths[lengthIndex];
  231. var patternBufferUcs2 =
  232. allCharsBufferUcs2.slice(index, index + length);
  233. assert(
  234. index, allCharsBufferUcs2.includes(patternBufferUcs2, 0, 'ucs2'));
  235. var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
  236. assert(
  237. index, allCharsBufferUcs2.includes(patternStringUcs2, 0, 'ucs2'));
  238. }
  239. }
  240. assert.throws(function() {
  241. b.includes(function() { });
  242. });
  243. assert.throws(function() {
  244. b.includes({});
  245. });
  246. assert.throws(function() {
  247. b.includes([]);
  248. });
  249. // test truncation of Number arguments to uint8
  250. {
  251. var buf = Buffer.from('this is a test');
  252. assert.ok(buf.includes(0x6973));
  253. assert.ok(buf.includes(0x697320));
  254. assert.ok(buf.includes(0x69732069));
  255. assert.ok(buf.includes(0x697374657374));
  256. assert.ok(buf.includes(0x69737374));
  257. assert.ok(buf.includes(0x69737465));
  258. assert.ok(buf.includes(0x69737465));
  259. assert.ok(buf.includes(-140));
  260. assert.ok(buf.includes(-152));
  261. assert.ok(!buf.includes(0xff));
  262. assert.ok(!buf.includes(0xffff));
  263. }