Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:10367 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 90394 invoked by uid 1010); 10 Jun 2004 07:50:43 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 90369 invoked by uid 1007); 10 Jun 2004 07:50:43 -0000 To: internals@lists.php.net References: <20040609150030.18f59a2c.tony2001@phpclub.net> <20040609155610.0425464f.tony2001@phpclub.net> Message-ID: Date: Thu, 10 Jun 2004 10:50:09 +0300 Organization: none Content-Type: text/plain; format=flowed; delsp=yes; charset=koi8-r MIME-Version: 1.0 Content-Transfer-Encoding: 8bit User-Agent: Opera M2/7.50 (Win32, build 3778) X-Posted-By: 217.23.116.150 Subject: Re: [PHP-DEV] stripslashes() improvements From: valyala@tut.by ("Alexander Valyalkin") On Wed, 9 Jun 2004 15:56:10 +0400, Antony Dovgal wrote: > If you think it's wrong - prove it. > There are quite detailed reasons why those bugs were considered to > be bogus. > > I'm ok if you're going to improve stripslashes() efficiency and/or to > make it just faster, this could be very useful addon. But I doubt > it needs some kind of'fixing', 'cos it works ok ATM, as I know. > > --- > WBR, > Antony Dovgal aka tony2001 > tony2001@phpclub.net || antony@dovgal.com Ok, my version of stripslashes() is faster, clearer, works correctly with old tests and solves mentioned bugs. Are you still doubt? Try to compare the old (current) code to new one. Here is improved version of my code. Unnesessary [dst_len] variable has been removed. ================cut=============== PHPAPI void php_stripslashes(char *str, int *len TSRMLS_DC) { char *s, *t; size_t l; if (len != NULL) l = (size_t) *len; else l = strlen(str); if (l < 2) return; /* there is no characters to strip */ s = t = str; if (PG(magic_quotes_sybase)) { /* sybase magic_quotes ( '' -> ', \0 -> NULL) */ while (l > 1) { if (*t == '\'' && *(t + 1) == '\'') { *s++ = '\''; t += 2; l--; } else if (*t == '\\' && *(t + 1) == '0') { *s++ = '\0'; t += 2; l--; } else *s++ = *t++; l--; } } else { /* ordinary magic_qoutes (not sybase) ( \\ -> \, \' -> ', \" -> ", \0 -> NULL) */ while (l > 1) { if (*t == '\\') { t++; switch (*t) { case '\\' : case '\'' : case '"' : *s++ = *t++; break; case '0' : *s++ = '\0'; t++; break; default : *s++ = '\\'; *s++ = *t++; break; } l -= 2; } else { *s++ = *t++; l--; } } } if (l == 1) *s++ = *t; /* copy the last symbol */ if (len != NULL) *len = (int) (s - str); /* set length of the stripped string */ *s = '\0'; } ================cut=============== unified diff: ================cut=============== --- string.c Thu May 13 20:44:32 2004 +++ string_new.c Thu Jun 10 10:40:58 2004 @@ -2159,70 +2159,48 @@ PHPAPI void php_stripslashes(char *str, int *len TSRMLS_DC) { char *s, *t; - int l; - - if (len != NULL) { - l = *len; - } else { - l = strlen(str); - } - s = str; - t = str; + size_t l; - if (PG(magic_quotes_sybase)) { - while (l > 0) { - if (*t == '\'') { - if ((l > 0) && (t[1] == '\'')) { - t++; - if (len != NULL) - (*len)--; + if (len != NULL) l = (size_t) *len; + else l = strlen(str); + if (l < 2) return; /* there is no characters to strip */ + s = t = str; + + if (PG(magic_quotes_sybase)) { /* sybase magic_quotes ( '' -> ', \0 -> NULL) */ + while (l > 1) { + if (*t == '\'' && *(t + 1) == '\'') { + *s++ = '\''; + t += 2; l--; - } - *s++ = *t++; - } else if (*t == '\\' && l > 0 && t[1] == '0') { + } else if (*t == '\\' && *(t + 1) == '0') { *s++='\0'; t += 2; - if (len != NULL) - (*len)--; l--; - } else { - *s++ = *t++; - } + } else *s++ = *t++; l--; } - *s = '\0'; - - return; - } - - while (l > 0) { + } else { /* ordinary magic_qoutes (not sybase) ( \\ -> \, \' -> ', \" -> ", \0 -> NULL) */ + while (l > 1) { if (*t == '\\') { - t++; /* skip the slash */ - if (len != NULL) - (*len)--; - l--; - if (l > 0) { - if (*t == '0') { - *s++='\0'; t++; - } else { - *s++ = *t++; /* preserve the next character */ - } - l--; + switch (*t) { + case '\\' : + case '\'' : + case '"' : + *s++ = *t++; break; + case '0' : *s++ = '\0'; t++; break; + default : *s++ = '\\'; *s++ = *t++; break; } + l -= 2; } else { - if (s != t) { *s++ = *t++; - } else { - s++; - t++; - } l--; } } - if (s != t) { - *s = '\0'; } + if (l == 1) *s++ = *t; /* copy the last symbol */ + if (len != NULL) *len = (int) (s - str); /* set length of the stripped string */ + *s = '\0'; } /* }}} */ ================cut=============== -- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/