Bug #18428 Updated: ctype functions return TRUE on zero-length strings

From: Date: Fri, 19 Jul 2002 15:58:41 +0000
Subject: Bug #18428 Updated: ctype functions return TRUE on zero-length strings
References: 1  Groups: php.bugs php.doc 
Request: Send a blank email to php-bugs+get-14638@lists.php.net to get a copy of this message
ID: 18428 Updated by: hholzgra@php.net Reported By: busterb@mail.utexas.edu Status: Closed Bug Type: Documentation problem Operating System: Linux PHP Version: 4CVS-2002-07-19 New Comment: "If there is nothing, there is nothing to match." i'm more for "if theres nothing, there's nothing to fail" and comparing this to the ctype functions just makes no sense, C operates on single characters only and the behaviour of the PHP functions on single characters (either single character string or integer character code) exactly matches the c behavior if i was to implement the functions now i would maybe consider the "empty is false" variant the better one, but back then i didn't the extension has been marked EXPERIMENTAL for a loooooong time and nobody objected, now it has been declared stable and so it will be Previous Comments: ------------------------------------------------------------------------ [2002-07-19 09:03:35] busterb@mail.utexas.edu While I accept the backwards-compatibility argument conditionaly, the rest of the reasoning, i.e. "can't see the contradiction here as in an empty string every of the 0 charactetrs matches the criteriy ..." is hard to believe. If there is nothing, there is nothing to match. With the C versions of these functions, it is obviously not possible to run into this situation; essentially they _require_ a 'string' that is one character long, taking a character argument. Therefore, these functions should have required that the string be at least one character long in the first place. It is unfortunate that what inconsistent behavior with the real ctype functions must be maintained for backwards compatibility, especially for an extension that is so young (it has only been a default option since 4.2). If there were ever a time to correct its behavior, better sooner than later. I had included ctype_ equivalent functions in PEAR under DBA/ctype.php due to the fact that these functions are not commonly included with PHP installations. I wrote that exhibit what I feel is the correct behavior before noticing the ctype functions built-in to PHP. Should I now rewrite them so that they and are inconsistent with C's ctype functions but compatible with PHP's? ------------------------------------------------------------------------ [2002-07-19 06:01:35] hholzgra@php.net an empty string is a string ;) the documentation says: " When called with a string argument they will check every character in the string and will only return TRUE if every character in the string matches the requested criteria. " can't see the contradiction here as in an empty string every of the 0 charactetrs matches the criteriy ... will add a note about that in the documentation, but the behaviour will not change for backwards compatibility reasons ------------------------------------------------------------------------ [2002-07-19 01:12:35] busterb@mail.utexas.edu The PHP ctype functions return TRUE when passed a zero length string. This is incorrect behavior; see the documentation for the contradiction. This is not an issue with the C ctype functions, since they deal with characters, which cannot be zero-length. The following patch corrects this behavior --- ctype.c 2002-07-18 23:58:02.000000000 -0500 +++ ctype_fixed.c 2002-07-18 23:57:48.000000000 -0500 @@ -102,8 +102,11 @@ { \ char *p; \ int n, len; \ - p=Z_STRVAL_P(c); \ len = Z_STRLEN_P(c); \ + if (!len) { \ + RETURN_FALSE; \ + } \ + p = Z_STRVAL_P(c); \ for(n=0;n<len;n++) { \ if(!iswhat(*p++)) RETURN_FALSE; \ } \ ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=18428&edit=1

« previous php.bugs (#14638) next »