Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102874 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 59955 invoked from network); 17 Jul 2018 04:11:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Jul 2018 04:11:39 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=levim@php.net; sender-id=unknown Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.208.176 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.208.176 mail-lj1-f176.google.com Received: from [209.85.208.176] ([209.85.208.176:38637] helo=mail-lj1-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 52/B2-37178-87C6D4B5 for ; Tue, 17 Jul 2018 00:11:38 -0400 Received: by mail-lj1-f176.google.com with SMTP id p6-v6so31849155ljc.5 for ; Mon, 16 Jul 2018 21:11:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=saWDx8HltE/9q1X/JWCJhW5B1T+3eJzizkYlH6jRyMc=; b=ZMDlu08M7CHsWfeerLLa/lsOAezblcpL24fJK9oLFMsnT5VSsSCJ2EvMhRvJQhSs8u sRkhT7KqdDdNus7qjmCS5w1DuDw9Mku95JaIRLsNj+t0folVe6HC81ZHJmTMvQGHE1mJ UFFfmmevztzRj7YjAQqwRCkltx5v0lRPrsJ7MHEZHqBZqlLvfy6p0y9x92GOH8sbfHhu GDod9BH+2T/6CRbepBCv/0WC9bbYClRHxrQzcLs3LtVKZOFKrJZWR6CdrYQS3AM1d0kK yoEHlC2gHr/37q49tKqzb6juQg70erD9dKCDpC7mZWh2pVsFhzVnVI17BmKOJGvj3cep 9vcQ== X-Gm-Message-State: AOUpUlEQTvozNAKTCagNLhlT0G+UGFlOQHP7jjgfBCIU5BNjg1H0Cb71 N56rU3CVC/BIW3lecNFDenZorCo4n/fTH7EZbzSIjQ== X-Google-Smtp-Source: AAOMgpcQTxSw25+Wfn7f/VuoEPqocGKEGpwNo58qbNPCdJxTLZwll1L+V914Tp4PxuX/4YXJpdXDJx/PGT3d4ufghHM= X-Received: by 2002:a2e:2f19:: with SMTP id v25-v6mr11544020ljv.113.1531800693497; Mon, 16 Jul 2018 21:11:33 -0700 (PDT) MIME-Version: 1.0 Date: Mon, 16 Jul 2018 22:11:16 -0600 Message-ID: To: internals Content-Type: text/plain; charset="UTF-8" Subject: Replacing array_slice From: levim@php.net (Levi Morrison) Recently I've been scrutinizing array_slice. It's been painful. For instance, `$preserve_keys` is totally ignored for string keys. This was undocumented until Christoph integrated it 2-3 days ago (thanks, cmb). However, this is really not a good design decision: why have a parameter that is called `preserve_keys` if the data gets the final say, especially since there isn't any technical requirement for this? Here's another "fun" behavior: negative offsets whose absolute values are larger than the size of the array (eg the array has 3 elements and you pass an offset of -5) will shift the window to begin at zero instead of truncating the out-of-range values (which is what happens on the positive side). And another: `$length` is a length when it's a positive number, but it's an offset from the end when it's negative. You could perhaps convince me that when length is positive it's an offset from the `$offset` parameter. However, if you look at the design of slicing functions in other languages (eg Python, Ruby, JavaScript) they take beginning and ending offsets, not a beginning offset and a length. And another: it always iterates from the beginning of the array even if you are only interested in the last half (somewhat common for various algorithms that split things in half). Fixing `array_slice` would probably do more harm than good at this stage. Instead I would like to provide an alternative function that does not have all this baggage, and will have decent performance much of the time. The best ideas I have for names are not that great: - `array_real_slice`? - `array_slice2`? We could also split it into 2 functions, one that preserves keys and one that doesn't, instead of using a parameter. Any ideas there?