Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107600 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 74876 invoked from network); 20 Oct 2019 17:17:08 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by 76.75.200.58 with SMTP; 20 Oct 2019 17:17:08 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 84F592C1DDC for ; Sun, 20 Oct 2019 08:00:35 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: No Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Sun, 20 Oct 2019 08:00:32 -0700 (PDT) Received: by mail-il1-x12c.google.com with SMTP id z10so9683319ilo.8 for ; Sun, 20 Oct 2019 08:00:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=59bsR/Br+iyBCSsu++nr00cax9Aaem3SUVbFgssKsW4=; b=sp7WJV2kPwV07Mud5RYngtUGAoKwi+Fo8cD1hhWM7HiJ1C0WK99qQcpt5y2GSoq6ei tKrOlRKrOwuEEXYAvtLWBaLT5AXW//m/dpK++Tt3EQXz1YlZo4pDsH9/iU7rDZ1Rw8vt fYXgOBnwb3agtyyHvIYQxBJmD2K8EgeI4MutTwrRbahugZHM0Mx1D9d3wLgHZ98bpWWB Fs4YwLzZoBS6u8oUqXXfH8bzMffgf82XrsasP3CwzB0pRrsQBbsGD2JM+TfAAb0TwxVa VPRkrdhkFR8Fp7MbqsG60XgQt4K3QdyBOt8bgsEWHGGw+8ilxZF1S5xqsx4xsBhppSiu nhFA== 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=59bsR/Br+iyBCSsu++nr00cax9Aaem3SUVbFgssKsW4=; b=R9HmX15i32keoX5VV6N11LyF62UhY302ykVZfrPUu9M+0qxYdbpv3HFuCWRwvXtaf5 fTuXOvX8uVbOnfHYMUhRSRiOmvKC4/Exv2hPF+wxb6Ie9wLgfxlx28yNz97ZgresvNCw PcqCtZ6NVi13KttqfIIF+sJ7uZIRnLe8kERJ8pfrdU/wT++Rb65MbUoCXivjARiby+5H IFqMzNw4OqnmduS/f+PFaGMt/tD09jKL7CQSmaLVZ/tROLHrZw3biT+F1yh3vDADoVyI SBoZj4ENgC/FSGoO/uY2geWxUYunEWzblz16/M4N5QJZopIWpbwvBXPzaXG5djkjGlGQ ulMg== X-Gm-Message-State: APjAAAW7xPktr1JOWrXumh2ZWduT+hI3Mr0jb0i9zvgUvL5n5ns9LTSi Dh6AXylL8Drv51DSL1R/ZIPR+LL4KvCP02+aJ3AeytYO2ic= X-Google-Smtp-Source: APXvYqy/B1yH6ENXe6NJT47D2czuSLHaf1yvayJlA+8I0vFRWN97PMXR7KZBEB5im4MnnqiO3MgPcLf2YTiHERlv2VY= X-Received: by 2002:a92:d0a:: with SMTP id 10mr19686868iln.218.1571583630692; Sun, 20 Oct 2019 08:00:30 -0700 (PDT) MIME-Version: 1.0 Date: Sun, 20 Oct 2019 11:00:15 -0400 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="0000000000000ad745059558d43f" X-Envelope-From: Subject: substr with null length From: colinodell@gmail.com ("Colin O'Dell") --0000000000000ad745059558d43f Content-Type: text/plain; charset="UTF-8" Hello Internals, Is there any particular reason why the substr() function doesn't accept a null $length like mb_substr() does? It seems the behavior to read through the end of the string can only be controlled by the presence or absence of the $length parameter: https://3v4l.org/YpuO1 I discovered this discrepancy between the two methods while attempting to create a specialized string wrapper class with a method like this: public function getSubstring(int $start, ?int $length = null): string { if ($this->isMultibyte) { return mb_substr($this->line, $start, $length, $this->encoding); } else { return substr($this->line, $start, $length); } } This method would not work as expected without additional boilerplate like: public function getSubstring (int $start, ?int $length = null): string { if ($this->isMultibyte) { return mb_substr($this->line, $start, $length, $this->encoding); } elseif ($length === null) { return substr($this->line, $start); } else { return substr($this->line, $start, $length); } } Or: public function getSubstring (int $start, ?int $length = null): string { if ($this->isMultibyte) { return mb_substr($this->line, $start, $length, $this->encoding); } else { return substr($this->line, $start, $length ?? (strlen($this->line) - $start)); } } Are there any historical reasons preventing substr() from accepting a null $length like mb_substr() does? I'd be happy to write the RFC and take a stab at the implementation if there's interest in such a change. Regards, Colin O'Dell colinodell@gmail.com --0000000000000ad745059558d43f--