Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:73733 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 97674 invoked from network); 17 Apr 2014 22:11:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Apr 2014 22:11:14 -0000 Authentication-Results: pb1.pair.com header.from=tjerk.meesters@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=tjerk.meesters@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.174 as permitted sender) X-PHP-List-Original-Sender: tjerk.meesters@gmail.com X-Host-Fingerprint: 209.85.128.174 mail-ve0-f174.google.com Received: from [209.85.128.174] ([209.85.128.174:43829] helo=mail-ve0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6D/00-30647-F7150535 for ; Thu, 17 Apr 2014 18:11:13 -0400 Received: by mail-ve0-f174.google.com with SMTP id oz11so1403550veb.5 for ; Thu, 17 Apr 2014 15:11:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=1obJx0RFs0zMNKUR54tqHATkH721RjdKDCMUqbvN7+s=; b=CmCpb06igPY0/AkJ86CqWAmjc3HcE00swKS5e4JhHD73Q1n6s/9mS1y+E3xh4Te7q8 NBd3nJLIDCrOCEf/fG609kjagqwZ3M6wtcgy7jKmNlhabbtfcwzgKdKNeoVL5wS7Eblv hqAn/mMEP9ztQDRmICeXQ3cOwuj4szGSjtGvJYTt+Dn48XsRWO03tjfH2DbfojHD6eqz Ia4aUCfhcdGph+zt0XC1ktZvkB4H4kzpUFU1kBDGBzYv3n8+kSL3XGqhV2QW+lkZN2Bg e3F+DuxpahEyoHpPWJVvLAMvrD19JgxUL6OYJXy6vF13tfXOhrYnDVB2CItf6ZVc/apM 6SuA== MIME-Version: 1.0 X-Received: by 10.52.144.42 with SMTP id sj10mr7736694vdb.19.1397772669073; Thu, 17 Apr 2014 15:11:09 -0700 (PDT) Received: by 10.58.133.84 with HTTP; Thu, 17 Apr 2014 15:11:09 -0700 (PDT) In-Reply-To: References: <1397744009.2829.3212.camel@guybrush> <1397747255.2829.3225.camel@guybrush> Date: Fri, 18 Apr 2014 06:11:09 +0800 Message-ID: To: Andrey Andreev Cc: =?UTF-8?Q?Johannes_Schl=C3=BCter?= , Leigh , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=bcaec52e635d6ac10b04f7444f7f Subject: Re: [PHP-DEV] Negative string offsets From: tjerk.meesters@gmail.com (Tjerk Meesters) --bcaec52e635d6ac10b04f7444f7f Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, On Thu, Apr 17, 2014 at 11:22 PM, Andrey Andreev wrote: > Hi, > > On Thu, Apr 17, 2014 at 6:07 PM, Johannes Schl=C3=BCter > wrote: > > On Thu, 2014-04-17 at 17:18 +0300, Andrey Andreev wrote: > >> For example, every time you need to check for a filename extension, > >> last segment of a path, etc. When I need that, I always endup with > >> substr(), strrchr() or explode(), end() ... both are suboptimal. > > > > I don't see where this helps in that case. > > > > $sep =3D strrpos($filename, "."); > > if ($sep !=3D=3D false) { > > $ext =3D substr($filename, $sep + 1); > > } > > > > is clear and concise, two fcalls, can be understood even after years an= d > > the new programmer easily. > > > > This is the most concise form I came up using this new offset: > > > > $ext =3D ''; > > $pos =3D -1; > > do { > > $ext =3D $filename[$pos].$ext; > > } while ($filename[--$pos] !=3D '.'); > > > > While this form is bugged as it assumes that there is a . without > > checking. For checking I'd have to use strlen(), but if i call strlen() > > I can also initialize $pos accordingly and iterate from strlen() to 0. > > Also this form does more comparisons and way more allocations and is (i= n > > my opinion) way harder to understand. > > > > Maye I didn't see a simple form ... > > > > johannes > > For most filename extensions: > > if ($filename[-4] =3D=3D=3D '.') > { > $ext =3D substr($filename, -3); > } > I know that this was just an example of the use-case, but to get a file extension you would typically do: $ext =3D pathinfo($filename, PATHINFO_EXTENSION); ;-) > Or, checking if a path has a slash at the end: > > if ($path[-1] =3D=3D=3D '/') { ... } > > I'm talking strictly about validation here (and yes, the filename > example won't work with i.e. .jpeg, but would satisfy a large amount > of use cases), comparing stuff on the fly. > > Cheers, > Andrey. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --=20 -- Tjerk --bcaec52e635d6ac10b04f7444f7f--