Ask a Question related to PHP Development, Design and Development.
-
Morten Poulsen #1
[PHP-DEV] [PATCH] substr() returns false
--=-sQWDp5Weadel0FX8XkrW
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
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 [url]http://www.perldoc.com/perl5.6/pod/func/substr.html[/url])
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>
[url]http://www.afdelingp.dk/[/url]
--=-sQWDp5Weadel0FX8XkrW
Content-Disposition: attachment; filename=php-4.3.4-substr-1.diff
Content-Type: text/plain; name=php-4.3.4-substr-1.diff; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
--- php-4.3.4-orig/ext/standard/string.c Mon Sep 29 04:23:52 2003
+++ php-4.3.4/ext/standard/string.c Wed Nov 19 14:18:08 2003
@@ -1679,7 +1679,11 @@
}
}
- if (f >= Z_STRLEN_PP(str)) {
+ /* if "from" position is beyond the end of the string, emit a warning
+ * and return false
+ */
+ if (f > Z_STRLEN_PP(str)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Second argument has to be less than or equal the length of the first argument");
RETURN_FALSE;
}
--=-sQWDp5Weadel0FX8XkrW
Content-Disposition: attachment; filename=php-4.3.4-substr-2.diff
Content-Type: text/plain; name=php-4.3.4-substr-2.diff; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
--- php-4.3.4-orig/ext/standard/string.c Mon Sep 29 04:23:52 2003
+++ php-4.3.4/ext/standard/string.c Wed Nov 19 12:14:41 2003
@@ -1679,8 +1679,11 @@
}
}
+ /* if "from" position is past the end of the string, return an empty
+ * string
+ */
if (f >= Z_STRLEN_PP(str)) {
- RETURN_FALSE;
+ RETURN_STRING("", 1);
}
if ((f + l) > Z_STRLEN_PP(str)) {
--=-sQWDp5Weadel0FX8XkrW
Content-Type: text/plain; charset=us-ascii
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
--=-sQWDp5Weadel0FX8XkrW--
Morten Poulsen Guest
-
mail() returns FALSE, no err msg
Sometimes my mail() function returns FALSE, but there is no error message. Error reporting is set to E_ALL. It seems to be a problem of the bcc:... -
#24627 [Bgs]: feof always returns false
ID: 24627 Updated by: sniper@php.net Reported By: duerra at yahoo dot com Status: Bogus Bug Type: ... -
#24627 [Opn->Bgs]: feof always returns false
ID: 24627 Updated by: elmicha@php.net Reported By: duerra at yahoo dot com -Status: Open +Status: ... -
#24627 [Bgs->Opn]: feof always returns false
ID: 24627 User updated by: duerra at yahoo dot com Reported By: duerra at yahoo dot com -Status: Bogus +Status: ... -
#24627 [NEW]: feof always returns false
From: duerra at yahoo dot com Operating system: XP Pro PHP version: 4.3.3RC1 PHP Bug Type: Filesystem function related Bug... -
Andi Gutmans #2
Re: [PHP-DEV] [PATCH] substr() returns false
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 [url]http://www.perldoc.com/perl5.6/pod/func/substr.html[/url])
>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>
>[url]http://www.afdelingp.dk/[/url]
>
>
>--
>PHP Internals - PHP Runtime Development Mailing List
>To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
Andi Gutmans Guest
-
Morten Poulsen #3
Re: [PHP-DEV] [PATCH] substr() returns false
On Wed, 2003-11-19 at 19:30, Andi Gutmans wrote:
You could easily argue, that beyond the end is an error, but I agree> Isn't it a mistake to give a start position at the end or beyond the
> string?
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
If things doesn't make sense, I think there should be some kind of> In this case, doesn't it make sense to return false?
warning too (like with array to string cast).
On Wed, 2003-11-19 at 22:30, Jani Taskinen wrote:True, but if you do it like Perl, it will only break BC for the case> IF you change it, it'll break BC.. :)
where start is at the end of string, which is an error now.
Yes, with the case where start is beyond the end (which is an error),> IMO, it should just give an error in such case, and return FALSE too.
there should be a warning or an error.
Morten
--
Morten Poulsen <morten-Qg2caEh8@afdelingp.dk>
[url]http://www.afdelingp.dk/[/url]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
Morten Poulsen Guest



Reply With Quote

