stripslashes() improvements

From: Date: Tue, 08 Jun 2004 13:56:18 +0000
Subject: stripslashes() improvements
Groups: php.internals 
Request: Send a blank email to internals+get-10317@lists.php.net to get a copy of this message
Today I checked stripslashes() function. My version of the function if faster, more clearer and bugs free (I hope :)) Below you can see the source and unified diff: ============source============= PHPAPI void php_stripslashes(char *str, size_t *len TSRMLS_DC) {
    char *s, *t;
    size_t l;
    size_t dst_len; /* length the of stripped string */
    if (len != NULL) l = dst_len = *len;
    else l = dst_len = 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--;
                dst_len--;
            } else if (*t == '\\' && *(t + 1) == '0') {
                *s++ = '\0';
                t += 2;
                l--;
                dst_len--;
            } 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++; dst_len--; break;
                    case '0' : *s++ = '\0'; t++; dst_len--; 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 = dst_len; /* set length of the stripped string */
    *s = '\0';
} ============source============= unified diff: ====================cut===================
--- string.c    Thu May 13 20:44:32 2004
+++ string_new.c    Tue Jun 08 16:49:26 2004
@@ -2156,73 +2156,54 @@ /* {{{ php_stripslashes * * be careful, this edits the string in-place */ -PHPAPI void php_stripslashes(char *str, int *len TSRMLS_DC) +PHPAPI void php_stripslashes(char *str, size_t *len TSRMLS_DC) {
    char *s, *t;
- int l; - - if (len != NULL) {
-       l = *len;
- } else {
-       l = strlen(str);
- } - s = str; - t = str;
+    size_t l;
+    size_t dst_len; /* length the of stripped string */
- if (PG(magic_quotes_sybase)) {
-       while (l > 0) {
-           if (*t == '\'') {
-               if ((l > 0) && (t[1] == '\'')) {
-                   t++;
-                   if (len != NULL)
-                       (*len)--;
+    if (len != NULL) l = dst_len = *len;
+    else l = dst_len = 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') {
+                dst_len--;
+            } else if (*t == '\\' && *(t + 1) == '0') {
                    *s++='\0';
                    t += 2;
-                   if (len != NULL)
-                       (*len)--;
                    l--;
-           } else {
-               *s++ = *t++;
-           }
+                dst_len--;
+            } 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++; dst_len--; break;
+                    case '0' : *s++ = '\0'; t++; dst_len--; 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 = dst_len; /* set length of the stripped string */
+    *s = '\0';
} /* }}} */ =======================cut==================== -- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

« previous php.internals (#10317) next »