Re: stripslashes() improvements
| From: | Alexander Valyalkin | Date: | Thu, 10 Jun 2004 07:50:09 +0000 |
| Subject: | Re: stripslashes() improvements | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-10367@lists.php.net to get a copy of this message | ||
On Wed, 9 Jun 2004 15:56:10 +0400, Antony Dovgal <tony2001@phpclub.net> 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.comOk, 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/