11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_cirequire('../common'); 251cb0ef41Sopenharmony_ciconst assert = require('assert'); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('hello', 'utf8').slice(0, 0).length, 0); 281cb0ef41Sopenharmony_ciassert.strictEqual(Buffer('hello', 'utf8').slice(0, 0).length, 0); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciconst buf = Buffer.from('0123456789', 'utf8'); 311cb0ef41Sopenharmony_ciconst expectedSameBufs = [ 321cb0ef41Sopenharmony_ci [buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')], 331cb0ef41Sopenharmony_ci [buf.slice(-20, 10), Buffer.from('0123456789', 'utf8')], 341cb0ef41Sopenharmony_ci [buf.slice(-20, -10), Buffer.from('', 'utf8')], 351cb0ef41Sopenharmony_ci [buf.slice(), Buffer.from('0123456789', 'utf8')], 361cb0ef41Sopenharmony_ci [buf.slice(0), Buffer.from('0123456789', 'utf8')], 371cb0ef41Sopenharmony_ci [buf.slice(0, 0), Buffer.from('', 'utf8')], 381cb0ef41Sopenharmony_ci [buf.slice(undefined), Buffer.from('0123456789', 'utf8')], 391cb0ef41Sopenharmony_ci [buf.slice('foobar'), Buffer.from('0123456789', 'utf8')], 401cb0ef41Sopenharmony_ci [buf.slice(undefined, undefined), Buffer.from('0123456789', 'utf8')], 411cb0ef41Sopenharmony_ci [buf.slice(2), Buffer.from('23456789', 'utf8')], 421cb0ef41Sopenharmony_ci [buf.slice(5), Buffer.from('56789', 'utf8')], 431cb0ef41Sopenharmony_ci [buf.slice(10), Buffer.from('', 'utf8')], 441cb0ef41Sopenharmony_ci [buf.slice(5, 8), Buffer.from('567', 'utf8')], 451cb0ef41Sopenharmony_ci [buf.slice(8, -1), Buffer.from('8', 'utf8')], 461cb0ef41Sopenharmony_ci [buf.slice(-10), Buffer.from('0123456789', 'utf8')], 471cb0ef41Sopenharmony_ci [buf.slice(0, -9), Buffer.from('0', 'utf8')], 481cb0ef41Sopenharmony_ci [buf.slice(0, -10), Buffer.from('', 'utf8')], 491cb0ef41Sopenharmony_ci [buf.slice(0, -1), Buffer.from('012345678', 'utf8')], 501cb0ef41Sopenharmony_ci [buf.slice(2, -2), Buffer.from('234567', 'utf8')], 511cb0ef41Sopenharmony_ci [buf.slice(0, 65536), Buffer.from('0123456789', 'utf8')], 521cb0ef41Sopenharmony_ci [buf.slice(65536, 0), Buffer.from('', 'utf8')], 531cb0ef41Sopenharmony_ci [buf.slice(-5, -8), Buffer.from('', 'utf8')], 541cb0ef41Sopenharmony_ci [buf.slice(-5, -3), Buffer.from('56', 'utf8')], 551cb0ef41Sopenharmony_ci [buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')], 561cb0ef41Sopenharmony_ci [buf.slice('0', '1'), Buffer.from('0', 'utf8')], 571cb0ef41Sopenharmony_ci [buf.slice('-5', '10'), Buffer.from('56789', 'utf8')], 581cb0ef41Sopenharmony_ci [buf.slice('-10', '10'), Buffer.from('0123456789', 'utf8')], 591cb0ef41Sopenharmony_ci [buf.slice('-10', '-5'), Buffer.from('01234', 'utf8')], 601cb0ef41Sopenharmony_ci [buf.slice('-10', '-0'), Buffer.from('', 'utf8')], 611cb0ef41Sopenharmony_ci [buf.slice('111'), Buffer.from('', 'utf8')], 621cb0ef41Sopenharmony_ci [buf.slice('0', '-111'), Buffer.from('', 'utf8')], 631cb0ef41Sopenharmony_ci]; 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_cifor (let i = 0, s = buf.toString(); i < buf.length; ++i) { 661cb0ef41Sopenharmony_ci expectedSameBufs.push( 671cb0ef41Sopenharmony_ci [buf.slice(i), Buffer.from(s.slice(i))], 681cb0ef41Sopenharmony_ci [buf.slice(0, i), Buffer.from(s.slice(0, i))], 691cb0ef41Sopenharmony_ci [buf.slice(-i), Buffer.from(s.slice(-i))], 701cb0ef41Sopenharmony_ci [buf.slice(0, -i), Buffer.from(s.slice(0, -i))] 711cb0ef41Sopenharmony_ci ); 721cb0ef41Sopenharmony_ci} 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ciexpectedSameBufs.forEach(([buf1, buf2]) => { 751cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.compare(buf1, buf2), 0); 761cb0ef41Sopenharmony_ci}); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ciconst utf16Buf = Buffer.from('0123456789', 'utf16le'); 791cb0ef41Sopenharmony_ciassert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le')); 801cb0ef41Sopenharmony_ci// Try to slice a zero length Buffer. 811cb0ef41Sopenharmony_ci// See https://github.com/joyent/node/issues/5881 821cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.alloc(0).slice(0, 1).length, 0); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci{ 851cb0ef41Sopenharmony_ci // Single argument slice 861cb0ef41Sopenharmony_ci assert.strictEqual(Buffer.from('abcde', 'utf8').slice(1).toString('utf8'), 871cb0ef41Sopenharmony_ci 'bcde'); 881cb0ef41Sopenharmony_ci} 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci// slice(0,0).length === 0 911cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.from('hello', 'utf8').slice(0, 0).length, 0); 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci{ 941cb0ef41Sopenharmony_ci // Regression tests for https://github.com/nodejs/node/issues/9096 951cb0ef41Sopenharmony_ci const buf = Buffer.from('abcd', 'utf8'); 961cb0ef41Sopenharmony_ci assert.strictEqual(buf.slice(buf.length / 3).toString('utf8'), 'bcd'); 971cb0ef41Sopenharmony_ci assert.strictEqual( 981cb0ef41Sopenharmony_ci buf.slice(buf.length / 3, buf.length).toString(), 991cb0ef41Sopenharmony_ci 'bcd' 1001cb0ef41Sopenharmony_ci ); 1011cb0ef41Sopenharmony_ci} 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci{ 1041cb0ef41Sopenharmony_ci const buf = Buffer.from('abcdefg', 'utf8'); 1051cb0ef41Sopenharmony_ci assert.strictEqual(buf.slice(-(-1 >>> 0) - 1).toString('utf8'), 1061cb0ef41Sopenharmony_ci buf.toString('utf8')); 1071cb0ef41Sopenharmony_ci} 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci{ 1101cb0ef41Sopenharmony_ci const buf = Buffer.from('abc', 'utf8'); 1111cb0ef41Sopenharmony_ci assert.strictEqual(buf.slice(-0.5).toString('utf8'), buf.toString('utf8')); 1121cb0ef41Sopenharmony_ci} 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci{ 1151cb0ef41Sopenharmony_ci const buf = Buffer.from([ 1161cb0ef41Sopenharmony_ci 1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0, 1171cb0ef41Sopenharmony_ci 0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0, 1181cb0ef41Sopenharmony_ci ]); 1191cb0ef41Sopenharmony_ci const chunk1 = Buffer.from([ 1201cb0ef41Sopenharmony_ci 1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0, 1211cb0ef41Sopenharmony_ci ]); 1221cb0ef41Sopenharmony_ci const chunk2 = Buffer.from([ 1231cb0ef41Sopenharmony_ci 0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0, 1241cb0ef41Sopenharmony_ci ]); 1251cb0ef41Sopenharmony_ci const middle = buf.length / 2; 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.slice(0, middle), chunk1); 1281cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf.slice(middle), chunk2); 1291cb0ef41Sopenharmony_ci} 130