|
2 | 2 | // OpenZeppelin Contracts (last updated v5.4.0) (utils/Arrays.sol) |
3 | 3 | // This file was procedurally generated from scripts/generate/templates/Arrays.js. |
4 | 4 |
|
5 | | -pragma solidity ^0.8.20; |
| 5 | +pragma solidity ^0.8.24; |
6 | 6 |
|
7 | 7 | import {Comparators} from "./Comparators.sol"; |
8 | 8 | import {SlotDerivation} from "./SlotDerivation.sol"; |
@@ -375,6 +375,189 @@ library Arrays { |
375 | 375 | return low; |
376 | 376 | } |
377 | 377 |
|
| 378 | + /** |
| 379 | + * @dev Copies the content of `array`, from `start` (included) to the end of `array` into a new address array in |
| 380 | + * memory. |
| 381 | + * |
| 382 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`] |
| 383 | + */ |
| 384 | + function slice(address[] memory array, uint256 start) internal pure returns (address[] memory) { |
| 385 | + return slice(array, start, array.length); |
| 386 | + } |
| 387 | + |
| 388 | + /** |
| 389 | + * @dev Copies the content of `array`, from `start` (included) to `end` (excluded) into a new address array in |
| 390 | + * memory. The `end` argument is truncated to the length of the `array`. |
| 391 | + * |
| 392 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`] |
| 393 | + */ |
| 394 | + function slice(address[] memory array, uint256 start, uint256 end) internal pure returns (address[] memory) { |
| 395 | + // sanitize |
| 396 | + end = Math.min(end, array.length); |
| 397 | + start = Math.min(start, end); |
| 398 | + |
| 399 | + // allocate and copy |
| 400 | + address[] memory result = new address[](end - start); |
| 401 | + assembly ("memory-safe") { |
| 402 | + mcopy(add(result, 0x20), add(add(array, 0x20), mul(start, 0x20)), mul(sub(end, start), 0x20)) |
| 403 | + } |
| 404 | + |
| 405 | + return result; |
| 406 | + } |
| 407 | + |
| 408 | + /** |
| 409 | + * @dev Copies the content of `array`, from `start` (included) to the end of `array` into a new bytes32 array in |
| 410 | + * memory. |
| 411 | + * |
| 412 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`] |
| 413 | + */ |
| 414 | + function slice(bytes32[] memory array, uint256 start) internal pure returns (bytes32[] memory) { |
| 415 | + return slice(array, start, array.length); |
| 416 | + } |
| 417 | + |
| 418 | + /** |
| 419 | + * @dev Copies the content of `array`, from `start` (included) to `end` (excluded) into a new bytes32 array in |
| 420 | + * memory. The `end` argument is truncated to the length of the `array`. |
| 421 | + * |
| 422 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`] |
| 423 | + */ |
| 424 | + function slice(bytes32[] memory array, uint256 start, uint256 end) internal pure returns (bytes32[] memory) { |
| 425 | + // sanitize |
| 426 | + end = Math.min(end, array.length); |
| 427 | + start = Math.min(start, end); |
| 428 | + |
| 429 | + // allocate and copy |
| 430 | + bytes32[] memory result = new bytes32[](end - start); |
| 431 | + assembly ("memory-safe") { |
| 432 | + mcopy(add(result, 0x20), add(add(array, 0x20), mul(start, 0x20)), mul(sub(end, start), 0x20)) |
| 433 | + } |
| 434 | + |
| 435 | + return result; |
| 436 | + } |
| 437 | + |
| 438 | + /** |
| 439 | + * @dev Copies the content of `array`, from `start` (included) to the end of `array` into a new uint256 array in |
| 440 | + * memory. |
| 441 | + * |
| 442 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`] |
| 443 | + */ |
| 444 | + function slice(uint256[] memory array, uint256 start) internal pure returns (uint256[] memory) { |
| 445 | + return slice(array, start, array.length); |
| 446 | + } |
| 447 | + |
| 448 | + /** |
| 449 | + * @dev Copies the content of `array`, from `start` (included) to `end` (excluded) into a new uint256 array in |
| 450 | + * memory. The `end` argument is truncated to the length of the `array`. |
| 451 | + * |
| 452 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`] |
| 453 | + */ |
| 454 | + function slice(uint256[] memory array, uint256 start, uint256 end) internal pure returns (uint256[] memory) { |
| 455 | + // sanitize |
| 456 | + end = Math.min(end, array.length); |
| 457 | + start = Math.min(start, end); |
| 458 | + |
| 459 | + // allocate and copy |
| 460 | + uint256[] memory result = new uint256[](end - start); |
| 461 | + assembly ("memory-safe") { |
| 462 | + mcopy(add(result, 0x20), add(add(array, 0x20), mul(start, 0x20)), mul(sub(end, start), 0x20)) |
| 463 | + } |
| 464 | + |
| 465 | + return result; |
| 466 | + } |
| 467 | + |
| 468 | + /** |
| 469 | + * @dev Moves the content of `array`, from `start` (included) to the end of `array` to the start of that array. |
| 470 | + * |
| 471 | + * NOTE: This function modifies the provided array in place. If you need to preserve the original array, use {slice} instead. |
| 472 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice[Javascript's `Array.splice`] |
| 473 | + */ |
| 474 | + function splice(address[] memory array, uint256 start) internal pure returns (address[] memory) { |
| 475 | + return splice(array, start, array.length); |
| 476 | + } |
| 477 | + |
| 478 | + /** |
| 479 | + * @dev Moves the content of `array`, from `start` (included) to `end` (excluded) to the start of that array. The |
| 480 | + * `end` argument is truncated to the length of the `array`. |
| 481 | + * |
| 482 | + * NOTE: This function modifies the provided array in place. If you need to preserve the original array, use {slice} instead. |
| 483 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice[Javascript's `Array.splice`] |
| 484 | + */ |
| 485 | + function splice(address[] memory array, uint256 start, uint256 end) internal pure returns (address[] memory) { |
| 486 | + // sanitize |
| 487 | + end = Math.min(end, array.length); |
| 488 | + start = Math.min(start, end); |
| 489 | + |
| 490 | + // move and resize |
| 491 | + assembly ("memory-safe") { |
| 492 | + mcopy(add(array, 0x20), add(add(array, 0x20), mul(start, 0x20)), mul(sub(end, start), 0x20)) |
| 493 | + mstore(array, sub(end, start)) |
| 494 | + } |
| 495 | + |
| 496 | + return array; |
| 497 | + } |
| 498 | + |
| 499 | + /** |
| 500 | + * @dev Moves the content of `array`, from `start` (included) to the end of `array` to the start of that array. |
| 501 | + * |
| 502 | + * NOTE: This function modifies the provided array in place. If you need to preserve the original array, use {slice} instead. |
| 503 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice[Javascript's `Array.splice`] |
| 504 | + */ |
| 505 | + function splice(bytes32[] memory array, uint256 start) internal pure returns (bytes32[] memory) { |
| 506 | + return splice(array, start, array.length); |
| 507 | + } |
| 508 | + |
| 509 | + /** |
| 510 | + * @dev Moves the content of `array`, from `start` (included) to `end` (excluded) to the start of that array. The |
| 511 | + * `end` argument is truncated to the length of the `array`. |
| 512 | + * |
| 513 | + * NOTE: This function modifies the provided array in place. If you need to preserve the original array, use {slice} instead. |
| 514 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice[Javascript's `Array.splice`] |
| 515 | + */ |
| 516 | + function splice(bytes32[] memory array, uint256 start, uint256 end) internal pure returns (bytes32[] memory) { |
| 517 | + // sanitize |
| 518 | + end = Math.min(end, array.length); |
| 519 | + start = Math.min(start, end); |
| 520 | + |
| 521 | + // move and resize |
| 522 | + assembly ("memory-safe") { |
| 523 | + mcopy(add(array, 0x20), add(add(array, 0x20), mul(start, 0x20)), mul(sub(end, start), 0x20)) |
| 524 | + mstore(array, sub(end, start)) |
| 525 | + } |
| 526 | + |
| 527 | + return array; |
| 528 | + } |
| 529 | + |
| 530 | + /** |
| 531 | + * @dev Moves the content of `array`, from `start` (included) to the end of `array` to the start of that array. |
| 532 | + * |
| 533 | + * NOTE: This function modifies the provided array in place. If you need to preserve the original array, use {slice} instead. |
| 534 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice[Javascript's `Array.splice`] |
| 535 | + */ |
| 536 | + function splice(uint256[] memory array, uint256 start) internal pure returns (uint256[] memory) { |
| 537 | + return splice(array, start, array.length); |
| 538 | + } |
| 539 | + |
| 540 | + /** |
| 541 | + * @dev Moves the content of `array`, from `start` (included) to `end` (excluded) to the start of that array. The |
| 542 | + * `end` argument is truncated to the length of the `array`. |
| 543 | + * |
| 544 | + * NOTE: This function modifies the provided array in place. If you need to preserve the original array, use {slice} instead. |
| 545 | + * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice[Javascript's `Array.splice`] |
| 546 | + */ |
| 547 | + function splice(uint256[] memory array, uint256 start, uint256 end) internal pure returns (uint256[] memory) { |
| 548 | + // sanitize |
| 549 | + end = Math.min(end, array.length); |
| 550 | + start = Math.min(start, end); |
| 551 | + |
| 552 | + // move and resize |
| 553 | + assembly ("memory-safe") { |
| 554 | + mcopy(add(array, 0x20), add(add(array, 0x20), mul(start, 0x20)), mul(sub(end, start), 0x20)) |
| 555 | + mstore(array, sub(end, start)) |
| 556 | + } |
| 557 | + |
| 558 | + return array; |
| 559 | + } |
| 560 | + |
378 | 561 | /** |
379 | 562 | * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check. |
380 | 563 | * |
|
0 commit comments