Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100142 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 71379 invoked from network); 1 Aug 2017 04:57:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Aug 2017 04:57:59 -0000 Authentication-Results: pb1.pair.com smtp.mail=andreas@dqxtech.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=andreas@dqxtech.net; sender-id=unknown Received-SPF: error (pb1.pair.com: domain dqxtech.net from 209.85.214.53 cause and error) X-PHP-List-Original-Sender: andreas@dqxtech.net X-Host-Fingerprint: 209.85.214.53 mail-it0-f53.google.com Received: from [209.85.214.53] ([209.85.214.53:32793] helo=mail-it0-f53.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6C/54-07025-65A00895 for ; Tue, 01 Aug 2017 00:57:59 -0400 Received: by mail-it0-f53.google.com with SMTP id v127so17823905itd.0 for ; Mon, 31 Jul 2017 21:57:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=6+EoTxzu39g733fAF/nKe+LRAdxCLF5mZUxIKzw3cpA=; b=lnJFzzY7Xoox22ekVC5EosH6RzgiHzAgXnTpowhRj1IwdxUJtLWhi2FvePIyV7/n2X YrFtdznvP2lFRGiHN/Rpzp+oqHujdb1in4iyLZA574YbooFruoyiSv7hbv+WYjy6Aaje pxW/GsNYD0V2DY0iR7+zx7YrCvQlpPX/5ojJ6+yTq46FZLByHsbXy70J+6G/ZKluL5cE R0mTUwaD8p5e2ZeD6NH61yQsAXLEJC1Gsu0oudcIrtBG0ZTHX/XDvkv0Yf1Fvv0K90Jy 3zDapyQuUyNS7fGqFZ1KWhtV5A/7/OQJLrVds2lBv0DwnkddVqZal7D8RpaxdzsmW6zv Q6Dw== 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=6+EoTxzu39g733fAF/nKe+LRAdxCLF5mZUxIKzw3cpA=; b=ShEXZE6ECxeID6+V6Eeta3ozNKppKaFaZBmr+rQj7nTZfHCR4DOVvCn7Wq1GPKebja JsFpdpLGlDgruVagDkTycfuOer3GEAmPHS23g39bWm9GMrL5uVFwTAM89a5SDWIs7gpf GNa5rmoYKYaE1g6yD7R9OBfaBQRjtUsluB7TaT21Gc3cNSWYozPt7Z3Z2uUm1Qby31TT 2iPR+FvoAtumF3t510YQzVoRC7n2fSmTU0HCdUFgE7c/X2UoP9+cmokpA8txhIRo9O3c tcHc0My/J0iOaSVDWvDAARlDBitJ6bxI1KWjz4RmBZqZMa7wLL8apZSaNWohUcFXndg/ Y6pA== X-Gm-Message-State: AIVw110glQgMlVQhkEcHT8qss9oMNYyJ2BGW7FDJiEgaSk/qVHp0LaYq 82wqpL9cVPizZJTaCyg= X-Received: by 10.36.107.68 with SMTP id v65mr471920itc.129.1501563475502; Mon, 31 Jul 2017 21:57:55 -0700 (PDT) Received: from mail-io0-f181.google.com (mail-io0-f181.google.com. [209.85.223.181]) by smtp.googlemail.com with ESMTPSA id 77sm5965481itw.3.2017.07.31.21.57.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Jul 2017 21:57:54 -0700 (PDT) Received: by mail-io0-f181.google.com with SMTP id m88so3847617iod.2 for ; Mon, 31 Jul 2017 21:57:54 -0700 (PDT) X-Received: by 10.107.128.206 with SMTP id k75mr21561265ioi.115.1501563474463; Mon, 31 Jul 2017 21:57:54 -0700 (PDT) MIME-Version: 1.0 Received: by 10.36.245.194 with HTTP; Mon, 31 Jul 2017 21:57:33 -0700 (PDT) Date: Tue, 1 Aug 2017 06:57:33 +0200 X-Gmail-Original-Message-ID: Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: New functions: string_starts_with(), string_ends_with() From: andreas@dqxtech.net (Andreas Hennings) Hello list, a quite common use case is that one needs to find out if a string $haystack begins or ends with another string $needle. Or in other words, if $needle is a prefix or a suffix of $haystack. One prominent example would be in PSR-4 or PSR-0 class loaders. Maybe the use case also occurs when writing parsers.. In each of these two examples (parsers, class loaders), we care about performance. (forgive me if this was discussed before, I did not find it anywhere in the archives) -------------------------- Existing solutions to this problem feel non-trivial, and/or are suboptimal in performance. https://stackoverflow.com/questions/2790899/how-to-check-if-a-string-starts-with-a-specified-string https://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php This answer compares different solutions, https://stackoverflow.com/a/7168986/246724 Existing solutions: (Let's focus on string_starts_with(), the other case is mostly equivalent / symmetric) if (0 === strpos($haystack, $needle)) {..} I have often seen this presented as the preferable solution. Unfortunately, this searches the entire string, not just the beginning. Especially if $haystack is really long, this can be a waste. E.g. if (0 === strpos(file_get_contents('some_source_file.php'), '