[PATCH] substr() returns false

php.internals

Morten Poulsen

22 years ago
Hi, Even though this is documented, it is strange behaviour: If the from parameter to substr() is at, or past, the end of the input string, the function returns false. The function is documented as string substr ( string string, int start [, int length]) IMHO substr() should either 1) be changed to do like Perl, which returns an error only if start is _beyond_ the end (not _at_): (from http://www.perldoc.com/perl5.6/pod/func/substr.html) my $null = substr $name, 6, 2; # returns '' (no warning) my $oops = substr $name, 7; # returns undef, with warning 2) be changed to return an empty string, if start is either at or beyond the end of the input string. I have attached patches for both solutions. If you don't change it to be like Perl, I think it should be noted in the documentation (as with chop()). If, on the other hand, it _is_ changed to behave like Perl, I think it should be emphasized that you are not guaranteed that the result is a string, because this will cause (eg. has caused us) problems when using the result as part of the $data array to PEAR DB::execute(). Happy hacking, Morten
-- Morten Poulsen <morten-Qg2caEh8@afdelingp.dk> http://www.afdelingp.dk/

Andi Gutmans

22 years ago
Not sure if/how this should be changed. Isn't it a mistake to give a start position at the end or beyond the string? In this case, doesn't it make sense to return false? Andi At 02:39 PM 11/19/2003 +0100, Morten Poulsen wrote:

Jani Taskinen

22 years ago
IF you change it, it'll break BC.. :) IMO, it should just give an error in such case, and return FALSE too. --Jani On Wed, 19 Nov 2003, Andi Gutmans wrote:
>Not sure if/how this should be changed. >Isn't it a mistake to give a start position at the end or beyond the >string? In this case, doesn't it make sense to return false? > >Andi > >At 02:39 PM 11/19/2003 +0100, Morten Poulsen wrote: >>Hi, >> >>Even though this is documented, it is strange behaviour: If the from >>parameter to substr() is at, or past, the end of the input string, the >>function returns false. >> >>The function is documented as >>string substr ( string string, int start [, int length]) >> >>IMHO substr() should either >> >>1) be changed to do like Perl, which returns an error only if start is >>_beyond_ the end (not _at_): >>(from http://www.perldoc.com/perl5.6/pod/func/substr.html) >>my $null = substr $name, 6, 2; # returns '' (no warning) >>my $oops = substr $name, 7; # returns undef, with warning >> >>2) be changed to return an empty string, if start is either at or beyond >>the end of the input string. >> >>I have attached patches for both solutions. >> >>If you don't change it to be like Perl, I think it should be noted in >>the documentation (as with chop()). >>If, on the other hand, it _is_ changed to behave like Perl, I think it >>should be emphasized that you are not guaranteed that the result is a >>string, because this will cause (eg. has caused us) problems when using >>the result as part of the $data array to PEAR DB::execute(). >> >>Happy hacking, >>Morten >> >>-- >>Morten Poulsen <morten-Qg2caEh8@afdelingp.dk> >>http://www.afdelingp.dk/ >> >> >>-- >>PHP Internals - PHP Runtime Development Mailing List >>To unsubscribe, visit: http://www.php.net/unsub.php > >
-- https://www.paypal.com/xclick/business=sniper@php.net&no_note=1&tax=0&currency_code=EUR

Morten Poulsen

22 years ago
On Wed, 2003-11-19 at 19:30, Andi Gutmans wrote:
> Isn't it a mistake to give a start position at the end or beyond the > string?
You could easily argue, that beyond the end is an error, but I agree with the way Perl allows the start to be at the end. I think of it as with (pseudo-) C: char str[] = { 'A', 'B', '\0' }; substr(str, 0) == "AB" substr(str, 2) == "" substr(str, 3) ERROR
> In this case, doesn't it make sense to return false?
If things doesn't make sense, I think there should be some kind of warning too (like with array to string cast). On Wed, 2003-11-19 at 22:30, Jani Taskinen wrote:
> IF you change it, it'll break BC.. :)
True, but if you do it like Perl, it will only break BC for the case where start is at the end of string, which is an error now.
> IMO, it should just give an error in such case, and return FALSE too.
Yes, with the case where start is beyond the end (which is an error), there should be a warning or an error. Morten
-- Morten Poulsen <morten-Qg2caEh8@afdelingp.dk> http://www.afdelingp.dk/