Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:73721 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 65190 invoked from network); 17 Apr 2014 15:07:54 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Apr 2014 15:07:54 -0000 Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 217.114.215.10 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 217.114.215.10 mail.experimentalworks.net Received: from [217.114.215.10] ([217.114.215.10:57750] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 10/A3-39661-94EEF435 for ; Thu, 17 Apr 2014 11:07:53 -0400 Received: from [192.168.2.31] (ppp-188-174-41-40.dynamic.mnet-online.de [188.174.41.40]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: johannes@schlueters.de) by mail.experimentalworks.net (Postfix) with ESMTPSA id A50EE3FE8F; Thu, 17 Apr 2014 17:08:41 +0200 (CEST) To: Andrey Andreev Cc: Leigh , "internals@lists.php.net" In-Reply-To: References: <1397744009.2829.3212.camel@guybrush> Content-Type: text/plain; charset="UTF-8" Date: Thu, 17 Apr 2014 17:07:34 +0200 Message-ID: <1397747255.2829.3225.camel@guybrush> Mime-Version: 1.0 X-Mailer: Evolution 2.30.3 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Negative string offsets From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) 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 = strrpos($filename, "."); if ($sep !== false) { $ext = substr($filename, $sep + 1); } is clear and concise, two fcalls, can be understood even after years and the new programmer easily. This is the most concise form I came up using this new offset: $ext = ''; $pos = -1; do { $ext = $filename[$pos].$ext; } while ($filename[--$pos] != '.'); 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 (in my opinion) way harder to understand. Maye I didn't see a simple form ... johannes