Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108634 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 23928 invoked from network); 17 Feb 2020 09:03:05 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Feb 2020 09:03:05 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B023A180531 for ; Sun, 16 Feb 2020 23:18:24 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 16 Feb 2020 23:18:24 -0800 (PST) Received: by mail-lf1-f46.google.com with SMTP id 203so11029448lfa.12 for ; Sun, 16 Feb 2020 23:18:24 -0800 (PST) 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=hcWsfvg+RzR9YAkEMxzPbJedEuN1o7PrUNRSPppNMsM=; b=FvF5OlxeLnsJrnC4y1+/pic8zRWbQABBUaQ+wQbBEJhLTfQeulXRQIHmVZm8Y8FuFA uWex8kE5NVmLyA7s3QjhFPpxnC4QXHR0MXsI3hSmALZnmjInDFvYy7RWX9V9lVcbi3r0 uH8VrdQoX3renriTmbxE4PJP06g1/xMFlMTYgOBF8pHecMQBQOAhKXwjcoeoIXPyM7+9 tStuWSWQaIaP9R7cX/WY8/vjopnKz5IotR7wPnB4dX+9ofGx661RXtMyRUZLRfPJHpa9 9vjs1bB9mGwANcRo1SxFc6H6RB0Ct3ptILJGhfZJnGJ75yjaBvBgqzhCaRsHOOWap5GJ c3Bg== 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=hcWsfvg+RzR9YAkEMxzPbJedEuN1o7PrUNRSPppNMsM=; b=VZQkR3kusHruXigdwRwf/ym2P9ZncMEKC+e77fel/oM+V05IsdDQ1AgO55kwHbtYyS RIat7kPLiNdiium/J0JeFCt4kISZWT9t+whAcRo+kENvI4jsDuYM+VZRSfx+U8/4NIPe iF7T3J8pH5w9EcJv0uvEnNIIan/BcPUq4FI14h7sclnLLK+T9Od4VklUhM+TvgVztcsA /ovxe718/6vwJg0qdeahnG7jjT6LiwpAHNmXLQ9KS25leb4uPIFipAE5hdfhqLruDM91 MHvpEhHeqn7yZkVgYfrHaWQGxuCfeY90sTk0Z9JHHwi/g0L6NQEDscHIYt2Unc6RgJbZ TdzA== X-Gm-Message-State: APjAAAUupVCW1A4K8QwEJpgPzjSQj9Yb0JIHZDc8/Lg4zenhakT1t8te B9NxIJpR+F09lzJC5dSR8s7yRl4rMT2JyGJq+w== X-Google-Smtp-Source: APXvYqx0sVkk+7HKKq8yBrUCiP+Cgg8NH1nnfTPsJYOx03GpFGakTQNw56rgO3j6mWPC+YG9Nw1wDHx8vLTc0hdW2iY= X-Received: by 2002:ac2:5ec3:: with SMTP id d3mr7433528lfq.176.1581923902635; Sun, 16 Feb 2020 23:18:22 -0800 (PST) MIME-Version: 1.0 References: <4a0f4d30-c8d3-0274-413f-0f4c582418d0@aegir.sexy> In-Reply-To: Date: Mon, 17 Feb 2020 08:18:11 +0100 Message-ID: To: "G. P. B." Cc: Aegir Leet , "internals@lists.php.net" Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Proposal for a new basic function: str_contains From: guilliam.xavier@gmail.com (Guilliam Xavier) On Fri, Feb 14, 2020 at 11:14 AM G. P. B. wrote: > > Moreover, checking for a substring to start/end a string seems > to be > fitting for the current strpos functions. Maybe in terms of semantics (`0 === strpos($haystack, $needle)`), but suboptimal in terms of performance, especially when $haystack is a *very long* string which *doesn't* contain $needle, strpos() will vainly search along the whole string, while a specialized function would stop as soon as possible (which is also the case of existing strncmp() but you need to write `0 === strncmp($haystack, $needle, strlen($needle))`, arguably not really the cleanest code...). For "contains" you have to search along the whole string anyway, so `str_contains()` is "just" `false !== strpos()` but cleaner. To be clear, I'm not against the current proposal (rather for actually) [I just would want `str_{starts,ends}_with` even more (without case-insensitive nor multibyte variants)] -- Guilliam Xavier On Fri, Feb 14, 2020 at 11:14 AM G. P. B. wrote: > > On Fri, 14 Feb 2020 at 10:58, Aegir Leet wrote: > > > I generally like the idea, but it seems many (most?) real-world > > implementations actually use mb_strpos() !== false by default. > > > > > > https://github.com/danielstjules/Stringy/blob/df24ab62d2d8213bbbe88cc36fc35a4503b4bd7e/src/Stringy.php#L206-L215 > > > > https://github.com/illuminate/support/blob/6eff6cff19f7ad5540b9a61a9fb3612ca8218c19/Str.php#L157-L166 > > > > So there should definitely be an mb_str_contains in ext/mbstring in > > addition to the regular str_contains proposed here. > > > > The biggest reason to have an mb_* variant if for when comparing with case > insensitivity. > The only other reason is if you need to check a string which is in a > different encoding, > which is, I'm assuming, is a quasi non-existent problem as everything > things is UTF-8 > nowadays. > > The reason why I personally voted no on the previous RFC was that I don't > see the > value of having functions checking if a string starts/ends with a sequence > but not a > general one. Moreover, checking for a substring to start/end a string seems > to be > fitting for the current strpos functions. > > This function on it's own is way more reasonable and useful to add IMHO > > Best regards > > George P. Banyard -- Guilliam Xavier