| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a38e2f5 commit 4810e4b
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -240,7 +240,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) { | |||
| 240 | 240 | return _copyActual(source, target, targetStart, sourceStart, sourceEnd); | |
| 241 | 241 | } | |
| 242 | 242 | ||
| 243 | - function _copyActual(source, target, targetStart, sourceStart, sourceEnd) { | ||
| 243 | + function _copyActual(source, target, targetStart, sourceStart, sourceEnd, isUint8Copy = false) { | ||
| 244 | 244 | if (sourceEnd - sourceStart > target.byteLength - targetStart) | |
| 245 | 245 | sourceEnd = sourceStart + target.byteLength - targetStart; | |
| 246 | 246 | ||
@@ -252,7 +252,11 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd) { | |||
| 252 | 252 | if (nb <= 0) | |
| 253 | 253 | return 0; | |
| 254 | 254 | ||
| 255 | - _copy(source, target, targetStart, sourceStart, nb); | ||
| 255 | + if (sourceStart === 0 && nb === sourceLen && (isUint8Copy || isUint8Array(target))) { | ||
| 256 | + TypedArrayPrototypeSet(target, source, targetStart); | ||
| 257 | + } else { | ||
| 258 | + _copy(source, target, targetStart, sourceStart, nb); | ||
| 259 | + } | ||
| 256 | 260 | ||
| 257 | 261 | return nb; | |
| 258 | 262 | } | |
@@ -600,7 +604,7 @@ Buffer.concat = function concat(list, length) { | |||
| 600 | 604 | throw new ERR_INVALID_ARG_TYPE( | |
| 601 | 605 | `list[${i}]`, ['Buffer', 'Uint8Array'], list[i]); | |
| 602 | 606 | } | |
| 603 | - pos += _copyActual(buf, buffer, pos, 0, buf.length); | ||
| 607 | + pos += _copyActual(buf, buffer, pos, 0, buf.length, true); | ||
| 604 | 608 | } | |
| 605 | 609 | ||
| 606 | 610 | // Note: `length` is always equal to `buffer.length` at this point | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -76,6 +76,13 @@ assert.throws(() => { | |||
| 76 | 76 | const random10 = common.hasCrypto ? | |
| 77 | 77 | require('crypto').randomBytes(10) : | |
| 78 | 78 | Buffer.alloc(10, 1); | |
| 79 | + const derived18 = Buffer.alloc(18); | ||
| 80 | + for (let i = 0, j = 0; i < 18; i++) { | ||
| 81 | + if (i < 10) | ||
| 82 | + derived18[i] = random10[i]; | ||
| 83 | + else | ||
| 84 | + derived18[i] = random10[j++]; | ||
| 85 | + } | ||
| 79 | 86 | const empty = Buffer.alloc(0); | |
| 80 | 87 | ||
| 81 | 88 | assert.notDeepStrictEqual(random10, empty); | |
@@ -85,6 +92,7 @@ assert.deepStrictEqual(Buffer.concat([], 100), empty); | |||
| 85 | 92 | assert.deepStrictEqual(Buffer.concat([random10], 0), empty); | |
| 86 | 93 | assert.deepStrictEqual(Buffer.concat([random10], 10), random10); | |
| 87 | 94 | assert.deepStrictEqual(Buffer.concat([random10, random10], 10), random10); | |
| 95 | + assert.deepStrictEqual(Buffer.concat([random10, random10], 18), derived18); | ||
| 88 | 96 | assert.deepStrictEqual(Buffer.concat([empty, random10]), random10); | |
| 89 | 97 | assert.deepStrictEqual(Buffer.concat([random10, empty, empty]), random10); | |
| 90 | 98 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -226,6 +226,23 @@ b.copy(c, 'not a valid offset'); | |||
| 226 | 226 | // Make sure this acted like a regular copy with `0` offset. | |
| 227 | 227 | assert.deepStrictEqual(c, b.slice(0, c.length)); | |
| 228 | 228 | ||
| 229 | + // Copy into a Uint16Array target; bytes are packed into 16-bit elements. | ||
| 230 | + { | ||
| 231 | + const x = new Uint16Array(4); | ||
| 232 | + const buf = Buffer.of(1, 2, 3, 4); | ||
| 233 | + const copied = buf.copy(x); | ||
| 234 | + assert.strictEqual(copied, 4); | ||
| 235 | + assert.ok(x instanceof Uint16Array); | ||
| 236 | + const bytes = new Uint8Array(x.buffer, x.byteOffset, 4); | ||
| 237 | + assert.deepStrictEqual(Array.from(bytes), [1, 2, 3, 4]); | ||
| 238 | + const remaining = new Uint8Array( | ||
| 239 | + x.buffer, | ||
| 240 | + x.byteOffset + 4, | ||
| 241 | + x.byteLength - 4 | ||
| 242 | + ); | ||
| 243 | + assert.ok(remaining.every((b) => b === 0)); | ||
| 244 | + } | ||
| 245 | + | ||
| 229 | 246 | { | |
| 230 | 247 | c.fill('C'); | |
| 231 | 248 | assert.throws(() => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments