Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107604 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 56796 invoked from network); 21 Oct 2019 17:43:34 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 21 Oct 2019 17:43:34 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 543932C050F for ; Mon, 21 Oct 2019 08:29:15 -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: X-Spam-Virus: No Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) (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 ; Mon, 21 Oct 2019 08:29:14 -0700 (PDT) Received: by mail-lj1-x22f.google.com with SMTP id m7so13833206lji.2 for ; Mon, 21 Oct 2019 08:29:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=2AUWw9h32lpFgpa8e9XFjMJIb4+zwQueg2sC7NSOCqE=; b=X3u1PxwIGKfdkuI2lbymDdXwp5HVc6GtL6U6xe15/ZCQn/ogipxKxJlMfQVBekL1K5 DT8obzydv1D5FXWa2YYqYJKGAYzxpYGDV/v0fZdgZuJeNepMZxkIH0wabYnhDJmhY/ps 98bFTZYWVg9ltez9K+/it//m+/koQ6nJg4KvteuK1Kqy5ac7/AEF8NSQ3By6+gCWCbrI CzQmUtDIsAtDo/szAUz+v1NrYKNoa77hPrjNPZx8f2S2XoXqwqTgNvJ674fu1pNa0g9l MQGzOZ0KH9R43MxO1yNlq6hU/+pGEGVvWi7w/c+Ah3t6twUcbwQoBjgnVP7t3oROKGjp Dkcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=2AUWw9h32lpFgpa8e9XFjMJIb4+zwQueg2sC7NSOCqE=; b=BdrpxVqPOe7aCNjpDzJIsqvQYA4kiDOH1H0/v5tdyfmzGpjAI33GLmVYPLPST6tYXk j1fZSgJKvM57FW/UdT1sWXa1rGIolmFruagN1qRGXgBDhzToyFHtAymMpeH7uL5sVXz3 qvq9uNf/532d976zByo7enbBhum61aM72T3eRyedXDcWJYQcsU3deh0YCrlIzz8xwezr I4glfpfEsRLVqNMVb7S50yLBmn7j/mDoe8PQv7dUrwlpw1PMDoq0juTXqxWCgap+ZnST PS0u87ceNOCyOMGNNPwOCustvRl8NaXs5lHnbzrbsz3FV+Qwes27lWOK2hcUpCPC04+p C3nQ== X-Gm-Message-State: APjAAAU/ltS+puajCLC5foXlJTQnBwnmFCkp5LpzZvAGs7azEK2YWW5z VY+xW3FZUHM1Zh5dhNls3qScqM4xu9SUiKRzbrQ= X-Google-Smtp-Source: APXvYqzppfLjh1PFI97uFZOMk5qgGklG9VjdYlevZMeBYAfh6e2jVb6djbfZe0PlAs+pn2ilsx+8dryTD+0s0JagOjQ= X-Received: by 2002:a2e:9693:: with SMTP id q19mr15139372lji.6.1571671753495; Mon, 21 Oct 2019 08:29:13 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 21 Oct 2019 17:28:56 +0200 Message-ID: To: "Colin O'Dell" Cc: PHP Internals Content-Type: multipart/alternative; boundary="00000000000092167f05956d5803" X-Envelope-From: Subject: Re: [PHP-DEV] substr with null length From: nikita.ppv@gmail.com (Nikita Popov) --00000000000092167f05956d5803 Content-Type: text/plain; charset="UTF-8" On Mon, Oct 21, 2019 at 5:03 PM Colin O'Dell wrote: > 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. > This is a fairly common deficiency in the implementation of internal functions. Part of the reason is that historically zend_parse_parameters did not support the ! modifier for integers, and partly these are just implementation oversights. Feel free to send a PR to fix this for PHP 8, no RFC needed. If you're interested in addressing this for more functions, do a grep for "= UNKNOWN" on the codebase, which gives you (with a few exceptions) a list of functions that currently handle null parameters incorrectly and should ideally be fixed for PHP 8. Nikita --00000000000092167f05956d5803--