Hi Internals,
What are peoples thoughts on allowing negative string offsets, that return
characters from the end of a string.
Example:
$string = 'foobar';
print $string[-1]; // prints 'r';
print $string[-2]; // prints 'a';
Patch:
https://github.com/lt/php-src/compare/string_negative_offset
Regards,
Leigh.
Hi Internals,
What are peoples thoughts on allowing negative string offsets, that return
characters from the end of a string.Example:
$string = 'foobar';
print $string[-1]; // prints 'r';
print $string[-2]; // prints 'a';
Good idea.
--
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256 http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h
Pretty interesting, but I'm sure this is (yet another) stepping stone until
someone suggests the Python syntax for slicing strings and arrays :p
Hi Internals,
What are peoples thoughts on allowing negative string offsets, that
return
characters from the end of a string.Example:
$string = 'foobar';
print $string[-1]; // prints 'r';
print $string[-2]; // prints 'a';Good idea.
--
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT
Lecturer.
+44 (0) 787 668 0256 http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information:
http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h
What are peoples thoughts on allowing negative string offsets, that
return
characters from the end of a string.Example:
$string = 'foobar';
print $string[-1]; // prints 'r';
print $string[-2]; // prints 'a';
Generally I like this, but I think it's confusing and inconsistent in comparison to array-indiexes, as you can have an array like array(-1 => 'foo')
And in my opinion one should not have differences in $string[x] and $array[x] access.
On Thu, Apr 17, 2014 at 2:11 PM, Christian Stoller stoller@leonex.dewrote:
What are peoples thoughts on allowing negative string offsets, that
return
characters from the end of a string.Example:
$string = 'foobar';
print $string[-1]; // prints 'r';
print $string[-2]; // prints 'a';Generally I like this, but I think it's confusing and inconsistent in
comparison to array-indiexes, as you can have an array likearray(-1 => 'foo')
And in my opinion one should not have differences in $string[x] and
$array[x] access.
That's an interesting notion, but I think in previous discussions the
consensus was that array-index access and string offset were never going to
be the same since non numeric array keys or multi-associative, etc don't
translate to string offsets anyway.
Can't remember how long/in regards to what this was, though!
Hi,
What are peoples thoughts on allowing negative string offsets, that return
characters from the end of a string.
I'm +/-0 on this. But mind: Current form makes it simpler to find bugs
when miscalculating the offset. Allowing negative offsets gives harder
to debug results. Additionally I wonder how often this is actually
needed, thus justifying to change the language rules.
johannes
Hi,
On Thu, Apr 17, 2014 at 5:13 PM, Johannes Schlüter
johannes@schlueters.de wrote:
Hi,
What are peoples thoughts on allowing negative string offsets, that return
characters from the end of a string.I'm +/-0 on this. But mind: Current form makes it simpler to find bugs
when miscalculating the offset. Allowing negative offsets gives harder
to debug results. Additionally I wonder how often this is actually
needed, thus justifying to change the language rules.johannes
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.
Cheers,
Andrey.
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()
orexplode()
,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
Hi,
On Thu, Apr 17, 2014 at 6:07 PM, Johannes Schlüter
johannes@schlueters.de 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()
orexplode()
,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 usestrlen()
, but if i callstrlen()
I can also initialize $pos accordingly and iterate fromstrlen()
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] === '.')
{
$ext = substr($filename, -3);
}
Or, checking if a path has a slash at the end:
if ($path[-1] === '/') { ... }
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.
> Hi,
>
> On Thu, Apr 17, 2014 at 6:07 PM, Johannes Schlüter
>
> >
> >> 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
>
> For most filename extensions:
>
> if ($filename[-4] === '.')
> {
> $ext = 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 = pathinfo($filename, PATHINFO_EXTENSION);
;-)
> Or, checking if a path has a slash at the end:
>
> if ($path[-1] === '/') { ... }
>
> 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.
>
> --
>
>
>
>
--
--
Tjerk