Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:73722 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68033 invoked from network); 17 Apr 2014 15:22:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Apr 2014 15:22:25 -0000 Authentication-Results: pb1.pair.com smtp.mail=narf@devilix.net; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=narf@devilix.net; sender-id=pass Received-SPF: pass (pb1.pair.com: domain devilix.net designates 209.85.217.174 as permitted sender) X-PHP-List-Original-Sender: narf@devilix.net X-Host-Fingerprint: 209.85.217.174 mail-lb0-f174.google.com Received: from [209.85.217.174] ([209.85.217.174:60510] helo=mail-lb0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C6/34-39661-FA1FF435 for ; Thu, 17 Apr 2014 11:22:25 -0400 Received: by mail-lb0-f174.google.com with SMTP id u14so493913lbd.5 for ; Thu, 17 Apr 2014 08:22:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devilix.net; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=2evrHK+7TkvERMicfqp7E9rJTqVXmOZ15ifHi3JyXC8=; b=I+aaeJTrNfHdlvNp8qdY/QKf5c844jY+iapZtruTEmSvh8w7zOdXscUbaJWcak2Jlj bqRmCwk8Uh27VXHLG5kGqOBBNus9Yjc+pTBAhpBugHfxd2ufSTbm3UNklH3HgTULw2gI W1OFKvd8zQp1HlzIm8vE6W3Fw/YeM56S6Tgz8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=2evrHK+7TkvERMicfqp7E9rJTqVXmOZ15ifHi3JyXC8=; b=XrVXhzIAvK88k9VO69mDZZ2cbPhaDaKWm5K29iLu1rsMnjUhyIXg//n3eFjOgX/xXy 0HrDo0jyVY+Q6s6Ej3bG0OSMt01ejDBl73HQWyJ6HGdwWJGBZLHo2ekfTSKjTiFxRYnm KXkjY2qUOQXR/LXfJ8Zkb9yQcgIDuYUJeDxlc23wrZR79yeU01Q6i0J1IefhAxzTw/IG YnKK4thuJjAYcp3Dt1lEeXtaESW5K2EJgfHqULLBQqyIJkBdr1311U0QHI3cU4GzHewd 5RRGu3+9otdwbDlE0Td2LUVz1vNhY7pwUv2aLgE4z9yiLvBUFhu9LYatUwEJzOc0FRbl mHWQ== X-Gm-Message-State: ALoCoQn2BgAmsTLXRL6kRrZKVm9BBKz6TGFpRSnCQzjW+G7wX9aP+o5wSe00mj6GIJStUF/ju8Wk MIME-Version: 1.0 X-Received: by 10.152.42.196 with SMTP id q4mr10350409lal.14.1397748139603; Thu, 17 Apr 2014 08:22:19 -0700 (PDT) Received: by 10.152.87.80 with HTTP; Thu, 17 Apr 2014 08:22:19 -0700 (PDT) In-Reply-To: <1397747255.2829.3225.camel@guybrush> References: <1397744009.2829.3212.camel@guybrush> <1397747255.2829.3225.camel@guybrush> Date: Thu, 17 Apr 2014 18:22:19 +0300 Message-ID: To: =?UTF-8?Q?Johannes_Schl=C3=BCter?= Cc: Leigh , "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Negative string offsets From: narf@devilix.net (Andrey Andreev) 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 and > 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 (in > 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); } 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.